예제 #1
0
    def test_get(self):
        journal, created = Journal.from_dict({"name": "Science Journal"})
        self.assertTrue(created)
        self.assertIsNotNone(journal.id)

        journal2 = Journal.get("Science Journal")
        self.assertIsNotNone(journal2)
        self.assertEquals(journal, journal2)

        journal2 = Journal.get("science")
        self.assertIsNotNone(journal2)
        self.assertEquals(journal, journal2)

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