Exemplo n.º 1
0
    def test_run(self):
        # double check that data is setup correctly before
        # running pdbwipe

        assert get_user_model().objects.all().count() == 2

        for reftag, cls in REFTAG_MAP.items():
            assert cls.objects.all().count() > 1

        assert Group.objects.all().count() > 2

        # run pdb_wipe
        settings.TUTORIAL_MODE = True
        call_command("pdb_wipe", commit=True)
        settings.TUTORIAL_MODE = False

        # all entities should be gone
        for reftag, cls in REFTAG_MAP.items():
            assert cls.objects.all().count() == 0

        # only the user and guest groups should be left
        assert Group.objects.all().count() == 2

        # only the superuser should be left
        assert get_user_model().objects.all().count() == 1
        assert get_user_model().objects.first().is_superuser == True
Exemplo n.º 2
0
    def test_run_with_sync(self):
        """
        Test running `pdb_wipe` and sync data from
        test.peeringdb.com
        """

        dates = {}

        for reftag, cls in REFTAG_MAP.items():
            assert cls.objects.all().count() > 1
            dates[reftag] = cls.objects.all().first().created.replace(
                tzinfo=UTC())

        settings.TUTORIAL_MODE = True
        call_command(
            "pdb_wipe",
            commit=True,
            load_data=True,
            load_data_url="https://test.peeringdb.com/api",
        )
        settings.TUTORIAL_MODE = False

        for reftag, cls in REFTAG_MAP.items():
            created = cls.objects.all().first().created.replace(tzinfo=UTC())
            assert created != dates[reftag]
            assert cls.objects.all().count() > 1
Exemplo n.º 3
0
    def test_run(self):
        # double check that data is setup correctly before
        # running pdbwipe

        assert get_user_model().objects.all().count() == 2

        for reftag, cls in REFTAG_MAP.items():
            assert cls.objects.all().count() > 1

        assert Group.objects.all().count() > 2

        # run pdb_wipe
        settings.TUTORIAL_MODE = True
        call_command("pdb_wipe", commit=True)
        settings.TUTORIAL_MODE = False

        # all entities should be gone
        for reftag, cls in REFTAG_MAP.items():
            assert cls.objects.all().count() == 0

        # only the user and guest groups should be left
        assert Group.objects.all().count() == 2

        # only the superuser should be left
        assert get_user_model().objects.all().count() == 1
        assert get_user_model().objects.first().is_superuser == True
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
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
Exemplo n.º 6
0
    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
Exemplo n.º 7
0
 def test_run(self):
     call_command("pdb_generate_test_data", limit=2, commit=True)
     for reftag, cls in list(REFTAG_MAP.items()):
         self.assertGreater(cls.objects.count(), 0)
         for instance in cls.objects.all():
             instance.full_clean()
Exemplo n.º 8
0
    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)
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
 def test_run(self):
     call_command("pdb_generate_test_data", limit=2, commit=True)
     for reftag, cls in REFTAG_MAP.items():
         self.assertGreater(cls.objects.count(), 0)
         for instance in cls.objects.all():
             instance.full_clean()