Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 1 | from core.models.slice import Slice |
Scott Baker | e791dc6 | 2014-08-28 14:02:54 -0700 | [diff] [blame] | 2 | from plus import PlusObjectMixin |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 3 | |
Scott Baker | e791dc6 | 2014-08-28 14:02:54 -0700 | [diff] [blame] | 4 | class SlicePlus(Slice, PlusObjectMixin): |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 5 | class Meta: |
| 6 | proxy = True |
| 7 | |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 8 | def getSliceInfo(self, user=None): |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 9 | used_sites = {} |
| 10 | used_deployments = {} |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 11 | sliverCount = 0 |
| 12 | for sliver in self.slivers.all(): |
| 13 | site = sliver.node.site |
| 14 | deployment = sliver.node.deployment |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 15 | used_sites[site.name] = used_sites.get(site.name, 0) + 1 |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 16 | used_deployments[deployment.name] = used_deployments.get(deployment.name, 0) + 1 |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 17 | sliverCount = sliverCount + 1 |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 18 | |
| 19 | roles = [] |
| 20 | if (user!=None): |
Scott Baker | c865508 | 2014-12-23 11:03:58 -0800 | [diff] [blame] | 21 | roles = [x.role.role for x in self.sliceprivileges.filter(user=user)] |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 22 | |
| 23 | return {"sitesUsed": used_sites, |
| 24 | "deploymentsUsed": used_deployments, |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 25 | "sliverCount": sliverCount, |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 26 | "siteCount": len(used_sites.keys()), |
| 27 | "roles": roles} |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 28 | |
| 29 | @staticmethod |
| 30 | def select_by_user(user): |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 31 | if user.is_admin: |
| 32 | qs = SlicePlus.objects.all() |
| 33 | else: |
| 34 | slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)] |
| 35 | qs = SlicePlus.objects.filter(id__in=slice_ids) |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 36 | return qs |