blob: 4e7de44d7676f9b2a5c20aa084b85aff6c88a38b [file] [log] [blame]
Scott Baker171d35e2016-06-20 17:36:29 -07001from django.contrib import admin
2
3from django import forms
4from django.utils.safestring import mark_safe
5from django.contrib.auth.admin import UserAdmin
6from django.contrib.admin.widgets import FilteredSelectMultiple
7from django.contrib.auth.forms import ReadOnlyPasswordHashField
8from django.contrib.auth.signals import user_logged_in
9from django.utils import timezone
10from django.contrib.contenttypes import generic
11from suit.widgets import LinkedSelect
12from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, TenantRootTenantInline, TenantRootPrivilegeInline
13from core.middleware import get_request
14
15from services.vtr.models import *
16from services.volt.models import CordSubscriberRoot
17
18from functools import update_wrapper
19from django.contrib.admin.views.main import ChangeList
20from django.core.urlresolvers import reverse
21from django.contrib.admin.utils import quote
22
23class VTRServiceAdmin(ReadOnlyAwareAdmin):
24 model = VTRService
25 verbose_name = "vTR Service"
26 verbose_name_plural = "vTR Service"
27 list_display = ("backend_status_icon", "name", "enabled")
28 list_display_links = ('backend_status_icon', 'name', )
29 fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description',"view_url","icon_url" ], 'classes':['suit-tab suit-tab-general']})]
30 readonly_fields = ('backend_status_text', )
31 inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline]
32
33 extracontext_registered_admins = True
34
35 user_readonly_fields = ["name", "enabled", "versionNumber", "description"]
36
37 suit_form_tabs =(('general', 'vTR Service Details'),
38 ('administration', 'Administration'),
39 ('slices','Slices'),
40 ('serviceattrs','Additional Attributes'),
41 ('serviceprivileges','Privileges'),
42 )
43
44 suit_form_includes = (('vtradmin.html', 'top', 'administration'),
45 ) #('hpctools.html', 'top', 'tools') )
46
Zack Williams93a777d2016-06-27 13:08:09 -070047 def get_queryset(self, request):
Scott Baker171d35e2016-06-20 17:36:29 -070048 return VTRService.get_service_objects_by_user(request.user)
49
50class VTRTenantForm(forms.ModelForm):
51 test = forms.ChoiceField(choices=VTRTenant.TEST_CHOICES, required=True)
52 scope = forms.ChoiceField(choices=VTRTenant.SCOPE_CHOICES, required=True)
53 argument = forms.CharField(required=False)
54 result_code = forms.CharField(required=False)
55 result = forms.CharField(required=False, widget=forms.Textarea(attrs={'rows': 10, 'cols': 80, 'class': 'input-xxlarge'}))
56 target = forms.ModelChoiceField(queryset=CordSubscriberRoot.objects.all())
57
58 def __init__(self,*args,**kwargs):
59 super (VTRTenantForm,self ).__init__(*args,**kwargs)
60 self.fields['provider_service'].queryset = VTRService.get_service_objects().all()
61 if self.instance:
62 # fields for the attributes
63 self.fields['test'].initial = self.instance.test
64 self.fields['argument'].initial = self.instance.argument
65 self.fields['target'].initial = self.instance.target
66 self.fields['scope'].initial = self.instance.scope
67 if (self.instance.enacted is not None) and (self.instance.enacted >= self.instance.updated):
68 self.fields['result'].initial = self.instance.result
69 self.fields['result_code'].initial = self.instance.result_code
70 else:
71 self.fields['result'].initial = ""
72 self.fields['result_code'].initial= ""
73 if (not self.instance) or (not self.instance.pk):
74 # default fields for an 'add' form
75 self.fields['kind'].initial = VTR_KIND
76 self.fields["scope"].initial = VTRTenant.get_default_attribute("scope")
77 if VTRService.get_service_objects().exists():
78 self.fields["provider_service"].initial = VTRService.get_service_objects().all()[0]
79
80 def save(self, commit=True):
81 self.instance.test = self.cleaned_data.get("test")
82 self.instance.argument = self.cleaned_data.get("argument")
83 self.instance.target = self.cleaned_data.get("target")
84 self.instance.result = self.cleaned_data.get("result")
85 self.instance.result_code = self.cleaned_data.get("result_code")
86 self.instance.scope = self.cleaned_data.get("scope")
87 return super(VTRTenantForm, self).save(commit=commit)
88
89 class Meta:
90 model = VTRTenant
Zack Williams69b3edc2016-06-27 08:35:17 -070091 fields = '__all__'
Scott Baker171d35e2016-06-20 17:36:29 -070092
93class VTRTenantAdmin(ReadOnlyAwareAdmin):
94 list_display = ('backend_status_icon', 'id', 'target', 'test', 'argument' )
95 list_display_links = ('backend_status_icon', 'id')
96 fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'provider_service', # 'subscriber_root', 'service_specific_id', 'service_specific_attribute',
97 'target', 'scope', 'test', 'argument', 'is_synced', 'result_code', 'result'],
98 'classes':['suit-tab suit-tab-general']})]
99 readonly_fields = ('backend_status_text', 'service_specific_attribute', 'is_synced')
100 form = VTRTenantForm
101
102 suit_form_tabs = (('general','Details'),)
103
104 def is_synced(self, obj):
105 return (obj.enacted is not None) and (obj.enacted >= obj.updated)
106
Zack Williams93a777d2016-06-27 13:08:09 -0700107 def get_queryset(self, request):
Scott Baker171d35e2016-06-20 17:36:29 -0700108 return VTRTenant.get_tenant_objects_by_user(request.user)
109
110admin.site.register(VTRService, VTRServiceAdmin)
111admin.site.register(VTRTenant, VTRTenantAdmin)
112