def test_get(self): binding, created = Binding.from_dict({"name": "Gebundene Ausgabe"}) self.assertTrue(created) self.assertIsNotNone(binding.id) binding2 = Binding.get("Gebundene Ausgabe") self.assertIsNotNone(binding2) self.assertEquals(binding, binding2) binding2 = Binding.get(str(binding.id)) self.assertIsNotNone(binding2) self.assertEquals(binding, binding2)
def test_delete(self): binding, created = Binding.from_dict({"name": "Taschenbuch"}) self.assertTrue(created) self.assertIsNotNone(binding.id) deleted = binding.delete() self.assertIsNone(binding.id) self.assertEquals((1, {"bindings.Binding": 1}), deleted)
def test_save(self): binding = Binding(name="E-Book") binding.save() self.assertIsNotNone(binding.id) self.assertEquals("e-book", binding.slug) binding = Binding(name="Taschenbuch") binding.save() self.assertIsNotNone(binding.id) self.assertEquals("taschenbuch", binding.slug)
def edit(self: T, field: str, value: str, *args, **kwargs): """Change field by given value.""" assert field in [ "alternate_title", "alternate-title", "binding", "cover", "isbn", "person", "publishing_date", "publishing-date", "publisher", "language", "link", "file", ] if field == "alternate_title" or field == "alternate-title": self.alternate_title = value elif field == "binding": self.binding = Binding.get_or_create(value) elif field == "cover": self.cover_image.save(os.path.basename(str(value)), DJFile(open(str(value), "rb"))) elif field == "isbn": self.isbn = value elif field == "person": person = Person.get_or_create(value) if self.persons.filter(pk=person.pk).exists(): self.persons.remove(person) else: self.persons.add(person) elif field == "publishing_date" or field == "publishing-date": self.publishing_date = value elif field == "publisher": self.publisher = Publisher.get_or_create(value) elif field == "language": language = Language.get_or_create(value) if self.languages.filter(pk=language.pk).exists(): self.languages.remove(language) else: self.languages.add(language) elif field == "link": link = Link.get_or_create(value) if self.links.filter(pk=link.pk).exists(): self.links.remove(link) else: self.links.add(link) elif field == "file": file, created = File.from_dict({"path": value}) if self.files.filter(pk=file.pk).exists(): self.files.remove(file) file.delete() else: self.files.add(file) self.save(*args, **kwargs)
def from_dict(cls: Type[T], data: Dict, book: Book) -> Tuple[T, bool]: """Create from dict. Returns True if was crated, i. e. was not found in the DB. """ defaults: Dict = {} if "alternate_title" in data and data["alternate_title"]: defaults["alternate_title"] = data["alternate_title"] if "isbn" in data and data["isbn"]: defaults["isbn"] = data["isbn"] if "publishing_date" in data and data["publishing_date"]: defaults["publishing_date"] = datetime.datetime.strptime( data["publishing_date"], "%Y-%m-%d").date() if "publisher" in data and data["publisher"]: defaults["publisher"] = Publisher.from_dict(data["publisher"])[0] if "binding" in data and data["binding"]: defaults["binding"] = Binding.from_dict(data["binding"])[0] if "bibtex" in data and data["bibtex"]: defaults["bibtex"] = data["bibtex"] edition, created = cls.objects.get_or_create( book=book, alternate_title=data["alternate_title"] if "alternate_title" in data else None, isbn=data["isbn"] if "isbn" in data else None, publishing_date=data["publishing_date"] if "publishing_date" in data else None, defaults=defaults, ) if "cover_image" in data and data["cover_image"]: edition.cover_image.save( os.path.basename(data["cover_image"]), DJFile(open(data["cover_image"], "rb")), ) if "languages" in data and data["languages"]: for i in data["languages"]: edition.languages.add(Language.from_dict(i)[0]) if "links" in data and data["links"]: for i in data["links"]: edition.links.add(Link.from_dict(i)[0]) if "persons" in data and data["persons"]: for i in data["persons"]: edition.persons.add(Person.from_dict(i)[0]) if "acquisitions" in data and data["acquisitions"]: for i in data["acquisitions"]: Acquisition.from_dict(i, edition) if "files" in data and data["files"]: for i in data["files"]: File.from_dict(i, edition) if "reads" in data and data["reads"]: for i in data["reads"]: Read.from_dict(i, edition) edition.save() return edition, created
def test_search(self): binding, created = Binding.from_dict({"name": "Broschiert"}) self.assertTrue(created) self.assertIsNotNone(binding.id) binding, created = Binding.from_dict({"name": "Paperback"}) self.assertTrue(created) self.assertIsNotNone(binding.id) binding, created = Binding.from_dict({"name": "Leather Binding"}) self.assertTrue(created) self.assertIsNotNone(binding.id) bindings = Binding.objects.all() self.assertEquals(3, len(bindings)) bindings = Binding.search("leather") self.assertEquals(1, len(bindings)) bindings = Binding.search("er") self.assertEquals(3, len(bindings))
def test_print(self): binding, created = Binding.from_dict({"name": "Leather Binding"}) self.assertTrue(created) self.assertIsNotNone(binding.id) with StringIO() as cout: binding.print(cout) self.assertEquals( "Field Value " + " \n==================================" + "==================================================================\n" + "Id 1 " + " \n__________________________________" + "__________________________________________________________________\n" + "Name Leather Binding " + " \n__________________________________" + "__________________________________________________________________\n", cout.getvalue(), )
def test_from_to_dict(self): binding, created = Binding.objects.get_or_create(name="Taschenbuch") self.assertTrue(created) self.assertIsNotNone(binding.id) self.assertEquals({"name": "Taschenbuch"}, binding.to_dict()) self.assertEquals((binding, False), Binding.from_dict({"name": "Taschenbuch"}))
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 _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)