Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 1 | from django.contrib import admin |
| 2 | |
| 3 | from django import forms |
| 4 | from django.utils.safestring import mark_safe |
| 5 | from django.contrib.auth.admin import UserAdmin |
| 6 | from django.contrib.admin.widgets import FilteredSelectMultiple |
| 7 | from django.contrib.auth.forms import ReadOnlyPasswordHashField |
| 8 | from django.contrib.auth.signals import user_logged_in |
| 9 | from django.utils import timezone |
| 10 | from django.contrib.contenttypes import generic |
| 11 | from suit.widgets import LinkedSelect |
| 12 | from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, TenantRootTenantInline, TenantRootPrivilegeInline |
| 13 | from core.middleware import get_request |
| 14 | |
| 15 | from services.vtr.models import * |
| 16 | from services.volt.models import CordSubscriberRoot |
| 17 | |
| 18 | from functools import update_wrapper |
| 19 | from django.contrib.admin.views.main import ChangeList |
| 20 | from django.core.urlresolvers import reverse |
| 21 | from django.contrib.admin.utils import quote |
| 22 | |
| 23 | class 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 Williams | 93a777d | 2016-06-27 13:08:09 -0700 | [diff] [blame] | 47 | def get_queryset(self, request): |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 48 | return VTRService.get_service_objects_by_user(request.user) |
| 49 | |
| 50 | class 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 Williams | 69b3edc | 2016-06-27 08:35:17 -0700 | [diff] [blame] | 91 | fields = '__all__' |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 92 | |
| 93 | class 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 Williams | 93a777d | 2016-06-27 13:08:09 -0700 | [diff] [blame] | 107 | def get_queryset(self, request): |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 108 | return VTRTenant.get_tenant_objects_by_user(request.user) |
| 109 | |
| 110 | admin.site.register(VTRService, VTRServiceAdmin) |
| 111 | admin.site.register(VTRTenant, VTRTenantAdmin) |
| 112 | |