Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1 | from django.contrib import admin |
| 2 | |
jcnelson | 575a135 | 2014-07-10 19:33:34 -0400 | [diff] [blame] | 3 | from syndicate_storage.models import * |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 4 | from django import forms |
| 5 | from django.utils.safestring import mark_safe |
| 6 | from django.contrib.auth.admin import UserAdmin |
| 7 | from django.contrib.admin.widgets import FilteredSelectMultiple |
| 8 | from django.contrib.auth.forms import ReadOnlyPasswordHashField |
| 9 | from django.contrib.auth.signals import user_logged_in |
| 10 | from django.utils import timezone |
| 11 | from django.contrib.contenttypes import generic |
| 12 | from suit.widgets import LinkedSelect |
Scott Baker | 022cdcd | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 13 | from core.admin import ReadOnlyAwareAdmin,SingletonAdmin,SliceInline,ServiceAttrAsTabInline,XOSBaseAdmin, XOSTabularInline |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 14 | from suit.widgets import LinkedSelect |
Scott Baker | 65a670a | 2014-05-08 22:14:13 -0700 | [diff] [blame] | 15 | from django.core.exceptions import ValidationError, ObjectDoesNotExist |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 16 | |
| 17 | class SyndicateServiceAdmin(SingletonAdmin,ReadOnlyAwareAdmin): |
| 18 | model = SyndicateService |
jcnelson | 575a135 | 2014-07-10 19:33:34 -0400 | [diff] [blame] | 19 | verbose_name = "Syndicate Storage" |
| 20 | verbose_name_plural = "Syndicate Storage" |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 21 | list_display = ("name","enabled") |
| 22 | fieldsets = [(None, {'fields': ['name','enabled','versionNumber', 'description',], 'classes':['suit-tab suit-tab-general']})] |
| 23 | inlines = [SliceInline,ServiceAttrAsTabInline] |
| 24 | |
| 25 | user_readonly_fields = ['name','enabled','versionNumber','description'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 26 | |
jcnelson | 575a135 | 2014-07-10 19:33:34 -0400 | [diff] [blame] | 27 | suit_form_tabs =(('general', 'Syndicate Storage Details'), |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 28 | ('slices','Slices'), |
| 29 | ('serviceattrs','Additional Attributes'), |
| 30 | ) |
| 31 | |
Scott Baker | 65a670a | 2014-05-08 22:14:13 -0700 | [diff] [blame] | 32 | |
Scott Baker | 022cdcd | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 33 | class VolumeAccessRightInline(XOSTabularInline): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 34 | model = VolumeAccessRight |
| 35 | extra = 0 |
| 36 | suit_classes = 'suit-tab suit-tab-volumeAccessRights' |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 37 | |
Scott Baker | 65a670a | 2014-05-08 22:14:13 -0700 | [diff] [blame] | 38 | |
| 39 | class VolumeSliceFormSet( forms.models.BaseInlineFormSet ): |
| 40 | # verify that our VolumeSlice is valid |
| 41 | |
| 42 | @classmethod |
| 43 | def verify_unchanged( cls, volume_pk, slice_pk, field_name, new_value ): |
| 44 | vs = None |
| 45 | try: |
| 46 | vs = VolumeSlice.objects.get( volume_id=volume_pk, slice_id=slice_pk ) |
| 47 | except ObjectDoesNotExist, dne: |
| 48 | return True, None |
| 49 | |
| 50 | old_value = getattr( vs, field_name ) |
| 51 | if old_value != new_value: |
| 52 | return False, old_value |
| 53 | else: |
| 54 | return True, None |
| 55 | |
| 56 | |
| 57 | def clean( self ): |
| 58 | for form in self.forms: |
| 59 | # check each inline's cleaned data, if it's valid |
| 60 | cleaned_data = None |
| 61 | try: |
| 62 | if form.cleaned_data: |
| 63 | cleaned_data = form.cleaned_data |
| 64 | except AttributeError: |
| 65 | continue |
jcnelson | 575a135 | 2014-07-10 19:33:34 -0400 | [diff] [blame] | 66 | |
Scott Baker | 65a670a | 2014-05-08 22:14:13 -0700 | [diff] [blame] | 67 | # verify that the ports haven't changed |
| 68 | volume_pk = cleaned_data['volume_id'].pk |
| 69 | slice_pk = cleaned_data['slice_id'].pk |
| 70 | |
jcnelson | 575a135 | 2014-07-10 19:33:34 -0400 | [diff] [blame] | 71 | if not cleaned_data.has_key('UG_portnum'): |
| 72 | raise ValidationError("Missing UG port number") |
Scott Baker | 65a670a | 2014-05-08 22:14:13 -0700 | [diff] [blame] | 73 | |
jcnelson | 575a135 | 2014-07-10 19:33:34 -0400 | [diff] [blame] | 74 | if not cleaned_data.has_key('RG_portnum'): |
| 75 | raise ValidationError("Missing RG port number") |
Scott Baker | 65a670a | 2014-05-08 22:14:13 -0700 | [diff] [blame] | 76 | |
jcnelson | 575a135 | 2014-07-10 19:33:34 -0400 | [diff] [blame] | 77 | rc1, old_peer_port = VolumeSliceFormSet.verify_unchanged( volume_pk, slice_pk, 'UG_portnum', cleaned_data['UG_portnum'] ) |
| 78 | rc2, old_replicate_port = VolumeSliceFormSet.verify_unchanged( volume_pk, slice_pk, 'RG_portnum', cleaned_data['RG_portnum'] ) |
Scott Baker | 65a670a | 2014-05-08 22:14:13 -0700 | [diff] [blame] | 79 | |
| 80 | err1str = "" |
| 81 | err2str = "" |
| 82 | if not rc1: |
jcnelson | 575a135 | 2014-07-10 19:33:34 -0400 | [diff] [blame] | 83 | err1str = "change %s back to %s" % (cleaned_data['UG_portnum'], old_peer_port) |
Scott Baker | 65a670a | 2014-05-08 22:14:13 -0700 | [diff] [blame] | 84 | if not rc2: |
jcnelson | 575a135 | 2014-07-10 19:33:34 -0400 | [diff] [blame] | 85 | err2str = " and change %s back to %s" % (cleaned_data['RG_portnum'], old_replicate_port ) |
Scott Baker | 65a670a | 2014-05-08 22:14:13 -0700 | [diff] [blame] | 86 | |
| 87 | if not rc1 or not rc2: |
jcnelson | 575a135 | 2014-07-10 19:33:34 -0400 | [diff] [blame] | 88 | raise ValidationError("At this time, port numbers cannot be changed once they are set. Please %s %s" % (err1str, err2str)) |
Scott Baker | 65a670a | 2014-05-08 22:14:13 -0700 | [diff] [blame] | 89 | |
| 90 | |
| 91 | |
Scott Baker | 022cdcd | 2015-02-18 15:50:11 -0800 | [diff] [blame] | 92 | class VolumeSliceInline(XOSTabularInline): |
Scott Baker | 65a670a | 2014-05-08 22:14:13 -0700 | [diff] [blame] | 93 | model = VolumeSlice |
| 94 | extra = 0 |
| 95 | suit_classes = 'suit-tab suit-tab-volumeSlices' |
jcnelson | 575a135 | 2014-07-10 19:33:34 -0400 | [diff] [blame] | 96 | fields = ['volume_id', 'slice_id', 'cap_read_data', 'cap_write_data', 'cap_host_data', 'UG_portnum', 'RG_portnum'] |
Scott Baker | 65a670a | 2014-05-08 22:14:13 -0700 | [diff] [blame] | 97 | |
| 98 | formset = VolumeSliceFormSet |
| 99 | |
| 100 | readonly_fields = ['credentials_blob'] |
| 101 | |
| 102 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 103 | class VolumeAdmin(ReadOnlyAwareAdmin): |
| 104 | model = Volume |
Scott Baker | 65a670a | 2014-05-08 22:14:13 -0700 | [diff] [blame] | 105 | |
| 106 | def get_readonly_fields(self, request, obj=None ): |
jcnelson | 575a135 | 2014-07-10 19:33:34 -0400 | [diff] [blame] | 107 | always_readonly = [] |
Scott Baker | 65a670a | 2014-05-08 22:14:13 -0700 | [diff] [blame] | 108 | if obj == None: |
| 109 | # all fields are editable on add |
| 110 | return always_readonly |
| 111 | |
| 112 | else: |
| 113 | # can't change owner, slice id, or block size on update |
| 114 | return ['blocksize', 'owner_id'] + always_readonly |
| 115 | |
| 116 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 117 | list_display = ['name', 'owner_id'] |
| 118 | |
jcnelson | 575a135 | 2014-07-10 19:33:34 -0400 | [diff] [blame] | 119 | detailsFieldList = ['name', 'owner_id', 'description','blocksize', 'private','archive', 'cap_read_data', 'cap_write_data', 'cap_host_data' ] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 120 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 121 | fieldsets = [ |
| 122 | (None, {'fields': detailsFieldList, 'classes':['suit-tab suit-tab-general']}), |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 123 | ] |
| 124 | |
Scott Baker | 65a670a | 2014-05-08 22:14:13 -0700 | [diff] [blame] | 125 | inlines = [VolumeAccessRightInline, VolumeSliceInline] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 126 | |
jcnelson | 575a135 | 2014-07-10 19:33:34 -0400 | [diff] [blame] | 127 | user_readonly_fields = ['name','owner_id','description','blocksize','private', 'archive', 'cap_read_data', 'cap_write_data', 'cap_host_data'] |
Scott Baker | 65a670a | 2014-05-08 22:14:13 -0700 | [diff] [blame] | 128 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 129 | suit_form_tabs =(('general', 'Volume Details'), |
Scott Baker | 65a670a | 2014-05-08 22:14:13 -0700 | [diff] [blame] | 130 | ('volumeSlices', 'Slices'), |
jcnelson | 575a135 | 2014-07-10 19:33:34 -0400 | [diff] [blame] | 131 | ('volumeAccessRights', 'Volume Access Rights')) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 132 | |
jcnelson | 575a135 | 2014-07-10 19:33:34 -0400 | [diff] [blame] | 133 | def queryset(self, request): |
| 134 | # only show volumes that are public, or owned by the caller |
| 135 | return Volume.select_by_user(request.user) |
| 136 | |
| 137 | |
Scott Baker | 65a670a | 2014-05-08 22:14:13 -0700 | [diff] [blame] | 138 | # left panel: |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 139 | admin.site.register(SyndicateService, SyndicateServiceAdmin) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 140 | admin.site.register(Volume, VolumeAdmin) |