Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 1 | from core.admin import ReadOnlyAwareAdmin, SliceInline |
| 2 | from core.middleware import get_request |
| 3 | from core.models import User |
| 4 | |
| 5 | from django import forms |
| 6 | from django.contrib import admin |
| 7 | |
| 8 | from services.vmme.models import * |
| 9 | |
| 10 | class 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 | |
| 22 | class 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 Bhat | fcbe1eb | 2017-01-10 16:16:17 -0800 | [diff] [blame] | 48 | suit_form_includes = (('mcordadmin.html', |
| 49 | 'top', |
| 50 | 'administration'),) |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 51 | |
| 52 | def get_queryset(self, request): |
| 53 | return VMMEService.get_service_objects_by_user(request.user) |
| 54 | |
| 55 | admin.site.register(VMMEService, VMMEServiceAdmin) |
| 56 | |
| 57 | class 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 Bhat | 22afba8 | 2017-01-21 23:10:09 -0800 | [diff] [blame] | 76 | self.fields['image_name'].initial = self.instance.image_name |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 77 | |
| 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 Bhat | 22afba8 | 2017-01-21 23:10:09 -0800 | [diff] [blame] | 86 | self.instance.image_name = self.cleaned_data.get('image_name') |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 87 | return super(VMMETenantForm, self).save(commit=commit) |
| 88 | |
| 89 | |
| 90 | class VMMETenantAdmin(ReadOnlyAwareAdmin): |
| 91 | |
| 92 | verbose_name = "VMME Service Tenant" |
| 93 | verbose_name_plural = "VMME Service Tenants" |
| 94 | |
Saleil Bhat | 22afba8 | 2017-01-21 23:10:09 -0800 | [diff] [blame] | 95 | 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 Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 97 | |
| 98 | fieldsets = [(None, { |
Saleil Bhat | 22afba8 | 2017-01-21 23:10:09 -0800 | [diff] [blame] | 99 | 'fields': ['backend_status_text', 'kind', 'provider_service', 'instance', 'creator', 'tenant_message', 'image_name'], |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 100 | '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 | |
| 112 | admin.site.register(VMMETenant, VMMETenantAdmin) |