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 |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame^] | 13 | from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, ServiceInstanceAttrAsTabInline, SubscriberLinkInline, ProviderLinkInline, ProviderDependencyInline,SubscriberDependencyInline |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 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', ) |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame^] | 29 | inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline, ProviderDependencyInline,SubscriberDependencyInline] |
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'), |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame^] | 39 | ('servicetenants', 'Dependencies'), |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 40 | ('serviceprivileges','Privileges'), |
| 41 | ) |
| 42 | |
| 43 | suit_form_includes = (('onosadmin.html', 'top', 'administration'), |
| 44 | ) |
| 45 | |
Zack Williams | d60c773 | 2016-06-27 13:15:21 -0700 | [diff] [blame] | 46 | def get_queryset(self, request): |
Scott Baker | 2d20057 | 2017-05-25 10:52:00 -0700 | [diff] [blame] | 47 | return ONOSService.select_by_user(request.user) |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 48 | |
| 49 | class ONOSAppForm(forms.ModelForm): |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 50 | def __init__(self,*args,**kwargs): |
| 51 | super (ONOSAppForm,self ).__init__(*args,**kwargs) |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame^] | 52 | # self.fields['kind'].widget.attrs['readonly'] = True |
| 53 | self.fields['owner'].queryset = ONOSService.objects.all() |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 54 | if (not self.instance) or (not self.instance.pk): |
| 55 | # default fields for an 'add' form |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame^] | 56 | # self.fields['kind'].initial = ONOS_KIND |
| 57 | # self.fields['creator'].initial = get_request().user |
Scott Baker | 88b7109 | 2017-03-13 15:15:29 -0700 | [diff] [blame] | 58 | if ONOSService.objects.exists(): |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame^] | 59 | self.fields["owner"].initial = ONOSService.objects.all()[0] |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 60 | |
| 61 | def save(self, commit=True): |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 62 | return super(ONOSAppForm, self).save(commit=commit) |
| 63 | |
| 64 | class Meta: |
| 65 | model = ONOSApp |
Zack Williams | 3026c52 | 2016-06-27 08:30:53 -0700 | [diff] [blame] | 66 | fields = '__all__' |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 67 | |
| 68 | class ONOSAppAdmin(ReadOnlyAwareAdmin): |
| 69 | list_display = ('backend_status_icon', 'name', ) |
| 70 | list_display_links = ('backend_status_icon', 'name') |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame^] | 71 | fieldsets = [ (None, {'fields': ['backend_status_text', 'name', 'owner', 'service_specific_attribute', "dependencies"], |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 72 | 'classes':['suit-tab suit-tab-general']})] |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame^] | 73 | readonly_fields = ('backend_status_text', 'service_specific_attribute') |
| 74 | inlines = [ServiceInstanceAttrAsTabInline, ProviderLinkInline, SubscriberLinkInline] |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 75 | form = ONOSAppForm |
| 76 | |
Scott Baker | 8d8f992 | 2017-07-17 17:30:42 -0700 | [diff] [blame^] | 77 | suit_form_tabs = (('general','Details'), ('serviceinstanceattrs', 'Attributes'), ('servicelinks','Links'),) |
Scott Baker | 7a32759 | 2016-06-20 17:34:06 -0700 | [diff] [blame] | 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 | |