blob: df0f636e9bc51cc4e0cc9265b01c4f057ca0e8a4 [file] [log] [blame]
Saleil Bhat1c5593d2017-01-06 15:25:57 -08001from core.admin import ReadOnlyAwareAdmin, SliceInline
2from core.middleware import get_request
3from core.models import User
4
5from django import forms
6from django.contrib import admin
7
8from services.vmme.models import *
9
10class VMMEServiceForm(forms.ModelForm):
11
12 class Meta:
13 model = VMMEService
14 fields = '__all__'
15
16 def __init__(self, *args, **kwargs):
17 super(VMMEServiceForm, self).__init__(*args, **kwargs)
18
19 def save(self, commit=True):
20 return super(VMMEServiceForm, self).save(commit=commit)
21
22class VMMEServiceAdmin(ReadOnlyAwareAdmin):
23
24 model = VMMEService
25 verbose_name = "VMME Service"
26 verbose_name_plural = "VMME Services"
27 form = VMMEServiceForm
28 inlines = [SliceInline]
29
30 list_display = ('backend_status_icon', 'name', 'enabled')
31 list_display_links = ('backend_status_icon', 'name')
32
33 fieldsets = [(None, {
34 'fields': ['backend_status_text', 'name', 'enabled', 'versionNumber', 'description',],
35 'classes':['suit-tab suit-tab-general',],
36 })]
37
38 readonly_fields = ('backend_status_text', )
39 user_readonly_fields = ['name', 'enabled', 'versionNumber', 'description',]
40
41 extracontext_registered_admins = True
42
43 suit_form_tabs = (
44 ('general', 'VMME Service Details', ),
45 ('slices', 'Slices',),
46 )
47
Saleil Bhatfcbe1eb2017-01-10 16:16:17 -080048 suit_form_includes = (('mcordadmin.html',
49 'top',
50 'administration'),)
Saleil Bhat1c5593d2017-01-06 15:25:57 -080051
52 def get_queryset(self, request):
53 return VMMEService.get_service_objects_by_user(request.user)
54
55admin.site.register(VMMEService, VMMEServiceAdmin)
56
57class VMMETenantForm(forms.ModelForm):
58
59 class Meta:
60 model = VMMETenant
61 fields = '__all__'
62
63 creator = forms.ModelChoiceField(queryset=User.objects.all())
64
65 def __init__(self, *args, **kwargs):
66 super(VMMETenantForm, self).__init__(*args, **kwargs)
67
68 self.fields['kind'].widget.attrs['readonly'] = True
69 self.fields['kind'].initial = "vmme"
70
71 self.fields['provider_service'].queryset = VMMEService.get_service_objects().all()
72
73 if self.instance:
74 self.fields['creator'].initial = self.instance.creator
75 self.fields['tenant_message'].initial = self.instance.tenant_message
Saleil Bhat22afba82017-01-21 23:10:09 -080076 self.fields['image_name'].initial = self.instance.image_name
Saleil Bhat1c5593d2017-01-06 15:25:57 -080077
78 if (not self.instance) or (not self.instance.pk):
79 self.fields['creator'].initial = get_request().user
80 if VMMEService.get_service_objects().exists():
81 self.fields['provider_service'].initial = VMMEService.get_service_objects().all()[0]
82
83 def save(self, commit=True):
84 self.instance.creator = self.cleaned_data.get('creator')
85 self.instance.tenant_message = self.cleaned_data.get('tenant_message')
Saleil Bhat22afba82017-01-21 23:10:09 -080086 self.instance.image_name = self.cleaned_data.get('image_name')
Saleil Bhat1c5593d2017-01-06 15:25:57 -080087 return super(VMMETenantForm, self).save(commit=commit)
88
89
90class VMMETenantAdmin(ReadOnlyAwareAdmin):
91
92 verbose_name = "VMME Service Tenant"
93 verbose_name_plural = "VMME Service Tenants"
94
Saleil Bhat22afba82017-01-21 23:10:09 -080095 list_display = ('id', 'backend_status_icon', 'instance', 'tenant_message', 'image_name')
96 list_display_links = ('backend_status_icon', 'instance', 'tenant_message', 'id', 'image_name')
Saleil Bhat1c5593d2017-01-06 15:25:57 -080097
98 fieldsets = [(None, {
Saleil Bhat22afba82017-01-21 23:10:09 -080099 'fields': ['backend_status_text', 'kind', 'provider_service', 'instance', 'creator', 'tenant_message', 'image_name'],
Saleil Bhat1c5593d2017-01-06 15:25:57 -0800100 'classes': ['suit-tab suit-tab-general'],
101 })]
102
103 readonly_fields = ('backend_status_text', 'instance',)
104
105 form = VMMETenantForm
106
107 suit_form_tabs = (('general', 'Details'),)
108
109 def get_queryset(self, request):
110 return VMMETenant.get_tenant_objects_by_user(request.user)
111
112admin.site.register(VMMETenant, VMMETenantAdmin)