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