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