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