def undelete(self, reftag, _id, parent=None, date=None): cls = REFTAG_MAP.get(reftag) obj = cls.objects.get(id=_id) if date: version = reversion.models.Version.objects.get_for_object( obj).filter(revision__date_created__lt=date).order_by( "revision__date_created").last() try: status = json.loads( version.serialized_data)[0].get("fields")["status"] except: status = None if status == "deleted": self.log( "{} was already deleted at snapshot, skipping ..".format( obj)) return can_undelete_obj = True for field in cls._meta.get_fields(): if field.is_relation: if field.many_to_one: # relation parent try: relation = getattr(obj, field.name) except: continue if relation.status == "deleted" and relation != parent: can_undelete_obj = False self.log( "Cannot undelete {}, dependent relation marked as deleted: {}" .format(obj, relation)) if not can_undelete_obj: return if obj.status == "deleted": obj.status = "ok" self.log("Undeleting {}".format(obj)) if self.commit: obj.save() for field in cls._meta.get_fields(): if field.is_relation: if not field.many_to_one: # relation child try: relation = getattr(obj, field.name) except: continue if not hasattr(field.related_model, "ref_tag"): continue for child in relation.filter(updated__gte=self.date): self.undelete(child.ref_tag, child.id, obj, date=self.date)
def fk_handleref_filter(form, field, tag=None): """ This filters foreign key dropdowns that hold handleref objects to only contain undeleted objects and the object the instance is currently set to """ if tag is None: tag = field if tag in REFTAG_MAP and form.instance: model = REFTAG_MAP.get(tag) qset = model.handleref.filter( Q(status="ok") | Q(id=getattr(form.instance, "%s_id" % field))) try: qset = qset.order_by("name") except Exception: pass form.fields[field].queryset = qset
def create(self, reftag, **kwargs): """ Create a new instance of model specified in `reftag` Any arguments passed as kwargs will override mock field values. Note: Unless there are no relationships passed in kwargs, required parent objects will be automatically created as well. Returns: The created instance. """ model = REFTAG_MAP.get(reftag) data = {} data.update(**kwargs) # first we create any required parent relation ships for field in model._meta.get_fields(): if field.name in data: continue if field.is_relation and field.many_to_one: if hasattr(field.related_model, "ref_tag"): data[field.name] = self.create( field.related_model.handleref.tag) # then we set the other fields to mock values provided by this class for field in model._meta.get_fields(): # field value specified alrady, skip if field.name in data: continue # these we don't care about if field.name in ["id", "logo", "version", "created", "updated"]: continue # if reftag == "ixlan" and field.name != "id": # continue # elif reftag != "ixlan": # continue # this we don't care about either if field.name.find("geocode") == 0: continue # choice fields should automatically select a value from # the available choices # # PDB choices often have Not Disclosed at index 0 and 1 # so we try index 2 first. if (not field.is_relation and field.choices and not hasattr(self, field.name)): try: data[field.name] = field.choices[2][0] except IndexError: data[field.name] = field.choices[0][0] # bool fields set to True elif isinstance(field, models.BooleanField): data[field.name] = True # every other field elif not field.is_relation: # emails if field.name.find("email") > -1: data[field.name] = "*****@*****.**" # phone numbers elif field.name.find("phone") > -1: data[field.name] = "+12065550199" # URLs elif field.name.find("url") > -1: data[field.name] = "{}/{}".format( self.website(data, reftag=reftag), field.name) # everything else is routed to the apropriate method # with the same name as the field name else: data[field.name] = getattr(self, field.name)(data, reftag=reftag) obj = model(**data) obj.clean() obj.save() return obj
def undelete(self, reftag, _id, parent=None, date=None): cls = REFTAG_MAP.get(reftag) obj = cls.objects.get(id=_id) self.suppress_warning = False def _label(obj): if hasattr(obj, "descriptive_name"): return obj.descriptive_name return obj if date: version = ( reversion.models.Version.objects.get_for_object(obj) .filter(revision__date_created__lt=date) .order_by("revision__date_created") .last() ) try: status = json.loads(version.serialized_data)[0].get("fields")["status"] except Exception: status = None if status == "deleted": self.log_warn( "{} was already deleted at snapshot, skipping ..".format( _label(obj) ) ) return can_undelete_obj = True for field in cls._meta.get_fields(): if field.is_relation: if field.many_to_one: # relation parent try: relation = getattr(obj, field.name) except Exception: continue if relation.status == "deleted" and relation != parent: can_undelete_obj = False self.log_warn( "Cannot undelete {}, dependent relation marked as deleted: {}".format( _label(obj), relation ) ) if not can_undelete_obj: return if obj.status == "deleted": obj.status = "ok" self.log(f"Undeleting {_label(obj)}") handler = getattr(self, f"handle_{reftag}", None) if handler: handler(obj) try: obj.clean() if self.commit: obj.save() except Exception as exc: if not self.suppress_warning: self.log_warn(f"Cannot undelete {_label(obj)}: {exc}") for field in cls._meta.get_fields(): if field.is_relation: if not field.many_to_one: # relation child try: relation = getattr(obj, field.name) except Exception: continue if not hasattr(field.related_model, "ref_tag"): continue for child in relation.filter(updated__gte=self.date): self.undelete(child.ref_tag, child.id, obj, date=self.date)
def undelete(self, reftag, _id, parent=None, date=None): cls = REFTAG_MAP.get(reftag) obj = cls.objects.get(id=_id) self.suppress_warning = False def _label(obj): if hasattr(obj, "descriptive_name"): return obj.descriptive_name return obj if date: version = reversion.models.Version.objects.get_for_object( obj).filter(revision__date_created__lt=date).order_by( "revision__date_created").last() try: status = json.loads( version.serialized_data)[0].get("fields")["status"] except: status = None if status == "deleted": self.log_warn( "{} was already deleted at snapshot, skipping ..".format( _label(obj))) return can_undelete_obj = True for field in cls._meta.get_fields(): if field.is_relation: if field.many_to_one: # relation parent try: relation = getattr(obj, field.name) except: continue if relation.status == "deleted" and relation != parent: can_undelete_obj = False self.log_warn( "Cannot undelete {}, dependent relation marked as deleted: {}" .format(_label(obj), relation)) if not can_undelete_obj: return if obj.status == "deleted": obj.status = "ok" self.log("Undeleting {}".format(_label(obj))) handler = getattr(self, "handle_{}".format(reftag), None) if handler: handler(obj) try: obj.clean() if self.commit: obj.save() except Exception as exc: if not self.suppress_warning: self.log_warn("Cannot undelete {}: {}".format( _label(obj), exc)) for field in cls._meta.get_fields(): if field.is_relation: if not field.many_to_one: # relation child try: relation = getattr(obj, field.name) except: continue if not hasattr(field.related_model, "ref_tag"): continue for child in relation.filter(updated__gte=self.date): self.undelete(child.ref_tag, child.id, obj, date=self.date)