blob: 42c9f962684d18204f524ced91838b6f5740afa9 [file] [log] [blame]
Matteo Scandoloa86e0a12017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Scott Baker171d35e2016-06-20 17:36:29 -070017from django.contrib import admin
18
19from django import forms
20from django.utils.safestring import mark_safe
21from django.contrib.auth.admin import UserAdmin
22from django.contrib.admin.widgets import FilteredSelectMultiple
23from django.contrib.auth.forms import ReadOnlyPasswordHashField
24from django.contrib.auth.signals import user_logged_in
25from django.utils import timezone
26from django.contrib.contenttypes import generic
27from suit.widgets import LinkedSelect
Scott Bakerc7958a92017-07-18 11:07:00 -070028from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline
Scott Baker171d35e2016-06-20 17:36:29 -070029from core.middleware import get_request
30
31from services.vtr.models import *
Scott Baker6cc51472017-04-20 15:20:23 -070032from services.rcord.models import CordSubscriberRoot
Scott Baker171d35e2016-06-20 17:36:29 -070033
34from functools import update_wrapper
35from django.contrib.admin.views.main import ChangeList
36from django.core.urlresolvers import reverse
37from django.contrib.admin.utils import quote
Scott Baker5db44a92017-03-06 17:27:52 -080038from django.contrib.contenttypes.models import ContentType
Scott Baker171d35e2016-06-20 17:36:29 -070039
40class VTRServiceAdmin(ReadOnlyAwareAdmin):
41 model = VTRService
42 verbose_name = "vTR Service"
43 verbose_name_plural = "vTR Service"
44 list_display = ("backend_status_icon", "name", "enabled")
45 list_display_links = ('backend_status_icon', 'name', )
46 fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description',"view_url","icon_url" ], 'classes':['suit-tab suit-tab-general']})]
47 readonly_fields = ('backend_status_text', )
48 inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline]
49
50 extracontext_registered_admins = True
51
52 user_readonly_fields = ["name", "enabled", "versionNumber", "description"]
53
54 suit_form_tabs =(('general', 'vTR Service Details'),
55 ('administration', 'Administration'),
56 ('slices','Slices'),
57 ('serviceattrs','Additional Attributes'),
58 ('serviceprivileges','Privileges'),
59 )
60
61 suit_form_includes = (('vtradmin.html', 'top', 'administration'),
62 ) #('hpctools.html', 'top', 'tools') )
63
Zack Williams93a777d2016-06-27 13:08:09 -070064 def get_queryset(self, request):
Scott Baker379404c2017-05-24 17:13:45 -070065 return VTRService.select_by_user(request.user)
Scott Baker171d35e2016-06-20 17:36:29 -070066
67class VTRTenantForm(forms.ModelForm):
Scott Baker171d35e2016-06-20 17:36:29 -070068 target = forms.ModelChoiceField(queryset=CordSubscriberRoot.objects.all())
69
70 def __init__(self,*args,**kwargs):
71 super (VTRTenantForm,self ).__init__(*args,**kwargs)
Scott Bakerc7958a92017-07-18 11:07:00 -070072 self.fields['owner'].queryset = VTRService.objects.all()
Scott Baker171d35e2016-06-20 17:36:29 -070073 if self.instance:
Scott Baker5db44a92017-03-06 17:27:52 -080074 if self.instance.target_id:
Scott Baker4acbd4c2017-05-18 08:24:43 -070075 self.fields["target"].initial = CordSubscriberRoot.get_content_object(self.instance.target_type, self.instance.target_id)
Scott Baker171d35e2016-06-20 17:36:29 -070076 if (not self.instance) or (not self.instance.pk):
Scott Baker379404c2017-05-24 17:13:45 -070077 if VTRService.objects.exists():
Scott Bakerc7958a92017-07-18 11:07:00 -070078 self.fields["owner"].initial = VTRService.objects.all()[0]
Scott Baker171d35e2016-06-20 17:36:29 -070079
80 def save(self, commit=True):
Scott Baker5db44a92017-03-06 17:27:52 -080081 if self.cleaned_data.get("target"):
Scott Baker4acbd4c2017-05-18 08:24:43 -070082 self.instance.target_type = self.cleaned_data.get("target").get_content_type_key()
Scott Baker5db44a92017-03-06 17:27:52 -080083 self.instance.target_id = self.cleaned_data.get("target").id
Scott Baker171d35e2016-06-20 17:36:29 -070084 return super(VTRTenantForm, self).save(commit=commit)
85
86 class Meta:
87 model = VTRTenant
Zack Williams69b3edc2016-06-27 08:35:17 -070088 fields = '__all__'
Scott Baker171d35e2016-06-20 17:36:29 -070089
90class VTRTenantAdmin(ReadOnlyAwareAdmin):
Scott Baker825cd732017-05-19 08:40:30 -070091 list_display = ('backend_status_icon', 'id', 'target_type', 'target_id', 'test', 'argument' )
Scott Baker171d35e2016-06-20 17:36:29 -070092 list_display_links = ('backend_status_icon', 'id')
Scott Bakerc7958a92017-07-18 11:07:00 -070093 fieldsets = [ (None, {'fields': ['backend_status_text', 'owner',
Scott Baker171d35e2016-06-20 17:36:29 -070094 'target', 'scope', 'test', 'argument', 'is_synced', 'result_code', 'result'],
95 'classes':['suit-tab suit-tab-general']})]
96 readonly_fields = ('backend_status_text', 'service_specific_attribute', 'is_synced')
97 form = VTRTenantForm
98
99 suit_form_tabs = (('general','Details'),)
100
101 def is_synced(self, obj):
102 return (obj.enacted is not None) and (obj.enacted >= obj.updated)
103
Zack Williams93a777d2016-06-27 13:08:09 -0700104 def get_queryset(self, request):
Scott Baker379404c2017-05-24 17:13:45 -0700105 return VTRTenant.select_by_user(request.user)
Scott Baker171d35e2016-06-20 17:36:29 -0700106
107admin.site.register(VTRService, VTRServiceAdmin)
108admin.site.register(VTRTenant, VTRTenantAdmin)
109