def print(self: T, file: TextIO = sys.stdout): """Print.""" stdout.write([_("Field"), _("Value")], "=", [0.33], file=file) stdout.write([_("Id"), self.id], positions=[0.33], file=file) stdout.write([_("Name"), self.name], positions=[0.33], file=file) if self.links.count() > 0: for (i, link), has_next in lookahead(enumerate(self.links.all())): stdout.write( [_("Links") if i == 0 else "", f"{link.id}: {link.link}"], "" if has_next else "_", [0.33], file=file, ) else: stdout.write(f"{_('Links')}", file=file) if self.papers.count() > 0: for (i, paper), has_next in lookahead(enumerate(self.papers.all())): stdout.write( [_("Papers") if i == 0 else "", f"{paper.id}: {paper}"], "" if has_next else "_", [0.33], file=file, ) else: stdout.write(f"{_('Papers')}", file=file)
def print(self: T, file: TextIO = sys.stdout): """Print instance info.""" stdout.write([_("Field"), _("Value")], "=", [0.33], file=file) stdout.write([_("Id"), self.id], positions=[0.33], file=file) stdout.write([_("Name"), self.name], positions=[0.33], file=file) if self.links.count() > 0: for (i, link), has_next in lookahead(enumerate(self.links.all())): stdout.write( [_("Links") if i == 0 else "", f"{link.id}: {link.link}"], "" if has_next else "_", [0.33], file=file, ) else: stdout.write(f"{_('Links')}", file=file) if self.books.count() > 0: for (i, book), has_next in lookahead( enumerate(self.books.all().order_by("volume"))): stdout.write( [_("Books") if i == 0 else "", f"{book.id}: {book}"], "" if has_next else "_", [0.33], file=file, ) else: stdout.write(f"{_('Books')}", file=file)
def print(self: T, file: TextIO = sys.stdout): """Print info.""" stdout.write([_("Field"), _("Value")], "=", [0.33], file=file) stdout.write([_("Id"), self.id], positions=[0.33], file=file) stdout.write([_("Name"), self.name], positions=[0.33], file=file) if self.links.count() > 0: for (i, link), has_next in lookahead(enumerate(self.links.all())): stdout.write( [_("Links") if i == 0 else "", f"{link.id}: {link.link}"], "" if has_next else "_", [0.33], file=file, ) else: stdout.write(f"{_('Links')}", file=file) if self.editions.count() > 0: editions = self.editions.all().order_by("publishing_date") for (i, edition), has_next in lookahead(enumerate(editions)): stdout.write( [ _("Editions") if i == 0 else "", f"{edition.id}: {edition}" ], "" if has_next else "_", [0.33], file=file, ) else: stdout.write(f"{_('Editions')}", file=file)
def print(self: T, file: TextIO = sys.stdout): """Print instance info.""" stdout.write([_("Field"), _("Value")], "=", [0.33], file=file) stdout.write([_("Id"), self.pk], positions=[0.33], file=file) stdout.write([_("Name"), self.name], positions=[0.33], file=file) stdout.write( [_("Feed"), f"{self.feed.id}: {self.feed.link}" if self.feed else ""], positions=[0.33], file=file, ) if self.links.count() > 0: for (i, link), has_next in lookahead(enumerate(self.links.all())): stdout.write( ["" if i else _("Links"), f"{link.id}: {link.link}"], "" if has_next else "_", positions=[0.33], file=file, ) else: stdout.write([_("Links"), ""], positions=[0.33], file=file) if self.issues.count() > 0: issues = self.issues.all().order_by("publishing_date") for (i, issue), has_next in lookahead(enumerate(issues)): stdout.write( ["" if i else _("Issue"), f"{issue.id}: {issue.issue}"], "" if has_next else "_", positions=[0.33], file=file, ) else: stdout.write([_("Issue"), ""], positions=[0.33], file=file)
def _genre(args, file: TextIO = sys.stdout): genre: Optional[Genre] = None if args.subparser == "add": genre, created = Genre.from_dict({"name": args.name}) if created: stdout.write( _('Successfully added genre "%(name)s" with id "%(pk)d".') % {"name": genre.name, "pk": genre.pk}, "=", ) genre.print(file) else: stdout.write( _('The genre "%(name)s" already exists with id "%(pk)d", aborting...') % {"name": genre.name, "pk": genre.pk}, "", ) elif args.subparser == "delete": genre = Genre.get(args.genre) if genre: genre.delete() stdout.write( _('Successfully deleted genre with id "%(pk)d".') % {"pk": genre.pk}, "" ) else: stdout.write(_("No genre found."), "") elif args.subparser == "edit": genre = Genre.get(args.genre) if genre: genre.edit(args.field, args.value) stdout.write( _('Successfully edited genre "%(name)s" with id "%(pk)d".') % {"name": genre.name, "pk": genre.pk}, "", ) genre.print(file) else: stdout.write(_("No genre found."), "") elif args.subparser == "info": genre = Genre.get(args.genre) if genre: genre.print(file) else: stdout.write(_("No genre found."), "") elif args.subparser == "list": if args.search: genres = Genre.search(args.search) else: genres = Genre.objects.all() stdout.write([_("Id"), _("Name")], "=", [0.05], file=file) for i, has_next in lookahead(genres): stdout.write([i.id, i.name], "_" if has_next else "=", [0.05], file=file)
def print(self: T, file: TextIO = sys.stdout): """Print instance info.""" stdout.write([_("Field"), _("Value")], "=", [0.33], file=file) stdout.write([_("Id"), self.id], positions=[0.33], file=file) stdout.write([_("Name"), self.name], positions=[0.33], file=file) if self.books.count() > 0: for (i, book), has_next in lookahead(enumerate(self.books.all())): stdout.write( ["" if i else _("Books"), f"{book.id}: {book}"], "" if has_next else "_", [0.33], file=file, ) else: stdout.write([_("Books"), ""], positions=[0.33], file=file)
def _reading_list(args: Namespace, file: TextIO = sys.stdout): from books.models import Edition from magazines.models import Issue from papers.models import Paper from shelves.models import Acquisition reading_list = set() for acquisition in Acquisition.objects.all(): if acquisition.content_object.reads.count() == 0: reading_list.add((acquisition.content_object, acquisition.date)) reading_list = sorted(reading_list, key=lambda x: x[1] if x[1] else date.min) if args.limit: reading_list = reading_list[: args.limit] positions = [0.1, 0.15, 0.85, 1.0] stdout.write( [_("Type"), _("Id"), _("Title"), _("Acquisition")], "=", positions, file=file, ) for item, has_next in lookahead(reading_list): stype = "" if isinstance(item[0], Paper): stype = "Paper" elif isinstance(item[0], Edition): stype = "Book" elif isinstance(item[0], Issue): stype = "Issue" stdout.write( [stype, item[0].id, str(item[0]), item[1] if item[1] else ""], "_" if has_next else "=", positions, file=file, )
def _series(args: Namespace, file: TextIO = sys.stdout): series: Optional[Series] = None if args.subparser == "add": series, created = Series.from_dict({ "name": args.name, "links": [Link.get_or_create(link).to_dict() for link in args.link], }) if created: stdout.write( _('Successfully added series "%(name)s" with id "%(pk)d".') % { "name": series.name, "pk": series.pk }, "=", file=file, ) series.print(file) else: stdout.write( _('The series "%(name)s" already exists with id "%(pk)d", aborting...' ) % { "name": series.name, "pk": series.pk }, "", file=file, ) elif args.subparser == "delete": series = Series.get(args.series) if series: series.delete() stdout.write( _('Successfully deleted series with id "%(pk)d".') % {"pk": series.pk}, "", file=file, ) else: stdout.write(_("No series found."), "", file=file) elif args.subparser == "edit": series = Series.get(args.series) if series: series.edit(args.field, args.value) stdout.write( _('Successfully edited series "%(name)s" with id "%(pk)d".') % { "name": series.name, "pk": series.pk }, "", file=file, ) series.print(file) else: stdout.write(_("No series found."), "", file=file) elif args.subparser == "info": series = Series.get(args.series) if series: series.print(file) else: stdout.write(_("No series found."), "", file=file) elif args.subparser == "list": if args.search: series = Series.search(args.search) else: series = Series.objects.all() stdout.write( [_("Id"), _("Name"), _("Number of books")], "=", [0.05, 0.8], file=file) for i, has_next in lookahead(series): stdout.write( [i.id, i.name, i.books.count()], "_" if has_next else "=", [0.05, 0.8], file=file, )
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, )
def test_lookahead(self): self.assertEquals([(1, True), (2, True), (3, False)], list(lookahead([1, 2, 3]))) self.assertEquals([("1", True), ("2", True), ("3", False)], list(lookahead(["1", "2", "3"])))
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, )
def _person(args: Namespace, file: TextIO = sys.stdout): person: Optional[Person] = None if args.subparser == "add": person, created = Person.from_dict( { "name": args.name, "links": [Link.get_or_create(link).to_dict() for link in args.link], } ) if created: stdout.write( _('Successfully added person "%(name)s" with id "%(pk)d".') % {"name": person.name, "pk": person.pk}, "=", file=file, ) person.print(file) else: stdout.write( _('The person "%(name)s" already exists with id "%(pk)d", aborting...') % {"name": person.name, "pk": person.pk}, "", file=file, ) elif args.subparser == "delete": person = Person.get(args.person) if person: person.delete() stdout.write( _('Successfully deleted person with id "%(name)s".') % {"name": person.name}, "", file=file, ) else: stdout.write(_("No person found."), "", file=file) elif args.subparser == "edit": person = Person.get(args.person) if person: person.edit(args.field, args.value) stdout.write( _('Successfully edited person "%(name)s" with id "%(pk)d".') % {"name": person.name, "pk": person.pk}, "", file=file, ) person.print(file) else: stdout.write(_("No person found."), "", file=file) elif args.subparser == "info": person = Person.get(args.person) if person: person.print(file) else: stdout.write(_("No person found."), "", file=file) elif args.subparser == "list": if args.search: persons = Person.search(args.search) else: persons = Person.objects.all() stdout.write( [ _("Id"), _("Name"), _("Number of books"), _("Number of editions"), _("Number of papers"), ], "=", [0.05, 0.4, 0.6, 0.8], file=file, ) for i, has_next in lookahead(persons): stdout.write( [i.id, i.name, i.books.count(), i.editions.count(), i.papers.count()], "_" if has_next else "=", [0.05, 0.4, 0.6, 0.8], file=file, )
def _binding(args, file: TextIO = sys.stdout): binding: Optional[Binding] = None if args.subparser == "add": binding, created = Binding.from_dict({"name": args.name}) if created: stdout.write( _('Successfully added binding "%(name)s" with id "%(pk)d".') % { "name": binding.name, "pk": binding.pk }, "=", file=file, ) binding.print(file) else: stdout.write( _('The binding "%(name)s" already exists with id "%(pk)d", aborting...' ) % { "name": binding.name, "pk": binding.pk }, "", file=file, ) elif args.subparser == "delete": binding = Binding.get(args.binding) if binding: binding.delete() stdout.write( _('Successfully deleted binding with id "%(pk)d".') % {"pk": binding.pk}, "", file=file, ) else: stdout.write(_("No binding found."), "", file=file) elif args.subparser == "edit": binding = Binding.get(args.binding) if binding: binding.edit(args.field, args.value) stdout.write( _('Successfully edited binding "%(name)s" with id "%(pk)d".') % { "name": binding.name, "pk": binding.pk }, "", file=file, ) binding.print(file) else: stdout.write(_("No binding found."), "", file=file) elif args.subparser == "info": binding = Binding.get(args.binding) if binding: binding.print(file) else: stdout.write(_("No binding found."), "", file=file) elif args.subparser == "list": if args.search: bindings = Binding.search(args.search) else: bindings = Binding.objects.all() stdout.write([_("Id"), _("Name")], "=", [0.05], file=file) for i, has_next in lookahead(bindings): stdout.write([i.id, i.name], "_" if has_next else "=", [0.05], file=file)
def _paper(args: Namespace, file: TextIO = sys.stdout): paper: Optional[Paper] = None if args.subparser == "acquisition": paper = Paper.get(args.paper) acquisition: Optional[Acquisition] = None if args.acquisition_subparser == "add" and paper: acquisition, created = Acquisition.from_dict( { "date": args.date, "price": args.price }, paper) 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 paper: acquisition = Acquisition.get(args.acquisition, papers=paper) 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 paper: acquisition = Acquisition.get(args.acquisition, papers=paper) 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 paper found."), "", file=file) elif args.subparser == "add": paper, created = Paper.from_dict({ "title": args.title, "authors": [Person.get_or_create(author).to_dict() for author in args.author], "publishing_date": args.publishing_date, "journal": Journal.get_or_create(args.journal).to_dict() if args.journal else None, "volume": args.volume, "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], }) if created: stdout.write( _('Successfully added paper "%(title)s" with id "%(pk)d".') % { "title": paper.title, "pk": paper.pk }, "=", file=file, ) else: stdout.write( _('The paper "%(title)s" already exists with id "%(pk)d".') % { "title": paper.title, "pk": paper.pk }, "", file=file, ) paper.print(file) elif args.subparser == "delete": paper = Paper.get(args.paper) if paper: paper.delete() stdout.write( _('Successfully deleted paper with id "%(title)s".') % {"title": paper.title}, "", file=file, ) else: stdout.write(_("No paper found."), "", file=file) elif args.subparser == "edit": paper = Paper.get(args.paper) if paper: paper.edit(args.edit_subparser, args.value) stdout.write( _('Successfully edited paper "%(title)s" with id "%(pk)d".') % { "title": paper.title, "pk": paper.pk }, "", file=file, ) paper.print(file) else: stdout.write(_("No paper found."), "", file=file) elif args.subparser == "info": paper = Paper.get(args.paper) if paper: paper.print(file) else: stdout.write(_("No paper found."), "", file=file) elif args.subparser == "list": if args.search: papers = Paper.search(args.search) elif args.shelf: papers = Paper.by_shelf(args.shelf) else: papers = Paper.objects.all() stdout.write( [_("Id"), _("Name"), _("Journal"), _("Volume")], "=", [0.05, 0.7, 0.85], file=file, ) for i, has_next in lookahead(papers): stdout.write( [i.pk, i.name, i.journal.name, i.volume], "_" if has_next else "=", [0.05, 0.7, 0.85], file=file, ) elif args.subparser == "open": paper = Paper.get(args.paper) if paper: paper_file = paper.files.get(pk=args.file) path = settings.MEDIA_ROOT / paper_file.file.path if sys.platform == "linux": os.system(f'xdg-open "{path}"') else: os.system(f'open "{path}"') else: stdout.write(_("No paper found."), "", file=file) elif args.subparser == "parse": for paper, created in Paper.from_bibfile(args.bibfile, args.file): if created: stdout.write( _('Successfully added paper "%(title)s" with id "%(pk)d".') % { "title": paper.title, "pk": paper.pk }, file=file, ) if args.acquisition: acquisition, created = Acquisition.from_dict( {"date": datetime.date.today()}, paper) 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, ) else: stdout.write( _('The paper "%(title)s" already exists with id "%(pk)d".') % { "title": paper.title, "pk": paper.pk }, "=", file=file, ) paper.print(file) elif args.subparser == "read": paper = Paper.get(args.paper) read: Optional[Read] = None if args.read_subparser == "add" and paper: read, created = Read.from_dict( { "started": args.started, "finished": args.finished }, paper) 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 paper: read = Read.get(args.read, papers=paper) 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 paper: read = Read.get(args.read, papers=paper) 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 paper found."), "", file=file)
def print(self: T, file: TextIO = sys.stdout): """Print instance info.""" stdout.write([_("Field"), _("Value")], "=", [0.33], file=file) stdout.write([_("Id"), self.pk], positions=[0.33], file=file) stdout.write([_("Title"), self.title], positions=[0.33], file=file) if self.authors.count() > 0: for (i, author), has_next in lookahead(enumerate(self.authors.all())): stdout.write( ["" if i else _("Authors"), f"{author.pk}: {author}"], "" if has_next else "_", positions=[0.33], file=file, ) else: stdout.write([_("Authors"), ""], positions=[0.33], file=file) stdout.write( [ _("Series"), f"{self.series.id}: {self.series.name}" if self.series else "", ], positions=[0.33], file=file, ) stdout.write([_("Volume"), self.volume], positions=[0.33], file=file) if self.genres.count() > 0: for (i, g), has_next in lookahead(enumerate(self.genres.all())): stdout.write( ["" if i else _("Genres"), f"{g.id}: {g.name}"], "" if has_next else "_", positions=[0.33], file=file, ) else: stdout.write([_("Genres"), ""], positions=[0.33], file=file) if self.links.count() > 0: for (i, l), has_next in lookahead(enumerate(self.links.all())): stdout.write( ["" if i else _("Links"), f"{l.pk}: {l.link}"], "" if has_next else "_", positions=[0.33], file=file, ) else: stdout.write([_("Links"), ""], positions=[0.33], file=file) if self.editions.count() > 0: for (i, edition), has_next in lookahead( enumerate(self.editions.all())): s = "" if edition.alternate_title: s = f"{edition.alternate_title}, " if edition.binding: s += edition.binding.name stdout.write( ["" if i else _("Editions"), f"{edition.id}: {s}"], "" if has_next else "_", positions=[0.33], file=file, ) else: stdout.write([_("Editions"), ""], positions=[0.33], file=file)
def _magazine(args: Namespace, file: TextIO = sys.stdout): magazine: Optional[Magazine] = None if args.subparser == "add": magazine, created = Magazine.from_dict({ "name": args.name, "feed": Link.get_or_create(args.feed).to_dict() if args.feed else None, "links": [Link.get_or_create(link).to_dict() for link in args.link], }) if created: stdout.write( _('Successfully added magazine "%(name)s" with id "%(pk)d".') % { "name": magazine.name, "pk": magazine.pk }, "=", file=file, ) else: stdout.write( _('The magazine "%(name)s" already exists with id "%(pk)d", ' + "aborting...") % { "name": magazine.name, "pk": magazine.pk }, "", file=file, ) magazine.print(file) elif args.subparser == "delete": magazine = Magazine.get(args.magazine) if magazine: magazine.delete() stdout.write( _('Successfully deleted magazine "%(name)s".') % {"name": magazine.name}, "", file=file, ) else: stdout.write(_("No magazine found."), "", file=file) elif args.subparser == "edit": magazine = Magazine.get(args.magazine) if magazine: magazine.edit(args.field, args.value) stdout.write( _('Successfully edited magazine "%(name)s" with id "%(pk)d".') % { "name": magazine.name, "pk": magazine.pk }, "", file=file, ) else: stdout.write(_("No magazine found."), "", file=file) elif args.subparser == "info": magazine = Magazine.get(args.magazine) if magazine: magazine.print(file) else: stdout.write(_("No magazine found."), "", file=file) elif args.subparser == "issue": magazine = Magazine.get(args.magazine) acquisition: Optional[Acquisition] = None if magazine: if args.issue_subparser == "acquisition" and magazine: issue = Issue.get(args.issue, magazine) if args.acquisition_subparser == "add" and issue: acquisition, created = Acquisition.from_dict( { "date": args.date, "price": args.price }, issue) 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 issue: acquisition = Acquisition.get(args.acquisition, issues=issue) 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 issue: acquisition = Acquisition.get(args.acquisition, issues=issue) 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 issue found."), "", file=file) elif args.issue_subparser == "add" and magazine: issue, created = Issue.from_dict( { "issue": args.issue, "publishing_date": args.publishing_date, "cover": args.cover, "languages": args.language, "links": [ Link.get_or_create(link).to_dict() for link in args.link ], "files": [{ "path": file } for file in args.file], }, magazine, ) if created: stdout.write( _('Successfully added issue "%(issue)s" with id "%(pk)d".' ) % { "issue": issue.issue, "pk": issue.pk }, "=", file=file, ) else: stdout.write( _('The issue "%(issue)s" already exists with id "%(pk)d".' ) % { "issue": issue.issue, "pk": issue.pk }, "", file=file, ) issue.print(file) elif args.subparser == "delete" and magazine: issue = Issue.get(args.issue) if issue: issue.delete() stdout.write( _('Successfully deleted issue with id "%(pk)s".') % {"pk": issue.pk}, "", file=file, ) else: stdout.write(_("No issue found."), "", file=file) elif args.issue_subparser == "edit" and magazine: issue = Issue.get(args.issue, magazine) if issue: issue.edit(args.edit_subparser, args.value) stdout.write( _('Successfully edited issue "%(issue)s" with id "%(pk)d".' ) % { "issue": issue.issue, "pk": issue.pk }, "", file=file, ) issue.print(file) else: stdout.write(_("No issue found."), "", file=file) elif args.issue_subparser == "info" and magazine: issue = Issue.get(args.issue, magazine) if issue: issue.print(file) else: stdout.write(_("No issue found."), "", file=file) elif args.issue_subparser == "list" and magazine: if args.search: issues = Issue.search(args.search) elif args.shelf: issues = Issue.by_shelf(args.shelf) else: issues = Issue.objects.filter(magazine=magazine) stdout.write( [_("Id"), _("Magazine"), _("Issue"), _("Publishing date")], "=", [0.05, 0.40, 0.85], file=file, ) for i, has_next in lookahead(issues): stdout.write( [i.pk, i.magazine.name, i.issue, i.publishing_date], "_" if has_next else "=", [0.05, 0.40, 0.85], file=file, ) elif args.issue_subparser == "open" and magazine: issue = Issue.get(args.issue, magazine) if issue: issue_file = issue.files.get(pk=args.file) path = settings.MEDIA_ROOT / issue_file.file.path if sys.platform == "linux": os.system(f'xdg-open "{path}"') else: os.system(f'open "{path}"') else: stdout.write(_("No issue found."), "", file=file) elif args.issue_subparser == "read" and magazine: issue = Issue.get(args.issue, magazine) read: Optional[Read] = None if args.read_subparser == "add" and issue: read, created = Read.from_dict( { "started": args.started, "finished": args.finished }, issue) if created: stdout.write( _('Successfully added read with id "%(pk)s".') % {"pk": read.pk}, "=", file=file, ) else: stdout.write( _('The read already exists with id "%(pk)s".') % {"pk": read.pk}, "", file=file, ) read.print(file) elif args.read_subparser == "delete" and issue: read = Read.get(args.read, issues=issue) if read: read.delete() stdout.write( _('Successfully deleted read with id "%(pk)s".') % {"pk": read.pk}, "", file=file, ) else: stdout.write(_("No read found."), "", file=file) elif args.read_subparser == "edit" and issue: read = Read.get(args.read, issues=issue) if read: read.edit(args.field, args.value) stdout.write( _('Successfully edited read with id "%(pk)s".') % {"pk": read.pk}, "=", file=file, ) read.info(file) else: stdout.write(_("No read found."), "", file=file) else: stdout.write(_("No issue found."), "", file=file) else: stdout.write(_("No magazine found."), "", file=file) elif args.subparser == "list": if args.search: magazines = Magazine.search(args.search) else: magazines = Magazine.objects.all() stdout.write( [_("Id"), _("Name"), _("Number of issues")], "=", [0.05, 0.8], file=file, ) for i, has_next in lookahead(magazines): stdout.write( [i.pk, i.name, i.issues.count()], "_" if has_next else "=", [0.05, 0.8], file=file, )
def print(self: T, file: TextIO = sys.stdout): """Print instance info.""" stdout.write([_("Field"), _("Value")], "=", [0.33], file=file) stdout.write([_("Id"), self.pk], positions=[0.33], file=file) stdout.write([_("Book"), f"{self.book.pk}: {self.book}"], positions=[0.33], file=file) stdout.write([_("Alternate title"), self.alternate_title], positions=[0.33], file=file) stdout.write([_("ISBN"), self.isbn], positions=[0.33], file=file) stdout.write([_("Publishing date"), self.publishing_date], positions=[0.33], file=file) stdout.write([_("Cover"), self.cover_image], positions=[0.33], file=file) stdout.write( [ _("Binding"), f"{self.binding.pk}: {self.binding.name}" if self.binding else "", ], positions=[0.33], file=file, ) stdout.write( [ _("Publisher"), f"{self.publisher.pk}: {self.publisher.name}" if self.publisher else "", ], positions=[0.33], file=file, ) if self.persons.count() > 0: for (i, p), has_next in lookahead(enumerate(self.persons.all())): stdout.write( ["" if i else _("Persons"), f"{p.pk}: {p.name}"], "" if has_next else "_", positions=[0.33], file=file, ) else: stdout.write([_("Persons"), ""], positions=[0.33], file=file) if self.languages.count() > 0: for (i, l), has_next in lookahead(enumerate(self.languages.all())): stdout.write( ["" if i else _("Languages"), f"{l.pk}: {l.name}"], "" if has_next else "_", positions=[0.33], file=file, ) else: stdout.write([_("Languages"), ""], positions=[0.33], file=file) if self.links.count() > 0: for (i, l), has_next in lookahead(enumerate(self.links.all())): stdout.write( ["" if i else _("Links"), f"{l.pk}: {l.link}"], "" if has_next else "_", positions=[0.33], file=file, ) else: stdout.write([_("Links"), ""], positions=[0.33], file=file) if self.files.count() > 0: for (i, f), has_next in lookahead(enumerate(self.files.all())): stdout.write( ["" if i else _("Files"), f"{f.pk}: {f}"], "" if has_next else "_", positions=[0.33], file=file, ) else: stdout.write([_("Files"), ""], positions=[0.33], file=file) if self.acquisitions.count() > 0: for (i, a), has_next in lookahead(enumerate(self.acquisitions.all())): stdout.write( [ "" if i else _("Acquisitions"), f"{a.pk}: {_('date')}={a.date}, {_('price')}={a.price:0.2f}", ], "" if has_next else "_", positions=[0.33], file=file, ) else: stdout.write([_("Acquisitions"), ""], positions=[0.33], file=file) if self.reads.count() > 0: for (i, r), has_next in lookahead(enumerate(self.reads.all())): stdout.write( [ "" if i else _("Read"), f"{r.pk}: {_('date started')}={r.started}, " + f"{_('date finished')}={r.finished}", ], "" if has_next else "=", positions=[0.33], file=file, ) else: stdout.write([_("Read"), ""], positions=[0.33], file=file)
def print(self: T, file: TextIO = sys.stdout): """Print instance info.""" stdout.write([_("Field"), _("Value")], "=", [0.33], file=file) stdout.write([_("Id"), self.pk], positions=[0.33], file=file) stdout.write([_("Title"), self.title], positions=[0.33], file=file) if self.authors.count() > 0: for (i, author), has_next in lookahead(enumerate(self.authors.all())): stdout.write( ["" if i else _("Authors"), f"{author.pk}: {author}"], "" if has_next else "_", positions=[0.33], file=file, ) else: stdout.write([_("Authors"), ""], positions=[0.33], file=file) stdout.write( [ _("Journal"), f"{self.journal.pk}: {self.journal.name}" if self.journal else "", ], positions=[0.33], file=file, ) stdout.write( [_("Volume"), self.volume if self.volume else ""], positions=[0.33], file=file, ) stdout.write([_("Publishing date"), self.publishing_date], positions=[0.33], file=file) if self.languages.count() > 0: for (i, l), has_next in lookahead(enumerate(self.languages.all())): stdout.write( ["" if i else _("Languages"), f"{l.pk}: {l}"], "" if has_next else "_", positions=[0.33], file=file, ) else: stdout.write([_("Languages"), ""], positions=[0.33], file=file) if self.files.count() > 0: for (i, f), has_next in lookahead(enumerate(self.files.all())): stdout.write( ["" if i else _("Files"), f"{f.pk}: {f}"], "" if has_next else "_", positions=[0.33], file=file, ) else: stdout.write([_("Files"), ""], positions=[0.33], file=file) if self.links.count() > 0: for (i, l), has_next in lookahead(enumerate(self.links.all())): stdout.write( ["" if i else _("Links"), f"{l.pk}: {l.link}"], "" if has_next else "_", positions=[0.33], file=file, ) else: stdout.write([_("Links"), ""], positions=[0.33], file=file) if self.acquisitions.count() > 0: date_trans = _("date") price_trans = _("price") for (i, a), has_next in lookahead(enumerate(self.acquisitions.all())): s = f"{a.pk}: {date_trans}={a.date}, {price_trans}={a.price:0.2f}" stdout.write( ["" if i else _("Acquisitions"), s], "" if has_next else "_", positions=[0.33], file=file, ) else: stdout.write([_("Acquisitions"), ""], positions=[0.33], file=file) if self.reads.count() > 0: for (i, r), has_next in lookahead(enumerate(self.reads.all())): stdout.write( [ "" if i else _("Reads"), f"{r.pk}: {_('date started')}={r.started}, " + f"{_('date finished')}={r.finished}", ], "" if has_next else "_", positions=[0.33], file=file, ) else: stdout.write([_("Reads"), ""], positions=[0.33], file=file)
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, )