Murat Parlakisik | 66fde4d | 2016-09-22 13:28:13 -0700 | [diff] [blame] | 1 | # admin.py - VSGW Django Admin |
| 2 | |
| 3 | from core.admin import ReadOnlyAwareAdmin, SliceInline |
| 4 | from core.middleware import get_request |
| 5 | from core.models import User |
| 6 | from django import forms |
| 7 | from django.contrib import admin |
| 8 | from services.vsgw.models import * |
| 9 | |
| 10 | class VSGWServiceForm(forms.ModelForm): |
| 11 | |
| 12 | class Meta: |
| 13 | model = VSGWService |
| 14 | fields = '__all__' |
| 15 | |
| 16 | def __init__(self, *args, **kwargs): |
JianHao | ce45e04 | 2017-01-16 11:06:29 +0000 | [diff] [blame] | 17 | super(VSGWServiceForm, self).__init__(*args, **kwargs) |
Murat Parlakisik | 66fde4d | 2016-09-22 13:28:13 -0700 | [diff] [blame] | 18 | |
| 19 | if self.instance: |
| 20 | self.fields['service_message'].initial = self.instance.service_message |
| 21 | |
| 22 | def save(self, commit=True): |
| 23 | self.instance.service_message = self.cleaned_data.get('service_message') |
| 24 | return super(VSGWServiceForm, self).save(commit=commit) |
| 25 | |
| 26 | class VSGWServiceAdmin(ReadOnlyAwareAdmin): |
| 27 | |
| 28 | model = VSGWService |
| 29 | verbose_name = SERVICE_NAME_VERBOSE |
| 30 | verbose_name_plural = SERVICE_NAME_VERBOSE_PLURAL |
JianHao | ce45e04 | 2017-01-16 11:06:29 +0000 | [diff] [blame] | 31 | form = VSGWServiceForm |
Murat Parlakisik | 66fde4d | 2016-09-22 13:28:13 -0700 | [diff] [blame] | 32 | inlines = [SliceInline] |
| 33 | |
| 34 | list_display = ('backend_status_icon', 'name', 'service_message', 'enabled') |
| 35 | list_display_links = ('backend_status_icon', 'name', 'service_message' ) |
| 36 | |
| 37 | fieldsets = [(None, { |
| 38 | 'fields': ['backend_status_text', 'name', 'enabled', 'versionNumber', 'service_message', 'description',], |
| 39 | 'classes':['suit-tab suit-tab-general',], |
| 40 | })] |
| 41 | |
| 42 | readonly_fields = ('backend_status_text', ) |
| 43 | user_readonly_fields = ['name', 'enabled', 'versionNumber', 'description',] |
| 44 | |
| 45 | extracontext_registered_admins = True |
| 46 | |
| 47 | suit_form_tabs = ( |
| 48 | ('general', 'Example Service Details', ), |
| 49 | ('slices', 'Slices',), |
| 50 | ) |
| 51 | |
| 52 | suit_form_includes = (( |
| 53 | 'top', |
| 54 | 'administration'), |
| 55 | ) |
| 56 | |
| 57 | def get_queryset(self, request): |
| 58 | return ExampleService.get_service_objects_by_user(request.user) |
| 59 | |
| 60 | admin.site.register(VSGWService, VSGWServiceAdmin) |
| 61 | |
| 62 | class VSGWTenantForm(forms.ModelForm): |
| 63 | |
| 64 | class Meta: |
| 65 | model = VSGWTenant |
| 66 | fields = '__all__' |
| 67 | |
| 68 | creator = forms.ModelChoiceField(queryset=User.objects.all()) |
| 69 | |
| 70 | def __init__(self, *args, **kwargs): |
JianHao | ce45e04 | 2017-01-16 11:06:29 +0000 | [diff] [blame] | 71 | super(VSGWTenantForm, self).__init__(*args, **kwargs) |
Murat Parlakisik | 66fde4d | 2016-09-22 13:28:13 -0700 | [diff] [blame] | 72 | |
| 73 | self.fields['kind'].widget.attrs['readonly'] = True |
| 74 | self.fields['kind'].initial = SERVICE_NAME |
| 75 | |
| 76 | self.fields['provider_service'].queryset = VSGWService.get_service_objects().all() |
| 77 | |
| 78 | if self.instance: |
| 79 | self.fields['creator'].initial = self.instance.creator |
| 80 | self.fields['tenant_message'].initial = self.instance.tenant_message |
Saleil Bhat | 42e8c5e | 2017-02-01 15:59:54 -0800 | [diff] [blame] | 81 | self.fields['image_name'].initial = self.instance.image_name |
Murat Parlakisik | 66fde4d | 2016-09-22 13:28:13 -0700 | [diff] [blame] | 82 | |
| 83 | if (not self.instance) or (not self.instance.pk): |
| 84 | self.fields['creator'].initial = get_request().user |
| 85 | if VSGWService.get_service_objects().exists(): |
| 86 | self.fields['provider_service'].initial = VSGWService.get_service_objects().all()[0] |
| 87 | |
| 88 | def save(self, commit=True): |
| 89 | self.instance.creator = self.cleaned_data.get('creator') |
| 90 | self.instance.tenant_message = self.cleaned_data.get('tenant_message') |
Saleil Bhat | 42e8c5e | 2017-02-01 15:59:54 -0800 | [diff] [blame] | 91 | self.instance.image_name = self.cleaned_data.get('image_name') |
Murat Parlakisik | 66fde4d | 2016-09-22 13:28:13 -0700 | [diff] [blame] | 92 | return super(VSGWTenantForm, self).save(commit=commit) |
| 93 | |
| 94 | |
| 95 | class VSGWTenantAdmin(ReadOnlyAwareAdmin): |
| 96 | |
| 97 | verbose_name = TENANT_NAME_VERBOSE |
| 98 | verbose_name_plural = TENANT_NAME_VERBOSE_PLURAL |
| 99 | |
Saleil Bhat | 42e8c5e | 2017-02-01 15:59:54 -0800 | [diff] [blame] | 100 | list_display = ('id', 'backend_status_icon', 'instance', 'tenant_message', 'image_name') |
| 101 | list_display_links = ('backend_status_icon', 'instance', 'tenant_message', 'id', 'image_name') |
Murat Parlakisik | 66fde4d | 2016-09-22 13:28:13 -0700 | [diff] [blame] | 102 | |
| 103 | fieldsets = [(None, { |
Saleil Bhat | 42e8c5e | 2017-02-01 15:59:54 -0800 | [diff] [blame] | 104 | 'fields': ['backend_status_text', 'kind', 'provider_service', 'instance', 'creator', 'tenant_message', 'image_name'], |
Murat Parlakisik | 66fde4d | 2016-09-22 13:28:13 -0700 | [diff] [blame] | 105 | 'classes': ['suit-tab suit-tab-general'], |
| 106 | })] |
| 107 | |
| 108 | readonly_fields = ('backend_status_text', 'instance',) |
| 109 | |
| 110 | form = VSGWTenantForm |
| 111 | |
| 112 | suit_form_tabs = (('general', 'Details'),) |
| 113 | |
| 114 | def get_queryset(self, request): |
| 115 | return VSGWTenant.get_tenant_objects_by_user(request.user) |
| 116 | |
JianHao | ce45e04 | 2017-01-16 11:06:29 +0000 | [diff] [blame] | 117 | admin.site.register(VSGWTenant, VSGWTenantAdmin) |