Matteo Scandolo | a86e0a1 | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 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 Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 17 | from django.contrib import admin |
| 18 | |
| 19 | from django import forms |
| 20 | from django.utils.safestring import mark_safe |
| 21 | from django.contrib.auth.admin import UserAdmin |
| 22 | from django.contrib.admin.widgets import FilteredSelectMultiple |
| 23 | from django.contrib.auth.forms import ReadOnlyPasswordHashField |
| 24 | from django.contrib.auth.signals import user_logged_in |
| 25 | from django.utils import timezone |
| 26 | from django.contrib.contenttypes import generic |
| 27 | from suit.widgets import LinkedSelect |
Scott Baker | c7958a9 | 2017-07-18 11:07:00 -0700 | [diff] [blame] | 28 | from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 29 | from core.middleware import get_request |
| 30 | |
| 31 | from services.vtr.models import * |
Scott Baker | 6cc5147 | 2017-04-20 15:20:23 -0700 | [diff] [blame] | 32 | from services.rcord.models import CordSubscriberRoot |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 33 | |
| 34 | from functools import update_wrapper |
| 35 | from django.contrib.admin.views.main import ChangeList |
| 36 | from django.core.urlresolvers import reverse |
| 37 | from django.contrib.admin.utils import quote |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 38 | from django.contrib.contenttypes.models import ContentType |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 39 | |
| 40 | class 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 Williams | 93a777d | 2016-06-27 13:08:09 -0700 | [diff] [blame] | 64 | def get_queryset(self, request): |
Scott Baker | 379404c | 2017-05-24 17:13:45 -0700 | [diff] [blame] | 65 | return VTRService.select_by_user(request.user) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 66 | |
| 67 | class VTRTenantForm(forms.ModelForm): |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 68 | target = forms.ModelChoiceField(queryset=CordSubscriberRoot.objects.all()) |
| 69 | |
| 70 | def __init__(self,*args,**kwargs): |
| 71 | super (VTRTenantForm,self ).__init__(*args,**kwargs) |
Scott Baker | c7958a9 | 2017-07-18 11:07:00 -0700 | [diff] [blame] | 72 | self.fields['owner'].queryset = VTRService.objects.all() |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 73 | if self.instance: |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 74 | if self.instance.target_id: |
Scott Baker | 4acbd4c | 2017-05-18 08:24:43 -0700 | [diff] [blame] | 75 | self.fields["target"].initial = CordSubscriberRoot.get_content_object(self.instance.target_type, self.instance.target_id) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 76 | if (not self.instance) or (not self.instance.pk): |
Scott Baker | 379404c | 2017-05-24 17:13:45 -0700 | [diff] [blame] | 77 | if VTRService.objects.exists(): |
Scott Baker | c7958a9 | 2017-07-18 11:07:00 -0700 | [diff] [blame] | 78 | self.fields["owner"].initial = VTRService.objects.all()[0] |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 79 | |
| 80 | def save(self, commit=True): |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 81 | if self.cleaned_data.get("target"): |
Scott Baker | 4acbd4c | 2017-05-18 08:24:43 -0700 | [diff] [blame] | 82 | self.instance.target_type = self.cleaned_data.get("target").get_content_type_key() |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 83 | self.instance.target_id = self.cleaned_data.get("target").id |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 84 | return super(VTRTenantForm, self).save(commit=commit) |
| 85 | |
| 86 | class Meta: |
| 87 | model = VTRTenant |
Zack Williams | 69b3edc | 2016-06-27 08:35:17 -0700 | [diff] [blame] | 88 | fields = '__all__' |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 89 | |
| 90 | class VTRTenantAdmin(ReadOnlyAwareAdmin): |
Scott Baker | 825cd73 | 2017-05-19 08:40:30 -0700 | [diff] [blame] | 91 | list_display = ('backend_status_icon', 'id', 'target_type', 'target_id', 'test', 'argument' ) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 92 | list_display_links = ('backend_status_icon', 'id') |
Scott Baker | c7958a9 | 2017-07-18 11:07:00 -0700 | [diff] [blame] | 93 | fieldsets = [ (None, {'fields': ['backend_status_text', 'owner', |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 94 | '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 Williams | 93a777d | 2016-06-27 13:08:09 -0700 | [diff] [blame] | 104 | def get_queryset(self, request): |
Scott Baker | 379404c | 2017-05-24 17:13:45 -0700 | [diff] [blame] | 105 | return VTRTenant.select_by_user(request.user) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 106 | |
| 107 | admin.site.register(VTRService, VTRServiceAdmin) |
| 108 | admin.site.register(VTRTenant, VTRTenantAdmin) |
| 109 | |