Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 1 | from core.models import Site |
| 2 | from core.models import * |
| 3 | from openstack.manager import OpenStackManager |
Tony Mack | e59a7c8 | 2013-04-27 11:08:10 -0400 | [diff] [blame] | 4 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 5 | from django.contrib import admin |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 6 | from django.contrib.auth.models import Group |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 7 | from django import forms |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 8 | from django.utils.safestring import mark_safe |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 9 | from django.contrib.auth.admin import UserAdmin |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 10 | from django.contrib.admin.widgets import FilteredSelectMultiple |
Scott Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 11 | from django.contrib.auth.forms import ReadOnlyPasswordHashField, AdminPasswordChangeForm |
Scott Baker | acd4514 | 2013-05-19 16:19:16 -0700 | [diff] [blame] | 12 | from django.contrib.auth.signals import user_logged_in |
| 13 | from django.utils import timezone |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 14 | from django.contrib.contenttypes import generic |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 15 | from suit.widgets import LinkedSelect |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 16 | from django.core.exceptions import PermissionDenied |
Scott Baker | e2bbf7e | 2014-01-13 12:09:31 -0800 | [diff] [blame] | 17 | from django.core.urlresolvers import reverse, NoReverseMatch |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 18 | |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 19 | import django_evolution |
Scott Baker | 0a5633b | 2014-10-06 17:51:20 -0700 | [diff] [blame] | 20 | import threading |
| 21 | |
| 22 | # thread locals necessary to work around a django-suit issue |
| 23 | _thread_locals = threading.local() |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 24 | |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 25 | def backend_icon(obj): # backend_status, enacted, updated): |
| 26 | #return "%s %s %s" % (str(obj.updated), str(obj.enacted), str(obj.backend_status)) |
| 27 | if (obj.enacted is not None) and obj.enacted >= obj.updated: |
Scott Baker | fbf0664 | 2014-09-09 10:38:15 -0700 | [diff] [blame] | 28 | return '<span style="min-width:16px;"><img src="/static/admin/img/icon_success.gif"></span>' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 29 | else: |
| 30 | if obj.backend_status == "Provisioning in progress" or obj.backend_status=="": |
Scott Baker | fbf0664 | 2014-09-09 10:38:15 -0700 | [diff] [blame] | 31 | return '<span style="min-width:16px;" title="%s"><img src="/static/admin/img/icon_clock.gif"></span>' % obj.backend_status |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 32 | else: |
Scott Baker | fbf0664 | 2014-09-09 10:38:15 -0700 | [diff] [blame] | 33 | return '<span style="min-width:16px;" title="%s"><img src="/static/admin/img/icon_error.gif"></span>' % obj.backend_status |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 34 | |
| 35 | def backend_text(obj): |
| 36 | icon = backend_icon(obj) |
| 37 | if (obj.enacted is not None) and obj.enacted >= obj.updated: |
| 38 | return "%s %s" % (icon, "successfully enacted") # enacted on %s" % str(obj.enacted)) |
| 39 | else: |
| 40 | return "%s %s" % (icon, obj.backend_status) |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 41 | |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 42 | class PlainTextWidget(forms.HiddenInput): |
| 43 | input_type = 'hidden' |
| 44 | |
| 45 | def render(self, name, value, attrs=None): |
| 46 | if value is None: |
| 47 | value = '' |
| 48 | return mark_safe(str(value) + super(PlainTextWidget, self).render(name, value, attrs)) |
| 49 | |
Scott Baker | 86c83ab | 2014-10-03 13:10:47 -0700 | [diff] [blame] | 50 | class PermissionCheckingAdminMixin(object): |
Scott Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 51 | # call save_by_user and delete_by_user instead of save and delete |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 52 | |
| 53 | def has_add_permission(self, request, obj=None): |
| 54 | return (not self.__user_is_readonly(request)) |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 55 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 56 | def has_delete_permission(self, request, obj=None): |
| 57 | return (not self.__user_is_readonly(request)) |
| 58 | |
| 59 | def save_model(self, request, obj, form, change): |
| 60 | if self.__user_is_readonly(request): |
Scott Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 61 | # this 'if' might be redundant if save_by_user is implemented right |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 62 | raise PermissionDenied |
Scott Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 63 | |
| 64 | obj.caller = request.user |
| 65 | # update openstack connection to use this site/tenant |
| 66 | obj.save_by_user(request.user) |
| 67 | |
| 68 | def delete_model(self, request, obj): |
| 69 | obj.delete_by_user(request.user) |
| 70 | |
| 71 | def save_formset(self, request, form, formset, change): |
| 72 | instances = formset.save(commit=False) |
| 73 | for instance in instances: |
| 74 | instance.save_by_user(request.user) |
| 75 | |
| 76 | # BUG in django 1.7? Objects are not deleted by formset.save if |
| 77 | # commit is False. So let's delete them ourselves. |
| 78 | # |
| 79 | # code from forms/models.py save_existing_objects() |
| 80 | try: |
| 81 | forms_to_delete = formset.deleted_forms
|
| 82 | except AttributeError:
|
| 83 | forms_to_delete = [] |
| 84 | if formset.initial_forms: |
| 85 | for form in formset.initial_forms: |
| 86 | obj = form.instance |
| 87 | if form in forms_to_delete: |
| 88 | if obj.pk is None: |
| 89 | continue |
| 90 | formset.deleted_objects.append(obj) |
| 91 | obj.delete() |
| 92 | |
| 93 | formset.save_m2m() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 94 | |
| 95 | def get_actions(self,request): |
Scott Baker | 86c83ab | 2014-10-03 13:10:47 -0700 | [diff] [blame] | 96 | actions = super(PermissionCheckingAdminMixin,self).get_actions(request) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 97 | |
| 98 | if self.__user_is_readonly(request): |
| 99 | if 'delete_selected' in actions: |
| 100 | del actions['delete_selected'] |
| 101 | |
| 102 | return actions |
| 103 | |
| 104 | def change_view(self,request,object_id, extra_context=None): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 105 | if self.__user_is_readonly(request): |
Scott Baker | af73e10 | 2014-04-22 22:40:07 -0700 | [diff] [blame] | 106 | if not hasattr(self, "readonly_save"):
|
| 107 | # save the original readonly fields
|
| 108 | self.readonly_save = self.readonly_fields
|
| 109 | self.inlines_save = self.inlines
|
Scott Baker | e8859f9 | 2014-05-23 12:42:40 -0700 | [diff] [blame] | 110 | if hasattr(self, "user_readonly_fields"):
|
| 111 | self.readonly_fields=self.user_readonly_fields
|
| 112 | if hasattr(self, "user_readonly_inlines"):
|
| 113 | self.inlines = self.user_readonly_inlines
|
Scott Baker | af73e10 | 2014-04-22 22:40:07 -0700 | [diff] [blame] | 114 | else:
|
| 115 | if hasattr(self, "readonly_save"):
|
| 116 | # restore the original readonly fields
|
| 117 | self.readonly_fields = self.readonly_save
|
Scott Baker | e8859f9 | 2014-05-23 12:42:40 -0700 | [diff] [blame] | 118 | if hasattr(self, "inlines_save"):
|
Scott Baker | af73e10 | 2014-04-22 22:40:07 -0700 | [diff] [blame] | 119 | self.inlines = self.inlines_save |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 120 | |
| 121 | try: |
Scott Baker | 86c83ab | 2014-10-03 13:10:47 -0700 | [diff] [blame] | 122 | return super(PermissionCheckingAdminMixin, self).change_view(request, object_id, extra_context=extra_context) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 123 | except PermissionDenied: |
| 124 | pass |
| 125 | if request.method == 'POST': |
| 126 | raise PermissionDenied |
| 127 | request.readonly = True |
Scott Baker | 86c83ab | 2014-10-03 13:10:47 -0700 | [diff] [blame] | 128 | return super(PermissionCheckingAdminMixin, self).change_view(request, object_id, extra_context=extra_context) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 129 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 130 | def __user_is_readonly(self, request): |
| 131 | return request.user.isReadOnlyUser() |
| 132 | |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 133 | def backend_status_text(self, obj): |
| 134 | return mark_safe(backend_text(obj)) |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 135 | |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 136 | def backend_status_icon(self, obj): |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 137 | return mark_safe(backend_icon(obj)) |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 138 | backend_status_icon.short_description = "" |
| 139 | |
Scott Baker | 86c83ab | 2014-10-03 13:10:47 -0700 | [diff] [blame] | 140 | class ReadOnlyAwareAdmin(PermissionCheckingAdminMixin, admin.ModelAdmin): |
| 141 | # Note: Make sure PermissionCheckingAdminMixin is listed before |
| 142 | # admin.ModelAdmin in the class declaration. |
| 143 | |
Scott Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 144 | pass |
| 145 | |
| 146 | class PlanetStackBaseAdmin(ReadOnlyAwareAdmin): |
| 147 | save_on_top = False |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 148 | |
Scott Baker | e8859f9 | 2014-05-23 12:42:40 -0700 | [diff] [blame] | 149 | class SingletonAdmin (ReadOnlyAwareAdmin): |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 150 | def has_add_permission(self, request): |
Scott Baker | e8859f9 | 2014-05-23 12:42:40 -0700 | [diff] [blame] | 151 | if not super(SingletonAdmin, self).has_add_permission(request): |
| 152 | return False |
| 153 | |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 154 | num_objects = self.model.objects.count() |
| 155 | if num_objects >= 1: |
| 156 | return False |
| 157 | else: |
| 158 | return True |
| 159 | |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 160 | class PlStackTabularInline(admin.TabularInline): |
Scott Baker | 8656832 | 2014-01-12 16:53:31 -0800 | [diff] [blame] | 161 | def __init__(self, *args, **kwargs): |
| 162 | super(PlStackTabularInline, self).__init__(*args, **kwargs) |
| 163 | |
| 164 | # InlineModelAdmin as no get_fields() method, so in order to add |
| 165 | # the selflink field, we override __init__ to modify self.fields and |
| 166 | # self.readonly_fields. |
| 167 | |
Scott Baker | e2bbf7e | 2014-01-13 12:09:31 -0800 | [diff] [blame] | 168 | self.setup_selflink() |
| 169 | |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 170 | def get_change_url(self, model, id): |
| 171 | """ Get the URL to a change form in the admin for this model """ |
| 172 | reverse_path = "admin:%s_change" % (model._meta.db_table) |
Scott Baker | e2bbf7e | 2014-01-13 12:09:31 -0800 | [diff] [blame] | 173 | try: |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 174 | url = reverse(reverse_path, args=(id,)) |
Scott Baker | e2bbf7e | 2014-01-13 12:09:31 -0800 | [diff] [blame] | 175 | except NoReverseMatch: |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 176 | return None |
| 177 | |
| 178 | return url |
| 179 | |
| 180 | def setup_selflink(self): |
| 181 | if hasattr(self, "selflink_fieldname"): |
| 182 | """ self.selflink_model can be defined to punch through a relation |
| 183 | to its target object. For example, in SliceNetworkInline, set |
| 184 | selflink_model = "network", and the URL will lead to the Network |
| 185 | object instead of trying to bring up a change view of the |
| 186 | SliceNetwork object. |
| 187 | """ |
| 188 | self.selflink_model = getattr(self.model,self.selflink_fieldname).field.rel.to |
| 189 | else: |
| 190 | self.selflink_model = self.model |
| 191 | |
| 192 | url = self.get_change_url(self.selflink_model, 0) |
| 193 | |
| 194 | # We don't have an admin for this object, so don't create the |
| 195 | # selflink. |
| 196 | if (url == None): |
Scott Baker | e2bbf7e | 2014-01-13 12:09:31 -0800 | [diff] [blame] | 197 | return |
| 198 | |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 199 | # Since we need to add "selflink" to the field list, we need to create |
| 200 | # self.fields if it is None. |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 201 | if (self.fields is None): |
| 202 | self.fields = [] |
| 203 | for f in self.model._meta.fields: |
| 204 | if f.editable and f.name != "id": |
| 205 | self.fields.append(f.name) |
Scott Baker | 8656832 | 2014-01-12 16:53:31 -0800 | [diff] [blame] | 206 | |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 207 | self.fields = tuple(self.fields) + ("selflink", ) |
Scott Baker | 8656832 | 2014-01-12 16:53:31 -0800 | [diff] [blame] | 208 | |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 209 | if self.readonly_fields is None: |
| 210 | self.readonly_fields = () |
Scott Baker | 8656832 | 2014-01-12 16:53:31 -0800 | [diff] [blame] | 211 | |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 212 | self.readonly_fields = tuple(self.readonly_fields) + ("selflink", ) |
Scott Baker | 8656832 | 2014-01-12 16:53:31 -0800 | [diff] [blame] | 213 | |
| 214 | def selflink(self, obj): |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 215 | if hasattr(self, "selflink_fieldname"): |
| 216 | obj = getattr(obj, self.selflink_fieldname) |
| 217 | |
Scott Baker | 8656832 | 2014-01-12 16:53:31 -0800 | [diff] [blame] | 218 | if obj.id: |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 219 | url = self.get_change_url(self.selflink_model, obj.id) |
| 220 | return "<a href='%s'>Details</a>" % str(url) |
Scott Baker | 8656832 | 2014-01-12 16:53:31 -0800 | [diff] [blame] | 221 | else:
|
Scott Baker | e2bbf7e | 2014-01-13 12:09:31 -0800 | [diff] [blame] | 222 | return "Not present"
|
Scott Baker | 8656832 | 2014-01-12 16:53:31 -0800 | [diff] [blame] | 223 | |
| 224 | selflink.allow_tags = True |
| 225 | selflink.short_description = "Details" |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 226 | |
Scott Baker | b27b62c | 2014-08-15 16:29:16 -0700 | [diff] [blame] | 227 | def has_add_permission(self, request): |
| 228 | return not request.user.isReadOnlyUser() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 229 | |
| 230 | def get_readonly_fields(self, request, obj=None): |
Scott Baker | b27b62c | 2014-08-15 16:29:16 -0700 | [diff] [blame] | 231 | readonly_fields = list(self.readonly_fields)[:] |
| 232 | if request.user.isReadOnlyUser(): |
| 233 | for field in self.fields: |
| 234 | if not field in readonly_fields: |
| 235 | readonly_fields.append(field) |
| 236 | return readonly_fields |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 237 | |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 238 | def backend_status_icon(self, obj): |
| 239 | return mark_safe(backend_icon(obj)) |
| 240 | backend_status_icon.short_description = "" |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 241 | |
Scott Baker | b27b62c | 2014-08-15 16:29:16 -0700 | [diff] [blame] | 242 | class PlStackGenericTabularInline(generic.GenericTabularInline): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 243 | def has_add_permission(self, request): |
Scott Baker | b27b62c | 2014-08-15 16:29:16 -0700 | [diff] [blame] | 244 | return not request.user.isReadOnlyUser() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 245 | |
Scott Baker | b27b62c | 2014-08-15 16:29:16 -0700 | [diff] [blame] | 246 | def get_readonly_fields(self, request, obj=None): |
| 247 | readonly_fields = list(self.readonly_fields)[:] |
| 248 | if request.user.isReadOnlyUser(): |
| 249 | for field in self.fields: |
| 250 | if not field in readonly_fields: |
| 251 | readonly_fields.append(field) |
| 252 | return readonly_fields |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 253 | |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 254 | def backend_status_icon(self, obj): |
| 255 | return mark_safe(backend_icon(obj)) |
| 256 | backend_status_icon.short_description = "" |
| 257 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 258 | class ReservationInline(PlStackTabularInline): |
| 259 | model = Reservation |
| 260 | extra = 0 |
| 261 | suit_classes = 'suit-tab suit-tab-reservations' |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 262 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 263 | def queryset(self, request): |
| 264 | return Reservation.select_by_user(request.user) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 265 | |
Scott Baker | b27b62c | 2014-08-15 16:29:16 -0700 | [diff] [blame] | 266 | class TagInline(PlStackGenericTabularInline): |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 267 | model = Tag |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 268 | extra = 0 |
| 269 | suit_classes = 'suit-tab suit-tab-tags' |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 270 | fields = ['service', 'name', 'value'] |
| 271 | |
| 272 | def queryset(self, request): |
| 273 | return Tag.select_by_user(request.user) |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 274 | |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 275 | class NetworkLookerUpper: |
Siobhan Tully | 2c780ad | 2013-09-06 11:22:40 -0400 | [diff] [blame] | 276 | """ This is a callable that looks up a network name in a sliver and returns |
| 277 | the ip address for that network. |
| 278 | """ |
| 279 | |
Scott Baker | 434ca7e | 2014-08-15 12:29:20 -0700 | [diff] [blame] | 280 | byNetworkName = {} # class variable |
| 281 | |
Siobhan Tully | 2c780ad | 2013-09-06 11:22:40 -0400 | [diff] [blame] | 282 | def __init__(self, name): |
| 283 | self.short_description = name |
| 284 | self.__name__ = name |
| 285 | self.network_name = name |
| 286 | |
| 287 | def __call__(self, obj): |
| 288 | if obj is not None: |
| 289 | for nbs in obj.networksliver_set.all(): |
| 290 | if (nbs.network.name == self.network_name): |
| 291 | return nbs.ip |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 292 | return "" |
| 293 | |
| 294 | def __str__(self): |
| 295 | return self.network_name |
| 296 | |
Scott Baker | 434ca7e | 2014-08-15 12:29:20 -0700 | [diff] [blame] | 297 | @staticmethod |
| 298 | def get(network_name): |
| 299 | """ We want to make sure we alwars return the same NetworkLookerUpper |
| 300 | because sometimes django will cause them to be instantiated multiple |
| 301 | times (and we don't want different ones in form.fields vs |
| 302 | SliverInline.readonly_fields). |
| 303 | """ |
| 304 | if network_name not in NetworkLookerUpper.byNetworkName: |
| 305 | NetworkLookerUpper.byNetworkName[network_name] = NetworkLookerUpper(network_name) |
| 306 | return NetworkLookerUpper.byNetworkName[network_name] |
| 307 | |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 308 | class SliverInline(PlStackTabularInline): |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 309 | model = Sliver |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 310 | fields = ['backend_status_icon', 'all_ips_string', 'instance_name', 'slice', 'deploymentNetwork', 'flavor', 'image', 'node'] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 311 | extra = 0 |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 312 | readonly_fields = ['backend_status_icon', 'all_ips_string', 'instance_name'] |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 313 | suit_classes = 'suit-tab suit-tab-slivers' |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 314 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 315 | def queryset(self, request): |
| 316 | return Sliver.select_by_user(request.user) |
| 317 | |
Scott Baker | b24cc93 | 2014-06-09 10:51:16 -0700 | [diff] [blame] | 318 | def formfield_for_foreignkey(self, db_field, request=None, **kwargs): |
Scott Baker | b24cc93 | 2014-06-09 10:51:16 -0700 | [diff] [blame] | 319 | if db_field.name == 'deploymentNetwork': |
Scott Baker | 3b67874 | 2014-06-09 13:11:54 -0700 | [diff] [blame] | 320 | kwargs['queryset'] = Deployment.select_by_acl(request.user) |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 321 | kwargs['widget'] = forms.Select(attrs={'onChange': "sliver_deployment_changed(this);"}) |
Scott Baker | 3248131 | 2014-09-08 12:14:14 -0700 | [diff] [blame] | 322 | elif db_field.name == 'flavor': |
| 323 | kwargs['widget'] = forms.Select(attrs={'onChange': "sliver_flavor_changed(this);"}) |
Scott Baker | 3b67874 | 2014-06-09 13:11:54 -0700 | [diff] [blame] | 324 | |
| 325 | field = super(SliverInline, self).formfield_for_foreignkey(db_field, request, **kwargs) |
Scott Baker | b24cc93 | 2014-06-09 10:51:16 -0700 | [diff] [blame] | 326 | |
| 327 | return field |
| 328 | |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 329 | class SiteInline(PlStackTabularInline): |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 330 | model = Site |
| 331 | extra = 0 |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 332 | suit_classes = 'suit-tab suit-tab-sites' |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 333 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 334 | def queryset(self, request): |
| 335 | return Site.select_by_user(request.user) |
| 336 | |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 337 | class UserInline(PlStackTabularInline): |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 338 | model = User |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 339 | fields = ['backend_status_icon', 'email', 'firstname', 'lastname'] |
| 340 | readonly_fields = ('backend_status_icon', ) |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 341 | extra = 0 |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 342 | suit_classes = 'suit-tab suit-tab-users' |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 343 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 344 | def queryset(self, request): |
| 345 | return User.select_by_user(request.user) |
| 346 | |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 347 | class SliceInline(PlStackTabularInline): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 348 | model = Slice |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 349 | fields = ['backend_status_icon', 'name', 'site', 'serviceClass', 'service'] |
| 350 | readonly_fields = ('backend_status_icon', ) |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 351 | extra = 0 |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 352 | suit_classes = 'suit-tab suit-tab-slices' |
| 353 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 354 | def queryset(self, request): |
| 355 | return Slice.select_by_user(request.user) |
| 356 | |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 357 | class NodeInline(PlStackTabularInline): |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 358 | model = Node |
| 359 | extra = 0 |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 360 | suit_classes = 'suit-tab suit-tab-nodes' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 361 | fields = ['backend_status_icon', 'name','deployment','site'] |
| 362 | readonly_fields = ('backend_status_icon', ) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 363 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 364 | class DeploymentPrivilegeInline(PlStackTabularInline): |
| 365 | model = DeploymentPrivilege |
| 366 | extra = 0 |
| 367 | suit_classes = 'suit-tab suit-tab-deploymentprivileges' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 368 | fields = ['backend_status_icon', 'user','role','deployment'] |
| 369 | readonly_fields = ('backend_status_icon', ) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 370 | |
| 371 | def queryset(self, request): |
| 372 | return DeploymentPrivilege.select_by_user(request.user) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 373 | |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 374 | class SitePrivilegeInline(PlStackTabularInline): |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 375 | model = SitePrivilege |
| 376 | extra = 0 |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 377 | suit_classes = 'suit-tab suit-tab-siteprivileges' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 378 | fields = ['backend_status_icon', 'user','site', 'role'] |
| 379 | readonly_fields = ('backend_status_icon', ) |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 380 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 381 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 382 | if db_field.name == 'site': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 383 | kwargs['queryset'] = Site.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 384 | |
| 385 | if db_field.name == 'user': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 386 | kwargs['queryset'] = User.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 387 | return super(SitePrivilegeInline, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 388 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 389 | def queryset(self, request): |
| 390 | return SitePrivilege.select_by_user(request.user) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 391 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 392 | class SiteDeploymentInline(PlStackTabularInline): |
| 393 | model = SiteDeployments |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 394 | extra = 0 |
| 395 | suit_classes = 'suit-tab suit-tab-deployments' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 396 | fields = ['backend_status_icon', 'deployment','site'] |
| 397 | readonly_fields = ('backend_status_icon', ) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 398 | |
| 399 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 400 | if db_field.name == 'site': |
| 401 | kwargs['queryset'] = Site.select_by_user(request.user) |
| 402 | |
| 403 | if db_field.name == 'deployment': |
| 404 | kwargs['queryset'] = Deployment.select_by_user(request.user) |
| 405 | return super(SiteDeploymentInline, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 406 | |
| 407 | def queryset(self, request): |
| 408 | return SiteDeployments.select_by_user(request.user) |
| 409 | |
| 410 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 411 | class SlicePrivilegeInline(PlStackTabularInline): |
| 412 | model = SlicePrivilege |
| 413 | suit_classes = 'suit-tab suit-tab-sliceprivileges' |
| 414 | extra = 0 |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 415 | fields = ('backend_status_icon', 'user', 'slice', 'role') |
| 416 | readonly_fields = ('backend_status_icon', ) |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 417 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 418 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 419 | if db_field.name == 'slice': |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 420 | kwargs['queryset'] = Slice.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 421 | if db_field.name == 'user': |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 422 | kwargs['queryset'] = User.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 423 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 424 | return super(SlicePrivilegeInline, self).formfield_for_foreignkey(db_field, request, **kwargs) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 425 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 426 | def queryset(self, request): |
| 427 | return SlicePrivilege.select_by_user(request.user) |
| 428 | |
Scott Baker | a0015eb | 2013-08-14 17:28:14 -0700 | [diff] [blame] | 429 | class SliceNetworkInline(PlStackTabularInline): |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 430 | model = Network.slices.through |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 431 | selflink_fieldname = "network" |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 432 | extra = 0 |
| 433 | verbose_name = "Network Connection" |
| 434 | verbose_name_plural = "Network Connections" |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 435 | suit_classes = 'suit-tab suit-tab-slicenetworks' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 436 | fields = ['backend_status_icon', 'network'] |
| 437 | readonly_fields = ('backend_status_icon', ) |
Scott Baker | 2170b97 | 2014-06-03 12:14:07 -0700 | [diff] [blame] | 438 | |
| 439 | class ImageDeploymentsInline(PlStackTabularInline): |
| 440 | model = ImageDeployments |
| 441 | extra = 0 |
| 442 | verbose_name = "Image Deployments" |
| 443 | verbose_name_plural = "Image Deployments" |
| 444 | suit_classes = 'suit-tab suit-tab-imagedeployments' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 445 | fields = ['backend_status_icon', 'image', 'deployment', 'glance_image_id'] |
| 446 | readonly_fields = ['backend_status_icon', 'glance_image_id'] |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 447 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 448 | class SliceRoleAdmin(PlanetStackBaseAdmin): |
| 449 | model = SliceRole |
| 450 | pass |
| 451 | |
| 452 | class SiteRoleAdmin(PlanetStackBaseAdmin): |
| 453 | model = SiteRole |
| 454 | pass |
| 455 | |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 456 | class DeploymentAdminForm(forms.ModelForm): |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 457 | sites = forms.ModelMultipleChoiceField( |
| 458 | queryset=Site.objects.all(), |
| 459 | required=False, |
Scott Baker | 7098318 | 2014-06-09 22:10:00 -0700 | [diff] [blame] | 460 | help_text="Select which sites are allowed to host nodes in this deployment", |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 461 | widget=FilteredSelectMultiple( |
| 462 | verbose_name=('Sites'), is_stacked=False |
| 463 | ) |
| 464 | ) |
Scott Baker | de0f441 | 2014-06-11 15:40:26 -0700 | [diff] [blame] | 465 | images = forms.ModelMultipleChoiceField( |
| 466 | queryset=Image.objects.all(), |
| 467 | required=False, |
| 468 | help_text="Select which images should be deployed on this deployment", |
| 469 | widget=FilteredSelectMultiple( |
| 470 | verbose_name=('Images'), is_stacked=False |
| 471 | ) |
| 472 | ) |
Scott Baker | 37b4790 | 2014-09-02 14:37:41 -0700 | [diff] [blame] | 473 | flavors = forms.ModelMultipleChoiceField( |
| 474 | queryset=Flavor.objects.all(), |
| 475 | required=False, |
| 476 | help_text="Select which flavors should be usable on this deployment", |
| 477 | widget=FilteredSelectMultiple( |
| 478 | verbose_name=('Flavors'), is_stacked=False |
| 479 | ) |
| 480 | ) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 481 | class Meta: |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 482 | model = Deployment |
Scott Baker | 37b4790 | 2014-09-02 14:37:41 -0700 | [diff] [blame] | 483 | many_to_many = ["flavors",] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 484 | |
Siobhan Tully | 320b462 | 2014-01-17 15:11:14 -0500 | [diff] [blame] | 485 | def __init__(self, *args, **kwargs): |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 486 | request = kwargs.pop('request', None) |
Siobhan Tully | 320b462 | 2014-01-17 15:11:14 -0500 | [diff] [blame] | 487 | super(DeploymentAdminForm, self).__init__(*args, **kwargs) |
| 488 | |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 489 | self.fields['accessControl'].initial = "allow site " + request.user.site.name |
| 490 | |
Siobhan Tully | 320b462 | 2014-01-17 15:11:14 -0500 | [diff] [blame] | 491 | if self.instance and self.instance.pk: |
Scott Baker | c9b14f7 | 2014-05-22 13:44:20 -0700 | [diff] [blame] | 492 | self.fields['sites'].initial = [x.site for x in self.instance.sitedeployments_set.all()] |
Scott Baker | de0f441 | 2014-06-11 15:40:26 -0700 | [diff] [blame] | 493 | self.fields['images'].initial = [x.image for x in self.instance.imagedeployments_set.all()] |
Scott Baker | 37b4790 | 2014-09-02 14:37:41 -0700 | [diff] [blame] | 494 | self.fields['flavors'].initial = self.instance.flavors.all() |
Scott Baker | de0f441 | 2014-06-11 15:40:26 -0700 | [diff] [blame] | 495 | |
| 496 | def manipulate_m2m_objs(self, this_obj, selected_objs, all_relations, relation_class, local_attrname, foreign_attrname): |
| 497 | """ helper function for handling m2m relations from the MultipleChoiceField |
| 498 | |
| 499 | this_obj: the source object we want to link from |
| 500 | |
| 501 | selected_objs: a list of destination objects we want to link to |
| 502 | |
| 503 | all_relations: the full set of relations involving this_obj, including ones we don't want |
| 504 | |
| 505 | relation_class: the class that implements the relation from source to dest |
| 506 | |
| 507 | local_attrname: field name representing this_obj in relation_class |
| 508 | |
| 509 | foreign_attrname: field name representing selected_objs in relation_class |
| 510 | |
| 511 | This function will remove all newobjclass relations from this_obj |
| 512 | that are not contained in selected_objs, and add any relations that |
| 513 | are in selected_objs but don't exist in the data model yet. |
| 514 | """ |
| 515 | |
| 516 | existing_dest_objs = [] |
| 517 | for relation in list(all_relations): |
| 518 | if getattr(relation, foreign_attrname) not in selected_objs: |
| 519 | #print "deleting site", sdp.site |
| 520 | relation.delete() |
| 521 | else: |
| 522 | existing_dest_objs.append(getattr(relation, foreign_attrname)) |
| 523 | |
| 524 | for dest_obj in selected_objs: |
| 525 | if dest_obj not in existing_dest_objs: |
| 526 | #print "adding site", site |
| 527 | kwargs = {foreign_attrname: dest_obj, local_attrname: this_obj} |
| 528 | relation = relation_class(**kwargs) |
| 529 | relation.save() |
Siobhan Tully | 320b462 | 2014-01-17 15:11:14 -0500 | [diff] [blame] | 530 | |
| 531 | def save(self, commit=True): |
| 532 | deployment = super(DeploymentAdminForm, self).save(commit=False) |
| 533 | |
| 534 | if commit: |
| 535 | deployment.save() |
Scott Baker | 61b6aec | 2014-10-06 17:17:40 -0700 | [diff] [blame] | 536 | # this has to be done after save() if/when a deployment is first created |
| 537 | deployment.flavors = self.cleaned_data['flavors'] |
Siobhan Tully | 320b462 | 2014-01-17 15:11:14 -0500 | [diff] [blame] | 538 | |
| 539 | if deployment.pk: |
Scott Baker | c9b14f7 | 2014-05-22 13:44:20 -0700 | [diff] [blame] | 540 | # save_m2m() doesn't seem to work with 'through' relations. So we |
| 541 | # create/destroy the through models ourselves. There has to be |
| 542 | # a better way... |
| 543 | |
Scott Baker | de0f441 | 2014-06-11 15:40:26 -0700 | [diff] [blame] | 544 | self.manipulate_m2m_objs(deployment, self.cleaned_data['sites'], deployment.sitedeployments_set.all(), SiteDeployments, "deployment", "site") |
| 545 | self.manipulate_m2m_objs(deployment, self.cleaned_data['images'], deployment.imagedeployments_set.all(), ImageDeployments, "deployment", "image") |
Scott Baker | c9b14f7 | 2014-05-22 13:44:20 -0700 | [diff] [blame] | 546 | |
Scott Baker | 37b4790 | 2014-09-02 14:37:41 -0700 | [diff] [blame] | 547 | self.save_m2m() |
Siobhan Tully | 320b462 | 2014-01-17 15:11:14 -0500 | [diff] [blame] | 548 | |
| 549 | return deployment |
| 550 | |
Scott Baker | ff5e0f3 | 2014-05-22 14:40:27 -0700 | [diff] [blame] | 551 | class DeploymentAdminROForm(DeploymentAdminForm): |
| 552 | def save(self, commit=True): |
| 553 | raise PermissionDenied |
| 554 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 555 | class SiteAssocInline(PlStackTabularInline): |
| 556 | model = Site.deployments.through |
| 557 | extra = 0 |
| 558 | suit_classes = 'suit-tab suit-tab-sites' |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 559 | |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 560 | class DeploymentAdmin(PlanetStackBaseAdmin): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 561 | model = Deployment |
Tony Mack | 2cbd380 | 2014-09-29 16:10:52 -0400 | [diff] [blame] | 562 | fieldList = ['backend_status_text', 'name', 'availability_zone', 'sites', 'images', 'flavors', 'accessControl'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 563 | fieldsets = [(None, {'fields': fieldList, 'classes':['suit-tab suit-tab-sites']})] |
Scott Baker | de0f441 | 2014-06-11 15:40:26 -0700 | [diff] [blame] | 564 | inlines = [DeploymentPrivilegeInline,NodeInline,TagInline] # ,ImageDeploymentsInline] |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 565 | list_display = ['backend_status_icon', 'name'] |
| 566 | list_display_links = ('backend_status_icon', 'name', ) |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 567 | readonly_fields = ('backend_status_text', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 568 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 569 | user_readonly_fields = ['name'] |
| 570 | |
Scott Baker | de0f441 | 2014-06-11 15:40:26 -0700 | [diff] [blame] | 571 | suit_form_tabs =(('sites','Deployment Details'),('nodes','Nodes'),('deploymentprivileges','Privileges'),('tags','Tags')) # ,('imagedeployments','Images')) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 572 | |
Scott Baker | ff5e0f3 | 2014-05-22 14:40:27 -0700 | [diff] [blame] | 573 | def get_form(self, request, obj=None, **kwargs): |
| 574 | if request.user.isReadOnlyUser(): |
| 575 | kwargs["form"] = DeploymentAdminROForm |
| 576 | else: |
| 577 | kwargs["form"] = DeploymentAdminForm |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 578 | adminForm = super(DeploymentAdmin,self).get_form(request, obj, **kwargs) |
| 579 | |
| 580 | # from stackexchange: pass the request object into the form |
| 581 | |
| 582 | class AdminFormMetaClass(adminForm): |
| 583 | def __new__(cls, *args, **kwargs): |
| 584 | kwargs['request'] = request |
| 585 | return adminForm(*args, **kwargs) |
| 586 | |
| 587 | return AdminFormMetaClass |
| 588 | |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 589 | class ServiceAttrAsTabInline(PlStackTabularInline): |
| 590 | model = ServiceAttribute |
| 591 | fields = ['name','value'] |
| 592 | extra = 0 |
| 593 | suit_classes = 'suit-tab suit-tab-serviceattrs' |
| 594 | |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 595 | class ServiceAdmin(PlanetStackBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 596 | list_display = ("backend_status_icon","name","description","versionNumber","enabled","published") |
| 597 | list_display_links = ('backend_status_icon', 'name', ) |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 598 | fieldList = ["backend_status_text","name","description","versionNumber","enabled","published"] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 599 | fieldsets = [(None, {'fields': fieldList, 'classes':['suit-tab suit-tab-general']})] |
| 600 | inlines = [ServiceAttrAsTabInline,SliceInline] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 601 | readonly_fields = ('backend_status_text', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 602 | |
| 603 | user_readonly_fields = fieldList |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 604 | |
| 605 | suit_form_tabs =(('general', 'Service Details'), |
| 606 | ('slices','Slices'), |
| 607 | ('serviceattrs','Additional Attributes'), |
| 608 | ) |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 609 | |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame] | 610 | class SiteAdmin(PlanetStackBaseAdmin): |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 611 | fieldList = ['backend_status_text', 'name', 'site_url', 'enabled', 'is_public', 'login_base', 'accountLink','location'] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 612 | fieldsets = [ |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 613 | (None, {'fields': fieldList, 'classes':['suit-tab suit-tab-general']}), |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 614 | #('Deployment Networks', {'fields': ['deployments'], 'classes':['suit-tab suit-tab-deployments']}), |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 615 | ] |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 616 | suit_form_tabs =(('general', 'Site Details'), |
| 617 | ('users','Users'), |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 618 | ('siteprivileges','Privileges'), |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 619 | ('deployments','Deployments'), |
| 620 | ('slices','Slices'), |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 621 | ('nodes','Nodes'), |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 622 | ('tags','Tags'), |
| 623 | ) |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 624 | readonly_fields = ['backend_status_text', 'accountLink'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 625 | |
| 626 | user_readonly_fields = ['name', 'deployments','site_url', 'enabled', 'is_public', 'login_base', 'accountLink'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 627 | |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 628 | list_display = ('backend_status_icon', 'name', 'login_base','site_url', 'enabled') |
| 629 | list_display_links = ('backend_status_icon', 'name', ) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 630 | filter_horizontal = ('deployments',) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 631 | inlines = [SliceInline,UserInline,TagInline, NodeInline, SitePrivilegeInline, SiteDeploymentInline] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 632 | search_fields = ['name'] |
| 633 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 634 | def queryset(self, request): |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 635 | return Site.select_by_user(request.user) |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 636 | |
Tony Mack | 5cd1320 | 2013-05-01 21:48:38 -0400 | [diff] [blame] | 637 | def get_formsets(self, request, obj=None): |
| 638 | for inline in self.get_inline_instances(request, obj): |
| 639 | # hide MyInline in the add view |
| 640 | if obj is None: |
| 641 | continue |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 642 | if isinstance(inline, SliceInline): |
| 643 | inline.model.caller = request.user |
| 644 | yield inline.get_formset(request, obj) |
| 645 | |
| 646 | def get_formsets(self, request, obj=None): |
| 647 | for inline in self.get_inline_instances(request, obj): |
| 648 | # hide MyInline in the add view |
| 649 | if obj is None: |
| 650 | continue |
| 651 | if isinstance(inline, SliverInline): |
| 652 | inline.model.caller = request.user |
Tony Mack | 5cd1320 | 2013-05-01 21:48:38 -0400 | [diff] [blame] | 653 | yield inline.get_formset(request, obj) |
| 654 | |
Scott Baker | 545db2a | 2013-12-09 18:44:43 -0800 | [diff] [blame] | 655 | def accountLink(self, obj): |
| 656 | link_obj = obj.accounts.all() |
| 657 | if link_obj: |
| 658 | reverse_path = "admin:core_account_change" |
| 659 | url = reverse(reverse_path, args =(link_obj[0].id,)) |
| 660 | return "<a href='%s'>%s</a>" % (url, "view billing details") |
| 661 | else: |
| 662 | return "no billing data for this site" |
| 663 | accountLink.allow_tags = True |
| 664 | accountLink.short_description = "Billing" |
| 665 | |
Tony Mack | 332ee1d | 2014-02-04 15:33:45 -0500 | [diff] [blame] | 666 | def save_model(self, request, obj, form, change): |
| 667 | # update openstack connection to use this site/tenant |
| 668 | obj.save_by_user(request.user) |
| 669 | |
| 670 | def delete_model(self, request, obj): |
| 671 | obj.delete_by_user(request.user) |
| 672 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 673 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 674 | class SitePrivilegeAdmin(PlanetStackBaseAdmin): |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 675 | fieldList = ['backend_status_text', 'user', 'site', 'role'] |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 676 | fieldsets = [ |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 677 | (None, {'fields': fieldList, 'classes':['collapse']}) |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 678 | ] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 679 | readonly_fields = ('backend_status_text', ) |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 680 | list_display = ('backend_status_icon', 'user', 'site', 'role') |
| 681 | list_display_links = list_display |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 682 | user_readonly_fields = fieldList |
| 683 | user_readonly_inlines = [] |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 684 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 685 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 686 | if db_field.name == 'site': |
| 687 | if not request.user.is_admin: |
| 688 | # only show sites where user is an admin or pi |
| 689 | sites = set() |
| 690 | for site_privilege in SitePrivilege.objects.filer(user=request.user): |
| 691 | if site_privilege.role.role_type in ['admin', 'pi']: |
| 692 | sites.add(site_privilege.site) |
| 693 | kwargs['queryset'] = Site.objects.filter(site__in=list(sites)) |
| 694 | |
| 695 | if db_field.name == 'user': |
| 696 | if not request.user.is_admin: |
| 697 | # only show users from sites where caller has admin or pi role |
| 698 | roles = Role.objects.filter(role_type__in=['admin', 'pi']) |
| 699 | site_privileges = SitePrivilege.objects.filter(user=request.user).filter(role__in=roles) |
| 700 | sites = [site_privilege.site for site_privilege in site_privileges] |
| 701 | site_privileges = SitePrivilege.objects.filter(site__in=sites) |
| 702 | emails = [site_privilege.user.email for site_privilege in site_privileges] |
| 703 | users = User.objects.filter(email__in=emails) |
| 704 | kwargs['queryset'] = users |
| 705 | |
| 706 | return super(SitePrivilegeAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 707 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 708 | def queryset(self, request): |
| 709 | # admins can see all privileges. Users can only see privileges at sites |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 710 | # where they have the admin role or pi role. |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 711 | qs = super(SitePrivilegeAdmin, self).queryset(request) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 712 | #if not request.user.is_admin: |
| 713 | # roles = Role.objects.filter(role_type__in=['admin', 'pi']) |
| 714 | # site_privileges = SitePrivilege.objects.filter(user=request.user).filter(role__in=roles) |
| 715 | # login_bases = [site_privilege.site.login_base for site_privilege in site_privileges] |
| 716 | # sites = Site.objects.filter(login_base__in=login_bases) |
| 717 | # qs = qs.filter(site__in=sites) |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 718 | return qs |
| 719 | |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 720 | class SliceForm(forms.ModelForm): |
| 721 | class Meta: |
| 722 | model = Slice |
| 723 | widgets = { |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 724 | 'service': LinkedSelect |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 725 | } |
| 726 | |
Tony Mack | 2cbd380 | 2014-09-29 16:10:52 -0400 | [diff] [blame] | 727 | def clean(self): |
| 728 | cleaned_data = super(SliceForm, self).clean() |
| 729 | name = cleaned_data.get('name') |
Scott Baker | 6efad46 | 2014-10-06 23:09:59 -0700 | [diff] [blame] | 730 | site = cleaned_data.get('site') |
| 731 | if (not isinstance(site,Site)): |
| 732 | # previous code indicates 'site' could be a site_id and not a site? |
| 733 | site = Slice.objects.get(id=site.id) |
Tony Mack | 2cbd380 | 2014-09-29 16:10:52 -0400 | [diff] [blame] | 734 | if not name.startswith(site.login_base): |
| 735 | raise forms.ValidationError('slice name must begin with %s' % site.login_base) |
| 736 | return cleaned_data |
| 737 | |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 738 | class SliceAdmin(PlanetStackBaseAdmin): |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 739 | form = SliceForm |
Tony Mack | fbb26fc | 2014-09-02 07:03:27 -0400 | [diff] [blame] | 740 | fieldList = ['backend_status_text', 'site', 'name', 'serviceClass', 'enabled','description', 'service', 'slice_url', 'max_slivers'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 741 | fieldsets = [('Slice Details', {'fields': fieldList, 'classes':['suit-tab suit-tab-general']}),] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 742 | readonly_fields = ('backend_status_text', ) |
Tony Mack | 7d45990 | 2014-09-03 13:18:57 -0400 | [diff] [blame] | 743 | list_display = ('backend_status_icon', 'name', 'site','serviceClass', 'slice_url', 'max_slivers') |
| 744 | list_display_links = ('backend_status_icon', 'name', ) |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 745 | inlines = [SlicePrivilegeInline,SliverInline, TagInline, ReservationInline,SliceNetworkInline] |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 746 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 747 | user_readonly_fields = fieldList |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 748 | |
| 749 | suit_form_tabs =(('general', 'Slice Details'), |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 750 | ('slicenetworks','Networks'), |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 751 | ('sliceprivileges','Privileges'), |
| 752 | ('slivers','Slivers'), |
| 753 | ('tags','Tags'), |
| 754 | ('reservations','Reservations'), |
| 755 | ) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 756 | |
Scott Baker | 510fdbb | 2014-08-05 17:19:24 -0700 | [diff] [blame] | 757 | def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None): |
Scott Baker | 510fdbb | 2014-08-05 17:19:24 -0700 | [diff] [blame] | 758 | deployment_nodes = [] |
| 759 | for node in Node.objects.all(): |
| 760 | deployment_nodes.append( (node.deployment.id, node.id, node.name) ) |
| 761 | |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 762 | deployment_flavors = [] |
| 763 | for flavor in Flavor.objects.all(): |
| 764 | for deployment in flavor.deployments.all(): |
| 765 | deployment_flavors.append( (deployment.id, flavor.id, flavor.name) ) |
| 766 | |
Scott Baker | 93e80cd | 2014-09-09 09:58:49 -0700 | [diff] [blame] | 767 | deployment_images = [] |
| 768 | for image in Image.objects.all(): |
| 769 | for imageDeployment in image.imagedeployments_set.all(): |
| 770 | deployment_images.append( (imageDeployment.deployment.id, image.id, image.name) ) |
| 771 | |
Tony Mack | ec23b99 | 2014-09-02 21:18:45 -0400 | [diff] [blame] | 772 | site_login_bases = [] |
| 773 | for site in Site.objects.all(): |
Scott Baker | 93e80cd | 2014-09-09 09:58:49 -0700 | [diff] [blame] | 774 | site_login_bases.append((site.id, site.login_base)) |
| 775 | |
Scott Baker | 510fdbb | 2014-08-05 17:19:24 -0700 | [diff] [blame] | 776 | context["deployment_nodes"] = deployment_nodes |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 777 | context["deployment_flavors"] = deployment_flavors |
Scott Baker | 93e80cd | 2014-09-09 09:58:49 -0700 | [diff] [blame] | 778 | context["deployment_images"] = deployment_images |
Tony Mack | ec23b99 | 2014-09-02 21:18:45 -0400 | [diff] [blame] | 779 | context["site_login_bases"] = site_login_bases |
Scott Baker | 510fdbb | 2014-08-05 17:19:24 -0700 | [diff] [blame] | 780 | return super(SliceAdmin, self).render_change_form(request, context, add, change, form_url, obj) |
| 781 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 782 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 783 | if db_field.name == 'site': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 784 | kwargs['queryset'] = Site.select_by_user(request.user) |
Tony Mack | ec23b99 | 2014-09-02 21:18:45 -0400 | [diff] [blame] | 785 | kwargs['widget'] = forms.Select(attrs={'onChange': "update_slice_prefix(this, $($(this).closest('fieldset')[0]).find('.field-name input')[0].id)"}) |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 786 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 787 | return super(SliceAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 788 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 789 | def queryset(self, request): |
| 790 | # admins can see all keys. Users can only see slices they belong to. |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 791 | return Slice.select_by_user(request.user) |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 792 | |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 793 | def get_formsets(self, request, obj=None): |
| 794 | for inline in self.get_inline_instances(request, obj): |
| 795 | # hide MyInline in the add view |
| 796 | if obj is None: |
| 797 | continue |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 798 | if isinstance(inline, SliverInline): |
| 799 | inline.model.caller = request.user |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 800 | yield inline.get_formset(request, obj) |
| 801 | |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 802 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 803 | class SlicePrivilegeAdmin(PlanetStackBaseAdmin): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 804 | fieldsets = [ |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 805 | (None, {'fields': ['backend_status_text', 'user', 'slice', 'role']}) |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 806 | ] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 807 | readonly_fields = ('backend_status_text', ) |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 808 | list_display = ('backend_status_icon', 'user', 'slice', 'role') |
| 809 | list_display_links = list_display |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 810 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 811 | user_readonly_fields = ['user', 'slice', 'role'] |
| 812 | user_readonly_inlines = [] |
| 813 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 814 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 815 | if db_field.name == 'slice': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 816 | kwargs['queryset'] = Slice.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 817 | |
| 818 | if db_field.name == 'user': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 819 | kwargs['queryset'] = User.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 820 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 821 | return super(SlicePrivilegeAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 822 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 823 | def queryset(self, request): |
| 824 | # admins can see all memberships. Users can only see memberships of |
| 825 | # slices where they have the admin role. |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 826 | return SlicePrivilege.select_by_user(request.user) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 827 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 828 | def save_model(self, request, obj, form, change): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 829 | # update openstack connection to use this site/tenant |
| 830 | auth = request.session.get('auth', {}) |
Tony Mack | f7f79a1 | 2014-08-11 11:21:42 -0400 | [diff] [blame] | 831 | auth['tenant'] = obj.slice.slicename |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 832 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 833 | obj.save() |
| 834 | |
| 835 | def delete_model(self, request, obj): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 836 | # update openstack connection to use this site/tenant |
| 837 | auth = request.session.get('auth', {}) |
Tony Mack | f7f79a1 | 2014-08-11 11:21:42 -0400 | [diff] [blame] | 838 | auth['tenant'] = obj.slice.slicename |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 839 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 840 | obj.delete() |
| 841 | |
Siobhan Tully | 567e3e6 | 2013-06-21 18:03:16 -0400 | [diff] [blame] | 842 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 843 | class ImageAdmin(PlanetStackBaseAdmin): |
| 844 | |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 845 | fieldsets = [('Image Details', |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 846 | {'fields': ['backend_status_text', 'name', 'disk_format', 'container_format'], |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 847 | 'classes': ['suit-tab suit-tab-general']}) |
| 848 | ] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 849 | readonly_fields = ('backend_status_text', ) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 850 | |
Scott Baker | 2170b97 | 2014-06-03 12:14:07 -0700 | [diff] [blame] | 851 | suit_form_tabs =(('general','Image Details'),('slivers','Slivers'),('imagedeployments','Deployments')) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 852 | |
Scott Baker | 2170b97 | 2014-06-03 12:14:07 -0700 | [diff] [blame] | 853 | inlines = [SliverInline, ImageDeploymentsInline] |
Scott Baker | b6f9924 | 2014-06-11 11:34:44 -0700 | [diff] [blame] | 854 | |
Tony Mack | 32e1ce3 | 2014-05-07 13:29:41 -0400 | [diff] [blame] | 855 | user_readonly_fields = ['name', 'disk_format', 'container_format'] |
Scott Baker | b27b62c | 2014-08-15 16:29:16 -0700 | [diff] [blame] | 856 | |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 857 | list_display = ['backend_status_icon', 'name'] |
| 858 | list_display_links = ('backend_status_icon', 'name', ) |
| 859 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 860 | class NodeForm(forms.ModelForm): |
| 861 | class Meta: |
| 862 | widgets = { |
| 863 | 'site': LinkedSelect, |
| 864 | 'deployment': LinkedSelect |
| 865 | } |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 866 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 867 | class NodeAdmin(PlanetStackBaseAdmin): |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 868 | form = NodeForm |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 869 | list_display = ('backend_status_icon', 'name', 'site', 'deployment') |
| 870 | list_display_links = ('backend_status_icon', 'name', ) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 871 | list_filter = ('deployment',) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 872 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 873 | inlines = [TagInline,SliverInline] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 874 | fieldsets = [('Node Details', {'fields': ['backend_status_text', 'name','site','deployment'], 'classes':['suit-tab suit-tab-details']})] |
| 875 | readonly_fields = ('backend_status_text', ) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 876 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 877 | user_readonly_fields = ['name','site','deployment'] |
| 878 | user_readonly_inlines = [TagInline,SliverInline] |
| 879 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 880 | suit_form_tabs =(('details','Node Details'),('slivers','Slivers'),('tags','Tags')) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 881 | |
Siobhan Tully | 567e3e6 | 2013-06-21 18:03:16 -0400 | [diff] [blame] | 882 | |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 883 | class SliverForm(forms.ModelForm): |
| 884 | class Meta: |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 885 | model = Sliver |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 886 | ip = forms.CharField(widget=PlainTextWidget) |
Tony Mack | 1826181 | 2013-05-02 16:39:20 -0400 | [diff] [blame] | 887 | instance_name = forms.CharField(widget=PlainTextWidget) |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 888 | widgets = { |
| 889 | 'ip': PlainTextWidget(), |
Tony Mack | 1826181 | 2013-05-02 16:39:20 -0400 | [diff] [blame] | 890 | 'instance_name': PlainTextWidget(), |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 891 | 'slice': LinkedSelect, |
| 892 | 'deploymentNetwork': LinkedSelect, |
| 893 | 'node': LinkedSelect, |
| 894 | 'image': LinkedSelect |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 895 | } |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 896 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 897 | class TagAdmin(PlanetStackBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 898 | list_display = ['backend_status_icon', 'service', 'name', 'value', 'content_type', 'content_object',] |
| 899 | list_display_links = list_display |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 900 | user_readonly_fields = ['service', 'name', 'value', 'content_type', 'content_object',] |
| 901 | user_readonly_inlines = [] |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 902 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 903 | class SliverAdmin(PlanetStackBaseAdmin): |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 904 | form = SliverForm |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 905 | fieldsets = [ |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 906 | ('Sliver Details', {'fields': ['backend_status_text', 'slice', 'deploymentNetwork', 'node', 'ip', 'instance_name', 'flavor', 'image', ], 'classes': ['suit-tab suit-tab-general'], }) |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 907 | ] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 908 | readonly_fields = ('backend_status_text', ) |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 909 | list_display = ['backend_status_icon', 'ip', 'instance_name', 'slice', 'flavor', 'image', 'node', 'deploymentNetwork'] |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 910 | list_display_links = ('backend_status_icon', 'ip',) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 911 | |
| 912 | suit_form_tabs =(('general', 'Sliver Details'), |
| 913 | ('tags','Tags'), |
| 914 | ) |
| 915 | |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 916 | inlines = [TagInline] |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 917 | |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 918 | user_readonly_fields = ['slice', 'deploymentNetwork', 'node', 'ip', 'instance_name', 'flavor', 'image'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 919 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 920 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 921 | if db_field.name == 'slice': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 922 | kwargs['queryset'] = Slice.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 923 | |
| 924 | return super(SliverAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 925 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 926 | def queryset(self, request): |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 927 | # admins can see all slivers. Users can only see slivers of |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 928 | # the slices they belong to. |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 929 | return Sliver.select_by_user(request.user) |
| 930 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 931 | |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 932 | def get_formsets(self, request, obj=None): |
| 933 | # make some fields read only if we are updating an existing record |
| 934 | if obj == None: |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 935 | #self.readonly_fields = ('ip', 'instance_name') |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 936 | self.readonly_fields = ('backend_status_text') |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 937 | else: |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 938 | self.readonly_fields = ('backend_status_text') |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 939 | #self.readonly_fields = ('ip', 'instance_name', 'slice', 'image', 'key') |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 940 | |
| 941 | for inline in self.get_inline_instances(request, obj): |
| 942 | # hide MyInline in the add view |
| 943 | if obj is None: |
| 944 | continue |
Scott Baker | 526b71e | 2014-05-13 13:18:01 -0700 | [diff] [blame] | 945 | if isinstance(inline, SliverInline): |
| 946 | inline.model.caller = request.user |
| 947 | yield inline.get_formset(request, obj) |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 948 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 949 | #def save_model(self, request, obj, form, change): |
| 950 | # # update openstack connection to use this site/tenant |
| 951 | # auth = request.session.get('auth', {}) |
| 952 | # auth['tenant'] = obj.slice.name |
| 953 | # obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
| 954 | # obj.creator = request.user |
| 955 | # obj.save() |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 956 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 957 | #def delete_model(self, request, obj): |
| 958 | # # update openstack connection to use this site/tenant |
| 959 | # auth = request.session.get('auth', {}) |
| 960 | # auth['tenant'] = obj.slice.name |
| 961 | # obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
| 962 | # obj.delete() |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 963 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 964 | class UserCreationForm(forms.ModelForm): |
| 965 | """A form for creating new users. Includes all the required |
| 966 | fields, plus a repeated password.""" |
| 967 | password1 = forms.CharField(label='Password', widget=forms.PasswordInput) |
| 968 | password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput) |
| 969 | |
| 970 | class Meta: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 971 | model = User |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 972 | fields = ('email', 'firstname', 'lastname', 'phone', 'public_key') |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 973 | |
| 974 | def clean_password2(self): |
| 975 | # Check that the two password entries match |
| 976 | password1 = self.cleaned_data.get("password1") |
| 977 | password2 = self.cleaned_data.get("password2") |
| 978 | if password1 and password2 and password1 != password2: |
| 979 | raise forms.ValidationError("Passwords don't match") |
| 980 | return password2 |
| 981 | |
| 982 | def save(self, commit=True): |
| 983 | # Save the provided password in hashed format |
| 984 | user = super(UserCreationForm, self).save(commit=False) |
Tony Mack | f9f4afb | 2013-05-01 21:02:12 -0400 | [diff] [blame] | 985 | user.password = self.cleaned_data["password1"] |
| 986 | #user.set_password(self.cleaned_data["password1"]) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 987 | if commit: |
| 988 | user.save() |
| 989 | return user |
| 990 | |
Siobhan Tully | 567e3e6 | 2013-06-21 18:03:16 -0400 | [diff] [blame] | 991 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 992 | class UserChangeForm(forms.ModelForm): |
| 993 | """A form for updating users. Includes all the fields on |
| 994 | the user, but replaces the password field with admin's |
| 995 | password hash display field. |
| 996 | """ |
Siobhan Tully | 63b7ba4 | 2014-01-12 10:35:11 -0500 | [diff] [blame] | 997 | password = ReadOnlyPasswordHashField(label='Password', |
| 998 | help_text= '<a href=\"password/\">Change Password</a>.') |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 999 | |
| 1000 | class Meta: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 1001 | model = User |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1002 | |
| 1003 | def clean_password(self): |
| 1004 | # Regardless of what the user provides, return the initial value. |
| 1005 | # This is done here, rather than on the field, because the |
| 1006 | # field does not have access to the initial value |
| 1007 | return self.initial["password"] |
| 1008 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 1009 | class UserDashboardViewInline(PlStackTabularInline): |
| 1010 | model = UserDashboardView |
| 1011 | extra = 0 |
| 1012 | suit_classes = 'suit-tab suit-tab-dashboards' |
| 1013 | fields = ['user', 'dashboardView', 'order'] |
| 1014 | |
Scott Baker | 86c83ab | 2014-10-03 13:10:47 -0700 | [diff] [blame] | 1015 | class UserAdmin(PermissionCheckingAdminMixin, UserAdmin): |
| 1016 | # Note: Make sure PermissionCheckingAdminMixin is listed before |
| 1017 | # admin.ModelAdmin in the class declaration. |
| 1018 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1019 | class Meta: |
| 1020 | app_label = "core" |
| 1021 | |
| 1022 | # The forms to add and change user instances |
| 1023 | form = UserChangeForm |
| 1024 | add_form = UserCreationForm |
| 1025 | |
| 1026 | # The fields to be used in displaying the User model. |
| 1027 | # These override the definitions on the base UserAdmin |
| 1028 | # that reference specific fields on auth.User. |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1029 | list_display = ('email', 'firstname', 'lastname', 'site', 'last_login') |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1030 | list_filter = ('site',) |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 1031 | inlines = [SlicePrivilegeInline,SitePrivilegeInline,DeploymentPrivilegeInline,UserDashboardViewInline] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1032 | |
Scott Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 1033 | fieldListLoginDetails = ['backend_status_text', 'email','site','password','is_active','is_readonly','is_admin','public_key'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1034 | fieldListContactInfo = ['firstname','lastname','phone','timezone'] |
| 1035 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1036 | fieldsets = ( |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1037 | ('Login Details', {'fields': ['backend_status_text', 'email', 'site','password', 'is_active', 'is_readonly', 'is_admin', 'public_key'], 'classes':['suit-tab suit-tab-general']}), |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1038 | ('Contact Information', {'fields': ('firstname','lastname','phone', 'timezone'), 'classes':['suit-tab suit-tab-contact']}), |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 1039 | #('Dashboard Views', {'fields': ('dashboards',), 'classes':['suit-tab suit-tab-dashboards']}), |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1040 | #('Important dates', {'fields': ('last_login',)}), |
| 1041 | ) |
| 1042 | add_fieldsets = ( |
| 1043 | (None, { |
| 1044 | 'classes': ('wide',), |
Scott Baker | 0a5633b | 2014-10-06 17:51:20 -0700 | [diff] [blame] | 1045 | 'fields': ('email', 'firstname', 'lastname', 'is_readonly', 'phone', 'public_key','password1', 'password2')}, |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1046 | ), |
| 1047 | ) |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1048 | readonly_fields = ('backend_status_text', ) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1049 | search_fields = ('email',) |
| 1050 | ordering = ('email',) |
| 1051 | filter_horizontal = () |
| 1052 | |
Scott Baker | 3ca51f6 | 2014-05-23 12:05:11 -0700 | [diff] [blame] | 1053 | user_readonly_fields = fieldListLoginDetails + fieldListContactInfo |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1054 | |
Scott Baker | 0a5633b | 2014-10-06 17:51:20 -0700 | [diff] [blame] | 1055 | def get_form(self, request, obj=None): |
| 1056 | # Save obj in thread-local storage, so suit_form_tabs can use it to |
| 1057 | # determine whether we're in edit or add mode. |
| 1058 | _thread_locals.request = request |
| 1059 | _thread_locals.obj = obj |
| 1060 | return super(UserAdmin, self).get_form(request, obj) |
| 1061 | |
| 1062 | @property |
| 1063 | def suit_form_tabs(self): |
| 1064 | if getattr(_thread_locals, "obj", None) is None: |
| 1065 | return [] |
| 1066 | else: |
| 1067 | return (('general','Login Details'), |
| 1068 | ('contact','Contact Information'), |
| 1069 | ('sliceprivileges','Slice Privileges'), |
| 1070 | ('siteprivileges','Site Privileges'), |
| 1071 | ('deploymentprivileges','Deployment Privileges'), |
| 1072 | ('dashboards','Dashboard Views')) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1073 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 1074 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 1075 | if db_field.name == 'site': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1076 | kwargs['queryset'] = Site.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 1077 | |
| 1078 | return super(UserAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 1079 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1080 | def queryset(self, request): |
| 1081 | return User.select_by_user(request.user) |
| 1082 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 1083 | class DashboardViewAdmin(PlanetStackBaseAdmin): |
| 1084 | fieldsets = [('Dashboard View Details', |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1085 | {'fields': ['backend_status_text', 'name', 'url'], |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 1086 | 'classes': ['suit-tab suit-tab-general']}) |
| 1087 | ] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1088 | readonly_fields = ('backend_status_text', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1089 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 1090 | suit_form_tabs =(('general','Dashboard View Details'),) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1091 | |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1092 | class ServiceResourceInline(PlStackTabularInline): |
Scott Baker | 3de3e37 | 2013-05-10 16:50:44 -0700 | [diff] [blame] | 1093 | model = ServiceResource |
| 1094 | extra = 0 |
| 1095 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1096 | class ServiceClassAdmin(PlanetStackBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1097 | list_display = ('backend_status_icon', 'name', 'commitment', 'membershipFee') |
| 1098 | list_display_links = ('backend_status_icon', 'name', ) |
Scott Baker | 3de3e37 | 2013-05-10 16:50:44 -0700 | [diff] [blame] | 1099 | inlines = [ServiceResourceInline] |
| 1100 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1101 | user_readonly_fields = ['name', 'commitment', 'membershipFee'] |
| 1102 | user_readonly_inlines = [] |
| 1103 | |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1104 | class ReservedResourceInline(PlStackTabularInline): |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1105 | model = ReservedResource |
| 1106 | extra = 0 |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1107 | suit_classes = 'suit-tab suit-tab-reservedresources' |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1108 | |
| 1109 | def formfield_for_foreignkey(self, db_field, request=None, **kwargs): |
| 1110 | field = super(ReservedResourceInline, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 1111 | |
| 1112 | if db_field.name == 'resource': |
| 1113 | # restrict resources to those that the slice's service class allows |
| 1114 | if request._slice is not None: |
| 1115 | field.queryset = field.queryset.filter(serviceClass = request._slice.serviceClass, calendarReservable=True) |
| 1116 | if len(field.queryset) > 0: |
| 1117 | field.initial = field.queryset.all()[0] |
| 1118 | else:
|
| 1119 | field.queryset = field.queryset.none()
|
| 1120 | elif db_field.name == 'sliver':
|
| 1121 | # restrict slivers to those that belong to the slice
|
| 1122 | if request._slice is not None:
|
| 1123 | field.queryset = field.queryset.filter(slice = request._slice) |
| 1124 | else: |
| 1125 | field.queryset = field.queryset.none()
|
| 1126 |
|
| 1127 | return field |
| 1128 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1129 | def queryset(self, request): |
| 1130 | return ReservedResource.select_by_user(request.user) |
| 1131 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1132 | class ReservationChangeForm(forms.ModelForm): |
| 1133 | class Meta: |
| 1134 | model = Reservation |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1135 | widgets = { |
| 1136 | 'slice' : LinkedSelect |
| 1137 | } |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1138 | |
| 1139 | class ReservationAddForm(forms.ModelForm): |
| 1140 | slice = forms.ModelChoiceField(queryset=Slice.objects.all(), widget=forms.Select(attrs={"onChange":"document.getElementById('id_refresh').value=1; submit()"})) |
| 1141 | refresh = forms.CharField(widget=forms.HiddenInput()) |
| 1142 | |
| 1143 | class Media: |
| 1144 | css = {'all': ('planetstack.css',)} # .field-refresh { display: none; } |
| 1145 | |
| 1146 | def clean_slice(self): |
| 1147 | slice = self.cleaned_data.get("slice") |
| 1148 | x = ServiceResource.objects.filter(serviceClass = slice.serviceClass, calendarReservable=True) |
| 1149 | if len(x) == 0: |
| 1150 | raise forms.ValidationError("The slice you selected does not have a service class that allows reservations") |
| 1151 | return slice |
| 1152 | |
| 1153 | class Meta: |
| 1154 | model = Reservation |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1155 | widgets = { |
| 1156 | 'slice' : LinkedSelect |
| 1157 | } |
| 1158 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1159 | |
| 1160 | class ReservationAddRefreshForm(ReservationAddForm): |
| 1161 | """ This form is displayed when the Reservation Form receives an update |
| 1162 | from the Slice dropdown onChange handler. It doesn't validate the |
| 1163 | data and doesn't save the data. This will cause the form to be |
| 1164 | redrawn. |
| 1165 | """ |
| 1166 | |
Scott Baker | 8737e5f | 2013-05-17 09:35:32 -0700 | [diff] [blame] | 1167 | """ don't validate anything other than slice """ |
| 1168 | dont_validate_fields = ("startTime", "duration") |
| 1169 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1170 | def full_clean(self): |
| 1171 | result = super(ReservationAddForm, self).full_clean() |
Scott Baker | 8737e5f | 2013-05-17 09:35:32 -0700 | [diff] [blame] | 1172 | |
| 1173 | for fieldname in self.dont_validate_fields: |
| 1174 | if fieldname in self._errors: |
| 1175 | del self._errors[fieldname] |
| 1176 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1177 | return result |
| 1178 | |
| 1179 | """ don't save anything """ |
| 1180 | def is_valid(self): |
| 1181 | return False |
| 1182 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1183 | class ReservationAdmin(PlanetStackBaseAdmin): |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1184 | fieldList = ['backend_status_text', 'slice', 'startTime', 'duration'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1185 | fieldsets = [('Reservation Details', {'fields': fieldList, 'classes': ['suit-tab suit-tab-general']})] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1186 | readonly_fields = ('backend_status_text', ) |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1187 | list_display = ('startTime', 'duration') |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1188 | form = ReservationAddForm |
| 1189 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1190 | suit_form_tabs = (('general','Reservation Details'), ('reservedresources','Reserved Resources')) |
| 1191 | |
| 1192 | inlines = [ReservedResourceInline] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1193 | user_readonly_fields = fieldList |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1194 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1195 | def add_view(self, request, form_url='', extra_context=None): |
Scott Baker | acd4514 | 2013-05-19 16:19:16 -0700 | [diff] [blame] | 1196 | timezone.activate(request.user.timezone) |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1197 | request._refresh = False |
| 1198 | request._slice = None |
| 1199 | if request.method == 'POST': |
Scott Baker | 8737e5f | 2013-05-17 09:35:32 -0700 | [diff] [blame] | 1200 | # "refresh" will be set to "1" if the form was submitted due to |
| 1201 | # a change in the Slice dropdown. |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1202 | if request.POST.get("refresh","1") == "1": |
| 1203 | request._refresh = True |
| 1204 | request.POST["refresh"] = "0" |
Scott Baker | 8737e5f | 2013-05-17 09:35:32 -0700 | [diff] [blame] | 1205 | |
| 1206 | # Keep track of the slice that was selected, so the |
| 1207 | # reservedResource inline can filter items for the slice. |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1208 | request._slice = request.POST.get("slice",None) |
| 1209 | if (request._slice is not None): |
| 1210 | request._slice = Slice.objects.get(id=request._slice) |
| 1211 | |
| 1212 | result = super(ReservationAdmin, self).add_view(request, form_url, extra_context) |
| 1213 | return result |
| 1214 | |
Scott Baker | acd4514 | 2013-05-19 16:19:16 -0700 | [diff] [blame] | 1215 | def changelist_view(self, request, extra_context = None): |
| 1216 | timezone.activate(request.user.timezone) |
| 1217 | return super(ReservationAdmin, self).changelist_view(request, extra_context) |
| 1218 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1219 | def get_form(self, request, obj=None, **kwargs): |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 1220 | request._obj_ = obj |
| 1221 | if obj is not None: |
| 1222 | # For changes, set request._slice to the slice already set in the |
| 1223 | # object. |
| 1224 | request._slice = obj.slice |
| 1225 | self.form = ReservationChangeForm |
| 1226 | else: |
| 1227 | if getattr(request, "_refresh", False): |
| 1228 | self.form = ReservationAddRefreshForm |
| 1229 | else: |
| 1230 | self.form = ReservationAddForm |
| 1231 | return super(ReservationAdmin, self).get_form(request, obj, **kwargs) |
| 1232 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1233 | def get_readonly_fields(self, request, obj=None): |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 1234 | if (obj is not None): |
| 1235 | # Prevent slice from being changed after the reservation has been |
| 1236 | # created. |
| 1237 | return ['slice'] |
| 1238 | else: |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1239 | return [] |
Scott Baker | 3de3e37 | 2013-05-10 16:50:44 -0700 | [diff] [blame] | 1240 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1241 | def queryset(self, request): |
| 1242 | return Reservation.select_by_user(request.user) |
| 1243 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1244 | class NetworkParameterTypeAdmin(PlanetStackBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1245 | list_display = ("backend_status_icon", "name", ) |
| 1246 | list_display_links = ('backend_status_icon', 'name', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1247 | user_readonly_fields = ['name'] |
| 1248 | user_readonly_inlines = [] |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1249 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1250 | class RouterAdmin(PlanetStackBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1251 | list_display = ("backend_status_icon", "name", ) |
| 1252 | list_display_links = ('backend_status_icon', 'name', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1253 | user_readonly_fields = ['name'] |
| 1254 | user_readonly_inlines = [] |
| 1255 | |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1256 | class RouterInline(PlStackTabularInline): |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1257 | model = Router.networks.through |
| 1258 | extra = 0 |
| 1259 | verbose_name_plural = "Routers" |
| 1260 | verbose_name = "Router" |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 1261 | suit_classes = 'suit-tab suit-tab-routers' |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1262 | |
Scott Baker | b27b62c | 2014-08-15 16:29:16 -0700 | [diff] [blame] | 1263 | class NetworkParameterInline(PlStackGenericTabularInline): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1264 | model = NetworkParameter |
Scott Baker | 618e379 | 2014-08-15 13:42:29 -0700 | [diff] [blame] | 1265 | extra = 0 |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1266 | verbose_name_plural = "Parameters" |
| 1267 | verbose_name = "Parameter" |
| 1268 | suit_classes = 'suit-tab suit-tab-netparams' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1269 | fields = ['backend_status_icon', 'parameter', 'value'] |
| 1270 | readonly_fields = ('backend_status_icon', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1271 | |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1272 | class NetworkSliversInline(PlStackTabularInline): |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1273 | fields = ['backend_status_icon', 'network','sliver','ip'] |
| 1274 | readonly_fields = ("backend_status_icon", "ip", ) |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1275 | model = NetworkSliver |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 1276 | selflink_fieldname = "sliver" |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1277 | extra = 0 |
| 1278 | verbose_name_plural = "Slivers" |
| 1279 | verbose_name = "Sliver" |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 1280 | suit_classes = 'suit-tab suit-tab-networkslivers' |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1281 | |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1282 | class NetworkSlicesInline(PlStackTabularInline): |
Scott Baker | d7d2a39 | 2013-08-06 08:57:30 -0700 | [diff] [blame] | 1283 | model = NetworkSlice |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 1284 | selflink_fieldname = "slice" |
Scott Baker | d7d2a39 | 2013-08-06 08:57:30 -0700 | [diff] [blame] | 1285 | extra = 0 |
| 1286 | verbose_name_plural = "Slices" |
| 1287 | verbose_name = "Slice" |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 1288 | suit_classes = 'suit-tab suit-tab-networkslices' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1289 | fields = ['backend_status_icon', 'network','slice'] |
| 1290 | readonly_fields = ('backend_status_icon', ) |
Scott Baker | d7d2a39 | 2013-08-06 08:57:30 -0700 | [diff] [blame] | 1291 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1292 | class NetworkAdmin(PlanetStackBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1293 | list_display = ("backend_status_icon", "name", "subnet", "ports", "labels") |
| 1294 | list_display_links = ('backend_status_icon', 'name', ) |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1295 | readonly_fields = ("subnet", ) |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 1296 | |
Scott Baker | d7d2a39 | 2013-08-06 08:57:30 -0700 | [diff] [blame] | 1297 | inlines = [NetworkParameterInline, NetworkSliversInline, NetworkSlicesInline, RouterInline] |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1298 | |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 1299 | fieldsets = [ |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1300 | (None, {'fields': ['backend_status_text', 'name','template','ports','labels','owner','guaranteedBandwidth', 'permitAllSlices','permittedSlices','network_id','router_id','subnet_id','subnet'], 'classes':['suit-tab suit-tab-general']}),] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1301 | |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1302 | readonly_fields = ('backend_status_text', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1303 | user_readonly_fields = ['name','template','ports','labels','owner','guaranteedBandwidth', 'permitAllSlices','permittedSlices','network_id','router_id','subnet_id','subnet'] |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 1304 | |
| 1305 | suit_form_tabs =( |
| 1306 | ('general','Network Details'), |
| 1307 | ('netparams', 'Parameters'), |
| 1308 | ('networkslivers','Slivers'), |
| 1309 | ('networkslices','Slices'), |
| 1310 | ('routers','Routers'), |
| 1311 | ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1312 | class NetworkTemplateAdmin(PlanetStackBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1313 | list_display = ("backend_status_icon", "name", "guaranteedBandwidth", "visibility") |
| 1314 | list_display_links = ('backend_status_icon', 'name', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1315 | user_readonly_fields = ["name", "guaranteedBandwidth", "visibility"] |
| 1316 | user_readonly_inlines = [] |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1317 | |
Scott Baker | 37b4790 | 2014-09-02 14:37:41 -0700 | [diff] [blame] | 1318 | class FlavorAdmin(PlanetStackBaseAdmin): |
| 1319 | list_display = ("backend_status_icon", "name", "flavor", "order", "default") |
| 1320 | list_display_links = ("backend_status_icon", "name") |
| 1321 | user_readonly_fields = ("name", "flavor") |
| 1322 | fields = ("name", "description", "flavor", "order", "default") |
| 1323 | |
Tony Mack | 31c2b8f | 2013-04-26 20:01:42 -0400 | [diff] [blame] | 1324 | # register a signal that caches the user's credentials when they log in |
| 1325 | def cache_credentials(sender, user, request, **kwds): |
| 1326 | auth = {'username': request.POST['username'], |
| 1327 | 'password': request.POST['password']} |
| 1328 | request.session['auth'] = auth |
| 1329 | user_logged_in.connect(cache_credentials) |
| 1330 | |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1331 | def dollar_field(fieldName, short_description): |
| 1332 | def newFunc(self, obj): |
| 1333 | try: |
| 1334 | x= "$ %0.2f" % float(getattr(obj, fieldName, 0.0)) |
| 1335 | except: |
| 1336 | x=getattr(obj, fieldName, 0.0) |
| 1337 | return x |
| 1338 | newFunc.short_description = short_description |
| 1339 | return newFunc |
| 1340 | |
| 1341 | def right_dollar_field(fieldName, short_description): |
| 1342 | def newFunc(self, obj): |
| 1343 | try: |
| 1344 | #x= '<div align=right style="width:6em">$ %0.2f</div>' % float(getattr(obj, fieldName, 0.0)) |
| 1345 | x= '<div align=right>$ %0.2f</div>' % float(getattr(obj, fieldName, 0.0)) |
| 1346 | except: |
| 1347 | x=getattr(obj, fieldName, 0.0) |
| 1348 | return x |
| 1349 | newFunc.short_description = short_description |
| 1350 | newFunc.allow_tags = True |
| 1351 | return newFunc |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1352 | |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1353 | class InvoiceChargeInline(PlStackTabularInline): |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1354 | model = Charge |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1355 | extra = 0 |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1356 | verbose_name_plural = "Charges" |
| 1357 | verbose_name = "Charge" |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1358 | exclude = ['account'] |
Scott Baker | 9cb88a2 | 2013-12-09 18:56:00 -0800 | [diff] [blame] | 1359 | fields = ["date", "kind", "state", "object", "coreHours", "dollar_amount", "slice"] |
| 1360 | readonly_fields = ["date", "kind", "state", "object", "coreHours", "dollar_amount", "slice"] |
| 1361 | can_delete = False |
| 1362 | max_num = 0 |
| 1363 | |
| 1364 | dollar_amount = right_dollar_field("amount", "Amount") |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1365 | |
| 1366 | class InvoiceAdmin(admin.ModelAdmin): |
| 1367 | list_display = ("date", "account") |
| 1368 | |
| 1369 | inlines = [InvoiceChargeInline] |
| 1370 | |
Scott Baker | 9cb88a2 | 2013-12-09 18:56:00 -0800 | [diff] [blame] | 1371 | fields = ["date", "account", "dollar_amount"] |
| 1372 | readonly_fields = ["date", "account", "dollar_amount"] |
| 1373 | |
| 1374 | dollar_amount = dollar_field("amount", "Amount") |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1375 | |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1376 | class InvoiceInline(PlStackTabularInline): |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1377 | model = Invoice |
| 1378 | extra = 0 |
| 1379 | verbose_name_plural = "Invoices" |
| 1380 | verbose_name = "Invoice" |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1381 | fields = ["date", "dollar_amount"] |
| 1382 | readonly_fields = ["date", "dollar_amount"] |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1383 | suit_classes = 'suit-tab suit-tab-accountinvoice' |
| 1384 | can_delete=False |
| 1385 | max_num=0 |
| 1386 | |
| 1387 | dollar_amount = right_dollar_field("amount", "Amount") |
| 1388 | |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1389 | class PendingChargeInline(PlStackTabularInline): |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1390 | model = Charge |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1391 | extra = 0 |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1392 | verbose_name_plural = "Charges" |
| 1393 | verbose_name = "Charge" |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1394 | exclude = ["invoice"] |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1395 | fields = ["date", "kind", "state", "object", "coreHours", "dollar_amount", "slice"] |
| 1396 | readonly_fields = ["date", "kind", "state", "object", "coreHours", "dollar_amount", "slice"] |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1397 | suit_classes = 'suit-tab suit-tab-accountpendingcharges' |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1398 | can_delete=False |
| 1399 | max_num=0 |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1400 | |
| 1401 | def queryset(self, request): |
| 1402 | qs = super(PendingChargeInline, self).queryset(request) |
| 1403 | qs = qs.filter(state="pending") |
| 1404 | return qs |
| 1405 | |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1406 | dollar_amount = right_dollar_field("amount", "Amount") |
| 1407 | |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1408 | class PaymentInline(PlStackTabularInline): |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1409 | model=Payment |
| 1410 | extra = 1 |
| 1411 | verbose_name_plural = "Payments" |
| 1412 | verbose_name = "Payment" |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1413 | fields = ["date", "dollar_amount"] |
| 1414 | readonly_fields = ["date", "dollar_amount"] |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1415 | suit_classes = 'suit-tab suit-tab-accountpayments' |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1416 | can_delete=False |
| 1417 | max_num=0 |
| 1418 | |
| 1419 | dollar_amount = right_dollar_field("amount", "Amount") |
| 1420 | |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1421 | class AccountAdmin(admin.ModelAdmin): |
| 1422 | list_display = ("site", "balance_due") |
| 1423 | |
| 1424 | inlines = [InvoiceInline, PaymentInline, PendingChargeInline] |
| 1425 | |
| 1426 | fieldsets = [ |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1427 | (None, {'fields': ['site', 'dollar_balance_due', 'dollar_total_invoices', 'dollar_total_payments'],'classes':['suit-tab suit-tab-general']}),] |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1428 | |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1429 | readonly_fields = ['site', 'dollar_balance_due', 'dollar_total_invoices', 'dollar_total_payments'] |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1430 | |
| 1431 | suit_form_tabs =( |
| 1432 | ('general','Account Details'), |
| 1433 | ('accountinvoice', 'Invoices'), |
| 1434 | ('accountpayments', 'Payments'), |
| 1435 | ('accountpendingcharges','Pending Charges'), |
| 1436 | ) |
| 1437 | |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1438 | dollar_balance_due = dollar_field("balance_due", "Balance Due") |
| 1439 | dollar_total_invoices = dollar_field("total_invoices", "Total Invoices") |
| 1440 | dollar_total_payments = dollar_field("total_payments", "Total Payments") |
| 1441 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1442 | # Now register the new UserAdmin... |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 1443 | admin.site.register(User, UserAdmin) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1444 | # ... and, since we're not using Django's builtin permissions, |
| 1445 | # unregister the Group model from admin. |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 1446 | #admin.site.unregister(Group) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1447 | |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1448 | #Do not show django evolution in the admin interface |
| 1449 | from django_evolution.models import Version, Evolution |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1450 | #admin.site.unregister(Version) |
| 1451 | #admin.site.unregister(Evolution) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1452 | |
| 1453 | |
| 1454 | # When debugging it is often easier to see all the classes, but for regular use |
| 1455 | # only the top-levels should be displayed |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1456 | showAll = False |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1457 | |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1458 | admin.site.register(Deployment, DeploymentAdmin) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1459 | admin.site.register(Site, SiteAdmin) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1460 | admin.site.register(Slice, SliceAdmin) |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 1461 | admin.site.register(Service, ServiceAdmin) |
smbaker | a3cf70c | 2013-06-27 02:01:41 -0700 | [diff] [blame] | 1462 | admin.site.register(Reservation, ReservationAdmin) |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1463 | admin.site.register(Network, NetworkAdmin) |
| 1464 | admin.site.register(Router, RouterAdmin) |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1465 | admin.site.register(NetworkTemplate, NetworkTemplateAdmin) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1466 | admin.site.register(Account, AccountAdmin) |
| 1467 | admin.site.register(Invoice, InvoiceAdmin) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1468 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1469 | if True: |
| 1470 | admin.site.register(NetworkParameterType, NetworkParameterTypeAdmin) |
| 1471 | admin.site.register(ServiceClass, ServiceClassAdmin) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1472 | #admin.site.register(PlanetStack) |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 1473 | admin.site.register(Tag, TagAdmin) |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 1474 | admin.site.register(DeploymentRole) |
| 1475 | admin.site.register(SiteRole) |
| 1476 | admin.site.register(SliceRole) |
| 1477 | admin.site.register(PlanetStackRole) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1478 | admin.site.register(Node, NodeAdmin) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1479 | #admin.site.register(SlicePrivilege, SlicePrivilegeAdmin) |
| 1480 | #admin.site.register(SitePrivilege, SitePrivilegeAdmin) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1481 | admin.site.register(Sliver, SliverAdmin) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1482 | admin.site.register(Image, ImageAdmin) |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 1483 | admin.site.register(DashboardView, DashboardViewAdmin) |
Scott Baker | 37b4790 | 2014-09-02 14:37:41 -0700 | [diff] [blame] | 1484 | admin.site.register(Flavor, FlavorAdmin) |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 1485 | |