Exemplo n.º 1
0
    def test_get(self):
        publisher, created = Publisher.from_dict({"name": "Publishing House"})
        self.assertTrue(created)
        self.assertIsNotNone(publisher.id)

        publisher2 = Publisher.get("Publishing House")
        self.assertIsNotNone(publisher2)
        self.assertEquals(publisher, publisher2)

        publisher2 = Publisher.get("house")
        self.assertIsNotNone(publisher2)
        self.assertEquals(publisher, publisher2)

        publisher2 = Publisher.get(str(publisher.id))
        self.assertIsNotNone(publisher2)
        self.assertEquals(publisher, publisher2)
Exemplo n.º 2
0
def _publisher(args: Namespace, file: TextIO = sys.stdout):
    publisher: Optional[Publisher] = None
    if args.subparser == "add":
        publisher, created = Publisher.from_dict({
            "name":
            args.name,
            "links":
            [Link.get_or_create(link).to_dict() for link in args.link],
        })
        if created:
            stdout.write(
                _('Successfully added publisher "%(name)s" with id "%(pk)d".')
                % {
                    "name": publisher.name,
                    "pk": publisher.pk
                },
                "=",
                file=file,
            )
            publisher.print(file)
        else:
            stdout.write(
                _('The publisher "%(name)s" already exists with id "%(pk)d", '
                  + "aborting...") % {
                      "name": publisher.name,
                      "pk": publisher.pk
                  },
                "",
                file=file,
            )
    elif args.subparser == "delete":
        publisher = Publisher.get(args.publisher)
        if publisher:
            publisher.delete()
            stdout.write(
                _('Successfully deleted publisher with id "%(pk)d".') %
                {"pk": publisher.pk},
                "",
                file=file,
            )
        else:
            stdout.write(_("No publisher found."), "", file=file)
    elif args.subparser == "edit":
        publisher = Publisher.get(args.publisher)
        if publisher:
            publisher.edit(args.field, args.value)
            stdout.write(
                _('Successfully edited publisher "%(name)s" with id "%(pk)d".')
                % {
                    "name": publisher.name,
                    "pk": publisher.pk
                },
                "",
                file=file,
            )
            publisher.print(file)
        else:
            stdout.write(_("No publisher found."), "", file=file)
    elif args.subparser == "info":
        publisher = Publisher.get(args.publisher)
        if publisher:
            publisher.print(file)
        else:
            stdout.write(_("No publisher found."), "", file=file)
    elif args.subparser == "list":
        if args.search:
            publishers = Publisher.search(args.search)
        else:
            publishers = Publisher.objects.all()
        stdout.write(
            [_("Id"), _("Name"), _("Number of editions")],
            "=", [0.05, 0.8],
            file=file)
        for i, has_next in lookahead(publishers):
            stdout.write(
                [i.id, i.name, i.editions.count()],
                "_" if has_next else "=",
                [0.05, 0.8],
                file=file,
            )