blob: d6745ebbeb3d08a3ba3ac8f30fedb731467fd9bc [file] [log] [blame]
Scott Baker619de672016-06-20 12:49:38 -07001# admin.py - ExampleService Django Admin
2
3from core.admin import ReadOnlyAwareAdmin, SliceInline
4from core.middleware import get_request
5from core.models import User
6
7from django import forms
8from django.contrib import admin
9
10from services.exampleservice.models import *
11
12class ExampleServiceForm(forms.ModelForm):
13
14 class Meta:
15 model = ExampleService
Zack Williamsc6318902016-06-27 08:21:19 -070016 fields = '__all__'
Scott Baker619de672016-06-20 12:49:38 -070017
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
28class 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 Williams86f0b7f2016-06-27 13:21:24 -070059 def get_queryset(self, request):
Scott Baker589e6c42017-05-25 16:55:18 -070060 return ExampleService.select_by_user(request.user)
Scott Baker619de672016-06-20 12:49:38 -070061
62admin.site.register(ExampleService, ExampleServiceAdmin)
63
64class ExampleTenantForm(forms.ModelForm):
65
66 class Meta:
67 model = ExampleTenant
Zack Williamsc6318902016-06-27 08:21:19 -070068 fields = '__all__'
Scott Baker619de672016-06-20 12:49:38 -070069
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
Scott Baker589e6c42017-05-25 16:55:18 -070078 self.fields['provider_service'].queryset = ExampleService.objects.all()
Scott Baker619de672016-06-20 12:49:38 -070079
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
Scott Baker589e6c42017-05-25 16:55:18 -070086 if ExampleService.objects.exists():
87 self.fields['provider_service'].initial = ExampleService.objects.all()[0]
Scott Baker619de672016-06-20 12:49:38 -070088
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
95class 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 Williams86f0b7f2016-06-27 13:21:24 -0700114 def get_queryset(self, request):
Scott Baker589e6c42017-05-25 16:55:18 -0700115 return ExampleTenant.select_by_user(request.user)
Scott Baker619de672016-06-20 12:49:38 -0700116
117admin.site.register(ExampleTenant, ExampleTenantAdmin)
118