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 | |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 21 | class ONOSServiceAdmin(ReadOnlyAwareAdmin): |
| 22 | model = ONOSService |
| 23 | verbose_name = "ONOS Service" |
| 24 | verbose_name_plural = "ONOS Services" |
| 25 | list_display = ("backend_status_icon", "name", "enabled") |
| 26 | list_display_links = ('backend_status_icon', 'name', ) |
| 27 | 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']})] |
| 28 | readonly_fields = ('backend_status_text', ) |
| 29 | inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline] |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 30 | |
| 31 | extracontext_registered_admins = True |
| 32 | |
| 33 | user_readonly_fields = ["name", "enabled", "versionNumber", "description"] |
| 34 | |
| 35 | suit_form_tabs =(('general', 'ONOS Service Details'), |
| 36 | ('administration', 'Administration'), |
| 37 | ('slices','Slices'), |
| 38 | ('serviceattrs','Additional Attributes'), |
| 39 | ('serviceprivileges','Privileges'), |
| 40 | ) |
| 41 | |
| 42 | suit_form_includes = (('onosadmin.html', 'top', 'administration'), |
| 43 | ) |
| 44 | |
Zack Williams | d60c773 | 2016-06-27 13:15:21 -0700 | [diff] [blame] | 45 | def get_queryset(self, request): |
Scott Baker | 2d20057 | 2017-05-25 10:52:00 -0700 | [diff] [blame] | 46 | return ONOSService.select_by_user(request.user) |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 47 | |
| 48 | class ONOSAppForm(forms.ModelForm): |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 49 | def __init__(self,*args,**kwargs): |
| 50 | super (ONOSAppForm,self ).__init__(*args,**kwargs) |
| 51 | self.fields['kind'].widget.attrs['readonly'] = True |
Scott Baker | 88b7109 | 2017-03-13 15:15:29 -0700 | [diff] [blame] | 52 | self.fields['provider_service'].queryset = ONOSService.objects.all() |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 53 | if (not self.instance) or (not self.instance.pk): |
| 54 | # default fields for an 'add' form |
| 55 | self.fields['kind'].initial = ONOS_KIND |
| 56 | self.fields['creator'].initial = get_request().user |
Scott Baker | 88b7109 | 2017-03-13 15:15:29 -0700 | [diff] [blame] | 57 | if ONOSService.objects.exists(): |
Scott Baker | 2d20057 | 2017-05-25 10:52:00 -0700 | [diff] [blame] | 58 | self.fields["provider_service"].initial = ONOSService.objects.all()[0] |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 59 | |
| 60 | def save(self, commit=True): |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 61 | return super(ONOSAppForm, self).save(commit=commit) |
| 62 | |
| 63 | class Meta: |
| 64 | model = ONOSApp |
Zack Williams | 3026c52 | 2016-06-27 08:30:53 -0700 | [diff] [blame] | 65 | fields = '__all__' |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 66 | |
| 67 | class ONOSAppAdmin(ReadOnlyAwareAdmin): |
| 68 | list_display = ('backend_status_icon', 'name', ) |
| 69 | list_display_links = ('backend_status_icon', 'name') |
| 70 | fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'name', 'provider_service', 'subscriber_service', 'service_specific_attribute', "dependencies", |
| 71 | 'creator'], |
| 72 | 'classes':['suit-tab suit-tab-general']})] |
| 73 | readonly_fields = ('backend_status_text', 'instance', 'service_specific_attribute') |
| 74 | inlines = [TenantAttrAsTabInline] |
| 75 | form = ONOSAppForm |
| 76 | |
| 77 | suit_form_tabs = (('general','Details'), ('tenantattrs', 'Attributes')) |
| 78 | |
Zack Williams | d60c773 | 2016-06-27 13:15:21 -0700 | [diff] [blame] | 79 | def get_queryset(self, request): |
Scott Baker | 2d20057 | 2017-05-25 10:52:00 -0700 | [diff] [blame] | 80 | return ONOSApp.select_by_user(request.user) |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 81 | |
| 82 | admin.site.register(ONOSService, ONOSServiceAdmin) |
| 83 | admin.site.register(ONOSApp, ONOSAppAdmin) |
| 84 | |