def test_get(self): issue, created = Issue.from_dict( { "issue": "2/2021", }, self.magazine, ) self.assertTrue(created) self.assertIsNotNone(issue.id) issue2 = Issue.get("Stuff 2/2021") self.assertIsNotNone(issue2) self.assertEquals(issue, issue2) issue2 = Issue.get(str(issue.id)) self.assertIsNotNone(issue2) self.assertEquals(issue, issue2)
def _info(args: Namespace, file: TextIO = sys.stdout): edition = Edition.get(args.obj) paper = Paper.get(args.obj) issue = Issue.get(args.obj) if edition is None and paper is None and issue is None: return elif edition is not None and paper is None and issue is None: edition.print(file) elif edition is None and paper is not None and issue is None: paper.print(file) elif edition is None and paper is None and issue is not None: issue.print(file) else: stdout.write(["More than one found."], after="=")
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 _acquisition(args: Namespace, file: TextIO = sys.stdout): acquisition: Optional[Acquisition] = None if args.subparser == "add": edition = Edition.get(args.obj) paper = Paper.get(args.obj) issue = Issue.get(args.obj) obj = None if edition is None and paper is None and issue is None: stdout.write(_("No edition, issue or paper found."), "", file=file) return elif edition is not None and paper is None and issue is None: obj = edition elif edition is None and paper is not None and issue is None: obj = paper elif edition is None and paper is None and issue is not None: obj = issue if obj: acquisition, created = Acquisition.from_dict( { "date": args.date, "price": args.price }, obj) if created: stdout.write( _('Successfully added acquisition with id "%(pk)d" to "%(obj)s".' ) % { "pk": acquisition.pk, "obj": obj }, "=", file=file, ) acquisition.print(file) else: stdout.write( _('The acquisition already exists with id "%(pk)d", aborting...' ) % {"pk": acquisition.pk}, "", file=file, ) else: stdout.write(_("More than one paper, issue or paper found."), "", file=file) elif args.subparser == "delete": acquisition = Acquisition.get(args.acquisition) if acquisition: acquisition.delete() stdout.write( _('Successfully deleted acquisition with id "%(pk)d".') % {"pk": acquisition.pk}, "", file=file, ) else: stdout.write(_("No acquisition found."), "", file=file) elif args.subparser == "edit": acquisition = Acquisition.get(args.acquisition) if acquisition: acquisition.edit(args.edit_subparser, 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) elif args.subparser == "info": acquisition = Acquisition.get(args.acquisition) if acquisition: acquisition.print(file) else: stdout.write(_("No acquisition found."), "", file=file)
def _read(args: Namespace, file: TextIO = sys.stdout): read: Optional[Acquisition] = None if args.subparser == "add": edition = Edition.get(args.obj) paper = Paper.get(args.obj) issue = Issue.get(args.obj) obj = None if edition is None and paper is None and issue is None: stdout.write(_("No edition, issue or paper found."), "", file=file) return elif edition is not None and paper is None and issue is None: obj = edition elif edition is None and paper is not None and issue is None: obj = paper elif edition is None and paper is None and issue is not None: obj = issue if obj: read, created = Read.from_dict( { "started": args.started, "finished": args.finished }, obj) if created: stdout.write( _('Successfully added read with id "%(pk)d" to "{obj}".') % { "pk": read.pk, "obj": obj }, "=", file=file, ) read.print(file) else: stdout.write( _('The read already exists with id "%(pk)d", aborting...') % {"pk": read.pk}, "", file=file, ) else: stdout.write(_("More than one paper, issue or paper found."), "", file=file) elif args.subparser == "delete": read = Read.get(args.read) 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.subparser == "edit": read = Read.get(args.read) if read: read.edit(args.field, args.value) stdout.write( _('Successfully edited read with id "%(pk)d".') % {"pk": read.pk}, "", file=file, ) read.print(file) else: stdout.write(_("No read found."), "", file=file) elif args.subparser == "info": read = Read.get(args.read) if read: read.print(file) else: stdout.write(_("No read found."), "", file=file)