helper code for getting a list of IP addresses from a sliver
diff --git a/planetstack/core/admin.py b/planetstack/core/admin.py
index 460a453..6db62ff 100644
--- a/planetstack/core/admin.py
+++ b/planetstack/core/admin.py
@@ -201,6 +201,8 @@
the ip address for that network.
"""
+ byNetworkName = {} # class variable
+
def __init__(self, name):
self.short_description = name
self.__name__ = name
@@ -216,6 +218,17 @@
def __str__(self):
return self.network_name
+ @staticmethod
+ def get(network_name):
+ """ We want to make sure we alwars return the same NetworkLookerUpper
+ because sometimes django will cause them to be instantiated multiple
+ times (and we don't want different ones in form.fields vs
+ SliverInline.readonly_fields).
+ """
+ if network_name not in NetworkLookerUpper.byNetworkName:
+ NetworkLookerUpper.byNetworkName[network_name] = NetworkLookerUpper(network_name)
+ return NetworkLookerUpper.byNetworkName[network_name]
+
class SliverROInline(ReadOnlyTabularInline):
model = Sliver
fields = ['ip', 'instance_name', 'slice', 'numberCores', 'deploymentNetwork', 'image', 'node']
@@ -223,9 +236,9 @@
class SliverInline(PlStackTabularInline):
model = Sliver
- fields = ['ip', 'instance_name', 'slice', 'numberCores', 'deploymentNetwork', 'image', 'node']
+ fields = ['all_ips_string', 'instance_name', 'slice', 'numberCores', 'deploymentNetwork', 'image', 'node']
extra = 0
- readonly_fields = ['ip', 'instance_name']
+ readonly_fields = ['all_ips_string', 'instance_name']
suit_classes = 'suit-tab suit-tab-slivers'
def queryset(self, request):
@@ -246,43 +259,45 @@
return field
-# Note this is breaking in the admin.py when trying to use an inline to add a node/image
-# def _declared_fieldsets(self):
-# # Return None so django will call get_fieldsets and we can insert our
-# # dynamic fields
-# return None
-#
-# def get_readonly_fields(self, request, obj=None):
-# readonly_fields = super(SliverInline, self).get_readonly_fields(request, obj)
-#
-# # Lookup the networks that are bound to the slivers, and add those
-# # network names to the list of readonly fields.
-#
-# for sliver in obj.slivers.all():
-# for nbs in sliver.networksliver_set.all():
-# if nbs.ip:
-# network_name = nbs.network.name
-# if network_name not in [str(x) for x in readonly_fields]:
-# readonly_fields.append(NetworkLookerUpper(network_name))
-#
-# return readonly_fields
-#
-# def get_fieldsets(self, request, obj=None):
-# form = self.get_formset(request, obj).form
-# # fields = the read/write files + the read-only fields
-# fields = self.fields
-# for fieldName in self.get_readonly_fields(request,obj):
-# if not fieldName in fields:
-# fields.append(fieldName)
-#
-# return [(None, {'fields': fields})]
+"""
+ SMBAKER: This is the old code that implemented each network type as a
+ separate column in the sliver table.
-
+ def _declared_fieldsets(self):
+ # Return None so django will call get_fieldsets and we can insert our
+ # dynamic fields
+ return None
+
+ def get_readonly_fields(self, request, obj=None):
+ readonly_fields = list(super(SliverInline, self).get_readonly_fields(request, obj))
+
+ # Lookup the networks that are bound to the slivers, and add those
+ # network names to the list of readonly fields.
+
+ for sliver in obj.slivers.all():
+ for nbs in sliver.networksliver_set.all():
+ if nbs.ip:
+ network_name = nbs.network.name
+ if network_name not in [str(x) for x in readonly_fields]:
+ readonly_fields.append(NetworkLookerUpper.get(network_name))
+
+ return readonly_fields
+
+ def get_fieldsets(self, request, obj=None):
+ form = self.get_formset(request, obj).form
+ # fields = the read/write files + the read-only fields
+ fields = list(self.fields)
+ for fieldName in self.get_readonly_fields(request,obj):
+ if not fieldName in fields:
+ fields.append(fieldName)
+
+ return [(None, {'fields': fields})]
+"""
class SiteROInline(ReadOnlyTabularInline):
model = Site
extra = 0
- fields = ['name', 'login_base', 'site_url', 'enabled']
+ fields = ['name', 'login_base', 'site_url', 'enabled']
suit_classes = 'suit-tab suit-tab-sites'
class SiteInline(PlStackTabularInline):
diff --git a/planetstack/core/models/sliver.py b/planetstack/core/models/sliver.py
index 8b05bb2..e0ce5e2 100644
--- a/planetstack/core/models/sliver.py
+++ b/planetstack/core/models/sliver.py
@@ -53,6 +53,21 @@
def can_update(self, user):
return self.slice.can_update(user)
+ def all_ips(self):
+ ips={}
+ for ns in self.networksliver_set.all():
+ ips[ns.network.name] = ns.ip
+ return ips
+
+ def all_ips_string(self):
+ result = []
+ ips = self.all_ips()
+ for key in sorted(ips.keys()):
+ #result.append("%s = %s" % (key, ips[key]))
+ result.append(ips[key])
+ return ", ".join(result)
+ all_ips_string.short_description = "addresses"
+
@staticmethod
def select_by_user(user):
if user.is_admin: