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