Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 1 | from django.contrib import admin |
| 2 | |
| 3 | from services.vrouter.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.models import AddressPool |
| 14 | from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, AddressPoolInline |
| 15 | from core.middleware import get_request |
| 16 | |
| 17 | from functools import update_wrapper |
| 18 | from django.contrib.admin.views.main import ChangeList |
| 19 | from django.core.urlresolvers import reverse |
| 20 | from django.contrib.admin.utils import quote |
| 21 | |
| 22 | class VRouterServiceForm(forms.ModelForm): |
| 23 | def __init__(self,*args,**kwargs): |
| 24 | super (VRouterServiceForm,self ).__init__(*args,**kwargs) |
| 25 | |
| 26 | def save(self, commit=True): |
| 27 | return super(VRouterServiceForm, self).save(commit=commit) |
| 28 | |
| 29 | class Meta: |
| 30 | model = VRouterService |
| 31 | |
| 32 | class VRouterServiceAdmin(ReadOnlyAwareAdmin): |
| 33 | model = VRouterService |
| 34 | verbose_name = "vRouter Service" |
| 35 | verbose_name_plural = "vRouter Service" |
| 36 | list_display = ("backend_status_icon", "name", "enabled") |
| 37 | list_display_links = ('backend_status_icon', 'name', ) |
| 38 | fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description', "view_url", "icon_url", ], |
| 39 | 'classes':['suit-tab suit-tab-general']})] |
| 40 | readonly_fields = ('backend_status_text', ) |
| 41 | inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline,AddressPoolInline] |
| 42 | form = VRouterServiceForm |
| 43 | |
| 44 | extracontext_registered_admins = True |
| 45 | |
| 46 | user_readonly_fields = ["name", "enabled", "versionNumber", "description"] |
| 47 | |
| 48 | suit_form_tabs =(('general', 'vRouter Service Details'), |
| 49 | ('administration', 'Administration'), |
| 50 | ('addresspools', 'Addresses'), |
| 51 | #('tools', 'Tools'), |
| 52 | ('slices','Slices'), |
| 53 | ('serviceattrs','Additional Attributes'), |
| 54 | ('serviceprivileges','Privileges'), |
| 55 | ) |
| 56 | |
| 57 | suit_form_includes = (('vrouteradmin.html', 'top', 'administration'), |
| 58 | ) #('hpctools.html', 'top', 'tools') ) |
| 59 | |
| 60 | def queryset(self, request): |
| 61 | return VRouterService.get_service_objects_by_user(request.user) |
| 62 | |
| 63 | class VRouterTenantForm(forms.ModelForm): |
| 64 | public_ip = forms.CharField(required=True) |
| 65 | public_mac = forms.CharField(required=True) |
| 66 | gateway_ip = forms.CharField(required=False) |
| 67 | gateway_mac = forms.CharField(required=False) |
| 68 | cidr = forms.CharField(required=False) |
| 69 | address_pool = forms.ModelChoiceField(queryset=AddressPool.objects.all(),required=False) |
| 70 | |
| 71 | def __init__(self,*args,**kwargs): |
| 72 | super (VRouterTenantForm,self ).__init__(*args,**kwargs) |
| 73 | self.fields['kind'].widget.attrs['readonly'] = True |
| 74 | self.fields['provider_service'].queryset = VRouterService.get_service_objects().all() |
| 75 | if self.instance: |
| 76 | # fields for the attributes |
| 77 | self.fields['address_pool'].initial = self.instance.address_pool |
| 78 | self.fields['public_ip'].initial = self.instance.public_ip |
| 79 | self.fields['public_mac'].initial = self.instance.public_mac |
| 80 | self.fields['gateway_ip'].initial = self.instance.gateway_ip |
| 81 | self.fields['gateway_mac'].initial = self.instance.gateway_mac |
| 82 | self.fields['cidr'].initial = self.instance.cidr |
| 83 | if (not self.instance) or (not self.instance.pk): |
| 84 | # default fields for an 'add' form |
| 85 | self.fields['kind'].initial = VROUTER_KIND |
| 86 | if VRouterService.get_service_objects().exists(): |
| 87 | self.fields["provider_service"].initial = VRouterService.get_service_objects().all()[0] |
| 88 | |
| 89 | def save(self, commit=True): |
| 90 | self.instance.public_ip = self.cleaned_data.get("public_ip") |
| 91 | self.instance.public_mac = self.cleaned_data.get("public_mac") |
| 92 | self.instance.address_pool = self.cleaned_data.get("address_pool") |
| 93 | return super(VRouterTenantForm, self).save(commit=commit) |
| 94 | |
| 95 | class Meta: |
| 96 | model = VRouterTenant |
| 97 | |
| 98 | class VRouterTenantAdmin(ReadOnlyAwareAdmin): |
| 99 | list_display = ('backend_status_icon', 'id', 'subscriber_tenant', 'public_ip' ) |
| 100 | list_display_links = ('backend_status_icon', 'id') |
| 101 | fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'provider_service', 'subscriber_tenant', 'subscriber_service', |
| 102 | 'address_pool', 'public_ip', 'public_mac', 'gateway_ip', 'gateway_mac', 'cidr'], |
| 103 | 'classes':['suit-tab suit-tab-general']})] |
| 104 | readonly_fields = ('backend_status_text', 'service_specific_attribute', 'gateway_ip', 'gateway_mac', 'cidr') |
| 105 | form = VRouterTenantForm |
| 106 | |
| 107 | suit_form_tabs = (('general','Details'),) |
| 108 | |
| 109 | def queryset(self, request): |
| 110 | return VRouterTenant.get_tenant_objects_by_user(request.user) |
| 111 | |
| 112 | admin.site.register(VRouterService, VRouterServiceAdmin) |
| 113 | admin.site.register(VRouterTenant, VRouterTenantAdmin) |
| 114 | |