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