Gabe Black | 9062322 | 2017-01-18 19:52:28 +0000 | [diff] [blame] | 1 | # admin.py - PassiveTest 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.passivetest.models import * |
| 11 | |
| 12 | class PassiveTestForm(forms.ModelForm): |
| 13 | |
| 14 | class Meta: |
| 15 | model = PassiveTestService |
| 16 | fields = '__all__' |
| 17 | |
| 18 | def __init__(self, *args, **kwargs): |
| 19 | super(PassiveTestForm, self).__init__(*args, **kwargs) |
| 20 | |
| 21 | if self.instance: |
| 22 | self.fields['tap_ports'].initial = self.instance.tap_ports |
| 23 | self.fields['reset_viv'].initial = self.instance.reset_viv |
| 24 | |
| 25 | def save(self, commit=True): |
| 26 | self.instance.tap_ports = self.cleaned_data.get('tap_ports') |
| 27 | self.instance.reset_viv = self.cleaned_data.get('reset_viv') |
| 28 | return super(PassiveTestForm, self).save(commit=commit) |
| 29 | |
| 30 | class PassiveTestAdmin(ReadOnlyAwareAdmin): |
| 31 | |
| 32 | model = PassiveTestService |
| 33 | verbose_name = PASSIVETEST_SERVICE_NAME_VERBOSE |
| 34 | verbose_name_plural = PASSIVETEST_SERVICE_NAME_VERBOSE_PLURAL |
| 35 | form = PassiveTestForm |
| 36 | inlines = [SliceInline] |
| 37 | |
| 38 | list_display = ('backend_status_icon', 'name', 'tap_ports', 'reset_viv', 'enabled') |
| 39 | list_display_links = ('backend_status_icon', 'name', 'tap_ports', 'reset_viv') |
| 40 | |
| 41 | fieldsets = [(None, { |
| 42 | 'fields': ['backend_status_text', 'name', 'enabled', 'versionNumber', 'tap_ports', 'reset_viv', 'description',], |
| 43 | 'classes':['suit-tab suit-tab-general',], |
| 44 | })] |
| 45 | |
| 46 | readonly_fields = ('backend_status_text', ) |
| 47 | user_readonly_fields = ['name', 'enabled', 'versionNumber', 'description',] |
| 48 | |
| 49 | extracontext_registered_admins = True |
| 50 | |
| 51 | suit_form_tabs = ( |
| 52 | ('general', 'PassiveTest Details', ), |
| 53 | ('slices', 'Slices',), |
| 54 | ) |
| 55 | |
| 56 | suit_form_includes = (( |
| 57 | 'top', |
| 58 | 'administration'), |
| 59 | ) |
| 60 | |
| 61 | def get_queryset(self, request): |
| 62 | return PassiveTestService.get_service_objects_by_user(request.user) |
| 63 | |
| 64 | admin.site.register(PassiveTestService, PassiveTestAdmin) |
| 65 | |
| 66 | class PassiveTestTenantForm(forms.ModelForm): |
| 67 | |
| 68 | class Meta: |
| 69 | model = PassiveTestTenant |
| 70 | fields = '__all__' |
| 71 | |
| 72 | creator = forms.ModelChoiceField(queryset=User.objects.all()) |
| 73 | |
| 74 | def __init__(self, *args, **kwargs): |
| 75 | super(PassiveTestTenantForm, self).__init__(*args, **kwargs) |
| 76 | |
| 77 | self.fields['kind'].widget.attrs['readonly'] = True |
| 78 | self.fields['kind'].initial = PASSIVETEST_KIND |
| 79 | |
| 80 | self.fields['provider_service'].queryset = PassiveTestService.get_service_objects().all() |
| 81 | |
| 82 | if self.instance: |
| 83 | self.fields['creator'].initial = self.instance.creator |
| 84 | |
| 85 | if (not self.instance) or (not self.instance.pk): |
| 86 | self.fields['creator'].initial = get_request().user |
| 87 | if PassiveTestService.get_service_objects().exists(): |
| 88 | self.fields['provider_service'].initial = PassiveTestService.get_service_objects().all()[0] |
| 89 | |
| 90 | def save(self, commit=True): |
| 91 | self.instance.creator = self.cleaned_data.get('creator') |
| 92 | return super(PassiveTestTenantForm, self).save(commit=commit) |
| 93 | |
| 94 | |
| 95 | class PassiveTestTenantAdmin(ReadOnlyAwareAdmin): |
| 96 | |
| 97 | verbose_name = PASSIVETEST_TENANT_NAME_VERBOSE |
| 98 | verbose_name_plural = PASSIVETEST_TENANT_NAME_VERBOSE_PLURAL |
| 99 | |
| 100 | list_display = ('id', 'backend_status_icon', 'instance') |
| 101 | list_display_links = ('backend_status_icon', 'instance', 'id') |
| 102 | |
| 103 | fieldsets = [(None, { |
| 104 | 'fields': ['backend_status_text', 'kind', 'provider_service', 'instance', 'creator'], |
| 105 | 'classes': ['suit-tab suit-tab-general'], |
| 106 | })] |
| 107 | |
| 108 | readonly_fields = ('backend_status_text', 'instance',) |
| 109 | |
| 110 | form = PassiveTestTenantForm |
| 111 | |
| 112 | suit_form_tabs = (('general', 'Details'),) |
| 113 | |
| 114 | def get_queryset(self, request): |
| 115 | return PassiveTestTenant.get_tenant_objects_by_user(request.user) |
| 116 | |
| 117 | admin.site.register(PassiveTestTenant, PassiveTestTenantAdmin) |
| 118 | |