def _create_one_to_many(self, source_mdl, target_mdl, listnode_name=None, verbose_name=None): # other side of n-to-many should be a ListNode # with our source model as the sole element if not listnode_name: listnode_name = '%s_set' % un_camel(source_mdl.__name__) from .listnode import ListNode source_instance = source_mdl() source_instance.setattrs(_is_auto_created=True) # create a new class which extends ListNode listnode = type( listnode_name, (ListNode, ), { un_camel(source_mdl.__name__): source_instance, '_is_auto_created': True }) # listnode._add_linked_model(source_mdl, o2o=False, field=listnode_name, # reverse=un_camel(source_mdl.__name__), offff=target_mdl) # source_mdl._add_linked_model(target_mdl, o2o=False, ) target_mdl._nodes[listnode_name] = listnode # add just created model_set to model instances that # initialized inside of another model as linked model for instance_ref in target_mdl._instance_registry: mdl = instance_ref() if mdl: # if not yet garbage collected mdl._instantiate_node(listnode_name, listnode)
def _clean_field_value(self, dct): # get values of fields for name, field_ins in self._fields.items(): path_name = self._path_of(name) if path_name in self._secured_data: dct[un_camel(name)] = self._secured_data[path_name] else: dct[un_camel(name)] = field_ins.clean_value(self._field_values.get(name)) return dct
def save(self): """Crud Hitap Kaydet Nesneyi kaydeder. Eğer kayıt yeni ise ``sync`` alanını 4, mevcut kayıt güncellemesi ise 2 olarak işaretler. Hemen ardından zato servisi ile değişikliği bildirir. """ self.set_form_data_to_object() obj_is_new = not self.object.is_in_db() action, self.object.sync = ('ekle', 4) if obj_is_new else ('guncelle', 2) self.object.tckn = self.current.task_data['personel_tckn'] self.object.save() service_name = un_camel(self.model_class.__name__, dash='-') + '-' + action service = HitapService(service_name=service_name, payload=self.object, auth={"kullanici_ad": "", "kullanici_sifre": ""}) try: result = service.zato_request() self.object.kayit_no = result['kayitNo'] self.object.sync = 1 self.object.blocking_save() except: pass self.current.task_data['object_id'] = self.object.key
def delete(self): """Crud Hitap Sil Nesneyi kaydeder. ``sync`` alanını 3 olarak işaretler. Hemen ardından zato servisi ile değişikliği bildirir. """ signals.crud_pre_delete.send(self, current=self.current, object=self.object) if 'object_id' in self.current.task_data: del self.current.task_data['object_id'] object_data = self.object._data self.object.sync = 3 service_name = un_camel(self.model_class.__name__, dash='-') + "-sil" service = HitapService(service_name=service_name, payload={"tckn": self.object.tckn, "kayit_no": self.object.kayit_no}, auth={"kullanici_ad": "", "kullanici_sifre": ""}) try: service.zato_request() self.object.sync = 1 self.object.blocking_delete() signals.crud_post_delete.send(self, current=self.current, object_data=object_data) self.set_client_cmd('reload') except: pass
def _process_links_from_nodes_of_mdl(self, source_mdl): # print("Node: %s" % source_mdl.__name__) for node in source_mdl._nodes.values(): # print(node) for lnk in node.get_links(): reverse_name = un_camel( lnk['reverse'] or source_mdl.__name__ + ('' if lnk['o2o'] else '_set')) if lnk['o2o']: lnk['mdl']._add_linked_model(source_mdl, o2o=True, field=reverse_name, reverse=lnk['field'], lnksrc='_prcs_lnks_frm_nodes_of_mdl__o2o') self._create_one_to_one(source_mdl, target_mdl=lnk['mdl'], field_name=reverse_name) else: lnk['mdl']._add_linked_model(source_mdl, o2o=False, field=reverse_name, reverse=lnk['field'], m2m=node._TYPE == 'ListNode', is_set=True, lnksrc='_prcs_lnks_frm_nodes_of_mdl__O2M') source_mdl._add_linked_model(lnk['mdl'], o2o=False, field=lnk['field'], reverse=reverse_name, m2m=node._TYPE == 'ListNode', is_set=True, lnksrc='_prcs_lnks_frm_nodes_of_mdl__O2M_SRCMDL') self._create_one_to_many(source_mdl, target_mdl=lnk['mdl'], listnode_name=lnk['reverse'])
def _process_links(self, mdl): for lnk in mdl.get_links(): # custom reverse name or model name if one to one # or model_name_set otherwise reverse_name = un_camel( lnk['reverse'] or mdl.__name__ + ('' if lnk['o2o'] else '_set')) if lnk['reverse'] is None: # fill the missing 'reverse' info idx = mdl._linked_models[lnk['mdl'].__name__].index(lnk) lnk['reverse'] = reverse_name mdl._linked_models[lnk['mdl'].__name__][idx] = lnk # self.link_registry[lnk['mdl']].append((name, mdl, reverse_name)) if lnk['o2o']: self._create_one_to_one(mdl, lnk['mdl'], reverse_name) lnk['mdl']._add_linked_model(mdl, o2o=True, field=reverse_name, null=lnk['null'], reverse=lnk['field'], lnksrc='_process_links__o2o') else: lnk['mdl']._add_linked_model(mdl, reverse=lnk['field'], null=lnk['null'], # node=lnk['node'], m2m='.' in lnk['field'], field=reverse_name, is_set=True, lnksrc='_process_links__O2M') self._create_one_to_many(mdl, lnk['mdl'], reverse_name)
def _path_of(self, prop): """ returns the dotted path of the given model attribute """ root = self.root or self return ('.'.join(list(self.path + [un_camel(self.__class__.__name__), prop]))).replace(root._get_bucket_name() + '.', '')
def clean_value(self): dct = {} for name in self._nodes: dct[un_camel(name)] = getattr(self, name).clean_value() if self._linked_models: dct['_cache'] = {} for name in self._linked_models: obj = getattr(self, name) dct[un_camel_id(name)] = obj.key or '' if obj.key: dct['_cache'][un_camel(name)] = obj.clean_value() dct['_cache'][un_camel(name)]['key'] = obj.key for name, field_ins in self._fields.items(): # if field_ins dct[un_camel(name)] = field_ins.clean_value(self._field_values[name]) return dct
def _load_data(self, data): for name in self._nodes: _name = un_camel(name) if _name in data: getattr(self, name)._load_data(data[_name]) self._set_fields_values(data) return self
def _clean_node_value(self, dct): # get values of nodes for name in self._nodes: node = getattr(self, name) node.processed_nodes = self.processed_nodes dct[un_camel(name)] = node.clean_value() return dct
def _process_links(self, mdl): for lnk in mdl.get_links(): # custom reverse name or model name if one to one # or model_name_set otherwise reverse_name = un_camel(lnk['reverse'] or mdl.__name__ + ('' if lnk['o2o'] else '_set')) if lnk['reverse'] is None: # fill the missing 'reverse' info idx = mdl._linked_models[lnk['mdl'].__name__].index(lnk) lnk['reverse'] = reverse_name mdl._linked_models[lnk['mdl'].__name__][idx] = lnk # self.link_registry[lnk['mdl']].append((name, mdl, reverse_name)) if lnk['o2o']: self._create_one_to_one(mdl, lnk['mdl'], reverse_name) lnk['mdl']._add_linked_model(mdl, o2o=True, field=reverse_name, null=lnk['null'], reverse=lnk['field'], lnksrc='_process_links__o2o') else: lnk['mdl']._add_linked_model( mdl, reverse=lnk['field'], null=lnk['null'], # node=lnk['node'], m2m='.' in lnk['field'], field=reverse_name, is_set=True, lnksrc='_process_links__O2M') self._create_one_to_many(mdl, lnk['mdl'], reverse_name)
def __init__(mcs, name, bases, attrs): if mcs.__name__ not in ('Model', 'Node', 'ListNode'): ModelMeta.process_objects(mcs) if mcs.__base__.__name__ == 'Model': # add models to model_registry mcs.objects = QuerySet(model_class=mcs) model_registry.register_model(mcs) if 'bucket_name' not in mcs.Meta.__dict__: mcs.Meta.bucket_name = un_camel(mcs.__name__)
def _load_data(self, data): """ """ for node_data in data: clone = self.__class__(**node_data) for name in self._nodes: _name = un_camel(name) if _name in node_data: # check for partial data getattr(clone, name)._load_data(node_data[_name]) self.node_stack.append(clone)
def _create_one_to_many(self, source_mdl, target_mdl, listnode_name=None, verbose_name=None): # other side of n-to-many should be a ListNode # with our source model as the sole element if not listnode_name: listnode_name = '%s_set' % un_camel(source_mdl.__name__) from .listnode import ListNode source_instance = source_mdl() source_instance.setattrs(_is_auto_created = True) # create a new class which extends ListNode listnode = type(listnode_name, (ListNode,), {un_camel(source_mdl.__name__): source_instance, '_is_auto_created': True}) # listnode._add_linked_model(source_mdl, o2o=False, field=listnode_name, # reverse=un_camel(source_mdl.__name__), offff=target_mdl) # source_mdl._add_linked_model(target_mdl, o2o=False, ) target_mdl._nodes[listnode_name] = listnode # add just created model_set to model instances that # initialized inside of another model as linked model for instance_ref in target_mdl._instance_registry: mdl = instance_ref() if mdl: # if not yet garbage collected mdl._instantiate_node(listnode_name, listnode)
def _make_instance(self, node_data): node_data['from_db'] = self._from_db clone = self.__call__(**node_data) clone.container = self clone._is_item = True for name in self._nodes: _name = un_camel(name) if _name in node_data: # check for partial data getattr(clone, name)._load_data(node_data[_name]) cache = node_data.get('_cache', {}) for name, (model, is_one_to_one) in self._linked_models.items(): _name = un_camel(name) if _name in cache: ins = model() ins(**cache[_name]) ins.key = cache[_name]['key'] ins.set_data(cache[_name], self._from_db) setattr(clone, name, ins) self.node_dict[clone.key] = clone # self.node_stack.append(clone) return clone
def _clean_linked_model_value(self, dct): # get vales of linked models dct['_cache'] = {} for name in self._linked_models: link_mdl = getattr(self, name) # print("LM: ", link_mdl) link_mdl.processed_nodes = self.processed_nodes _name = un_camel(name) dct[un_camel_id(name)] = link_mdl.key or '' if link_mdl.is_in_db() and not link_mdl._is_auto_created: dct['_cache'][_name] = link_mdl.clean_value() else: dct['_cache'][_name] = {} dct['_cache'][_name]['key'] = link_mdl.key
def _load_data(self, data): """ first calls supers load_data then fills linked models :param data: :return: """ super(Model, self)._load_data(data) cache = data.get('_cache', {}) for name in self._linked_models: _name = un_camel(name) if _name in cache: mdl = getattr(self, name) mdl.key = cache[_name]['key'] mdl._load_data(cache[_name]) return self
def create_channel_and_service_object(service, path): from ulakbus.models.zato import ZatoServiceChannel from pyoko.lib.utils import un_camel create_channel = ZatoServiceChannel() create_channel.cluster_id = 1 create_channel.service_name = service.get_name() create_channel.channel_name = service.get_name() + "-channel" create_channel.channel_url_path = '/%s/%s' % ('/'.join( path.split('/')[:-1]), un_camel(service.__name__, dash='-')) create_channel.channel_connection = "channel" create_channel.channel_data_format = "json" create_channel.channel_is_internal = False create_channel.channel_is_active = True create_channel.channel_transport = "plain_http" create_channel.save()
def _process_links_from_nodes_of_mdl(self, source_mdl): # print("Node: %s" % source_mdl.__name__) _src_mdl_ins = source_mdl(super_fake_context) for node_name in source_mdl._nodes.keys(): node = getattr(_src_mdl_ins, node_name) # print(node) for lnk in node.get_links(): reverse_name = un_camel(source_mdl.__name__ + ('' if lnk['o2o'] else '_set')) if lnk['o2o']: lnk['mdl']._add_linked_model( source_mdl, o2o=True, field=reverse_name, reverse=lnk['field'], null=lnk['null'], # node=node_name, lnksrc='_prcs_lnks_frm_nodes_of_mdl__o2o') self._create_one_to_one(source_mdl, target_mdl=lnk['mdl'], field_name=reverse_name) else: lnk['mdl']._add_linked_model( source_mdl, o2o=False, null=lnk['null'], field=reverse_name, reverse=node_name + '.' + lnk['field'], m2m=node._TYPE == 'ListNode', is_set=True, # node=node_name, lnksrc='_prcs_lnks_frm_nodes_of_mdl__O2M') source_mdl._add_linked_model( lnk['mdl'], o2o=False, null=lnk['null'], field=node_name + '.' + lnk['field'], reverse=reverse_name, m2m=node._TYPE == 'ListNode', is_set=True, # node=node_name, lnksrc='_prcs_lnks_frm_nodes_of_mdl__O2M_SRCMDL') self._create_one_to_many(source_mdl, target_mdl=lnk['mdl'], listnode_name=lnk['reverse'])
def register_model(self, klass): if klass not in self.registry: # register model to base registry self.registry[klass.__name__] = klass klass_name = un_camel(klass.__name__) self._process_many_to_many(klass, klass_name) for name, ( linked_model, is_one_to_one) in klass._linked_models.items(): # register models that linked from this model self.link_registry[linked_model].append((name, klass)) # register models that gives (back)links to this model self.back_link_registry[klass].append((name, klass_name, linked_model)) if is_one_to_one: self._process_one_to_one(klass, klass_name, linked_model) else: self._process_one_to_many(klass, klass_name, linked_model)
def set_data(self, data, from_db=False): """ first calls supers load_data then fills linked models :param from_db: if data coming from db then we will use related field type's _load_data method :param data: data :return: """ self._load_data(data, from_db) cache = data.get('_cache', {}) if cache: for name in self._linked_models: _name = un_camel(name) if _name in cache: mdl = getattr(self, name) mdl.key = cache[_name]['key'] mdl.set_data(cache[_name], from_db) return self
def _process_links_from_nodes_of_mdl(self, source_mdl): # print("Node: %s" % source_mdl.__name__) _src_mdl_ins = source_mdl(super_fake_context) for node_name in source_mdl._nodes.keys(): node = getattr(_src_mdl_ins, node_name) # print(node) for lnk in node.get_links(): reverse_name = un_camel(source_mdl.__name__ + ('' if lnk['o2o'] else '_set')) if lnk['o2o']: lnk['mdl']._add_linked_model(source_mdl, o2o=True, field=reverse_name, reverse=lnk['field'], null=lnk['null'], # node=node_name, lnksrc='_prcs_lnks_frm_nodes_of_mdl__o2o') self._create_one_to_one(source_mdl, target_mdl=lnk['mdl'], field_name=reverse_name) else: lnk['mdl']._add_linked_model(source_mdl, o2o=False, null=lnk['null'], field=reverse_name, reverse=node_name + '.' + lnk['field'], m2m=node._TYPE == 'ListNode', is_set=True, # node=node_name, lnksrc='_prcs_lnks_frm_nodes_of_mdl__O2M') source_mdl._add_linked_model(lnk['mdl'], o2o=False, null=lnk['null'], field=node_name + '.' + lnk['field'], reverse=reverse_name, m2m=node._TYPE == 'ListNode', is_set=True, # node=node_name, lnksrc='_prcs_lnks_frm_nodes_of_mdl__O2M_SRCMDL') self._create_one_to_many(source_mdl, target_mdl=lnk['mdl'], listnode_name=lnk['reverse'])
def sync(self): """Crud Hitap Sync Personele ait kayitlari hitap ile sync eder. Zamanlanmis sync islemini manuel olarak calistirir. """ # Sync işleminden önce ekleme veya güncelleme işlemlerinden biri yapıldıysa # self.current.task_data nın içinde en son işlem yapılan objenin keyi bulunuyor # eğer bu obje hitap ile sync işleminde silinirse 404 hatasına sebeb oluyor. # bu sorunu cozmek için aşağıdaki yöntem uygulanmıştır. if 'object_id' in self.current.task_data: del self.current.task_data['object_id'] service_name = un_camel(self.model_class.__name__, dash='-') + '-sync' service = TcknService(service_name=service_name, payload={"tckn": str(self.current.task_data['personel_tckn']), "kullanici_ad": "", "kullanici_sifre": ""}) service.zato_request()
def _process_links(self, mdl): for lnk in mdl.get_links(): # custom reverse name or model name if one to one # or model_name_set otherwise reverse_name = un_camel( lnk['reverse'] or mdl.__name__ + ('' if lnk['o2o'] else '_set')) # self.link_registry[lnk['mdl']].append((name, mdl, reverse_name)) if lnk['o2o']: self._create_one_to_one(mdl, lnk['mdl'], reverse_name) lnk['mdl']._add_linked_model(mdl, o2o=True, field=reverse_name, lnksrc='_process_links__o2o') else: lnk['mdl']._add_linked_model(mdl, reverse=lnk['field'], field=reverse_name, is_set=True, lnksrc='_process_links__O2M') self._create_one_to_many(mdl, lnk['mdl'], reverse_name)
def _load_data(self, data, from_db=False): """ With the data returned from riak: - fills model's fields, nodes and listnodes - instantiates linked model instances :type bool from_db: if data coming from db instead of calling self._set_fields_values() we simply use field's _load_data method. :param dict data: :return: self """ self._data = data.copy() for name in self._nodes: _name = un_camel(name) if _name in self._data: new = self._instantiate_node(name, getattr(self, name).__class__) # new = getattr(self, name).__class__(**{}) new._load_data(self._data[_name], from_db) # setattr(self, name, new) self._data['from_db'] = from_db self._set_fields_values(self._data) return self
def _get_bucket_name(cls): return cls._META.get('bucket_name', un_camel(cls.__name__))
def _path_of(self, prop): """ returns the dotted path of the given model attribute """ return '.'.join(list(self.path + [un_camel(self.__class__.__name__), prop]))
def register_model(self, kls): if kls not in self.registry: self.registry += [kls] for name, link_model in kls._linked_models.items(): self.link_registry[link_model].append((name, kls)) setattr(link_model, '%s_set' % un_camel(kls.__name__), kls)
def get_name(cls): return un_camel(cls.__name__, dash='-')
def _name(self): return un_camel(self.__class__.__name)
def clean_field_values(self): return dict((un_camel(name), field_ins.clean_value(self._field_values.get(name))) for name, field_ins in self._fields.items())