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