blob: 465cbda35e2cab9a5b448f9944e4686fd2603bc3 [file] [log] [blame]
Scott Baker5f4770d2014-07-13 11:17:58 -07001from core.models.slice import Slice
Scott Bakere791dc62014-08-28 14:02:54 -07002from plus import PlusObjectMixin
Scott Baker5f4770d2014-07-13 11:17:58 -07003
Scott Bakere791dc62014-08-28 14:02:54 -07004class SlicePlus(Slice, PlusObjectMixin):
Scott Baker88e34372014-07-13 11:46:36 -07005 class Meta:
6 proxy = True
7
Scott Baker5f4770d2014-07-13 11:17:58 -07008 def getSliceInfo(self, user=None):
Scott Baker88e34372014-07-13 11:46:36 -07009 used_sites = {}
10 used_deployments = {}
Scott Baker5f4770d2014-07-13 11:17:58 -070011 sliverCount = 0
12 for sliver in self.slivers.all():
13 site = sliver.node.site
14 deployment = sliver.node.deployment
Scott Baker88e34372014-07-13 11:46:36 -070015 used_sites[site.name] = used_sites.get(site.name, 0) + 1
Scott Baker5f4770d2014-07-13 11:17:58 -070016 used_deployments[deployment.name] = used_deployments.get(deployment.name, 0) + 1
Scott Baker88e34372014-07-13 11:46:36 -070017 sliverCount = sliverCount + 1
Scott Baker5f4770d2014-07-13 11:17:58 -070018
19 roles = []
20 if (user!=None):
Scott Bakerdf54cf22014-12-23 11:03:58 -080021 roles = [x.role.role for x in self.sliceprivileges.filter(user=user)]
Scott Baker5f4770d2014-07-13 11:17:58 -070022
23 return {"sitesUsed": used_sites,
24 "deploymentsUsed": used_deployments,
Scott Baker88e34372014-07-13 11:46:36 -070025 "sliverCount": sliverCount,
Scott Baker5f4770d2014-07-13 11:17:58 -070026 "siteCount": len(used_sites.keys()),
27 "roles": roles}
Scott Baker88e34372014-07-13 11:46:36 -070028
29 @staticmethod
30 def select_by_user(user):
Scott Baker88e34372014-07-13 11:46:36 -070031 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 Baker88e34372014-07-13 11:46:36 -070036 return qs