Example #1
0
    def handle(self, *args, **kwargs):
        token = kwargs.get('token')
        if not token:
            raise CommandError("No dropbox token specified.")
        try:
            downloader = DropboxDownloader(token)
        except ErrorResponse as e:
            msg = "Incorrect dropbox token: {}"
            raise CommandError(msg.format(str(e)))

        books = downloader.get_books(book_count=kwargs['count'])
        book_dicts = [book.to_dict()
                      for book in tqdm(books, total=kwargs['count'])]

        try:
            with transaction.atomic():
                BookNote.objects.all().delete()
                Book.objects.all().delete()

                for book_dict in book_dicts:
                    logger.info("Saving book {}.".format(book_dict))
                    Book.from_dict(book_dict)
        except IntegrityError:
            logger.exception("Transaction error.")
Example #2
0
    def setUp(self):
        self.paper, created = Paper.from_dict({"title": "Really cool stuff"})
        self.assertTrue(created)
        self.assertIsNotNone(self.paper.id)

        magazine, created = Magazine.from_dict({"name": "Example"})
        self.assertTrue(created)
        self.assertIsNotNone(magazine.pk)

        self.issue, created = Issue.from_dict({"issue": "1/2021"}, magazine)
        self.assertTrue(created)
        self.assertIsNotNone(self.issue.pk)

        book, created = Book.from_dict({"title": "Example"})
        self.assertTrue(created)
        self.assertIsNotNone(book.pk)

        self.edition, created = Edition.from_dict(
            {"publishing_date": "2021-02-01"}, book)
        self.assertTrue(created)
        self.assertIsNotNone(self.edition.pk)
Example #3
0
def _book(args: Namespace, file: TextIO = sys.stdout):
    book: Optional[Book] = None
    if args.subparser == "add":
        book, created = Book.from_dict({
            "title":
            args.title,
            "authors":
            [Person.get_or_create(author).to_dict() for author in args.author],
            "series":
            Series.get_or_create(args.series).to_dict()
            if args.series else None,
            "volume":
            args.volume,
            "genres":
            [Genre.get_or_create(genre).to_dict() for genre in args.genre],
            "links":
            [Link.get_or_create(link).to_dict() for link in args.link],
        })

        if created:
            stdout.write(
                _('Successfully added book "%(title)s" with id "%(pk)d".') % {
                    "title": book.title,
                    "pk": book.pk
                },
                "=",
                file=file,
            )
            book.print(file)
        else:
            stdout.write(
                _('The book "%(title)s" already exists with id "%(pk)d", aborting...'
                  ) % {
                      "title": book.title,
                      "pk": book.pk
                  },
                "",
                file=file,
            )
    elif args.subparser == "delete":
        book = Book.get(args.book)
        if book:
            book.delete()
            stdout.write(
                _('Successfully deleted book "%(title)s" with id "%(pk)d".') %
                {
                    "title": book.title,
                    "pk": book.pk
                },
                "",
                file=file,
            )
        else:
            stdout.write(_("No book found."), "", file=file)
    elif args.subparser == "edit":
        book = Book.get(args.book)
        if book:
            book.edit(args.edit_subparser, args.value)
            stdout.write(
                _('Successfully edited book "%(title)s" with id "%(pk)d".') % {
                    "title": book.title,
                    "pk": book.pk
                },
                "=",
                file=file,
            )
            book.print(file)
        else:
            stdout.write(_("No book found."), "", file=file)
    elif args.subparser == "edition":
        book = Book.get(args.book)
        if book:
            if args.edition_subparser == "acquisition" and book:
                edition = Edition.get(args.edition, book)
                acquisition: Optional[Acquisition] = None
                if args.acquisition_subparser == "add" and edition:
                    acquisition, created = Acquisition.from_dict(
                        {
                            "date": args.date,
                            "price": args.price
                        }, edition)
                    if created:
                        stdout.write(
                            _('Successfully added acquisition with id "%(pk)d".'
                              ) % {"pk": acquisition.pk},
                            "=",
                            file=file,
                        )
                    else:
                        stdout.write(
                            _('The acquisition already exists with id "%(pk)d".'
                              ) % {"pk": acquisition.pk},
                            "",
                            file=file,
                        )
                    acquisition.print(file)
                elif args.acquisition_subparser == "delete" and edition:
                    acquisition = Acquisition.get(args.acquisition,
                                                  editions=edition)
                    if acquisition:
                        acquisition.delete(acquisition)
                        stdout.write(
                            _('Successfully deleted acquisition with id "%(pk)d".'
                              ) % {"pk": acquisition.pk},
                            "",
                            file=file,
                        )
                    else:
                        stdout.write(_("No acquisition found."), "", file=file)
                elif args.acquisition_subparser == "edit" and edition:
                    acquisition = Acquisition.get(args.acquisition,
                                                  editions=edition)
                    if acquisition:
                        acquisition.edit(args.field, args.value)
                        stdout.write(
                            _('Successfully edited acquisition with id "%(pk)d".'
                              ) % {"pk": acquisition.pk},
                            "=",
                            file=file,
                        )
                        acquisition.print(file)
                    else:
                        stdout.write(_("No acquisition found."), "", file=file)
                else:
                    stdout.write([_("No edition found.")], "", file=file)
            elif args.edition_subparser == "add" and book:
                edition, created = Edition.from_dict(
                    {
                        "alternate_title":
                        args.alternate_title,
                        "isbn":
                        args.isbn,
                        "publishing_date":
                        args.publishing_date,
                        "cover":
                        args.cover,
                        "binding":
                        Binding.get_or_create(args.binding).to_dict()
                        if args.binding else None,
                        "publisher":
                        Publisher.get_or_create(args.publisher).to_dict()
                        if args.publisher else None,
                        "persons": [
                            Person.get_or_create(person).to_dict()
                            for person in args.person
                        ],
                        "languages": [
                            Language.get_or_create(language).to_dict()
                            for language in args.language
                        ],
                        "links": [
                            Link.get_or_create(link).to_dict()
                            for link in args.link
                        ],
                        "files": [{
                            "path": file
                        } for file in args.file],
                    },
                    book,
                )
                if created:
                    stdout.write(
                        _('Successfully added edition "%(edition)s" with id "%(pk)d".'
                          ) % {
                              "edition": edition,
                              "pk": edition.pk
                          },
                        "=",
                        file=file,
                    )
                    edition.print(file)
                else:
                    stdout.write(
                        _('The edition "%(edition)s" already exists with id "%(pk)d",'
                          + " aborting...") % {
                              "edition": edition,
                              "pk": edition.pk
                          },
                        "",
                        file=file,
                    )
            elif args.edition_subparser == "edit" and book:
                edition = Edition.get(args.edition, book)
                if edition:
                    edition.edit(args.edit_subparser, args.value)
                    stdout.write(
                        _('Successfully edited edition "%(edition)s" with id "%(pk)d".'
                          ) % {
                              "edition": edition,
                              "pk": edition.pk
                          },
                        "=",
                        file=file,
                    )
                    edition.print(file)
                else:
                    stdout.write(_("No edition found."), "", file=file)
            elif args.edition_subparser == "info" and book:
                edition = Edition.get(args.edition, book)
                if edition:
                    edition.print(file)
                else:
                    stdout.write(_("No edition found."), "", file=file)
            elif args.edition_subparser == "list" and book:
                if args.shelf:
                    editions = Edition.list.by_shelf(args.shelf, book)
                elif args.search:
                    editions = Edition.list.by_term(args.search, book)
                else:
                    editions = Edition.objects.filter(book=book)
                stdout.write(
                    [
                        _("Id"),
                        _("Title"),
                        _("Binding"),
                        _("ISBN"),
                        _("Publishing date"),
                    ],
                    "=",
                    [0.05, 0.55, 0.7, 0.85],
                    file=file,
                )
                for i, has_next in lookahead(editions):
                    stdout.write(
                        [
                            i.pk,
                            i.get_title(), i.binding, i.isbn, i.publishing_date
                        ],
                        "_" if has_next else "=",
                        [0.05, 0.55, 0.7, 0.85],
                        file=file,
                    )
            elif args.edition_subparser == "open" and book:
                edition = Edition.get(args.edition, book)
                if edition:
                    edition_file = edition.files.get(pk=args.file)
                    path = settings.MEDIA_ROOT / edition_file.file.path
                    if sys.platform == "linux":
                        os.system(f'xdg-open "{path}"')
                    else:
                        os.system(f'open "{path}"')
                else:
                    stdout.write(_("No edition found."), "", file=file)
            elif args.edition_subparser == "read" and book:
                edition = Edition.get(args.edition, book)
                read: Optional[Read] = None
                if args.read_subparser == "add" and edition:
                    read, created = Read.from_dict(
                        {
                            "started": args.started,
                            "finished": args.finished
                        }, edition)
                    if created:
                        stdout.write(
                            _('Successfully added read with id "%(pk)d".') %
                            {"pk": read.pk},
                            "=",
                            file=file,
                        )
                    else:
                        stdout.write(
                            _('The read already exists with id "%(pk)d".') %
                            {"pk": read.pk},
                            "",
                            file=file,
                        )
                    read.print(file)
                elif args.read_subparser == "delete" and edition:
                    read = Read.get(args.read, editions=edition)
                    if read:
                        read.delete()
                        stdout.write(
                            _('Successfully deleted read with id "%(pk)d".') %
                            {"pk": read.pk},
                            "",
                            file=file,
                        )
                    else:
                        stdout.write(_("No read found."), "", file=file)
                elif args.read_subparser == "edit" and edition:
                    read = Read.get(args.read, editions=edition)
                    if read:
                        read.edit(args.field, args.value)
                        stdout.write(
                            _('Successfully edited read with id "%(pk)d".') %
                            {"pk": read.pk},
                            "=",
                            file=file,
                        )
                        read.info(file)
                    else:
                        stdout.write(_("No read found."), "", file=file)
                else:
                    stdout.write(_("No edition found."), "", file=file)
        else:
            stdout.write(_("No book found."), "", file=file)
    elif args.subparser == "info":
        book = Book.get(args.book)
        if book:
            book.print(file)
        else:
            stdout.write(_("No book found."), "", file=file)
    elif args.subparser == "list":
        if args.search:
            books = Book.search(args.search)
        elif args.shelf:
            books = Book.by_shelf(args.shelf)
        else:
            books = Book.objects.all()
        stdout.write(
            [_("Id"), ("Title"),
             _("Authors"),
             _("Series"),
             _("Volume")],
            "=",
            [0.05, 0.5, 0.75, 0.9],
            file=file,
        )
        for i, has_next in lookahead(books):
            stdout.write(
                [
                    i.pk,
                    i.title,
                    " ,".join(f"{a}" for a in i.authors.all()),
                    i.series.name if i.series else "",
                    i.volume,
                ],
                "_" if has_next else "=",
                [0.05, 0.5, 0.75, 0.9],
                file=file,
            )