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