blob: 62c7c568acb9aa305c22235a0f90fd70fc66e77f [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 *
Scott Baker6cc51472017-04-20 15:20:23 -070016from services.rcord.models import CordSubscriberRoot
Scott Baker171d35e2016-06-20 17:36:29 -070017
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
Scott Baker5db44a92017-03-06 17:27:52 -080022from django.contrib.contenttypes.models import ContentType
Scott Baker171d35e2016-06-20 17:36:29 -070023
24class VTRServiceAdmin(ReadOnlyAwareAdmin):
25 model = VTRService
26 verbose_name = "vTR Service"
27 verbose_name_plural = "vTR Service"
28 list_display = ("backend_status_icon", "name", "enabled")
29 list_display_links = ('backend_status_icon', 'name', )
30 fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description',"view_url","icon_url" ], 'classes':['suit-tab suit-tab-general']})]
31 readonly_fields = ('backend_status_text', )
32 inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline]
33
34 extracontext_registered_admins = True
35
36 user_readonly_fields = ["name", "enabled", "versionNumber", "description"]
37
38 suit_form_tabs =(('general', 'vTR Service Details'),
39 ('administration', 'Administration'),
40 ('slices','Slices'),
41 ('serviceattrs','Additional Attributes'),
42 ('serviceprivileges','Privileges'),
43 )
44
45 suit_form_includes = (('vtradmin.html', 'top', 'administration'),
46 ) #('hpctools.html', 'top', 'tools') )
47
Zack Williams93a777d2016-06-27 13:08:09 -070048 def get_queryset(self, request):
Scott Baker171d35e2016-06-20 17:36:29 -070049 return VTRService.get_service_objects_by_user(request.user)
50
51class VTRTenantForm(forms.ModelForm):
Scott Baker171d35e2016-06-20 17:36:29 -070052 target = forms.ModelChoiceField(queryset=CordSubscriberRoot.objects.all())
53
54 def __init__(self,*args,**kwargs):
55 super (VTRTenantForm,self ).__init__(*args,**kwargs)
56 self.fields['provider_service'].queryset = VTRService.get_service_objects().all()
57 if self.instance:
Scott Baker5db44a92017-03-06 17:27:52 -080058 if self.instance.target_id:
59 self.fields["target"].initial = CordSubscriberRoot.objects.get(id=self.instance.target_id)
Scott Baker171d35e2016-06-20 17:36:29 -070060 if (not self.instance) or (not self.instance.pk):
Scott Baker171d35e2016-06-20 17:36:29 -070061 self.fields['kind'].initial = VTR_KIND
Scott Baker171d35e2016-06-20 17:36:29 -070062 if VTRService.get_service_objects().exists():
63 self.fields["provider_service"].initial = VTRService.get_service_objects().all()[0]
64
65 def save(self, commit=True):
Scott Baker5db44a92017-03-06 17:27:52 -080066 if self.cleaned_data.get("target"):
67 self.instance.target_type = ContentType.objects.get_for_model(CordSubscriberRoot)
68 self.instance.target_id = self.cleaned_data.get("target").id
Scott Baker171d35e2016-06-20 17:36:29 -070069 return super(VTRTenantForm, self).save(commit=commit)
70
71 class Meta:
72 model = VTRTenant
Zack Williams69b3edc2016-06-27 08:35:17 -070073 fields = '__all__'
Scott Baker171d35e2016-06-20 17:36:29 -070074
75class VTRTenantAdmin(ReadOnlyAwareAdmin):
76 list_display = ('backend_status_icon', 'id', 'target', 'test', 'argument' )
77 list_display_links = ('backend_status_icon', 'id')
Scott Baker5db44a92017-03-06 17:27:52 -080078 fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'provider_service',
Scott Baker171d35e2016-06-20 17:36:29 -070079 'target', 'scope', 'test', 'argument', 'is_synced', 'result_code', 'result'],
80 'classes':['suit-tab suit-tab-general']})]
81 readonly_fields = ('backend_status_text', 'service_specific_attribute', 'is_synced')
82 form = VTRTenantForm
83
84 suit_form_tabs = (('general','Details'),)
85
86 def is_synced(self, obj):
87 return (obj.enacted is not None) and (obj.enacted >= obj.updated)
88
Zack Williams93a777d2016-06-27 13:08:09 -070089 def get_queryset(self, request):
Scott Baker171d35e2016-06-20 17:36:29 -070090 return VTRTenant.get_tenant_objects_by_user(request.user)
91
92admin.site.register(VTRService, VTRServiceAdmin)
93admin.site.register(VTRTenant, VTRTenantAdmin)
94