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