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): |
| 17 | super(VSgwServiceForm, self).__init__(*args, **kwargs) |
| 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 |
| 31 | form = VSgwServiceForm |
| 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): |
| 71 | super(ExampleTenantForm, self).__init__(*args, **kwargs) |
| 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 |
| 81 | |
| 82 | if (not self.instance) or (not self.instance.pk): |
| 83 | self.fields['creator'].initial = get_request().user |
| 84 | if VSGWService.get_service_objects().exists(): |
| 85 | self.fields['provider_service'].initial = VSGWService.get_service_objects().all()[0] |
| 86 | |
| 87 | def save(self, commit=True): |
| 88 | self.instance.creator = self.cleaned_data.get('creator') |
| 89 | self.instance.tenant_message = self.cleaned_data.get('tenant_message') |
| 90 | return super(VSGWTenantForm, self).save(commit=commit) |
| 91 | |
| 92 | |
| 93 | class VSGWTenantAdmin(ReadOnlyAwareAdmin): |
| 94 | |
| 95 | verbose_name = TENANT_NAME_VERBOSE |
| 96 | verbose_name_plural = TENANT_NAME_VERBOSE_PLURAL |
| 97 | |
| 98 | list_display = ('id', 'backend_status_icon', 'instance', 'tenant_message') |
| 99 | list_display_links = ('backend_status_icon', 'instance', 'tenant_message', 'id') |
| 100 | |
| 101 | fieldsets = [(None, { |
| 102 | 'fields': ['backend_status_text', 'kind', 'provider_service', 'instance', 'creator', 'tenant_message'], |
| 103 | 'classes': ['suit-tab suit-tab-general'], |
| 104 | })] |
| 105 | |
| 106 | readonly_fields = ('backend_status_text', 'instance',) |
| 107 | |
| 108 | form = VSGWTenantForm |
| 109 | |
| 110 | suit_form_tabs = (('general', 'Details'),) |
| 111 | |
| 112 | def get_queryset(self, request): |
| 113 | return VSGWTenant.get_tenant_objects_by_user(request.user) |
| 114 | |
| 115 | admin.site.register(VSGWTenant, VSGWTenantAdmin) |