Exemple #1
0
 def get_object_stencil(mo: ManagedObject) -> Optional[Stencil]:
     if mo.shape:
         # Use mo's shape, if set
         return stencil_registry.get(mo.shape)
     elif mo.object_profile.shape:
         # Use profile's shape
         return stencil_registry.get(mo.object_profile.shape)
     return stencil_registry.get(stencil_registry.DEFAULT_STENCIL)
Exemple #2
0
 def instance_to_dict(self, o, fields=None):
     r = {}
     for f in o._meta.local_fields:
         if fields and f.name not in fields:
             continue  # Restrict to selected fields
         if (self.secret_fields and f.name in self.secret_fields
                 and getattr(o, f.name) and not self.has_secret()):
             # Sensitive fields (limit view only to *secret* permission)
             r[f.name] = self.SECRET_MASK
         elif f.name == "bi_id":
             # Long integer send as string
             r[f.name] = str(o.bi_id)
         elif f.name == "tags":
             # Send tags as a list
             r[f.name] = getattr(o, f.name)
         elif f.name == "shape":
             if o.shape:
                 v = stencil_registry.get(o.shape)
                 r[f.name] = v.id
                 r["%s__label" % f.name] = unicode(v.title)
         elif hasattr(f, "document"):
             # DocumentReferenceField
             v = getattr(o, f.name)
             if v:
                 r[f.name] = str(v.pk)
                 r["%s__label" % f.name] = unicode(v)
             else:
                 r[f.name] = None
                 r["%s__label" % f.name] = ""
         elif f.rel is None:
             v = f._get_val_from_obj(o)
             if (v is not None and not isinstance(v, six.string_types)
                     and not isinstance(v, six.integer_types)
                     and not isinstance(v, (bool, list))):
                 if isinstance(v, datetime.datetime):
                     v = v.isoformat()
                 else:
                     v = unicode(v)
             r[f.name] = v
         else:
             v = getattr(o, f.name)
             if v:
                 r[f.name] = v._get_pk_val()
                 r["%s__label" % f.name] = unicode(v)
             else:
                 r[f.name] = None
                 r["%s__label" % f.name] = ""
     # Add m2m fields
     for n in self.m2m_fields:
         r[n] = [{
             "id": str(mmo.pk),
             "label": unicode(mmo)
         } for mmo in getattr(o, n).all()]
         # r[n] = list(getattr(o, n).values_list("id", flat=True))
     # Add custom fields
     for f in self.custom_fields:
         if fields and f not in fields:
             continue
         r[f] = self.custom_fields[f](o)
     return r
Exemple #3
0
 def get_object_stencil(mo):
     if mo.shape:
         # Use mo's shape, if set
         shape_id = mo.shape
     elif mo.object_profile.shape:
         # Use profile's shape
         shape_id = mo.object_profile.shape
     else:
         shape_id = None
     return stencil_registry.get(shape_id)
Exemple #4
0
 def handle_htmlindex(self, out=None, *args, **options):
     stencils = defaultdict(list)
     for stencil_id in sorted(stencil_registry.stencils):
         coll, name = stencil_id.split("/", 1)
         stencils[coll] += [stencil_registry.get(stencil_id)]
     with open(self.INDEX_TEMPLATE_PATH) as f:
         tpl = Template(f.read())
     r = tpl.render({"stencils": stencils})
     if out:
         with open(out, "w") as f:
             f.write(r)
     else:
         sys.stdout.write(r)
Exemple #5
0
 def instance_to_dict(self, o, fields=None):
     r = {}
     for f in o._meta.local_fields:
         if fields and f.name not in fields:
             continue  # Restrict to selected fields
         if (
             self.secret_fields
             and f.name in self.secret_fields
             and getattr(o, f.name)
             and not self.has_secret()
         ):
             # Sensitive fields (limit view only to *secret* permission)
             r[f.name] = self.SECRET_MASK
         elif f.name == "bi_id":
             # Long integer send as string
             r[f.name] = str(o.bi_id)
         elif f.name == "tags":
             # Send tags as a list
             r[f.name] = getattr(o, f.name)
         elif f.name == "shape":
             if o.shape:
                 v = stencil_registry.get(o.shape)
                 r[f.name] = v.id
                 r["%s__label" % f.name] = smart_text(v.title)
         elif f.name == "labels" and isinstance(f, ArrayField):
             r["labels"] = [
                 {
                     "id": ll.name,
                     "is_protected": ll.is_protected,
                     "scope": ll.name.rsplit("::", 1)[0] if ll.is_scoped else "",
                     "name": ll.name,
                     "value": ll.name.split("::")[-1],
                     "bg_color1": f"#{ll.bg_color1:06x}",
                     "fg_color1": f"#{ll.fg_color1:06x}",
                     "bg_color2": f"#{ll.bg_color2:06x}",
                     "fg_color2": f"#{ll.fg_color2:06x}",
                 }
                 for ll in Label.objects.filter(name__in=getattr(o, f.name, []))
             ]
         elif hasattr(f, "document"):
             # DocumentReferenceField
             v = getattr(o, f.name)
             if v:
                 r[f.name] = str(v.pk)
                 r["%s__label" % f.name] = smart_text(v)
             else:
                 r[f.name] = None
                 r["%s__label" % f.name] = ""
         elif not is_related_field(f):
             v = f.value_from_object(o)
             if (
                 v is not None
                 and not isinstance(v, str)
                 and not isinstance(v, int)
                 and not isinstance(v, (bool, list))
             ):
                 if isinstance(v, datetime.datetime):
                     v = v.isoformat()
                 else:
                     v = smart_text(v)
             r[f.name] = v
         else:
             v = getattr(o, f.name)
             if v:
                 r[f.name] = v._get_pk_val()
                 r["%s__label" % f.name] = smart_text(v)
             else:
                 r[f.name] = None
                 r["%s__label" % f.name] = ""
     # Add m2m fields
     for n in self.m2m_fields:
         r[n] = [{"id": str(mmo.pk), "label": smart_text(mmo)} for mmo in getattr(o, n).all()]
         # r[n] = list(getattr(o, n).values_list("id", flat=True))
     # Add custom fields
     for f in self.custom_fields:
         if fields and f not in fields:
             continue
         r[f] = self.custom_fields[f](o)
     return r
Exemple #6
0
 def get_cloud_stencil(link):
     return stencil_registry.get(link.shape
                                 or stencil_registry.DEFAULT_CLOUD_STENCIL)