Scott Baker | 8e6647a | 2016-06-20 17:16:20 -0700 | [diff] [blame] | 1 | from django.contrib import admin |
| 2 | |
| 3 | from services.volt.models import * |
| 4 | from django import forms |
| 5 | from django.utils.safestring import mark_safe |
| 6 | from django.contrib.auth.admin import UserAdmin |
| 7 | from django.contrib.admin.widgets import FilteredSelectMultiple |
| 8 | from django.contrib.auth.forms import ReadOnlyPasswordHashField |
| 9 | from django.contrib.auth.signals import user_logged_in |
| 10 | from django.utils import timezone |
| 11 | from django.contrib.contenttypes import generic |
| 12 | from suit.widgets import LinkedSelect |
| 13 | from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, TenantRootTenantInline, TenantRootPrivilegeInline |
| 14 | from core.middleware import get_request |
| 15 | |
| 16 | from functools import update_wrapper |
| 17 | from django.contrib.admin.views.main import ChangeList |
| 18 | from django.core.urlresolvers import reverse |
| 19 | from django.contrib.admin.utils import quote |
| 20 | |
| 21 | #----------------------------------------------------------------------------- |
| 22 | # vOLT |
| 23 | #----------------------------------------------------------------------------- |
| 24 | |
| 25 | class VOLTServiceAdmin(ReadOnlyAwareAdmin): |
| 26 | model = VOLTService |
| 27 | verbose_name = "vOLT Service" |
| 28 | verbose_name_plural = "vOLT Service" |
| 29 | list_display = ("backend_status_icon", "name", "enabled") |
| 30 | list_display_links = ('backend_status_icon', 'name', ) |
| 31 | fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description',"view_url","icon_url" ], 'classes':['suit-tab suit-tab-general']})] |
| 32 | readonly_fields = ('backend_status_text', ) |
| 33 | inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline] |
| 34 | |
| 35 | extracontext_registered_admins = True |
| 36 | |
| 37 | user_readonly_fields = ["name", "enabled", "versionNumber", "description"] |
| 38 | |
| 39 | suit_form_tabs =(('general', 'vOLT Service Details'), |
| 40 | ('administration', 'Administration'), |
| 41 | #('tools', 'Tools'), |
| 42 | ('slices','Slices'), |
| 43 | ('serviceattrs','Additional Attributes'), |
| 44 | ('serviceprivileges','Privileges'), |
| 45 | ) |
| 46 | |
| 47 | suit_form_includes = (('voltadmin.html', 'top', 'administration'), |
| 48 | ) #('hpctools.html', 'top', 'tools') ) |
| 49 | |
| 50 | def queryset(self, request): |
| 51 | return VOLTService.get_service_objects_by_user(request.user) |
| 52 | |
| 53 | class VOLTTenantForm(forms.ModelForm): |
| 54 | s_tag = forms.CharField() |
| 55 | c_tag = forms.CharField() |
| 56 | creator = forms.ModelChoiceField(queryset=User.objects.all()) |
| 57 | |
| 58 | def __init__(self,*args,**kwargs): |
| 59 | super (VOLTTenantForm,self ).__init__(*args,**kwargs) |
| 60 | self.fields['kind'].widget.attrs['readonly'] = True |
| 61 | self.fields['provider_service'].queryset = VOLTService.get_service_objects().all() |
| 62 | if self.instance: |
| 63 | # fields for the attributes |
| 64 | self.fields['c_tag'].initial = self.instance.c_tag |
| 65 | self.fields['s_tag'].initial = self.instance.s_tag |
| 66 | self.fields['creator'].initial = self.instance.creator |
| 67 | if (not self.instance) or (not self.instance.pk): |
| 68 | # default fields for an 'add' form |
| 69 | self.fields['kind'].initial = VOLT_KIND |
| 70 | self.fields['creator'].initial = get_request().user |
| 71 | if VOLTService.get_service_objects().exists(): |
| 72 | self.fields["provider_service"].initial = VOLTService.get_service_objects().all()[0] |
| 73 | |
| 74 | def save(self, commit=True): |
| 75 | self.instance.s_tag = self.cleaned_data.get("s_tag") |
| 76 | self.instance.c_tag = self.cleaned_data.get("c_tag") |
| 77 | self.instance.creator = self.cleaned_data.get("creator") |
| 78 | return super(VOLTTenantForm, self).save(commit=commit) |
| 79 | |
| 80 | class Meta: |
| 81 | model = VOLTTenant |
| 82 | |
| 83 | class VOLTTenantAdmin(ReadOnlyAwareAdmin): |
| 84 | list_display = ('backend_status_icon', 'id', 'service_specific_id', 's_tag', 'c_tag', 'subscriber_root' ) |
| 85 | list_display_links = ('backend_status_icon', 'id') |
| 86 | fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'provider_service', 'subscriber_root', 'service_specific_id', # 'service_specific_attribute', |
| 87 | 's_tag', 'c_tag', 'creator'], |
| 88 | 'classes':['suit-tab suit-tab-general']})] |
| 89 | readonly_fields = ('backend_status_text', 'service_specific_attribute') |
| 90 | form = VOLTTenantForm |
| 91 | |
| 92 | suit_form_tabs = (('general','Details'),) |
| 93 | |
| 94 | def queryset(self, request): |
| 95 | return VOLTTenant.get_tenant_objects_by_user(request.user) |
| 96 | |
| 97 | class AccessDeviceInline(XOSTabularInline): |
| 98 | model = AccessDevice |
| 99 | fields = ['volt_device','uplink','vlan'] |
| 100 | readonly_fields = [] |
| 101 | extra = 0 |
| 102 | # max_num = 0 |
| 103 | suit_classes = 'suit-tab suit-tab-accessdevices' |
| 104 | |
| 105 | # @property |
| 106 | # def selflink_reverse_path(self): |
| 107 | # return "admin:cord_volttenant_change" |
| 108 | |
| 109 | class VOLTDeviceAdmin(ReadOnlyAwareAdmin): |
| 110 | list_display = ('backend_status_icon', 'name', 'openflow_id', 'driver' ) |
| 111 | list_display_links = ('backend_status_icon', 'name', 'openflow_id') |
| 112 | fieldsets = [ (None, {'fields': ['backend_status_text','name','volt_service','openflow_id','driver','access_agent'], |
| 113 | 'classes':['suit-tab suit-tab-general']})] |
| 114 | readonly_fields = ('backend_status_text',) |
| 115 | inlines = [AccessDeviceInline] |
| 116 | |
| 117 | suit_form_tabs = (('general','Details'), ('accessdevices','Access Devices')) |
| 118 | |
| 119 | class AccessDeviceAdmin(ReadOnlyAwareAdmin): |
| 120 | list_display = ('backend_status_icon', 'id', 'volt_device', 'uplink', 'vlan' ) |
| 121 | list_display_links = ('backend_status_icon', 'id') |
| 122 | fieldsets = [ (None, {'fields': ['backend_status_text','volt_device','uplink','vlan'], |
| 123 | 'classes':['suit-tab suit-tab-general']})] |
| 124 | readonly_fields = ('backend_status_text',) |
| 125 | |
| 126 | suit_form_tabs = (('general','Details'),) |
| 127 | |
| 128 | class AgentPortMappingInline(XOSTabularInline): |
| 129 | model = AgentPortMapping |
| 130 | fields = ['access_agent', 'mac', 'port'] |
| 131 | readonly_fields = [] |
| 132 | extra = 0 |
| 133 | # max_num = 0 |
| 134 | suit_classes = 'suit-tab suit-tab-accessportmaps' |
| 135 | |
| 136 | # @property |
| 137 | # def selflink_reverse_path(self): |
| 138 | # return "admin:cord_volttenant_change" |
| 139 | |
| 140 | class AccessAgentAdmin(ReadOnlyAwareAdmin): |
| 141 | list_display = ('backend_status_icon', 'name', 'mac' ) |
| 142 | list_display_links = ('backend_status_icon', 'name') |
| 143 | fieldsets = [ (None, {'fields': ['backend_status_text','name','volt_service','mac'], |
| 144 | 'classes':['suit-tab suit-tab-general']})] |
| 145 | readonly_fields = ('backend_status_text',) |
| 146 | inlines= [AgentPortMappingInline] |
| 147 | |
| 148 | suit_form_tabs = (('general','Details'), ('accessportmaps', 'Port Mappings')) |
| 149 | |
| 150 | # ------------------------------------------- |
| 151 | # CORDSubscriberRoot |
| 152 | # ------------------------------------------- |
| 153 | |
| 154 | class VOLTTenantInline(XOSTabularInline): |
| 155 | model = VOLTTenant |
| 156 | fields = ['provider_service', 'subscriber_root', 'service_specific_id'] |
| 157 | readonly_fields = ['provider_service', 'subscriber_root', 'service_specific_id'] |
| 158 | extra = 0 |
| 159 | max_num = 0 |
| 160 | suit_classes = 'suit-tab suit-tab-volttenants' |
| 161 | fk_name = 'subscriber_root' |
| 162 | verbose_name = 'subscribed tenant' |
| 163 | verbose_name_plural = 'subscribed tenants' |
| 164 | |
| 165 | @property |
| 166 | def selflink_reverse_path(self): |
| 167 | return "admin:cord_volttenant_change" |
| 168 | |
| 169 | def queryset(self, request): |
| 170 | qs = super(VOLTTenantInline, self).queryset(request) |
| 171 | return qs.filter(kind=VOLT_KIND) |
| 172 | |
| 173 | class CordSubscriberRootForm(forms.ModelForm): |
| 174 | url_filter_level = forms.CharField(required = False) |
| 175 | uplink_speed = forms.CharField(required = False) |
| 176 | downlink_speed = forms.CharField(required = False) |
| 177 | status = forms.ChoiceField(choices=CordSubscriberRoot.status_choices, required=True) |
| 178 | enable_uverse = forms.BooleanField(required=False) |
| 179 | cdn_enable = forms.BooleanField(required=False) |
| 180 | |
| 181 | def __init__(self,*args,**kwargs): |
| 182 | super (CordSubscriberRootForm,self ).__init__(*args,**kwargs) |
| 183 | self.fields['kind'].widget.attrs['readonly'] = True |
| 184 | if self.instance: |
| 185 | self.fields['url_filter_level'].initial = self.instance.url_filter_level |
| 186 | self.fields['uplink_speed'].initial = self.instance.uplink_speed |
| 187 | self.fields['downlink_speed'].initial = self.instance.downlink_speed |
| 188 | self.fields['status'].initial = self.instance.status |
| 189 | self.fields['enable_uverse'].initial = self.instance.enable_uverse |
| 190 | self.fields['cdn_enable'].initial = self.instance.cdn_enable |
| 191 | if (not self.instance) or (not self.instance.pk): |
| 192 | # default fields for an 'add' form |
| 193 | self.fields['kind'].initial = CORD_SUBSCRIBER_KIND |
| 194 | self.fields['uplink_speed'].initial = CordSubscriberRoot.get_default_attribute("uplink_speed") |
| 195 | self.fields['downlink_speed'].initial = CordSubscriberRoot.get_default_attribute("downlink_speed") |
| 196 | self.fields['status'].initial = CordSubscriberRoot.get_default_attribute("status") |
| 197 | self.fields['enable_uverse'].initial = CordSubscriberRoot.get_default_attribute("enable_uverse") |
| 198 | self.fields['cdn_enable'].initial = CordSubscriberRoot.get_default_attribute("cdn_enable") |
| 199 | |
| 200 | def save(self, commit=True): |
| 201 | self.instance.url_filter_level = self.cleaned_data.get("url_filter_level") |
| 202 | self.instance.uplink_speed = self.cleaned_data.get("uplink_speed") |
| 203 | self.instance.downlink_speed = self.cleaned_data.get("downlink_speed") |
| 204 | self.instance.status = self.cleaned_data.get("status") |
| 205 | self.instance.enable_uverse = self.cleaned_data.get("enable_uverse") |
| 206 | self.instance.cdn_enable = self.cleaned_data.get("cdn_enable") |
| 207 | return super(CordSubscriberRootForm, self).save(commit=commit) |
| 208 | |
| 209 | class Meta: |
| 210 | model = CordSubscriberRoot |
| 211 | |
| 212 | class CordSubscriberRootAdmin(ReadOnlyAwareAdmin): |
| 213 | list_display = ('backend_status_icon', 'id', 'name', ) |
| 214 | list_display_links = ('backend_status_icon', 'id', 'name', ) |
| 215 | fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'name', 'service_specific_id', # 'service_specific_attribute', |
| 216 | 'url_filter_level', "uplink_speed", "downlink_speed", "status", "enable_uverse", "cdn_enable"], |
| 217 | 'classes':['suit-tab suit-tab-general']})] |
| 218 | readonly_fields = ('backend_status_text', 'service_specific_attribute',) |
| 219 | form = CordSubscriberRootForm |
| 220 | inlines = (VOLTTenantInline, TenantRootPrivilegeInline) |
| 221 | |
| 222 | suit_form_tabs =(('general', 'Cord Subscriber Root Details'), |
| 223 | ('volttenants','VOLT Tenancy'), |
| 224 | ('tenantrootprivileges','Privileges') |
| 225 | ) |
| 226 | |
| 227 | def queryset(self, request): |
| 228 | return CordSubscriberRoot.get_tenant_objects_by_user(request.user) |
| 229 | |
| 230 | admin.site.register(VOLTService, VOLTServiceAdmin) |
| 231 | admin.site.register(VOLTTenant, VOLTTenantAdmin) |
| 232 | admin.site.register(VOLTDevice, VOLTDeviceAdmin) |
| 233 | admin.site.register(AccessDevice, AccessDeviceAdmin) |
| 234 | admin.site.register(AccessAgent, AccessAgentAdmin) |
| 235 | |
| 236 | admin.site.register(CordSubscriberRoot, CordSubscriberRootAdmin) |
| 237 | |