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