Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 1 | from django.contrib import admin |
| 2 | |
| 3 | from services.onos.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, TenantAttrAsTabInline |
| 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 | class ONOSServiceForm(forms.ModelForm): |
| 22 | rest_hostname = forms.CharField(required=False) |
| 23 | rest_port = forms.CharField(required=False) |
| 24 | no_container = forms.BooleanField(required=False) |
| 25 | # external_hostname = forms.CharField(required=False) |
| 26 | # external_container = forms.CharField(required=False) |
| 27 | |
| 28 | def __init__(self,*args,**kwargs): |
| 29 | super (ONOSServiceForm,self ).__init__(*args,**kwargs) |
| 30 | if self.instance: |
| 31 | # fields for the attributes |
| 32 | self.fields['rest_hostname'].initial = self.instance.rest_hostname |
| 33 | self.fields['rest_port'].initial = self.instance.rest_port |
| 34 | self.fields['no_container'].initial = self.instance.no_container |
| 35 | # self.fields['external_hostname'].initial = self.instance.external_hostname |
| 36 | # self.fields['external_container'].initial = self.instance.external_hostname |
| 37 | |
| 38 | def save(self, commit=True): |
| 39 | self.instance.rest_hostname = self.cleaned_data.get("rest_hostname") |
| 40 | self.instance.rest_port = self.cleaned_data.get("rest_port") |
| 41 | self.instance.no_container = self.cleaned_data.get("no_container") |
| 42 | # self.instance.external_hostname = self.cleaned_data.get("external_hostname") |
| 43 | # self.instance.external_container = self.cleaned_data.get("external_container") |
| 44 | return super(ONOSServiceForm, self).save(commit=commit) |
| 45 | |
| 46 | class Meta: |
| 47 | model = ONOSService |
| 48 | |
| 49 | class ONOSServiceAdmin(ReadOnlyAwareAdmin): |
| 50 | model = ONOSService |
| 51 | verbose_name = "ONOS Service" |
| 52 | verbose_name_plural = "ONOS Services" |
| 53 | list_display = ("backend_status_icon", "name", "enabled") |
| 54 | list_display_links = ('backend_status_icon', 'name', ) |
| 55 | fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description',"view_url","icon_url", "rest_hostname", "rest_port", "no_container" ], 'classes':['suit-tab suit-tab-general']})] |
| 56 | readonly_fields = ('backend_status_text', ) |
| 57 | inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline] |
| 58 | form = ONOSServiceForm |
| 59 | |
| 60 | extracontext_registered_admins = True |
| 61 | |
| 62 | user_readonly_fields = ["name", "enabled", "versionNumber", "description"] |
| 63 | |
| 64 | suit_form_tabs =(('general', 'ONOS Service Details'), |
| 65 | ('administration', 'Administration'), |
| 66 | ('slices','Slices'), |
| 67 | ('serviceattrs','Additional Attributes'), |
| 68 | ('serviceprivileges','Privileges'), |
| 69 | ) |
| 70 | |
| 71 | suit_form_includes = (('onosadmin.html', 'top', 'administration'), |
| 72 | ) |
| 73 | |
| 74 | def queryset(self, request): |
| 75 | return ONOSService.get_service_objects_by_user(request.user) |
| 76 | |
| 77 | class ONOSAppForm(forms.ModelForm): |
| 78 | creator = forms.ModelChoiceField(queryset=User.objects.all()) |
| 79 | name = forms.CharField() |
| 80 | dependencies = forms.CharField(required=False) |
| 81 | |
| 82 | def __init__(self,*args,**kwargs): |
| 83 | super (ONOSAppForm,self ).__init__(*args,**kwargs) |
| 84 | self.fields['kind'].widget.attrs['readonly'] = True |
| 85 | self.fields['provider_service'].queryset = ONOSService.get_service_objects().all() |
| 86 | if self.instance: |
| 87 | # fields for the attributes |
| 88 | self.fields['creator'].initial = self.instance.creator |
| 89 | self.fields['name'].initial = self.instance.name |
| 90 | self.fields['dependencies'].initial = self.instance.dependencies |
| 91 | if (not self.instance) or (not self.instance.pk): |
| 92 | # default fields for an 'add' form |
| 93 | self.fields['kind'].initial = ONOS_KIND |
| 94 | self.fields['creator'].initial = get_request().user |
| 95 | if ONOSService.get_service_objects().exists(): |
| 96 | self.fields["provider_service"].initial = ONOSService.get_service_objects().all()[0] |
| 97 | |
| 98 | def save(self, commit=True): |
| 99 | self.instance.creator = self.cleaned_data.get("creator") |
| 100 | self.instance.name = self.cleaned_data.get("name") |
| 101 | self.instance.dependencies = self.cleaned_data.get("dependencies") |
| 102 | return super(ONOSAppForm, self).save(commit=commit) |
| 103 | |
| 104 | class Meta: |
| 105 | model = ONOSApp |
| 106 | |
| 107 | class ONOSAppAdmin(ReadOnlyAwareAdmin): |
| 108 | list_display = ('backend_status_icon', 'name', ) |
| 109 | list_display_links = ('backend_status_icon', 'name') |
| 110 | fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'name', 'provider_service', 'subscriber_service', 'service_specific_attribute', "dependencies", |
| 111 | 'creator'], |
| 112 | 'classes':['suit-tab suit-tab-general']})] |
| 113 | readonly_fields = ('backend_status_text', 'instance', 'service_specific_attribute') |
| 114 | inlines = [TenantAttrAsTabInline] |
| 115 | form = ONOSAppForm |
| 116 | |
| 117 | suit_form_tabs = (('general','Details'), ('tenantattrs', 'Attributes')) |
| 118 | |
| 119 | def queryset(self, request): |
| 120 | return ONOSApp.get_tenant_objects_by_user(request.user) |
| 121 | |
| 122 | admin.site.register(ONOSService, ONOSServiceAdmin) |
| 123 | admin.site.register(ONOSApp, ONOSAppAdmin) |
| 124 | |