def get_beid_diffs(self, attrs): """Overrides :meth:`lino_xl.lib.beid.mixins.BeIdCardHolder.get_beid_diffs`. """ Address = rt.modules.addresses.Address DataSources = rt.modules.addresses.DataSources diffs = [] objects = [self] kw = dict(partner=self, data_source=DataSources.eid) try: addr = Address.objects.get(**kw) except Address.DoesNotExist: if Address.objects.filter(partner=self, primary=True).count() == 0: kw.update(primary=True) addr = Address(**kw) objects.append(addr) for fldname, new in attrs.items(): if fldname in Address.ADDRESS_FIELDS: obj = addr else: obj = self fld = get_field(obj, fldname) old = getattr(obj, fldname) if old != new: diffs.append( "%s : %s -> %s" % ( unicode(fld.verbose_name), dd.obj2str(old), dd.obj2str(new))) setattr(obj, fld.name, new) return objects, diffs
def add_gridfilters(qs, gridfilters): """Converts a `filter` request in the format used by :extux:`Ext.ux.grid.GridFilters` into a `Django field lookup <http://docs.djangoproject.com/en/1.2/ref/models/querysets/#field-lookups>`_ on a :class:`django.db.models.query.QuerySet`. :param qs: the queryset to be modified. :param gridfilters: a list of dictionaries, each having 3 keys `field`, `type` and `value`. """ if not isinstance(qs, QuerySet): raise NotImplementedError('TODO: filter also simple lists') q = models.Q() # logger.info("20160610 %s", gridfilters) # raise Exception("20160610 %s" % gridfilters) for flt in gridfilters: field = get_field(qs.model, flt['field']) flttype = flt['type'] kw = {} if flttype == 'string': if isinstance(field, models.CharField): kw[field.name + "__icontains"] = flt['value'] q = q & models.Q(**kw) elif isinstance(field, models.ForeignKey): qf = field.rel.model.quick_search_filter( flt['value'], prefix=field.name + "__") # logger.info("20160610 %s %s", field.rel.model, qf) q = q & qf # rq = models.Q() # search_field = field.rel.model.grid_search_field # for search_field in field.rel.model.quick_search_fields: # search_field = getattr(field.rel.model,'grid_search_field',None) # if search_field is not None: # rq = rq | models.Q(**{field.name+"__%s__icontains" % search_field : flt['value']}) # q = q & rq else: raise NotImplementedError(repr(flt)) elif flttype == 'numeric': cmp = str(flt['comparison']) if cmp == 'eq': cmp = 'exact' kw[field.name + "__" + cmp] = flt['value'] q = q & models.Q(**kw) elif flttype == 'boolean': kw[field.name] = flt['value'] # kw[field.name + "__equals"] = flt['value'] q = q & models.Q(**kw) elif flttype == 'date': v = datetime.date(*settings.SITE.parse_date(flt['value'])) # v = parse_js_date(flt['value'],field.name) cmp = str(flt['comparison']) if cmp == 'eq': cmp = 'exact' kw[field.name + "__" + cmp] = v q = q & models.Q(**kw) # print kw else: raise NotImplementedError(repr(flt)) return qs.filter(q)
def get_beid_diffs(obj, attrs): """Return two lists, one with the objects to save, and another with text lines to build a confirmation message explaining which changes are going to be applied after confirmation. The default implemantion is for the simple case where the holder is also a contacts.AddressLocation and the address is within the same database row. """ raise Exception("not tested") diffs = [] objects = [] # model = holder_model() model = obj.__class__ # the holder for fldname, new in attrs.items(): fld = get_field(model, fldname) old = getattr(obj, fldname) if old != new: diffs.append( "%s : %s -> %s" % ( unicode(fld.verbose_name), dd.obj2str(old), dd.obj2str(new))) setattr(obj, fld.name, new) return objects, diffs
def add_gridfilters(qs, gridfilters): """ Converts a `filter` request in the format used by :extux:`Ext.ux.grid.GridFilters` into a `Django field lookup <http://docs.djangoproject.com/en/1.2/ref/models/querysets/#field-lookups>`_ on a :class:`django.db.models.query.QuerySet`. :param qs: the queryset to be modified. :param gridfilters: a list of dictionaries, each having 3 keys `field`, `type` and `value`. """ if not isinstance(qs, QuerySet): raise NotImplementedError('TODO: filter also simple lists') q = models.Q() print 20150506, gridfilters for flt in gridfilters: field = get_field(qs.model, flt['field']) flttype = flt['type'] kw = {} if flttype == 'string': if isinstance(field, models.CharField): kw[field.name + "__icontains"] = flt['value'] q = q & models.Q(**kw) elif isinstance(field, models.ForeignKey): q = q & quick_search_filter( field.rel.to, flt['value'], field.name + "__") #~ rq = models.Q() #~ search_field = field.rel.to.grid_search_field #~ for search_field in field.rel.to.quick_search_fields: #~ search_field = getattr(field.rel.to,'grid_search_field',None) #~ if search_field is not None: #~ rq = rq | models.Q(**{field.name+"__%s__icontains" % search_field : flt['value']}) #~ q = q & rq else: raise NotImplementedError(repr(flt)) elif flttype == 'numeric': cmp = str(flt['comparison']) if cmp == 'eq': cmp = 'exact' kw[field.name + "__" + cmp] = flt['value'] q = q & models.Q(**kw) elif flttype == 'boolean': kw[field.name] = flt['value'] # kw[field.name + "__equals"] = flt['value'] q = q & models.Q(**kw) elif flttype == 'date': v = datetime.date(*settings.SITE.parse_date(flt['value'])) #~ v = parse_js_date(flt['value'],field.name) cmp = str(flt['comparison']) if cmp == 'eq': cmp = 'exact' kw[field.name + "__" + cmp] = v q = q & models.Q(**kw) #~ print kw else: raise NotImplementedError(repr(flt)) return qs.filter(q)
def get_beid_diffs(self, attrs): # Return two lists, one with the objects to save, and another # with text lines to build a confirmation message explaining # which changes are going to be applied after confirmation. # The default implemantion is for the simple case where the # holder is also a contacts.AddressLocation and the address is # within the same database row. diffs = [] objects = [self] model = self.__class__ # the holder for fldname, new in attrs.items(): fld = get_field(model, fldname) old = getattr(self, fldname) if old != new: diffs.append( "{} : {} -> {}".format( str(fld.verbose_name), dd.obj2str(old), dd.obj2str(new))) setattr(self, fld.name, new) return objects, diffs
def get_beid_diffs(self, attrs): # Return two lists, one with the objects to save, and another # with text lines to build a confirmation message explaining # which changes are going to be applied after confirmation. # The default implemantion is for the simple case where the # holder is also a contacts.AddressLocation and the address is # within the same database row. diffs = [] objects = [self] model = self.__class__ # the holder for fldname, new in attrs.items(): fld = get_field(model, fldname) old = getattr(self, fldname) if old != new: diffs.append( "{} : {} -> {}".format( fld.verbose_name, dd.obj2str(old), dd.obj2str(new))) setattr(self, fld.name, new) return objects, diffs
def get_beid_diffs(obj, attrs): """Return two lists, one with the objects to save, and another with text lines to build a confirmation message explaining which changes are going to be applied after confirmation. The default implemantion is for the simple case where the holder is also a contacts.AddressLocation and the address is within the same database row. """ raise Exception("not tested") diffs = [] objects = [] # model = holder_model() model = obj.__class__ # the holder for fldname, new in attrs.items(): fld = get_field(model, fldname) old = getattr(obj, fldname) if old != new: diffs.append("%s : %s -> %s" % (unicode( fld.verbose_name), dd.obj2str(old), dd.obj2str(new))) setattr(obj, fld.name, new) return objects, diffs
def add_gridfilters(qs, gridfilters): """Converts a `filter` request in the format used by :extux:`Ext.ux.grid.GridFilters` into a `Django field lookup <https://docs.djangoproject.com/en/1.11/ref/models/querysets/#field-lookups>`_ on a :class:`django.db.models.query.QuerySet`. :param qs: the queryset to be modified. :param gridfilters: a list of dictionaries, each having 3 keys `field`, `type` and `value`. """ if not isinstance(qs, QuerySet): raise NotImplementedError('TODO: filter also simple lists') q = models.Q() # logger.info("20160610 %s", gridfilters) # raise Exception("20160610 %s" % gridfilters) for flt in gridfilters: field = get_field(qs.model, flt.get('field', None) or flt.get("property")) flttype = flt['type'] kw = {} if flttype == 'string': if isinstance(field, models.CharField): kw[field.name + "__icontains"] = flt['value'] q = q & models.Q(**kw) elif isinstance(field, models.ForeignKey): qf = field.remote_field.model.quick_search_filter( flt['value'], prefix=field.name + "__") # logger.info("20160610 %s %s", field.remote_field.model, qf) q = q & qf # rq = models.Q() # search_field = field.remote_field.model.grid_search_field # for search_field in field.remote_field.model.quick_search_fields: # search_field = getattr(field.rel.model,'grid_search_field',None) # if search_field is not None: # rq = rq | models.Q(**{field.name+"__%s__icontains" % search_field : flt['value']}) # q = q & rq else: raise NotImplementedError(repr(flt)) elif flttype == 'numeric': cmp = str(flt.get('comparison', None) or flt.get('operator', None)) if cmp == 'eq': cmp = 'exact' kw[field.name + "__" + cmp] = flt['value'] q = q & models.Q(**kw) elif flttype == 'boolean': kw[field.name] = flt['value'] # kw[field.name + "__equals"] = flt['value'] q = q & models.Q(**kw) elif flttype == 'date': v = datetime.date(*settings.SITE.parse_date(flt['value'])) # v = parse_js_date(flt['value'],field.name) cmp = str(flt.get('comparison', None) or flt.get('operator', None)) if cmp == 'eq': cmp = 'exact' kw[field.name + "__" + cmp] = v q = q & models.Q(**kw) # print kw elif flttype == 'list': if isinstance(field, ChoiceListField): choices = [] for x in field.choices: if x[1] in flt['value']: choices.append(x[0]) kw[field.name + "__in"] = choices q = q & models.Q(**kw) else: raise NotImplementedError(repr(flt)) else: raise NotImplementedError(repr(flt)) return qs.filter(q)