blob: 094872d0fbad137aa5c3a34a78d9b62efb46fb37 [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
Scott Bakerc7958a92017-07-18 11:07:00 -070012from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline
Scott Baker171d35e2016-06-20 17:36:29 -070013from 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 Baker379404c2017-05-24 17:13:45 -070049 return VTRService.select_by_user(request.user)
Scott Baker171d35e2016-06-20 17:36:29 -070050
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)
Scott Bakerc7958a92017-07-18 11:07:00 -070056 self.fields['owner'].queryset = VTRService.objects.all()
Scott Baker171d35e2016-06-20 17:36:29 -070057 if self.instance:
Scott Baker5db44a92017-03-06 17:27:52 -080058 if self.instance.target_id:
Scott Baker4acbd4c2017-05-18 08:24:43 -070059 self.fields["target"].initial = CordSubscriberRoot.get_content_object(self.instance.target_type, self.instance.target_id)
Scott Baker171d35e2016-06-20 17:36:29 -070060 if (not self.instance) or (not self.instance.pk):
Scott Baker379404c2017-05-24 17:13:45 -070061 if VTRService.objects.exists():
Scott Bakerc7958a92017-07-18 11:07:00 -070062 self.fields["owner"].initial = VTRService.objects.all()[0]
Scott Baker171d35e2016-06-20 17:36:29 -070063
64 def save(self, commit=True):
Scott Baker5db44a92017-03-06 17:27:52 -080065 if self.cleaned_data.get("target"):
Scott Baker4acbd4c2017-05-18 08:24:43 -070066 self.instance.target_type = self.cleaned_data.get("target").get_content_type_key()
Scott Baker5db44a92017-03-06 17:27:52 -080067 self.instance.target_id = self.cleaned_data.get("target").id
Scott Baker171d35e2016-06-20 17:36:29 -070068 return super(VTRTenantForm, self).save(commit=commit)
69
70 class Meta:
71 model = VTRTenant
Zack Williams69b3edc2016-06-27 08:35:17 -070072 fields = '__all__'
Scott Baker171d35e2016-06-20 17:36:29 -070073
74class VTRTenantAdmin(ReadOnlyAwareAdmin):
Scott Baker825cd732017-05-19 08:40:30 -070075 list_display = ('backend_status_icon', 'id', 'target_type', 'target_id', 'test', 'argument' )
Scott Baker171d35e2016-06-20 17:36:29 -070076 list_display_links = ('backend_status_icon', 'id')
Scott Bakerc7958a92017-07-18 11:07:00 -070077 fieldsets = [ (None, {'fields': ['backend_status_text', 'owner',
Scott Baker171d35e2016-06-20 17:36:29 -070078 'target', 'scope', 'test', 'argument', 'is_synced', 'result_code', 'result'],
79 'classes':['suit-tab suit-tab-general']})]
80 readonly_fields = ('backend_status_text', 'service_specific_attribute', 'is_synced')
81 form = VTRTenantForm
82
83 suit_form_tabs = (('general','Details'),)
84
85 def is_synced(self, obj):
86 return (obj.enacted is not None) and (obj.enacted >= obj.updated)
87
Zack Williams93a777d2016-06-27 13:08:09 -070088 def get_queryset(self, request):
Scott Baker379404c2017-05-24 17:13:45 -070089 return VTRTenant.select_by_user(request.user)
Scott Baker171d35e2016-06-20 17:36:29 -070090
91admin.site.register(VTRService, VTRServiceAdmin)
92admin.site.register(VTRTenant, VTRTenantAdmin)
93