コード例 #1
0
ファイル: tests.py プロジェクト: jnphilipp/bibliothek
    def test_search(self):
        publisher, created = Publisher.from_dict({"name": "Publishing House"})
        self.assertTrue(created)
        self.assertIsNotNone(publisher.id)

        publisher, created = Publisher.from_dict({"name": "Old House"})
        self.assertTrue(created)
        self.assertIsNotNone(publisher.id)

        publisher, created = Publisher.from_dict({"name": "Publishing Press"})
        self.assertTrue(created)
        self.assertIsNotNone(publisher.id)

        self.assertEquals(3, Publisher.objects.all().count())
        self.assertEquals(2, Publisher.search("pub").count())
        self.assertEquals(2, Publisher.search("House").count())
コード例 #2
0
ファイル: argparse.py プロジェクト: jnphilipp/bibliothek
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,
            )