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 | 9f6b8ed | 2014-11-17 23:44:03 -0800 | [diff] [blame] | 10 | from django.contrib.admin.widgets import FilteredSelectMultiple, AdminTextareaWidget |
Scott Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 11 | from django.contrib.auth.forms import ReadOnlyPasswordHashField, AdminPasswordChangeForm |
Scott Baker | acd4514 | 2013-05-19 16:19:16 -0700 | [diff] [blame] | 12 | from django.contrib.auth.signals import user_logged_in |
| 13 | from django.utils import timezone |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 14 | from django.contrib.contenttypes import generic |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 15 | from suit.widgets import LinkedSelect |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 16 | from django.core.exceptions import PermissionDenied |
Scott Baker | e2bbf7e | 2014-01-13 12:09:31 -0800 | [diff] [blame] | 17 | from django.core.urlresolvers import reverse, NoReverseMatch |
Scott Baker | 9f6b8ed | 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 | 3cde737 | 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 | 0a5633b | 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 | fbf0664 | 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 | fbf0664 | 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 | 3cde737 | 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 | 3cde737 | 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 | 3cde737 | 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 | 9f6b8ed | 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 | 86c83ab | 2014-10-03 13:10:47 -0700 | [diff] [blame] | 65 | class PermissionCheckingAdminMixin(object): |
Scott Baker | cbfb600 | 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 | cbfb600 | 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 | cbfb600 | 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 | 86c83ab | 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 | 86c83ab | 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 | 86c83ab | 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 | 24ded6a | 2014-11-05 09:05:38 -0800 | [diff] [blame] | 155 | def get_form(self, request, obj=None, **kwargs): |
Scott Baker | 5c43269 | 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 | 24ded6a | 2014-11-05 09:05:38 -0800 | [diff] [blame] | 161 | return super(PermissionCheckingAdminMixin, self).get_form(request, obj, **kwargs) |
Scott Baker | 5c43269 | 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 | 86c83ab | 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 | cbfb600 | 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 |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 343 | fields = ['backend_status_icon', 'all_ips_string', 'instance_name', 'slice', 'controllerNetwork', '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): |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 352 | if db_field.name == 'controllerNetwork': |
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 | 3248131 | 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' |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 394 | fields = ['backend_status_icon', 'name', 'site_deployment'] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 395 | readonly_fields = ('backend_status_icon', ) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 396 | |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 397 | class DeploymentPrivilegeInline(PlStackTabularInline): |
| 398 | model = DeploymentPrivilege |
| 399 | extra = 0 |
| 400 | suit_classes = 'suit-tab suit-tab-admin-only' |
| 401 | fields = ['backend_status_icon', 'user','role','deployment'] |
| 402 | readonly_fields = ('backend_status_icon', ) |
| 403 | |
| 404 | def queryset(self, request): |
| 405 | return DeploymentPrivilege.select_by_user(request.user) |
| 406 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 407 | class ControllerPrivilegeInline(PlStackTabularInline): |
| 408 | model = ControllerPrivilege |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 409 | extra = 0 |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 410 | suit_classes = 'suit-tab suit-tab-admin-only' |
| 411 | fields = ['backend_status_icon', 'user','role','controller'] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 412 | readonly_fields = ('backend_status_icon', ) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 413 | |
| 414 | def queryset(self, request): |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 415 | return ControllerPrivilege.select_by_user(request.user) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 416 | |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 417 | class ControllerSiteDeploymentsInline(PlStackTabularInline): |
| 418 | model = ControllerSiteDeployments |
| 419 | extra = 0 |
| 420 | suit_classes = 'suit-tab suit-tab-admin-only' |
| 421 | fields = ['controller', 'site_deployment', 'tenant_id'] |
| 422 | |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 423 | class SitePrivilegeInline(PlStackTabularInline): |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 424 | model = SitePrivilege |
| 425 | extra = 0 |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 426 | suit_classes = 'suit-tab suit-tab-siteprivileges' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 427 | fields = ['backend_status_icon', 'user','site', 'role'] |
| 428 | readonly_fields = ('backend_status_icon', ) |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 429 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 430 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 431 | if db_field.name == 'site': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 432 | kwargs['queryset'] = Site.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 433 | |
| 434 | if db_field.name == 'user': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 435 | kwargs['queryset'] = User.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 436 | return super(SitePrivilegeInline, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 437 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 438 | def queryset(self, request): |
| 439 | return SitePrivilege.select_by_user(request.user) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 440 | |
Sapan Bhatia | 39e8558 | 2014-11-19 15:07:36 -0500 | [diff] [blame] | 441 | class SiteDeploymentsInline(PlStackTabularInline): |
| 442 | model = SiteDeployments |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 443 | extra = 0 |
| 444 | suit_classes = 'suit-tab suit-tab-deployments' |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 445 | fields = ['backend_status_icon', 'deployment','site', 'controller'] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 446 | readonly_fields = ('backend_status_icon', ) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 447 | |
| 448 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 449 | if db_field.name == 'site': |
| 450 | kwargs['queryset'] = Site.select_by_user(request.user) |
| 451 | |
| 452 | if db_field.name == 'deployment': |
| 453 | kwargs['queryset'] = Deployment.select_by_user(request.user) |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 454 | |
| 455 | if db_field.name == 'controller': |
| 456 | kwargs['queryset'] = Controller.select_by_user(request.user) |
| 457 | |
Sapan Bhatia | 39e8558 | 2014-11-19 15:07:36 -0500 | [diff] [blame] | 458 | return super(SiteDeploymentsInline, self).formfield_for_foreignkey(db_field, request, **kwargs) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 459 | |
| 460 | def queryset(self, request): |
Sapan Bhatia | 39e8558 | 2014-11-19 15:07:36 -0500 | [diff] [blame] | 461 | return SiteDeployments.select_by_user(request.user) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 462 | |
| 463 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 464 | class SlicePrivilegeInline(PlStackTabularInline): |
| 465 | model = SlicePrivilege |
| 466 | suit_classes = 'suit-tab suit-tab-sliceprivileges' |
| 467 | extra = 0 |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 468 | fields = ('backend_status_icon', 'user', 'slice', 'role') |
| 469 | readonly_fields = ('backend_status_icon', ) |
Siobhan Tully | aa1bcd5 | 2013-05-10 12:43:09 -0400 | [diff] [blame] | 470 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 471 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 472 | if db_field.name == 'slice': |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 473 | kwargs['queryset'] = Slice.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 474 | if db_field.name == 'user': |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 475 | kwargs['queryset'] = User.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 476 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 477 | return super(SlicePrivilegeInline, self).formfield_for_foreignkey(db_field, request, **kwargs) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 478 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 479 | def queryset(self, request): |
| 480 | return SlicePrivilege.select_by_user(request.user) |
| 481 | |
Scott Baker | a0015eb | 2013-08-14 17:28:14 -0700 | [diff] [blame] | 482 | class SliceNetworkInline(PlStackTabularInline): |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 483 | model = Network.slices.through |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 484 | selflink_fieldname = "network" |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 485 | extra = 0 |
| 486 | verbose_name = "Network Connection" |
| 487 | verbose_name_plural = "Network Connections" |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 488 | suit_classes = 'suit-tab suit-tab-slicenetworks' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 489 | fields = ['backend_status_icon', 'network'] |
| 490 | readonly_fields = ('backend_status_icon', ) |
Scott Baker | 2170b97 | 2014-06-03 12:14:07 -0700 | [diff] [blame] | 491 | |
Sapan Bhatia | e9f96f6 | 2014-11-19 15:10:16 -0500 | [diff] [blame] | 492 | class ImageDeploymentsInline(PlStackTabularInline): |
| 493 | model = ImageDeployments |
Scott Baker | 2170b97 | 2014-06-03 12:14:07 -0700 | [diff] [blame] | 494 | extra = 0 |
| 495 | verbose_name = "Image Deployments" |
| 496 | verbose_name_plural = "Image Deployments" |
| 497 | suit_classes = 'suit-tab suit-tab-imagedeployments' |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 498 | fields = ['backend_status_icon', 'image', 'deployment'] |
| 499 | readonly_fields = ['backend_status_icon'] |
| 500 | |
| 501 | class ControllerImagesInline(PlStackTabularInline): |
| 502 | model = ControllerImages |
| 503 | extra = 0 |
| 504 | verbose_name = "Controller Images" |
| 505 | verbose_name_plural = "Controller Images" |
| 506 | suit_classes = 'suit-tab suit-tab-admin-only' |
| 507 | fields = ['backend_status_icon', 'image', 'controller', 'glance_image_id'] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 508 | readonly_fields = ['backend_status_icon', 'glance_image_id'] |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 509 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 510 | class SliceRoleAdmin(PlanetStackBaseAdmin): |
| 511 | model = SliceRole |
| 512 | pass |
| 513 | |
| 514 | class SiteRoleAdmin(PlanetStackBaseAdmin): |
| 515 | model = SiteRole |
| 516 | pass |
| 517 | |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 518 | class DeploymentAdminForm(forms.ModelForm): |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 519 | sites = forms.ModelMultipleChoiceField( |
| 520 | queryset=Site.objects.all(), |
| 521 | required=False, |
Scott Baker | 7098318 | 2014-06-09 22:10:00 -0700 | [diff] [blame] | 522 | 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] | 523 | widget=FilteredSelectMultiple( |
| 524 | verbose_name=('Sites'), is_stacked=False |
| 525 | ) |
| 526 | ) |
Scott Baker | de0f441 | 2014-06-11 15:40:26 -0700 | [diff] [blame] | 527 | images = forms.ModelMultipleChoiceField( |
| 528 | queryset=Image.objects.all(), |
| 529 | required=False, |
| 530 | help_text="Select which images should be deployed on this deployment", |
| 531 | widget=FilteredSelectMultiple( |
| 532 | verbose_name=('Images'), is_stacked=False |
| 533 | ) |
| 534 | ) |
Scott Baker | 37b4790 | 2014-09-02 14:37:41 -0700 | [diff] [blame] | 535 | flavors = forms.ModelMultipleChoiceField( |
| 536 | queryset=Flavor.objects.all(), |
| 537 | required=False, |
| 538 | help_text="Select which flavors should be usable on this deployment", |
| 539 | widget=FilteredSelectMultiple( |
| 540 | verbose_name=('Flavors'), is_stacked=False |
| 541 | ) |
| 542 | ) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 543 | class Meta: |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 544 | model = Deployment |
Scott Baker | 37b4790 | 2014-09-02 14:37:41 -0700 | [diff] [blame] | 545 | many_to_many = ["flavors",] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 546 | |
Siobhan Tully | 320b462 | 2014-01-17 15:11:14 -0500 | [diff] [blame] | 547 | def __init__(self, *args, **kwargs): |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 548 | request = kwargs.pop('request', None) |
Siobhan Tully | 320b462 | 2014-01-17 15:11:14 -0500 | [diff] [blame] | 549 | super(DeploymentAdminForm, self).__init__(*args, **kwargs) |
| 550 | |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 551 | self.fields['accessControl'].initial = "allow site " + request.user.site.name |
| 552 | |
Siobhan Tully | 320b462 | 2014-01-17 15:11:14 -0500 | [diff] [blame] | 553 | if self.instance and self.instance.pk: |
Scott Baker | 9f6b8ed | 2014-11-17 23:44:03 -0800 | [diff] [blame] | 554 | self.fields['sites'].initial = [x.site for x in self.instance.sitedeployments.all()] |
| 555 | 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] | 556 | self.fields['flavors'].initial = self.instance.flavors.all() |
Scott Baker | de0f441 | 2014-06-11 15:40:26 -0700 | [diff] [blame] | 557 | |
| 558 | def manipulate_m2m_objs(self, this_obj, selected_objs, all_relations, relation_class, local_attrname, foreign_attrname): |
| 559 | """ helper function for handling m2m relations from the MultipleChoiceField |
| 560 | |
| 561 | this_obj: the source object we want to link from |
| 562 | |
| 563 | selected_objs: a list of destination objects we want to link to |
| 564 | |
| 565 | all_relations: the full set of relations involving this_obj, including ones we don't want |
| 566 | |
| 567 | relation_class: the class that implements the relation from source to dest |
| 568 | |
| 569 | local_attrname: field name representing this_obj in relation_class |
| 570 | |
| 571 | foreign_attrname: field name representing selected_objs in relation_class |
| 572 | |
| 573 | This function will remove all newobjclass relations from this_obj |
| 574 | that are not contained in selected_objs, and add any relations that |
| 575 | are in selected_objs but don't exist in the data model yet. |
| 576 | """ |
| 577 | |
| 578 | existing_dest_objs = [] |
| 579 | for relation in list(all_relations): |
| 580 | if getattr(relation, foreign_attrname) not in selected_objs: |
| 581 | #print "deleting site", sdp.site |
| 582 | relation.delete() |
| 583 | else: |
| 584 | existing_dest_objs.append(getattr(relation, foreign_attrname)) |
| 585 | |
| 586 | for dest_obj in selected_objs: |
| 587 | if dest_obj not in existing_dest_objs: |
| 588 | #print "adding site", site |
| 589 | kwargs = {foreign_attrname: dest_obj, local_attrname: this_obj} |
| 590 | relation = relation_class(**kwargs) |
| 591 | relation.save() |
Siobhan Tully | 320b462 | 2014-01-17 15:11:14 -0500 | [diff] [blame] | 592 | |
| 593 | def save(self, commit=True): |
| 594 | deployment = super(DeploymentAdminForm, self).save(commit=False) |
| 595 | |
| 596 | if commit: |
| 597 | deployment.save() |
Scott Baker | 61b6aec | 2014-10-06 17:17:40 -0700 | [diff] [blame] | 598 | # this has to be done after save() if/when a deployment is first created |
| 599 | deployment.flavors = self.cleaned_data['flavors'] |
Siobhan Tully | 320b462 | 2014-01-17 15:11:14 -0500 | [diff] [blame] | 600 | |
| 601 | if deployment.pk: |
Scott Baker | c9b14f7 | 2014-05-22 13:44:20 -0700 | [diff] [blame] | 602 | # save_m2m() doesn't seem to work with 'through' relations. So we |
| 603 | # create/destroy the through models ourselves. There has to be |
| 604 | # a better way... |
| 605 | |
Sapan Bhatia | 39e8558 | 2014-11-19 15:07:36 -0500 | [diff] [blame] | 606 | self.manipulate_m2m_objs(deployment, self.cleaned_data['sites'], deployment.sitedeployments.all(), SiteDeployments, "deployment", "site") |
Tony Mack | 592aa95 | 2014-12-15 11:45:02 -0500 | [diff] [blame] | 607 | self.manipulate_m2m_objs(deployment, self.cleaned_data['images'], deployment.imagedeployments.all(), ImageDeployments, "deployment", "image") |
| 608 | # manipulate_m2m_objs doesn't work for Flavor/Deployment relationship |
| 609 | # so well handle that manually here |
| 610 | for flavor in deployment.flavors.all(): |
| 611 | if getattr(flavor, 'name') not in self.cleaned_data['flavors']: |
Tony Mack | 11f4d20 | 2014-12-15 12:37:59 -0500 | [diff] [blame] | 612 | deployment.flavors.remove(flavor) |
Tony Mack | 592aa95 | 2014-12-15 11:45:02 -0500 | [diff] [blame] | 613 | for flavor in self.cleaned_data['flavors']: |
| 614 | if flavor not in deployment.flavors.all(): |
| 615 | flavor.deployments.add(deployment) |
Scott Baker | c9b14f7 | 2014-05-22 13:44:20 -0700 | [diff] [blame] | 616 | |
Scott Baker | 37b4790 | 2014-09-02 14:37:41 -0700 | [diff] [blame] | 617 | self.save_m2m() |
Siobhan Tully | 320b462 | 2014-01-17 15:11:14 -0500 | [diff] [blame] | 618 | |
| 619 | return deployment |
| 620 | |
Scott Baker | ff5e0f3 | 2014-05-22 14:40:27 -0700 | [diff] [blame] | 621 | class DeploymentAdminROForm(DeploymentAdminForm): |
| 622 | def save(self, commit=True): |
| 623 | raise PermissionDenied |
| 624 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 625 | class SiteAssocInline(PlStackTabularInline): |
| 626 | model = Site.deployments.through |
| 627 | extra = 0 |
| 628 | suit_classes = 'suit-tab suit-tab-sites' |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 629 | |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 630 | class DeploymentAdmin(PlanetStackBaseAdmin): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 631 | model = Deployment |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 632 | fieldList = ['backend_status_text', 'name', 'sites', 'images', 'flavors', 'accessControl'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 633 | fieldsets = [(None, {'fields': fieldList, 'classes':['suit-tab suit-tab-sites']})] |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 634 | # node no longer directly connected to deployment |
| 635 | #inlines = [DeploymentPrivilegeInline,NodeInline,TagInline,ImageDeploymentsInline] |
| 636 | inlines = [DeploymentPrivilegeInline,TagInline,ImageDeploymentsInline] |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 637 | list_display = ['backend_status_icon', 'name'] |
| 638 | list_display_links = ('backend_status_icon', 'name', ) |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 639 | readonly_fields = ('backend_status_text', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 640 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 641 | user_readonly_fields = ['name'] |
| 642 | |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 643 | # nodes no longer direclty connected to deployments |
| 644 | #suit_form_tabs =(('sites','Deployment Details'),('nodes','Nodes'),('deploymentprivileges','Privileges'),('tags','Tags'),('imagedeployments','Images')) |
| 645 | suit_form_tabs =(('sites','Deployment Details'),('deploymentprivileges','Privileges'),('tags','Tags'),('imagedeployments','Images')) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 646 | |
Scott Baker | ff5e0f3 | 2014-05-22 14:40:27 -0700 | [diff] [blame] | 647 | def get_form(self, request, obj=None, **kwargs): |
| 648 | if request.user.isReadOnlyUser(): |
| 649 | kwargs["form"] = DeploymentAdminROForm |
| 650 | else: |
| 651 | kwargs["form"] = DeploymentAdminForm |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 652 | adminForm = super(DeploymentAdmin,self).get_form(request, obj, **kwargs) |
| 653 | |
| 654 | # from stackexchange: pass the request object into the form |
| 655 | |
| 656 | class AdminFormMetaClass(adminForm): |
| 657 | def __new__(cls, *args, **kwargs): |
| 658 | kwargs['request'] = request |
| 659 | return adminForm(*args, **kwargs) |
| 660 | |
| 661 | return AdminFormMetaClass |
| 662 | |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 663 | class ControllerAdminForm(forms.ModelForm): |
| 664 | site_deployments = forms.ModelMultipleChoiceField( |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 665 | queryset=SiteDeployments.objects.all(), |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 666 | required=False, |
| 667 | help_text="Select which sites deployments are managed by this controller", |
| 668 | widget=FilteredSelectMultiple( |
| 669 | verbose_name=('Site Deployments'), is_stacked=False |
| 670 | ) |
| 671 | ) |
| 672 | |
| 673 | class Meta: |
| 674 | model = Controller |
| 675 | |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 676 | def __init__(self, *args, **kwargs): |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 677 | request = kwargs.pop('request', None) |
| 678 | super(ControllerAdminForm, self).__init__(*args, **kwargs) |
| 679 | |
| 680 | if self.instance and self.instance.pk: |
| 681 | self.fields['site_deployments'].initial = [x.site_deployment for x in self.instance.controllersitedeployments.all()] |
| 682 | |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 683 | def manipulate_m2m_objs(self, this_obj, selected_objs, all_relations, relation_class, local_attrname, foreign_attrname): |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 684 | """ helper function for handling m2m relations from the MultipleChoiceField |
| 685 | this_obj: the source object we want to link from |
| 686 | selected_objs: a list of destination objects we want to link to |
| 687 | all_relations: the full set of relations involving this_obj, including ones we don't want |
| 688 | relation_class: the class that implements the relation from source to dest |
| 689 | local_attrname: field name representing this_obj in relation_class |
| 690 | foreign_attrname: field name representing selected_objs in relation_class |
| 691 | This function will remove all newobjclass relations from this_obj |
| 692 | that are not contained in selected_objs, and add any relations that |
| 693 | are in selected_objs but don't exist in the data model yet. |
| 694 | """ |
| 695 | existing_dest_objs = [] |
| 696 | for relation in list(all_relations): |
| 697 | if getattr(relation, foreign_attrname) not in selected_objs: |
| 698 | #print "deleting site", sdp.site |
| 699 | relation.delete() |
| 700 | else: |
| 701 | existing_dest_objs.append(getattr(relation, foreign_attrname)) |
| 702 | |
| 703 | for dest_obj in selected_objs: |
| 704 | if dest_obj not in existing_dest_objs: |
| 705 | #print "adding site", site |
| 706 | kwargs = {foreign_attrname: dest_obj, local_attrname: this_obj} |
| 707 | relation = relation_class(**kwargs) |
| 708 | relation.save() |
| 709 | |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 710 | def save(self, commit=True): |
| 711 | controller = super(ControllerAdminForm, self).save(commit=False) |
| 712 | if commit: |
| 713 | controller.save() |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 714 | |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 715 | if controller.pk: |
| 716 | # save_m2m() doesn't seem to work with 'through' relations. So we |
| 717 | # create/destroy the through models ourselves. There has to be |
| 718 | # a better way... |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 719 | self.manipulate_m2m_objs(controller, self.cleaned_data['site_deployments'], controller.controllersitedeployments.all(), ControllerSiteDeployments, "controller", "site_deployment") |
| 720 | |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 721 | self.save_m2m() |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 722 | |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 723 | return controller |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 724 | |
| 725 | class ControllerAdmin(PlanetStackBaseAdmin): |
| 726 | model = Controller |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 727 | fieldList = ['name', 'version', 'backend_type', 'auth_url', 'admin_user', 'admin_tenant','admin_password'] |
| 728 | #fieldsets = [(None, {'fields': fieldList, 'classes':['suit-tab suit-tab-general']})] |
| 729 | inlines = [ControllerSiteDeploymentsInline] # ,ControllerImagesInline] |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 730 | list_display = ['backend_status_icon', 'name', 'version', 'backend_type'] |
| 731 | list_display_links = ('backend_status_icon', 'name', ) |
| 732 | readonly_fields = ('backend_status_text',) |
| 733 | |
| 734 | user_readonly_fields = [] |
| 735 | |
| 736 | def get_form(self, request, obj=None, **kwargs): |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 737 | print self.fieldsets |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 738 | if request.user.isReadOnlyUser(): |
| 739 | kwargs["form"] = ControllerAdminROForm |
| 740 | else: |
| 741 | kwargs["form"] = ControllerAdminForm |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 742 | adminForm = super(ControllerAdmin,self).get_form(request, obj, **kwargs) |
Tony Mack | 528d422 | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 743 | |
| 744 | # from stackexchange: pass the request object into the form |
| 745 | |
| 746 | class AdminFormMetaClass(adminForm): |
| 747 | def __new__(cls, *args, **kwargs): |
| 748 | kwargs['request'] = request |
| 749 | return adminForm(*args, **kwargs) |
| 750 | |
| 751 | return AdminFormMetaClass |
| 752 | |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 753 | class ServiceAttrAsTabInline(PlStackTabularInline): |
| 754 | model = ServiceAttribute |
| 755 | fields = ['name','value'] |
| 756 | extra = 0 |
| 757 | suit_classes = 'suit-tab suit-tab-serviceattrs' |
| 758 | |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 759 | class ServiceAdmin(PlanetStackBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 760 | list_display = ("backend_status_icon","name","description","versionNumber","enabled","published") |
| 761 | list_display_links = ('backend_status_icon', 'name', ) |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 762 | fieldList = ["backend_status_text","name","description","versionNumber","enabled","published"] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 763 | fieldsets = [(None, {'fields': fieldList, 'classes':['suit-tab suit-tab-general']})] |
| 764 | inlines = [ServiceAttrAsTabInline,SliceInline] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 765 | readonly_fields = ('backend_status_text', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 766 | |
| 767 | user_readonly_fields = fieldList |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 768 | |
| 769 | suit_form_tabs =(('general', 'Service Details'), |
| 770 | ('slices','Slices'), |
| 771 | ('serviceattrs','Additional Attributes'), |
| 772 | ) |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 773 | |
Tony Mack | 0553f28 | 2013-06-10 22:54:50 -0400 | [diff] [blame] | 774 | class SiteAdmin(PlanetStackBaseAdmin): |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 775 | 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] | 776 | fieldsets = [ |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 777 | (None, {'fields': fieldList, 'classes':['suit-tab suit-tab-general']}), |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 778 | #('Deployment Networks', {'fields': ['deployments'], 'classes':['suit-tab suit-tab-deployments']}), |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 779 | ] |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 780 | suit_form_tabs =(('general', 'Site Details'), |
| 781 | ('users','Users'), |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 782 | ('siteprivileges','Privileges'), |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 783 | ('deployments','Deployments'), |
| 784 | ('slices','Slices'), |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 785 | #('nodes','Nodes'), |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 786 | ('tags','Tags'), |
| 787 | ) |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 788 | readonly_fields = ['backend_status_text', 'accountLink'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 789 | |
| 790 | 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] | 791 | |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 792 | list_display = ('backend_status_icon', 'name', 'login_base','site_url', 'enabled') |
| 793 | list_display_links = ('backend_status_icon', 'name', ) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 794 | filter_horizontal = ('deployments',) |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 795 | inlines = [SliceInline,UserInline,TagInline, SitePrivilegeInline, SiteDeploymentsInline] |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 796 | search_fields = ['name'] |
| 797 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 798 | def queryset(self, request): |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 799 | return Site.select_by_user(request.user) |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 800 | |
Tony Mack | 5cd1320 | 2013-05-01 21:48:38 -0400 | [diff] [blame] | 801 | def get_formsets(self, request, obj=None): |
| 802 | for inline in self.get_inline_instances(request, obj): |
| 803 | # hide MyInline in the add view |
| 804 | if obj is None: |
| 805 | continue |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 806 | if isinstance(inline, SliverInline): |
| 807 | inline.model.caller = request.user |
Tony Mack | 5cd1320 | 2013-05-01 21:48:38 -0400 | [diff] [blame] | 808 | yield inline.get_formset(request, obj) |
| 809 | |
Scott Baker | 545db2a | 2013-12-09 18:44:43 -0800 | [diff] [blame] | 810 | def accountLink(self, obj): |
| 811 | link_obj = obj.accounts.all() |
| 812 | if link_obj: |
| 813 | reverse_path = "admin:core_account_change" |
| 814 | url = reverse(reverse_path, args =(link_obj[0].id,)) |
| 815 | return "<a href='%s'>%s</a>" % (url, "view billing details") |
| 816 | else: |
| 817 | return "no billing data for this site" |
| 818 | accountLink.allow_tags = True |
| 819 | accountLink.short_description = "Billing" |
| 820 | |
Tony Mack | 332ee1d | 2014-02-04 15:33:45 -0500 | [diff] [blame] | 821 | def save_model(self, request, obj, form, change): |
| 822 | # update openstack connection to use this site/tenant |
| 823 | obj.save_by_user(request.user) |
| 824 | |
| 825 | def delete_model(self, request, obj): |
| 826 | obj.delete_by_user(request.user) |
| 827 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 828 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 829 | class SitePrivilegeAdmin(PlanetStackBaseAdmin): |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 830 | fieldList = ['backend_status_text', 'user', 'site', 'role'] |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 831 | fieldsets = [ |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 832 | (None, {'fields': fieldList, 'classes':['collapse']}) |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 833 | ] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 834 | readonly_fields = ('backend_status_text', ) |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 835 | list_display = ('backend_status_icon', 'user', 'site', 'role') |
| 836 | list_display_links = list_display |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 837 | user_readonly_fields = fieldList |
| 838 | user_readonly_inlines = [] |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 839 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 840 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 841 | if db_field.name == 'site': |
| 842 | if not request.user.is_admin: |
| 843 | # only show sites where user is an admin or pi |
| 844 | sites = set() |
| 845 | for site_privilege in SitePrivilege.objects.filer(user=request.user): |
| 846 | if site_privilege.role.role_type in ['admin', 'pi']: |
| 847 | sites.add(site_privilege.site) |
| 848 | kwargs['queryset'] = Site.objects.filter(site__in=list(sites)) |
| 849 | |
| 850 | if db_field.name == 'user': |
| 851 | if not request.user.is_admin: |
| 852 | # only show users from sites where caller has admin or pi role |
| 853 | roles = Role.objects.filter(role_type__in=['admin', 'pi']) |
| 854 | site_privileges = SitePrivilege.objects.filter(user=request.user).filter(role__in=roles) |
| 855 | sites = [site_privilege.site for site_privilege in site_privileges] |
| 856 | site_privileges = SitePrivilege.objects.filter(site__in=sites) |
| 857 | emails = [site_privilege.user.email for site_privilege in site_privileges] |
| 858 | users = User.objects.filter(email__in=emails) |
| 859 | kwargs['queryset'] = users |
| 860 | |
| 861 | return super(SitePrivilegeAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 862 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 863 | def queryset(self, request): |
| 864 | # admins can see all privileges. Users can only see privileges at sites |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 865 | # where they have the admin role or pi role. |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 866 | qs = super(SitePrivilegeAdmin, self).queryset(request) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 867 | #if not request.user.is_admin: |
| 868 | # roles = Role.objects.filter(role_type__in=['admin', 'pi']) |
| 869 | # site_privileges = SitePrivilege.objects.filter(user=request.user).filter(role__in=roles) |
| 870 | # login_bases = [site_privilege.site.login_base for site_privilege in site_privileges] |
| 871 | # sites = Site.objects.filter(login_base__in=login_bases) |
| 872 | # qs = qs.filter(site__in=sites) |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 873 | return qs |
| 874 | |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 875 | class SliceForm(forms.ModelForm): |
| 876 | class Meta: |
| 877 | model = Slice |
| 878 | widgets = { |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 879 | 'service': LinkedSelect |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 880 | } |
| 881 | |
Tony Mack | 2cbd380 | 2014-09-29 16:10:52 -0400 | [diff] [blame] | 882 | def clean(self): |
| 883 | cleaned_data = super(SliceForm, self).clean() |
| 884 | name = cleaned_data.get('name') |
Scott Baker | 6efad46 | 2014-10-06 23:09:59 -0700 | [diff] [blame] | 885 | site = cleaned_data.get('site') |
Tony Mack | 585cb19 | 2014-10-22 12:54:19 -0400 | [diff] [blame] | 886 | slice_id = self.instance.id |
| 887 | if not site and slice_id: |
| 888 | site = Slice.objects.get(id=slice_id).site |
Scott Baker | 6efad46 | 2014-10-06 23:09:59 -0700 | [diff] [blame] | 889 | if (not isinstance(site,Site)): |
| 890 | # previous code indicates 'site' could be a site_id and not a site? |
| 891 | site = Slice.objects.get(id=site.id) |
Tony Mack | 2cbd380 | 2014-09-29 16:10:52 -0400 | [diff] [blame] | 892 | if not name.startswith(site.login_base): |
| 893 | raise forms.ValidationError('slice name must begin with %s' % site.login_base) |
| 894 | return cleaned_data |
| 895 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 896 | class ControllerSlicesInline(PlStackTabularInline): |
| 897 | model = ControllerSlices |
Scott Baker | c4efdc7 | 2014-10-15 16:54:04 -0700 | [diff] [blame] | 898 | extra = 0 |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 899 | verbose_name = "Controller Slices" |
| 900 | verbose_name_plural = "Controller Slices" |
Scott Baker | c4efdc7 | 2014-10-15 16:54:04 -0700 | [diff] [blame] | 901 | suit_classes = 'suit-tab suit-tab-admin-only' |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 902 | fields = ['backend_status_icon', 'controller', 'tenant_id'] |
Scott Baker | c4efdc7 | 2014-10-15 16:54:04 -0700 | [diff] [blame] | 903 | readonly_fields = ('backend_status_icon', ) |
| 904 | |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 905 | class SliceAdmin(PlanetStackBaseAdmin): |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 906 | form = SliceForm |
Tony Mack | fbb26fc | 2014-09-02 07:03:27 -0400 | [diff] [blame] | 907 | 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] | 908 | fieldsets = [('Slice Details', {'fields': fieldList, 'classes':['suit-tab suit-tab-general']}),] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 909 | readonly_fields = ('backend_status_text', ) |
Tony Mack | 7d45990 | 2014-09-03 13:18:57 -0400 | [diff] [blame] | 910 | list_display = ('backend_status_icon', 'name', 'site','serviceClass', 'slice_url', 'max_slivers') |
| 911 | list_display_links = ('backend_status_icon', 'name', ) |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 912 | inlines = [SlicePrivilegeInline,SliverInline, TagInline, ReservationInline,SliceNetworkInline] |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 913 | admin_inlines = [ControllerSlicesInline] |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 914 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 915 | user_readonly_fields = fieldList |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 916 | |
Scott Baker | c4efdc7 | 2014-10-15 16:54:04 -0700 | [diff] [blame] | 917 | @property |
| 918 | def suit_form_tabs(self): |
| 919 | tabs =[('general', 'Slice Details'), |
| 920 | ('slicenetworks','Networks'), |
| 921 | ('sliceprivileges','Privileges'), |
| 922 | ('slivers','Slivers'), |
| 923 | ('tags','Tags'), |
| 924 | ('reservations','Reservations'), |
| 925 | ] |
| 926 | |
| 927 | request=getattr(_thread_locals, "request", None) |
| 928 | if request and request.user.is_admin: |
| 929 | tabs.append( ('admin-only', 'Admin-Only') ) |
| 930 | |
| 931 | return tabs |
Tony Mack | 7b8505a | 2014-10-22 11:54:29 -0400 | [diff] [blame] | 932 | |
| 933 | def add_view(self, request, form_url='', extra_context=None): |
| 934 | # revert to default read-only fields |
| 935 | self.readonly_fields = ('backend_status_text',) |
| 936 | return super(SliceAdmin, self).add_view(request, form_url, extra_context=extra_context) |
| 937 | |
| 938 | def change_view(self, request, object_id, form_url='', extra_context=None): |
Tony Mack | 7b8505a | 2014-10-22 11:54:29 -0400 | [diff] [blame] | 939 | # cannot change the site of an existing slice so make the site field read only |
| 940 | if object_id: |
| 941 | self.readonly_fields = ('backend_status_text','site') |
| 942 | return super(SliceAdmin, self).change_view(request, object_id, form_url) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 943 | |
Scott Baker | 510fdbb | 2014-08-05 17:19:24 -0700 | [diff] [blame] | 944 | 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] | 945 | deployment_nodes = [] |
| 946 | for node in Node.objects.all(): |
Sapan Bhatia | 48208cd | 2014-12-22 01:37:26 -0500 | [diff] [blame] | 947 | deployment_nodes.append( (node.site_deployment.id, node.id, node.name) ) |
Scott Baker | 510fdbb | 2014-08-05 17:19:24 -0700 | [diff] [blame] | 948 | |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 949 | deployment_flavors = [] |
| 950 | for flavor in Flavor.objects.all(): |
| 951 | for deployment in flavor.deployments.all(): |
| 952 | deployment_flavors.append( (deployment.id, flavor.id, flavor.name) ) |
| 953 | |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 954 | deployment_images = [] |
Scott Baker | 93e80cd | 2014-09-09 09:58:49 -0700 | [diff] [blame] | 955 | for image in Image.objects.all(): |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 956 | for deployment_image in image.imagedeployments.all(): |
| 957 | deployment_images.append( (deployment_image.controller.id, image.id, image.name) ) |
Scott Baker | 93e80cd | 2014-09-09 09:58:49 -0700 | [diff] [blame] | 958 | |
Tony Mack | ec23b99 | 2014-09-02 21:18:45 -0400 | [diff] [blame] | 959 | site_login_bases = [] |
| 960 | for site in Site.objects.all(): |
Scott Baker | 93e80cd | 2014-09-09 09:58:49 -0700 | [diff] [blame] | 961 | site_login_bases.append((site.id, site.login_base)) |
| 962 | |
Scott Baker | 510fdbb | 2014-08-05 17:19:24 -0700 | [diff] [blame] | 963 | context["deployment_nodes"] = deployment_nodes |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 964 | context["deployment_flavors"] = deployment_flavors |
Scott Baker | 93e80cd | 2014-09-09 09:58:49 -0700 | [diff] [blame] | 965 | context["deployment_images"] = deployment_images |
Tony Mack | ec23b99 | 2014-09-02 21:18:45 -0400 | [diff] [blame] | 966 | context["site_login_bases"] = site_login_bases |
Scott Baker | 510fdbb | 2014-08-05 17:19:24 -0700 | [diff] [blame] | 967 | return super(SliceAdmin, self).render_change_form(request, context, add, change, form_url, obj) |
| 968 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 969 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 970 | if db_field.name == 'site': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 971 | kwargs['queryset'] = Site.select_by_user(request.user) |
Tony Mack | ec23b99 | 2014-09-02 21:18:45 -0400 | [diff] [blame] | 972 | 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] | 973 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 974 | return super(SliceAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 975 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 976 | def queryset(self, request): |
| 977 | # 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] | 978 | return Slice.select_by_user(request.user) |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 979 | |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 980 | def get_formsets(self, request, obj=None): |
| 981 | for inline in self.get_inline_instances(request, obj): |
| 982 | # hide MyInline in the add view |
| 983 | if obj is None: |
| 984 | continue |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 985 | if isinstance(inline, SliverInline): |
| 986 | inline.model.caller = request.user |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 987 | yield inline.get_formset(request, obj) |
| 988 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 989 | class SlicePrivilegeAdmin(PlanetStackBaseAdmin): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 990 | fieldsets = [ |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 991 | (None, {'fields': ['backend_status_text', 'user', 'slice', 'role']}) |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 992 | ] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 993 | readonly_fields = ('backend_status_text', ) |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 994 | list_display = ('backend_status_icon', 'user', 'slice', 'role') |
| 995 | list_display_links = list_display |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 996 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 997 | user_readonly_fields = ['user', 'slice', 'role'] |
| 998 | user_readonly_inlines = [] |
| 999 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 1000 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 1001 | if db_field.name == 'slice': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1002 | kwargs['queryset'] = Slice.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 1003 | |
| 1004 | if db_field.name == 'user': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1005 | kwargs['queryset'] = User.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 1006 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1007 | return super(SlicePrivilegeAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 1008 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 1009 | def queryset(self, request): |
| 1010 | # admins can see all memberships. Users can only see memberships of |
| 1011 | # slices where they have the admin role. |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1012 | return SlicePrivilege.select_by_user(request.user) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1013 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 1014 | def save_model(self, request, obj, form, change): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 1015 | # update openstack connection to use this site/tenant |
| 1016 | auth = request.session.get('auth', {}) |
Tony Mack | f7f79a1 | 2014-08-11 11:21:42 -0400 | [diff] [blame] | 1017 | auth['tenant'] = obj.slice.slicename |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 1018 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 1019 | obj.save() |
| 1020 | |
| 1021 | def delete_model(self, request, obj): |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 1022 | # update openstack connection to use this site/tenant |
| 1023 | auth = request.session.get('auth', {}) |
Tony Mack | f7f79a1 | 2014-08-11 11:21:42 -0400 | [diff] [blame] | 1024 | auth['tenant'] = obj.slice.slicename |
Tony Mack | 951dab4 | 2013-05-02 19:51:45 -0400 | [diff] [blame] | 1025 | obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 1026 | obj.delete() |
| 1027 | |
Siobhan Tully | 567e3e6 | 2013-06-21 18:03:16 -0400 | [diff] [blame] | 1028 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1029 | class ImageAdmin(PlanetStackBaseAdmin): |
| 1030 | |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 1031 | fieldsets = [('Image Details', |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1032 | {'fields': ['backend_status_text', 'name', 'disk_format', 'container_format'], |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1033 | 'classes': ['suit-tab suit-tab-general']}) |
| 1034 | ] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1035 | readonly_fields = ('backend_status_text', ) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1036 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 1037 | suit_form_tabs =(('general','Image Details'),('slivers','Slivers'),('imagedeployments','Deployments'), ('controllerimages', 'Controllers')) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1038 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 1039 | inlines = [SliverInline, ControllerImagesInline] |
Scott Baker | b6f9924 | 2014-06-11 11:34:44 -0700 | [diff] [blame] | 1040 | |
Tony Mack | 32e1ce3 | 2014-05-07 13:29:41 -0400 | [diff] [blame] | 1041 | user_readonly_fields = ['name', 'disk_format', 'container_format'] |
Scott Baker | b27b62c | 2014-08-15 16:29:16 -0700 | [diff] [blame] | 1042 | |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1043 | list_display = ['backend_status_icon', 'name'] |
| 1044 | list_display_links = ('backend_status_icon', 'name', ) |
| 1045 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1046 | class NodeForm(forms.ModelForm): |
| 1047 | class Meta: |
| 1048 | widgets = { |
| 1049 | 'site': LinkedSelect, |
| 1050 | 'deployment': LinkedSelect |
| 1051 | } |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1052 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1053 | class NodeAdmin(PlanetStackBaseAdmin): |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1054 | form = NodeForm |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 1055 | list_display = ('backend_status_icon', 'name', 'site_deployment') |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1056 | list_display_links = ('backend_status_icon', 'name', ) |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 1057 | list_filter = ('site_deployment',) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1058 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1059 | inlines = [TagInline,SliverInline] |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 1060 | fieldsets = [('Node Details', {'fields': ['backend_status_text', 'name','site_deployment'], 'classes':['suit-tab suit-tab-details']})] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1061 | readonly_fields = ('backend_status_text', ) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1062 | |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 1063 | user_readonly_fields = ['name','site_deployment'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1064 | user_readonly_inlines = [TagInline,SliverInline] |
| 1065 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1066 | suit_form_tabs =(('details','Node Details'),('slivers','Slivers'),('tags','Tags')) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1067 | |
Siobhan Tully | 567e3e6 | 2013-06-21 18:03:16 -0400 | [diff] [blame] | 1068 | |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 1069 | class SliverForm(forms.ModelForm): |
| 1070 | class Meta: |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 1071 | model = Sliver |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 1072 | ip = forms.CharField(widget=PlainTextWidget) |
Tony Mack | 1826181 | 2013-05-02 16:39:20 -0400 | [diff] [blame] | 1073 | instance_name = forms.CharField(widget=PlainTextWidget) |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 1074 | widgets = { |
| 1075 | 'ip': PlainTextWidget(), |
Tony Mack | 1826181 | 2013-05-02 16:39:20 -0400 | [diff] [blame] | 1076 | 'instance_name': PlainTextWidget(), |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1077 | 'slice': LinkedSelect, |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 1078 | 'controllerNetwork': LinkedSelect, |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1079 | 'node': LinkedSelect, |
| 1080 | 'image': LinkedSelect |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1081 | } |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 1082 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1083 | class TagAdmin(PlanetStackBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1084 | list_display = ['backend_status_icon', 'service', 'name', 'value', 'content_type', 'content_object',] |
| 1085 | list_display_links = list_display |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1086 | user_readonly_fields = ['service', 'name', 'value', 'content_type', 'content_object',] |
| 1087 | user_readonly_inlines = [] |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 1088 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 1089 | class SliverAdmin(PlanetStackBaseAdmin): |
Tony Mack | d90cdbf | 2013-04-16 22:48:40 -0400 | [diff] [blame] | 1090 | form = SliverForm |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 1091 | fieldsets = [ |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 1092 | ('Sliver Details', {'fields': ['backend_status_text', 'slice', 'controllerNetwork', 'node', 'ip', 'instance_name', 'flavor', 'image', ], 'classes': ['suit-tab suit-tab-general'], }) |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 1093 | ] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1094 | readonly_fields = ('backend_status_text', ) |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 1095 | list_display = ['backend_status_icon', 'ip', 'instance_name', 'slice', 'flavor', 'image', 'node', 'controllerNetwork'] |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1096 | list_display_links = ('backend_status_icon', 'ip',) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1097 | |
| 1098 | suit_form_tabs =(('general', 'Sliver Details'), |
| 1099 | ('tags','Tags'), |
| 1100 | ) |
| 1101 | |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 1102 | inlines = [TagInline] |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 1103 | |
Tony Mack | 93d1b03 | 2014-12-08 16:43:02 -0500 | [diff] [blame] | 1104 | user_readonly_fields = ['slice', 'controllerNetwork', 'node', 'ip', 'instance_name', 'flavor', 'image'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1105 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 1106 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 1107 | if db_field.name == 'slice': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1108 | kwargs['queryset'] = Slice.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 1109 | |
| 1110 | return super(SliverAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 1111 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 1112 | def queryset(self, request): |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 1113 | # admins can see all slivers. Users can only see slivers of |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 1114 | # the slices they belong to. |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1115 | return Sliver.select_by_user(request.user) |
| 1116 | |
Tony Mack | 0406283 | 2013-05-10 08:22:44 -0400 | [diff] [blame] | 1117 | |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 1118 | def get_formsets(self, request, obj=None): |
| 1119 | # make some fields read only if we are updating an existing record |
| 1120 | if obj == None: |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 1121 | #self.readonly_fields = ('ip', 'instance_name') |
Scott Baker | fbb4586 | 2014-10-17 16:27:23 -0700 | [diff] [blame] | 1122 | self.readonly_fields = ('backend_status_text',) |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 1123 | else: |
Scott Baker | fbb4586 | 2014-10-17 16:27:23 -0700 | [diff] [blame] | 1124 | self.readonly_fields = ('backend_status_text',) |
Scott Baker | 36f5087 | 2014-08-21 13:01:25 -0700 | [diff] [blame] | 1125 | #self.readonly_fields = ('ip', 'instance_name', 'slice', 'image', 'key') |
Tony Mack | 1d6b85f | 2013-05-07 18:49:14 -0400 | [diff] [blame] | 1126 | |
| 1127 | for inline in self.get_inline_instances(request, obj): |
| 1128 | # hide MyInline in the add view |
| 1129 | if obj is None: |
| 1130 | continue |
Scott Baker | 526b71e | 2014-05-13 13:18:01 -0700 | [diff] [blame] | 1131 | if isinstance(inline, SliverInline): |
| 1132 | inline.model.caller = request.user |
| 1133 | yield inline.get_formset(request, obj) |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 1134 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1135 | #def save_model(self, request, obj, form, change): |
| 1136 | # # update openstack connection to use this site/tenant |
| 1137 | # auth = request.session.get('auth', {}) |
| 1138 | # auth['tenant'] = obj.slice.name |
| 1139 | # obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
| 1140 | # obj.creator = request.user |
| 1141 | # obj.save() |
Tony Mack | 53106f3 | 2013-04-27 16:43:01 -0400 | [diff] [blame] | 1142 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1143 | #def delete_model(self, request, obj): |
| 1144 | # # update openstack connection to use this site/tenant |
| 1145 | # auth = request.session.get('auth', {}) |
| 1146 | # auth['tenant'] = obj.slice.name |
| 1147 | # obj.os_manager = OpenStackManager(auth=auth, caller=request.user) |
| 1148 | # obj.delete() |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 1149 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1150 | class UserCreationForm(forms.ModelForm): |
| 1151 | """A form for creating new users. Includes all the required |
| 1152 | fields, plus a repeated password.""" |
| 1153 | password1 = forms.CharField(label='Password', widget=forms.PasswordInput) |
| 1154 | password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput) |
| 1155 | |
| 1156 | class Meta: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 1157 | model = User |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1158 | fields = ('email', 'firstname', 'lastname', 'phone', 'public_key') |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1159 | |
| 1160 | def clean_password2(self): |
| 1161 | # Check that the two password entries match |
| 1162 | password1 = self.cleaned_data.get("password1") |
| 1163 | password2 = self.cleaned_data.get("password2") |
| 1164 | if password1 and password2 and password1 != password2: |
| 1165 | raise forms.ValidationError("Passwords don't match") |
| 1166 | return password2 |
| 1167 | |
| 1168 | def save(self, commit=True): |
| 1169 | # Save the provided password in hashed format |
| 1170 | user = super(UserCreationForm, self).save(commit=False) |
Tony Mack | f9f4afb | 2013-05-01 21:02:12 -0400 | [diff] [blame] | 1171 | user.password = self.cleaned_data["password1"] |
| 1172 | #user.set_password(self.cleaned_data["password1"]) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1173 | if commit: |
| 1174 | user.save() |
| 1175 | return user |
| 1176 | |
Siobhan Tully | 567e3e6 | 2013-06-21 18:03:16 -0400 | [diff] [blame] | 1177 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1178 | class UserChangeForm(forms.ModelForm): |
| 1179 | """A form for updating users. Includes all the fields on |
| 1180 | the user, but replaces the password field with admin's |
| 1181 | password hash display field. |
| 1182 | """ |
Siobhan Tully | 63b7ba4 | 2014-01-12 10:35:11 -0500 | [diff] [blame] | 1183 | password = ReadOnlyPasswordHashField(label='Password', |
| 1184 | help_text= '<a href=\"password/\">Change Password</a>.') |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1185 | |
| 1186 | class Meta: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 1187 | model = User |
Scott Baker | 9f6b8ed | 2014-11-17 23:44:03 -0800 | [diff] [blame] | 1188 | widgets = { 'public_key': UploadTextareaWidget, } |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1189 | |
| 1190 | def clean_password(self): |
| 1191 | # Regardless of what the user provides, return the initial value. |
| 1192 | # This is done here, rather than on the field, because the |
| 1193 | # field does not have access to the initial value |
| 1194 | return self.initial["password"] |
| 1195 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 1196 | class UserDashboardViewInline(PlStackTabularInline): |
| 1197 | model = UserDashboardView |
| 1198 | extra = 0 |
| 1199 | suit_classes = 'suit-tab suit-tab-dashboards' |
| 1200 | fields = ['user', 'dashboardView', 'order'] |
| 1201 | |
Scott Baker | 86c83ab | 2014-10-03 13:10:47 -0700 | [diff] [blame] | 1202 | class UserAdmin(PermissionCheckingAdminMixin, UserAdmin): |
| 1203 | # Note: Make sure PermissionCheckingAdminMixin is listed before |
| 1204 | # admin.ModelAdmin in the class declaration. |
| 1205 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1206 | class Meta: |
| 1207 | app_label = "core" |
| 1208 | |
| 1209 | # The forms to add and change user instances |
| 1210 | form = UserChangeForm |
| 1211 | add_form = UserCreationForm |
| 1212 | |
| 1213 | # The fields to be used in displaying the User model. |
| 1214 | # These override the definitions on the base UserAdmin |
| 1215 | # that reference specific fields on auth.User. |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1216 | list_display = ('email', 'firstname', 'lastname', 'site', 'last_login') |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1217 | list_filter = ('site',) |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 1218 | inlines = [SlicePrivilegeInline,SitePrivilegeInline,ControllerPrivilegeInline,UserDashboardViewInline] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1219 | |
Scott Baker | cbfb600 | 2014-10-03 00:32:37 -0700 | [diff] [blame] | 1220 | 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] | 1221 | fieldListContactInfo = ['firstname','lastname','phone','timezone'] |
| 1222 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1223 | fieldsets = ( |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1224 | ('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] | 1225 | ('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] | 1226 | #('Dashboard Views', {'fields': ('dashboards',), 'classes':['suit-tab suit-tab-dashboards']}), |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1227 | #('Important dates', {'fields': ('last_login',)}), |
| 1228 | ) |
| 1229 | add_fieldsets = ( |
| 1230 | (None, { |
| 1231 | 'classes': ('wide',), |
Scott Baker | 0a5633b | 2014-10-06 17:51:20 -0700 | [diff] [blame] | 1232 | 'fields': ('email', 'firstname', 'lastname', 'is_readonly', 'phone', 'public_key','password1', 'password2')}, |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1233 | ), |
| 1234 | ) |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1235 | readonly_fields = ('backend_status_text', ) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1236 | search_fields = ('email',) |
| 1237 | ordering = ('email',) |
| 1238 | filter_horizontal = () |
| 1239 | |
Scott Baker | 3ca51f6 | 2014-05-23 12:05:11 -0700 | [diff] [blame] | 1240 | user_readonly_fields = fieldListLoginDetails + fieldListContactInfo |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1241 | |
Scott Baker | 0a5633b | 2014-10-06 17:51:20 -0700 | [diff] [blame] | 1242 | @property |
| 1243 | def suit_form_tabs(self): |
| 1244 | if getattr(_thread_locals, "obj", None) is None: |
| 1245 | return [] |
| 1246 | else: |
| 1247 | return (('general','Login Details'), |
| 1248 | ('contact','Contact Information'), |
| 1249 | ('sliceprivileges','Slice Privileges'), |
| 1250 | ('siteprivileges','Site Privileges'), |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 1251 | ('controllerprivileges','Controller Privileges'), |
Scott Baker | 0a5633b | 2014-10-06 17:51:20 -0700 | [diff] [blame] | 1252 | ('dashboards','Dashboard Views')) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1253 | |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 1254 | def formfield_for_foreignkey(self, db_field, request, **kwargs): |
| 1255 | if db_field.name == 'site': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1256 | kwargs['queryset'] = Site.select_by_user(request.user) |
Tony Mack | c2835a9 | 2013-05-28 09:18:49 -0400 | [diff] [blame] | 1257 | |
| 1258 | return super(UserAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 1259 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1260 | def queryset(self, request): |
| 1261 | return User.select_by_user(request.user) |
| 1262 | |
Scott Baker | eef5a6b | 2014-12-19 16:41:12 -0800 | [diff] [blame] | 1263 | class ControllerDashboardInline(PlStackTabularInline): |
| 1264 | model = ControllerDashboard |
| 1265 | extra = 0 |
| 1266 | fields = ["controller", "url"] |
| 1267 | suit_classes = 'suit-tab suit-tab-controllers' |
| 1268 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 1269 | class DashboardViewAdmin(PlanetStackBaseAdmin): |
| 1270 | fieldsets = [('Dashboard View Details', |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1271 | {'fields': ['backend_status_text', 'name', 'url'], |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 1272 | 'classes': ['suit-tab suit-tab-general']}) |
| 1273 | ] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1274 | readonly_fields = ('backend_status_text', ) |
Scott Baker | eef5a6b | 2014-12-19 16:41:12 -0800 | [diff] [blame] | 1275 | inlines = [ControllerDashboardInline] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1276 | |
Scott Baker | eef5a6b | 2014-12-19 16:41:12 -0800 | [diff] [blame] | 1277 | suit_form_tabs =(('general','Dashboard View Details'), |
| 1278 | ('controllers', 'Per-controller Dashboard Details')) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1279 | |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1280 | class ServiceResourceInline(PlStackTabularInline): |
Scott Baker | 3de3e37 | 2013-05-10 16:50:44 -0700 | [diff] [blame] | 1281 | model = ServiceResource |
| 1282 | extra = 0 |
| 1283 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1284 | class ServiceClassAdmin(PlanetStackBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1285 | list_display = ('backend_status_icon', 'name', 'commitment', 'membershipFee') |
| 1286 | list_display_links = ('backend_status_icon', 'name', ) |
Scott Baker | 3de3e37 | 2013-05-10 16:50:44 -0700 | [diff] [blame] | 1287 | inlines = [ServiceResourceInline] |
| 1288 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1289 | user_readonly_fields = ['name', 'commitment', 'membershipFee'] |
| 1290 | user_readonly_inlines = [] |
| 1291 | |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1292 | class ReservedResourceInline(PlStackTabularInline): |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1293 | model = ReservedResource |
| 1294 | extra = 0 |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1295 | suit_classes = 'suit-tab suit-tab-reservedresources' |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1296 | |
| 1297 | def formfield_for_foreignkey(self, db_field, request=None, **kwargs): |
| 1298 | field = super(ReservedResourceInline, self).formfield_for_foreignkey(db_field, request, **kwargs) |
| 1299 | |
| 1300 | if db_field.name == 'resource': |
| 1301 | # restrict resources to those that the slice's service class allows |
| 1302 | if request._slice is not None: |
| 1303 | field.queryset = field.queryset.filter(serviceClass = request._slice.serviceClass, calendarReservable=True) |
| 1304 | if len(field.queryset) > 0: |
| 1305 | field.initial = field.queryset.all()[0] |
| 1306 | else:
|
| 1307 | field.queryset = field.queryset.none()
|
| 1308 | elif db_field.name == 'sliver':
|
| 1309 | # restrict slivers to those that belong to the slice
|
| 1310 | if request._slice is not None:
|
| 1311 | field.queryset = field.queryset.filter(slice = request._slice) |
| 1312 | else: |
| 1313 | field.queryset = field.queryset.none()
|
| 1314 |
|
| 1315 | return field |
| 1316 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1317 | def queryset(self, request): |
| 1318 | return ReservedResource.select_by_user(request.user) |
| 1319 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1320 | class ReservationChangeForm(forms.ModelForm): |
| 1321 | class Meta: |
| 1322 | model = Reservation |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1323 | widgets = { |
| 1324 | 'slice' : LinkedSelect |
| 1325 | } |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1326 | |
| 1327 | class ReservationAddForm(forms.ModelForm): |
| 1328 | slice = forms.ModelChoiceField(queryset=Slice.objects.all(), widget=forms.Select(attrs={"onChange":"document.getElementById('id_refresh').value=1; submit()"})) |
| 1329 | refresh = forms.CharField(widget=forms.HiddenInput()) |
| 1330 | |
| 1331 | class Media: |
| 1332 | css = {'all': ('planetstack.css',)} # .field-refresh { display: none; } |
| 1333 | |
| 1334 | def clean_slice(self): |
| 1335 | slice = self.cleaned_data.get("slice") |
| 1336 | x = ServiceResource.objects.filter(serviceClass = slice.serviceClass, calendarReservable=True) |
| 1337 | if len(x) == 0: |
| 1338 | raise forms.ValidationError("The slice you selected does not have a service class that allows reservations") |
| 1339 | return slice |
| 1340 | |
| 1341 | class Meta: |
| 1342 | model = Reservation |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1343 | widgets = { |
| 1344 | 'slice' : LinkedSelect |
| 1345 | } |
| 1346 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1347 | |
| 1348 | class ReservationAddRefreshForm(ReservationAddForm): |
| 1349 | """ This form is displayed when the Reservation Form receives an update |
| 1350 | from the Slice dropdown onChange handler. It doesn't validate the |
| 1351 | data and doesn't save the data. This will cause the form to be |
| 1352 | redrawn. |
| 1353 | """ |
| 1354 | |
Scott Baker | 8737e5f | 2013-05-17 09:35:32 -0700 | [diff] [blame] | 1355 | """ don't validate anything other than slice """ |
| 1356 | dont_validate_fields = ("startTime", "duration") |
| 1357 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1358 | def full_clean(self): |
| 1359 | result = super(ReservationAddForm, self).full_clean() |
Scott Baker | 8737e5f | 2013-05-17 09:35:32 -0700 | [diff] [blame] | 1360 | |
| 1361 | for fieldname in self.dont_validate_fields: |
| 1362 | if fieldname in self._errors: |
| 1363 | del self._errors[fieldname] |
| 1364 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1365 | return result |
| 1366 | |
| 1367 | """ don't save anything """ |
| 1368 | def is_valid(self): |
| 1369 | return False |
| 1370 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1371 | class ReservationAdmin(PlanetStackBaseAdmin): |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1372 | fieldList = ['backend_status_text', 'slice', 'startTime', 'duration'] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1373 | fieldsets = [('Reservation Details', {'fields': fieldList, 'classes': ['suit-tab suit-tab-general']})] |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1374 | readonly_fields = ('backend_status_text', ) |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1375 | list_display = ('startTime', 'duration') |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1376 | form = ReservationAddForm |
| 1377 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1378 | suit_form_tabs = (('general','Reservation Details'), ('reservedresources','Reserved Resources')) |
| 1379 | |
| 1380 | inlines = [ReservedResourceInline] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1381 | user_readonly_fields = fieldList |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1382 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1383 | def add_view(self, request, form_url='', extra_context=None): |
Scott Baker | acd4514 | 2013-05-19 16:19:16 -0700 | [diff] [blame] | 1384 | timezone.activate(request.user.timezone) |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1385 | request._refresh = False |
| 1386 | request._slice = None |
| 1387 | if request.method == 'POST': |
Scott Baker | 8737e5f | 2013-05-17 09:35:32 -0700 | [diff] [blame] | 1388 | # "refresh" will be set to "1" if the form was submitted due to |
| 1389 | # a change in the Slice dropdown. |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1390 | if request.POST.get("refresh","1") == "1": |
| 1391 | request._refresh = True |
| 1392 | request.POST["refresh"] = "0" |
Scott Baker | 8737e5f | 2013-05-17 09:35:32 -0700 | [diff] [blame] | 1393 | |
| 1394 | # Keep track of the slice that was selected, so the |
| 1395 | # reservedResource inline can filter items for the slice. |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1396 | request._slice = request.POST.get("slice",None) |
| 1397 | if (request._slice is not None): |
| 1398 | request._slice = Slice.objects.get(id=request._slice) |
| 1399 | |
| 1400 | result = super(ReservationAdmin, self).add_view(request, form_url, extra_context) |
| 1401 | return result |
| 1402 | |
Scott Baker | acd4514 | 2013-05-19 16:19:16 -0700 | [diff] [blame] | 1403 | def changelist_view(self, request, extra_context = None): |
| 1404 | timezone.activate(request.user.timezone) |
| 1405 | return super(ReservationAdmin, self).changelist_view(request, extra_context) |
| 1406 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1407 | def get_form(self, request, obj=None, **kwargs): |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 1408 | request._obj_ = obj |
| 1409 | if obj is not None: |
| 1410 | # For changes, set request._slice to the slice already set in the |
| 1411 | # object. |
| 1412 | request._slice = obj.slice |
| 1413 | self.form = ReservationChangeForm |
| 1414 | else: |
| 1415 | if getattr(request, "_refresh", False): |
| 1416 | self.form = ReservationAddRefreshForm |
| 1417 | else: |
| 1418 | self.form = ReservationAddForm |
| 1419 | return super(ReservationAdmin, self).get_form(request, obj, **kwargs) |
| 1420 | |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1421 | def get_readonly_fields(self, request, obj=None): |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 1422 | if (obj is not None): |
| 1423 | # Prevent slice from being changed after the reservation has been |
| 1424 | # created. |
| 1425 | return ['slice'] |
| 1426 | else: |
Scott Baker | 133c921 | 2013-05-17 09:09:11 -0700 | [diff] [blame] | 1427 | return [] |
Scott Baker | 3de3e37 | 2013-05-10 16:50:44 -0700 | [diff] [blame] | 1428 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 1429 | def queryset(self, request): |
| 1430 | return Reservation.select_by_user(request.user) |
| 1431 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1432 | class NetworkParameterTypeAdmin(PlanetStackBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1433 | list_display = ("backend_status_icon", "name", ) |
| 1434 | list_display_links = ('backend_status_icon', 'name', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1435 | user_readonly_fields = ['name'] |
| 1436 | user_readonly_inlines = [] |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1437 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1438 | class RouterAdmin(PlanetStackBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1439 | list_display = ("backend_status_icon", "name", ) |
| 1440 | list_display_links = ('backend_status_icon', 'name', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1441 | user_readonly_fields = ['name'] |
| 1442 | user_readonly_inlines = [] |
| 1443 | |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1444 | class RouterInline(PlStackTabularInline): |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1445 | model = Router.networks.through |
| 1446 | extra = 0 |
| 1447 | verbose_name_plural = "Routers" |
| 1448 | verbose_name = "Router" |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 1449 | suit_classes = 'suit-tab suit-tab-routers' |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1450 | |
Scott Baker | b27b62c | 2014-08-15 16:29:16 -0700 | [diff] [blame] | 1451 | class NetworkParameterInline(PlStackGenericTabularInline): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1452 | model = NetworkParameter |
Scott Baker | 618e379 | 2014-08-15 13:42:29 -0700 | [diff] [blame] | 1453 | extra = 0 |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1454 | verbose_name_plural = "Parameters" |
| 1455 | verbose_name = "Parameter" |
| 1456 | suit_classes = 'suit-tab suit-tab-netparams' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1457 | fields = ['backend_status_icon', 'parameter', 'value'] |
| 1458 | readonly_fields = ('backend_status_icon', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1459 | |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1460 | class NetworkSliversInline(PlStackTabularInline): |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1461 | fields = ['backend_status_icon', 'network','sliver','ip'] |
| 1462 | readonly_fields = ("backend_status_icon", "ip", ) |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1463 | model = NetworkSliver |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 1464 | selflink_fieldname = "sliver" |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1465 | extra = 0 |
| 1466 | verbose_name_plural = "Slivers" |
| 1467 | verbose_name = "Sliver" |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 1468 | suit_classes = 'suit-tab suit-tab-networkslivers' |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1469 | |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1470 | class NetworkSlicesInline(PlStackTabularInline): |
Scott Baker | d7d2a39 | 2013-08-06 08:57:30 -0700 | [diff] [blame] | 1471 | model = NetworkSlice |
Scott Baker | 874936e | 2014-01-13 18:15:34 -0800 | [diff] [blame] | 1472 | selflink_fieldname = "slice" |
Scott Baker | d7d2a39 | 2013-08-06 08:57:30 -0700 | [diff] [blame] | 1473 | extra = 0 |
| 1474 | verbose_name_plural = "Slices" |
| 1475 | verbose_name = "Slice" |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 1476 | suit_classes = 'suit-tab suit-tab-networkslices' |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1477 | fields = ['backend_status_icon', 'network','slice'] |
| 1478 | readonly_fields = ('backend_status_icon', ) |
Scott Baker | d7d2a39 | 2013-08-06 08:57:30 -0700 | [diff] [blame] | 1479 | |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 1480 | class ControllerNetworksInline(PlStackTabularInline): |
| 1481 | model = ControllerNetworks |
Scott Baker | fbb4586 | 2014-10-17 16:27:23 -0700 | [diff] [blame] | 1482 | extra = 0 |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 1483 | verbose_name_plural = "Controller Networks" |
| 1484 | verbose_name = "Controller Network" |
Scott Baker | fbb4586 | 2014-10-17 16:27:23 -0700 | [diff] [blame] | 1485 | suit_classes = 'suit-tab suit-tab-admin-only' |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 1486 | fields = ['backend_status_icon', 'controller','net_id','subnet_id'] |
Scott Baker | fbb4586 | 2014-10-17 16:27:23 -0700 | [diff] [blame] | 1487 | readonly_fields = ('backend_status_icon', ) |
| 1488 | |
Scott Baker | 9f6b8ed | 2014-11-17 23:44:03 -0800 | [diff] [blame] | 1489 | class NetworkForm(forms.ModelForm): |
| 1490 | class Meta: |
| 1491 | model = Network |
| 1492 | widgets = { |
| 1493 | 'topologyParameters': UploadTextareaWidget, |
| 1494 | 'controllerParameters': UploadTextareaWidget, |
| 1495 | } |
| 1496 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1497 | class NetworkAdmin(PlanetStackBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1498 | list_display = ("backend_status_icon", "name", "subnet", "ports", "labels") |
| 1499 | list_display_links = ('backend_status_icon', 'name', ) |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1500 | readonly_fields = ("subnet", ) |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 1501 | |
Scott Baker | d7d2a39 | 2013-08-06 08:57:30 -0700 | [diff] [blame] | 1502 | inlines = [NetworkParameterInline, NetworkSliversInline, NetworkSlicesInline, RouterInline] |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 1503 | admin_inlines = [ControllerNetworksInline] |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1504 | |
Scott Baker | 9f6b8ed | 2014-11-17 23:44:03 -0800 | [diff] [blame] | 1505 | form=NetworkForm |
| 1506 | |
Siobhan Tully | 2d95e48 | 2013-09-06 10:56:06 -0400 | [diff] [blame] | 1507 | fieldsets = [ |
Scott Baker | 4024871 | 2014-11-17 16:04:45 -0800 | [diff] [blame] | 1508 | (None, {'fields': ['backend_status_text', 'name','template','ports','labels','owner','guaranteedBandwidth', 'permitAllSlices','permittedSlices','network_id','router_id','subnet_id','subnet'], |
| 1509 | 'classes':['suit-tab suit-tab-general']}), |
Scott Baker | 7b6722e | 2014-11-17 16:18:00 -0800 | [diff] [blame] | 1510 | (None, {'fields': ['topologyParameters', 'controllerUrl', 'controllerParameters'], |
Scott Baker | 4024871 | 2014-11-17 16:04:45 -0800 | [diff] [blame] | 1511 | 'classes':['suit-tab suit-tab-sdn']}), |
| 1512 | ] |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1513 | |
Scott Baker | 40c0076 | 2014-08-21 16:55:59 -0700 | [diff] [blame] | 1514 | readonly_fields = ('backend_status_text', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1515 | 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] | 1516 | |
Scott Baker | fbb4586 | 2014-10-17 16:27:23 -0700 | [diff] [blame] | 1517 | @property |
| 1518 | def suit_form_tabs(self): |
| 1519 | tabs=[('general','Network Details'), |
Scott Baker | 4024871 | 2014-11-17 16:04:45 -0800 | [diff] [blame] | 1520 | ('sdn', 'SDN Configuration'), |
Scott Baker | fbb4586 | 2014-10-17 16:27:23 -0700 | [diff] [blame] | 1521 | ('netparams', 'Parameters'), |
| 1522 | ('networkslivers','Slivers'), |
| 1523 | ('networkslices','Slices'), |
| 1524 | ('routers','Routers'), |
| 1525 | ] |
| 1526 | |
| 1527 | request=getattr(_thread_locals, "request", None) |
| 1528 | if request and request.user.is_admin: |
| 1529 | tabs.append( ('admin-only', 'Admin-Only') ) |
| 1530 | |
| 1531 | return tabs |
| 1532 | |
| 1533 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1534 | class NetworkTemplateAdmin(PlanetStackBaseAdmin): |
Scott Baker | 63d1a55 | 2014-08-21 15:19:07 -0700 | [diff] [blame] | 1535 | list_display = ("backend_status_icon", "name", "guaranteedBandwidth", "visibility") |
| 1536 | list_display_links = ('backend_status_icon', 'name', ) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1537 | user_readonly_fields = ["name", "guaranteedBandwidth", "visibility"] |
| 1538 | user_readonly_inlines = [] |
Scott Baker | 4024871 | 2014-11-17 16:04:45 -0800 | [diff] [blame] | 1539 | fieldsets = [ |
| 1540 | (None, {'fields': ['name', 'description', 'guaranteedBandwidth', 'visibility', 'translation', 'sharedNetworkName', 'sharedNetworkId', 'topologyKind', 'controllerKind'], |
| 1541 | 'classes':['suit-tab suit-tab-general']}),] |
| 1542 | suit_form_tabs = (('general','Network Template Details'), ) |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1543 | |
Scott Baker | 37b4790 | 2014-09-02 14:37:41 -0700 | [diff] [blame] | 1544 | class FlavorAdmin(PlanetStackBaseAdmin): |
| 1545 | list_display = ("backend_status_icon", "name", "flavor", "order", "default") |
| 1546 | list_display_links = ("backend_status_icon", "name") |
| 1547 | user_readonly_fields = ("name", "flavor") |
| 1548 | fields = ("name", "description", "flavor", "order", "default") |
| 1549 | |
Tony Mack | 31c2b8f | 2013-04-26 20:01:42 -0400 | [diff] [blame] | 1550 | # register a signal that caches the user's credentials when they log in |
| 1551 | def cache_credentials(sender, user, request, **kwds): |
| 1552 | auth = {'username': request.POST['username'], |
| 1553 | 'password': request.POST['password']} |
| 1554 | request.session['auth'] = auth |
| 1555 | user_logged_in.connect(cache_credentials) |
| 1556 | |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1557 | def dollar_field(fieldName, short_description): |
| 1558 | def newFunc(self, obj): |
| 1559 | try: |
| 1560 | x= "$ %0.2f" % float(getattr(obj, fieldName, 0.0)) |
| 1561 | except: |
| 1562 | x=getattr(obj, fieldName, 0.0) |
| 1563 | return x |
| 1564 | newFunc.short_description = short_description |
| 1565 | return newFunc |
| 1566 | |
| 1567 | def right_dollar_field(fieldName, short_description): |
| 1568 | def newFunc(self, obj): |
| 1569 | try: |
| 1570 | #x= '<div align=right style="width:6em">$ %0.2f</div>' % float(getattr(obj, fieldName, 0.0)) |
| 1571 | x= '<div align=right>$ %0.2f</div>' % float(getattr(obj, fieldName, 0.0)) |
| 1572 | except: |
| 1573 | x=getattr(obj, fieldName, 0.0) |
| 1574 | return x |
| 1575 | newFunc.short_description = short_description |
| 1576 | newFunc.allow_tags = True |
| 1577 | return newFunc |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1578 | |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1579 | class InvoiceChargeInline(PlStackTabularInline): |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1580 | model = Charge |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1581 | extra = 0 |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1582 | verbose_name_plural = "Charges" |
| 1583 | verbose_name = "Charge" |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1584 | exclude = ['account'] |
Scott Baker | 9cb88a2 | 2013-12-09 18:56:00 -0800 | [diff] [blame] | 1585 | fields = ["date", "kind", "state", "object", "coreHours", "dollar_amount", "slice"] |
| 1586 | readonly_fields = ["date", "kind", "state", "object", "coreHours", "dollar_amount", "slice"] |
| 1587 | can_delete = False |
| 1588 | max_num = 0 |
| 1589 | |
| 1590 | dollar_amount = right_dollar_field("amount", "Amount") |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1591 | |
| 1592 | class InvoiceAdmin(admin.ModelAdmin): |
| 1593 | list_display = ("date", "account") |
| 1594 | |
| 1595 | inlines = [InvoiceChargeInline] |
| 1596 | |
Scott Baker | 9cb88a2 | 2013-12-09 18:56:00 -0800 | [diff] [blame] | 1597 | fields = ["date", "account", "dollar_amount"] |
| 1598 | readonly_fields = ["date", "account", "dollar_amount"] |
| 1599 | |
| 1600 | dollar_amount = dollar_field("amount", "Amount") |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1601 | |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1602 | class InvoiceInline(PlStackTabularInline): |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1603 | model = Invoice |
| 1604 | extra = 0 |
| 1605 | verbose_name_plural = "Invoices" |
| 1606 | verbose_name = "Invoice" |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1607 | fields = ["date", "dollar_amount"] |
| 1608 | readonly_fields = ["date", "dollar_amount"] |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1609 | suit_classes = 'suit-tab suit-tab-accountinvoice' |
| 1610 | can_delete=False |
| 1611 | max_num=0 |
| 1612 | |
| 1613 | dollar_amount = right_dollar_field("amount", "Amount") |
| 1614 | |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1615 | class PendingChargeInline(PlStackTabularInline): |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1616 | model = Charge |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1617 | extra = 0 |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1618 | verbose_name_plural = "Charges" |
| 1619 | verbose_name = "Charge" |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1620 | exclude = ["invoice"] |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1621 | fields = ["date", "kind", "state", "object", "coreHours", "dollar_amount", "slice"] |
| 1622 | readonly_fields = ["date", "kind", "state", "object", "coreHours", "dollar_amount", "slice"] |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1623 | suit_classes = 'suit-tab suit-tab-accountpendingcharges' |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1624 | can_delete=False |
| 1625 | max_num=0 |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1626 | |
| 1627 | def queryset(self, request): |
| 1628 | qs = super(PendingChargeInline, self).queryset(request) |
| 1629 | qs = qs.filter(state="pending") |
| 1630 | return qs |
| 1631 | |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1632 | dollar_amount = right_dollar_field("amount", "Amount") |
| 1633 | |
Scott Baker | 0165fac | 2014-01-13 11:49:26 -0800 | [diff] [blame] | 1634 | class PaymentInline(PlStackTabularInline): |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1635 | model=Payment |
| 1636 | extra = 1 |
| 1637 | verbose_name_plural = "Payments" |
| 1638 | verbose_name = "Payment" |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1639 | fields = ["date", "dollar_amount"] |
| 1640 | readonly_fields = ["date", "dollar_amount"] |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1641 | suit_classes = 'suit-tab suit-tab-accountpayments' |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1642 | can_delete=False |
| 1643 | max_num=0 |
| 1644 | |
| 1645 | dollar_amount = right_dollar_field("amount", "Amount") |
| 1646 | |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1647 | class AccountAdmin(admin.ModelAdmin): |
| 1648 | list_display = ("site", "balance_due") |
| 1649 | |
| 1650 | inlines = [InvoiceInline, PaymentInline, PendingChargeInline] |
| 1651 | |
| 1652 | fieldsets = [ |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1653 | (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] | 1654 | |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1655 | readonly_fields = ['site', 'dollar_balance_due', 'dollar_total_invoices', 'dollar_total_payments'] |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1656 | |
| 1657 | suit_form_tabs =( |
| 1658 | ('general','Account Details'), |
| 1659 | ('accountinvoice', 'Invoices'), |
| 1660 | ('accountpayments', 'Payments'), |
| 1661 | ('accountpendingcharges','Pending Charges'), |
| 1662 | ) |
| 1663 | |
Scott Baker | 15cddfa | 2013-12-09 13:45:19 -0800 | [diff] [blame] | 1664 | dollar_balance_due = dollar_field("balance_due", "Balance Due") |
| 1665 | dollar_total_invoices = dollar_field("total_invoices", "Total Invoices") |
| 1666 | dollar_total_payments = dollar_field("total_payments", "Total Payments") |
| 1667 | |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1668 | # Now register the new UserAdmin... |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 1669 | admin.site.register(User, UserAdmin) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1670 | # ... and, since we're not using Django's builtin permissions, |
| 1671 | # unregister the Group model from admin. |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 1672 | #admin.site.unregister(Group) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 1673 | |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1674 | #Do not show django evolution in the admin interface |
| 1675 | from django_evolution.models import Version, Evolution |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1676 | #admin.site.unregister(Version) |
| 1677 | #admin.site.unregister(Evolution) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1678 | |
| 1679 | |
| 1680 | # When debugging it is often easier to see all the classes, but for regular use |
| 1681 | # only the top-levels should be displayed |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1682 | showAll = False |
Scott Baker | 4310504 | 2013-12-06 23:23:36 -0800 | [diff] [blame] | 1683 | |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1684 | admin.site.register(Deployment, DeploymentAdmin) |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 1685 | admin.site.register(Controller, ControllerAdmin) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1686 | admin.site.register(Site, SiteAdmin) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1687 | admin.site.register(Slice, SliceAdmin) |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 1688 | admin.site.register(Service, ServiceAdmin) |
smbaker | a3cf70c | 2013-06-27 02:01:41 -0700 | [diff] [blame] | 1689 | admin.site.register(Reservation, ReservationAdmin) |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1690 | admin.site.register(Network, NetworkAdmin) |
| 1691 | admin.site.register(Router, RouterAdmin) |
Scott Baker | 74d8e62 | 2013-07-29 16:04:22 -0700 | [diff] [blame] | 1692 | admin.site.register(NetworkTemplate, NetworkTemplateAdmin) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1693 | admin.site.register(Account, AccountAdmin) |
| 1694 | admin.site.register(Invoice, InvoiceAdmin) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1695 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1696 | if True: |
| 1697 | admin.site.register(NetworkParameterType, NetworkParameterTypeAdmin) |
| 1698 | admin.site.register(ServiceClass, ServiceClassAdmin) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1699 | #admin.site.register(PlanetStack) |
Siobhan Tully | d351575 | 2013-06-21 16:34:53 -0400 | [diff] [blame] | 1700 | admin.site.register(Tag, TagAdmin) |
Tony Mack | 336e0f9 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 1701 | admin.site.register(ControllerRole) |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 1702 | admin.site.register(SiteRole) |
| 1703 | admin.site.register(SliceRole) |
| 1704 | admin.site.register(PlanetStackRole) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1705 | admin.site.register(Node, NodeAdmin) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 1706 | #admin.site.register(SlicePrivilege, SlicePrivilegeAdmin) |
| 1707 | #admin.site.register(SitePrivilege, SitePrivilegeAdmin) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1708 | admin.site.register(Sliver, SliverAdmin) |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 1709 | admin.site.register(Image, ImageAdmin) |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 1710 | admin.site.register(DashboardView, DashboardViewAdmin) |
Scott Baker | 37b4790 | 2014-09-02 14:37:41 -0700 | [diff] [blame] | 1711 | admin.site.register(Flavor, FlavorAdmin) |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 1712 | |