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 on_update(sender=None, watcher=None, request=None, **kw): """ Log a Change if there is a `change_watcher_spec`. """ master = get_master(watcher.watched) if master is None: # No master, nothing to log return cs = watcher.watched.change_watcher_spec if False: # html version changes = list(watcher.get_updates_html(cs.ignored_fields)) if len(changes) == 0: msg = '(no changes)' elif len(changes) == 1: msg = E.tostring(changes[0]) else: msg = E.tostring(E.ul(*changes)) else: changes = [] for k, old, new in watcher.get_updates(cs.ignored_fields): changes.append("%s : %s --> %s" % (k, dd.obj2str(old), dd.obj2str(new))) if len(changes) == 0: msg = '(no changes)' elif len(changes) == 1: msg = changes[0] else: msg = '- ' + ('\n- '.join(changes)) log_change(ChangeTypes.update, request, master, watcher.watched, msg)
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 on_auto_create(sender, **kw): #~ raise Warning("auto_create is not permitted here") logger.info("auto_create %s %s", dd.obj2str(sender), kw) from django.core.mail import mail_admins body = 'Record %s has been automatically created using %s' % ( dd.obj2str(sender), kw) mail_admins('auto_create', body, fail_silently=True)
def run(cls): """To be used when running this loader from a run script. Usage example:: from lino_xl.lib.tim2lino.spzloader2 import TimLoader TimLoader.run() """ self = cls(settings.SITE.legacy_data_path) counts = {} for o in self.expand(self.objects()): c = counts.setdefault(o.__class__, [0, 0]) try: o.full_clean() o.save() c[0] += 1 except Exception as e: c[1] += 1 dd.logger.warning( "Failed to save %s : %s", dd.obj2str(o), e) # temporary: # dd.logger.info("Saved %s", dd.obj2str(o)) self.finalize() if counts: for m in sorted(counts.keys()): c = counts[m] dd.logger.info( "%s : %d success, %d failed.", m, c[0], c[1]) else: dd.logger.info("No objects have been imported.")
def run(cls): """To be used when running this loader from a run script. Usage example:: from lino_xl.lib.tim2lino.spzloader2 import TimLoader TimLoader.run() """ self = cls(settings.SITE.legacy_data_path) counts = {} for o in self.expand(self.objects()): c = counts.setdefault(o.__class__, [0, 0]) try: o.full_clean() o.save() c[0] += 1 except Exception as e: c[1] += 1 dd.logger.warning("Failed to save %s : %s", dd.obj2str(o), e) # temporary: # dd.logger.info("Saved %s", dd.obj2str(o)) self.finalize() if counts: for m in sorted(counts.keys()): c = counts[m] dd.logger.info("%s : %d success, %d failed.", m, c[0], c[1]) else: dd.logger.info("No objects have been imported.")
def unused_save(self, *args, **kw): # see blog/2012/0929 if not isinstance(self.owner, Postable): # raise Exception("Controller of a Posting must be a Postable.") raise ValidationError("Controller %s (%r,%r) is not a Postable" % ( dd.obj2str(self.owner), self.owner_type, self.owner_id)) #~ raise ValidationError("Controller %s is not a Postable" % dd.obj2str(self.owner)) super(Posting, self).save(*args, **kw)
def checkcc(person, pk, nType): # ~ pk = ADR_id(cIdAdr) try: Company.objects.get(pk=pk) # ~ except ValueError,e: # ~ dblogger.warning(u"%s : invalid health_insurance or pharmacy %r",dd.obj2str(person),cIdAdr) # ~ return except Company.DoesNotExist, e: raise Exception("%s : Pharmacy or Health Insurance %s doesn't exist" % (dd.obj2str(person), pk))
def on_create(sender=None, request=None, **kw): """To be called when a new instance has actually been created and saved. """ master = get_master(sender) if master is None: return log_change(ChangeTypes.create, request, master, sender, dd.obj2str(sender, True))
def old_validate_and_save(self, obj): "Deserves more documentation." try: obj.full_clean() # ~ 20120921 dblogger.log_changes(REQUEST,obj) obj.save() except ValidationError, e: # here we only log an dd.obj2str() of the object # full traceback will be logged in watch() after process_line() dblogger.warning("Validation failed for %s : %s", dd.obj2str(obj), e) raise # re-raise (propagate) exception with original traceback
def on_update(sender=None, watcher=None, request=None, **kw): """ Log a Change if there is a `change_watcher_spec`. """ master = get_master(watcher.watched) if master is None: # No master, nothing to log return cs = watcher.watched.change_watcher_spec changes = [] for k, old, new in watcher.get_updates(cs.ignored_fields): changes.append("%s : %s --> %s" % (k, dd.obj2str(old), dd.obj2str(new))) if len(changes) == 0: msg = '(no changes)' elif len(changes) == 1: msg = changes[0] else: msg = '- ' + ('\n- '.join(changes)) log_change(ChangeTypes.update, request, master, watcher.watched, msg)
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 on_delete(sender=None, request=None, **kw): """Calls :func:`log_change` with `ChangeTypes.delete`. Note that you must call this before actually deleting the object, otherwise mysql (not sqlite) says ERROR: (1048, "Column 'object_id' cannot be null") """ master = get_master(sender) if master is None: return log_change(ChangeTypes.delete, request, master, sender, dd.obj2str(sender, True))
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(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 PUT_special(self, watcher, **kw): obj = watcher.watched model = PAR_model(kw["data"]) if obj.__class__ != model: dblogger.info( "%s:%s (%s) : %s becomes %s", kw["alias"], kw["id"], dd.obj2str(obj), obj.__class__.__name__, model.__name__, ) self.swapclass(watcher, model, kw["data"]) return True
def on_update(sender=None, watcher=None, request=None, **kw): """ Log a Change if there is a `change_watcher_spec`. `watcher` is a :class:`lino.core.diff.ChangeWatcher` instance. """ master = get_master(watcher.watched) if master is None: # No master, nothing to log return cs = watcher.watched.change_watcher_spec changed_fields = '' if False: # I tried a html version, but it doesn't help to make # things more end-user friendly. And it caused # problems when being rendered in a grid. changes = list(watcher.get_updates_html(cs.ignored_fields)) if len(changes) == 0: msg = '(no changes)' elif len(changes) == 1: msg = tostring(changes[0]) else: msg = tostring(E.ul(*changes)) else: changes = [] for k, old, new in watcher.get_updates(cs.ignored_fields): changed_fields += k + " " changes.append("%s : %s --> %s" % (k, dd.obj2str(old), dd.obj2str(new))) if len(changes) == 0: msg = '(no changes)' elif len(changes) == 1: msg = changes[0] else: msg = '- ' + ('\n- '.join(changes)) log_change( ChangeTypes.update, request, master, watcher.watched, msg, changed_fields)
def POST(self, **kw): # ~ dblogger.info("%s.POST(%s)",self.__class__.__name__,kw) # ~ self.prepare_data(kw['data']) obj = self.get_object(kw) if obj is None: obj = self.create_object(kw) if obj is None: dblogger.warning("%s:%s (%s) : ignored POST %s", kw["alias"], kw["id"], obj, kw["data"]) return # ~ watcher = changes.Watcher(obj,True) self.set_timestamp(kw["time"]) self.applydata(obj, kw["data"]) dblogger.info("%s:%s (%s) : POST %s", kw["alias"], kw["id"], dd.obj2str(obj), kw["data"]) self.validate_and_save(obj) dd.on_ui_created.send(sender=obj, request=REQUEST) # ~ changes.log_create(REQUEST,obj) else: watcher = ChangeWatcher(obj) dblogger.info("%s:%s : POST becomes PUT", kw["alias"], kw["id"]) self.set_timestamp(kw["time"]) self.applydata(obj, kw["data"]) dblogger.info("%s:%s (%s) : POST %s", kw["alias"], kw["id"], dd.obj2str(obj), kw["data"]) self.validate_and_save(obj) watcher.send_update(REQUEST)
def DELETE(self, **kw): obj = self.get_object(kw) if obj is None: dblogger.warning("%s:%s : DELETE failed (does not exist)", kw["alias"], kw["id"]) return msg = obj.disable_delete(REQUEST) if msg: dblogger.warning("%s:%s : DELETE failed: %s", kw["alias"], kw["id"], msg) return # ~ 20120921 dblogger.log_deleted(REQUEST,obj) dd.pre_ui_delete.send(sender=obj, request=REQUEST) # ~ changes.log_delete(REQUEST,obj) obj.delete() dblogger.info("%s:%s (%s) : DELETE ok", kw["alias"], kw["id"], dd.obj2str(obj))
def process_line(self, kw): del kw['a'] bd = kw['birth_date'] a = bd.split('/') if len(a) != 3: dd.logger.info("Ignored invalid birth_date %s.", ) return a = map(int, a) d = datetime.date(a[2], a[1], a[0]) kw['birth_date'] = d aaaamm = kw.pop('aaaamm') kw['created'] = datetime.date(int(aaaamm[:4]), int(aaaamm[-2:]), 1) kw['first_name'] = camelize(kw['first_name']) kw['last_name'] = camelize(kw['last_name']) Country = rt.modules.countries.Country Client = rt.modules.pcsw.Client nation = kw.pop('nation').strip() if nation: try: country = Country.objects.get(inscode=nation) kw.update(nationality=country) except Country.DoesNotExist: dd.logger.warning("No Country with INS %s", nation) # except Country.MultipleObjectsReturned: # dd.logger.warning("No Country with INS %s", nation) obj = Client(**kw) if not obj.gesdos_id: raise Exception("20140312") try: current = Client.objects.get(gesdos_id=obj.gesdos_id) # dd.logger.info("Existing %s", obj) for k in 'birth_date first_name last_name nationality'.split(): setattr(current, k, getattr(obj, k)) obj = current except Client.DoesNotExist: # dd.logger.info("New client %s", obj) pass obj.full_clean() obj.save() dd.logger.info("%s has been imported.", dd.obj2str(obj))
def PUT(self, **kw): # ~ dblogger.info("%s.PUT(%s)",self.__class__.__name__,kw) obj = self.get_object(kw) if obj is None: if self.allow_put2post: dblogger.info("%s:%s : PUT becomes POST", kw["alias"], kw["id"]) kw["method"] = "POST" return self.POST(**kw) else: dblogger.warning("%s:%s : PUT ignored (row does not exist)", kw["alias"], kw["id"]) return watcher = ChangeWatcher(obj) if self.PUT_special(watcher, **kw): return self.set_timestamp(kw["time"]) self.applydata(obj, kw["data"]) dblogger.info("%s:%s (%s) : PUT %s", kw["alias"], kw["id"], dd.obj2str(obj), kw["data"]) self.validate_and_save(obj) watcher.send_update(REQUEST)
def place2objects(country, place, parent=None): t = cd2type(place) if t is None: logger.info("20140612 ignoring place %s", place) return obj = countries.Place( country=country, type=t, name=place.name, parent=parent, zip_code=place.zip_code) # We must save the parent before we can generate children. try: obj.full_clean() except Exception as e: raise Exception("Could not save %s : %r" % ( dd.obj2str(obj), e)) obj.save() yield obj for cp in place.children: yield place2objects(country, cp, obj)
def __str__(self): if not self.ref: return dd.obj2str(self) # "{0} {1} (#{0})".format(self.pk, self.year) return self.ref
def __repr__(self): return "{0} {1} {2}".format(dd.obj2str(self.partner), self.match, self.balance)
qs = pcsw.ClientContact.objects.filter(client=person, type__id=nType) if qs.count() == 0: cc = pcsw.ClientContact(client=person, company_id=pk, type=pcsw.ClientContactType.objects.get(id=nType)) cc.save() dd.on_ui_created.send(sender=cc, request=REQUEST) # ~ changes.log_create(REQUEST,cc) elif qs.count() == 1: cc = qs[0] if cc.company_id != pk: watcher = ChangeWatcher(cc) cc.company_id = pk cc.save() watcher.send_update(REQUEST) # ~ watcher.log_diff(REQUEST) else: dblogger.warning(u"%s : more than 1 ClientContact (type=%r)", dd.obj2str(person), nType) def pxs2client(row, person): kw = {} store( kw, card_number=row["CARDNUMBER"], card_issuer=row.get("CARDISSUER", ""), # 20110110 noble_condition=row.get("NOBLEECOND", ""), # 20110110 birth_place=row.get("BIRTHPLACE", ""), remarks2=row.get("MEMO", ""), gender=convert_sex(row["SEXE"]), ) for k, v in kw.items():
def __repr__(self): return "{0} {1} {2}".format( dd.obj2str(self.partner), self.match, self.balance)