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 * |
Scott Baker | 6cc5147 | 2017-04-20 15:20:23 -0700 | [diff] [blame] | 16 | from services.rcord.models import CordSubscriberRoot |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 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 |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 22 | from django.contrib.contenttypes.models import ContentType |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 23 | |
| 24 | class 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 Williams | 93a777d | 2016-06-27 13:08:09 -0700 | [diff] [blame] | 48 | def get_queryset(self, request): |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 49 | return VTRService.get_service_objects_by_user(request.user) |
| 50 | |
| 51 | class VTRTenantForm(forms.ModelForm): |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 52 | 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 Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 58 | if self.instance.target_id: |
| 59 | self.fields["target"].initial = CordSubscriberRoot.objects.get(id=self.instance.target_id) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 60 | if (not self.instance) or (not self.instance.pk): |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 61 | self.fields['kind'].initial = VTR_KIND |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 62 | 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 Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 66 | 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 Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 69 | return super(VTRTenantForm, self).save(commit=commit) |
| 70 | |
| 71 | class Meta: |
| 72 | model = VTRTenant |
Zack Williams | 69b3edc | 2016-06-27 08:35:17 -0700 | [diff] [blame] | 73 | fields = '__all__' |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 74 | |
| 75 | class VTRTenantAdmin(ReadOnlyAwareAdmin): |
| 76 | list_display = ('backend_status_icon', 'id', 'target', 'test', 'argument' ) |
| 77 | list_display_links = ('backend_status_icon', 'id') |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 78 | fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'provider_service', |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 79 | '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 Williams | 93a777d | 2016-06-27 13:08:09 -0700 | [diff] [blame] | 89 | def get_queryset(self, request): |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 90 | return VTRTenant.get_tenant_objects_by_user(request.user) |
| 91 | |
| 92 | admin.site.register(VTRService, VTRServiceAdmin) |
| 93 | admin.site.register(VTRTenant, VTRTenantAdmin) |
| 94 | |