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