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