def prepare_schemes(**kwargs): attr = kwargs['attr'] #чистим дерево roots = Analytic.get_root_nodes() for root in roots: root.delete() #создаём все атрибутные схемы из AOTs schemes = AOT.objects.filter(attr=attr).values('schema').annotate(count=Count('schema')).order_by() #schemes = AOT.objects.filter(attr=attr, schema__id=1).values('schema').annotate(count=Count('schema')).order_by() for schema in schemes: schema = AS.objects.get(id=schema['schema'].__int__()) Analytic.add_root(nc_id=str(schema.id), text=schema.name, description=schema.description, type='AS', attr_schema_name=schema.name) #создаём схемы верхнего уровня(в том числе промежуточные) nodes = Analytic.objects.all() for node in nodes: schema = AS.objects.get(id=int(node.nc_id)) while schema.parent: try: Analytic.objects.get(nc_id=str(schema.parent.id)) except ObjectDoesNotExist: Analytic.add_root(nc_id=str(schema.parent.id), text=schema.parent.name, description=schema.parent.description, type='AS', attr_schema_name=schema.parent.name) schema = schema.parent #меняем parent для атрибутных схем nodes = Analytic.objects.all() for node in nodes: schema = AS.objects.get(id=int(node.nc_id)) if schema.parent: parent = Analytic.objects.get(nc_id=str(schema.parent.id)) node.parent = parent node.save() #update EXPANDED&LEAF nodes = Analytic.objects.all() for node in nodes: node.expanded = True node.leaf = False node.save()
def worker(req): """ функция генерациия аналитики """ attr = req.params()['attr'] #prepare schemes prepare_schemes(attr=attr) #создаём атрибутные схемы schemes = AOT.objects.filter(attr=attr).values('schema').annotate(count=Count('schema')).order_by() for schema in schemes: orig_schema = AS.objects.get(id=schema['schema'].__int__()) aots = AOT.objects.filter(attr=attr, schema=orig_schema).order_by('schema', 'object_type') #создаём все bind/unbind for aot in aots: #origin if aot.options == 0: origin = 'bind' if aot.options == 2: origin = 'unbind' if aot.options not in (0,2): origin = 'unknown' Analytic.objects.create(nc_id=aot.object_type.id, sib_order = 1, text=aot.object_type.name, description=aot.object_type.description, type='OT', attr_schema_name=aot.schema.name, origin=origin) #создаём промежуточные OT ots = Analytic.objects.filter(attr_schema_name=orig_schema.name, type='OT') for ot in ots: create_parent(orig_schema, ot) #проставляем parent ots = Analytic.objects.filter(attr_schema_name=orig_schema.name, type='OT') for ot in ots: object_type = OT.objects.get(id=ot.nc_id) if object_type.parent is not None: ot.parent = Analytic.objects.get(attr_schema_name=orig_schema.name, type='OT', nc_id=str(object_type.parent.id.__int__())) else: ot.parent = Analytic.objects.get(attr_schema_name=orig_schema.name, type='AS') ot.save() #update sib_order nodes = Analytic.objects.filter(nc_id='0') for node in nodes: node.sib_order = 1 node.save() #update AS icons nodes = Analytic.objects.filter(type='AS') for node in nodes: node.iconCls = settings.ATTR_SCHEMA_ICON node.save() nodes = Analytic.objects.filter(type='OT', attr_schema_name=orig_schema.name).order_by('sib_order') #update LEAF for node in nodes: if Analytic.objects.filter(parent=node).count() > 0: node.leaf = False else: node.leaf = True node.save() #update EXPANDED for node in nodes: if Analytic.objects.filter(parent=node).count() > 0: node.expanded = True else: node.expanded = False node.save() #update CHECKED&DISABLED for node in nodes: if node.origin == 'bind': node.iconCls = settings.BIND_ICON if node.origin == 'unbind': node.iconCls = settings.UNBIND_ICON if node.origin == 'unknown': node.iconCls = settings.UNKNOWN_ICON node.save() return Analytic.dump_bulk()