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 |
Scott Baker | c7958a9 | 2017-07-18 11:07:00 -0700 | [diff] [blame^] | 12 | from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 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 | 379404c | 2017-05-24 17:13:45 -0700 | [diff] [blame] | 49 | return VTRService.select_by_user(request.user) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 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) |
Scott Baker | c7958a9 | 2017-07-18 11:07:00 -0700 | [diff] [blame^] | 56 | self.fields['owner'].queryset = VTRService.objects.all() |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 57 | if self.instance: |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 58 | if self.instance.target_id: |
Scott Baker | 4acbd4c | 2017-05-18 08:24:43 -0700 | [diff] [blame] | 59 | 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] | 60 | if (not self.instance) or (not self.instance.pk): |
Scott Baker | 379404c | 2017-05-24 17:13:45 -0700 | [diff] [blame] | 61 | if VTRService.objects.exists(): |
Scott Baker | c7958a9 | 2017-07-18 11:07:00 -0700 | [diff] [blame^] | 62 | self.fields["owner"].initial = VTRService.objects.all()[0] |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 63 | |
| 64 | def save(self, commit=True): |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 65 | if self.cleaned_data.get("target"): |
Scott Baker | 4acbd4c | 2017-05-18 08:24:43 -0700 | [diff] [blame] | 66 | 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] | 67 | self.instance.target_id = self.cleaned_data.get("target").id |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 68 | return super(VTRTenantForm, self).save(commit=commit) |
| 69 | |
| 70 | class Meta: |
| 71 | model = VTRTenant |
Zack Williams | 69b3edc | 2016-06-27 08:35:17 -0700 | [diff] [blame] | 72 | fields = '__all__' |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 73 | |
| 74 | class VTRTenantAdmin(ReadOnlyAwareAdmin): |
Scott Baker | 825cd73 | 2017-05-19 08:40:30 -0700 | [diff] [blame] | 75 | list_display = ('backend_status_icon', 'id', 'target_type', 'target_id', 'test', 'argument' ) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 76 | list_display_links = ('backend_status_icon', 'id') |
Scott Baker | c7958a9 | 2017-07-18 11:07:00 -0700 | [diff] [blame^] | 77 | fieldsets = [ (None, {'fields': ['backend_status_text', 'owner', |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 78 | '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 Williams | 93a777d | 2016-06-27 13:08:09 -0700 | [diff] [blame] | 88 | def get_queryset(self, request): |
Scott Baker | 379404c | 2017-05-24 17:13:45 -0700 | [diff] [blame] | 89 | return VTRTenant.select_by_user(request.user) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 90 | |
| 91 | admin.site.register(VTRService, VTRServiceAdmin) |
| 92 | admin.site.register(VTRTenant, VTRTenantAdmin) |
| 93 | |