def test_by_shelf(self): paper, created = Paper.from_dict( { "title": "Random Science", } ) self.assertTrue(created) self.assertIsNotNone(paper.id) paper, created = Paper.from_dict( { "title": "Random Science stuff", "acquisitions": [{"date": "2021-01-02", "price": 10.0}], } ) self.assertTrue(created) self.assertIsNotNone(paper.id) paper, created = Paper.from_dict( { "title": "Random new stuff", "reads": [{"started": "2021-01-02", "finished": "2021-01-03"}], } ) self.assertTrue(created) self.assertIsNotNone(paper.id) paper, created = Paper.from_dict( { "title": "Stuff from Science", "reads": [{"started": "2021-01-02", "finished": "2021-01-03"}], } ) self.assertTrue(created) self.assertIsNotNone(paper.id) paper, created = Paper.from_dict( { "title": "Science stuff", "acquisitions": [{"date": "2021-01-02", "price": 10.0}], "reads": [{"started": "2021-01-02", "finished": "2021-01-03"}], } ) self.assertTrue(created) self.assertIsNotNone(paper.id) self.assertEquals(2, Paper.by_shelf("acquired").count()) self.assertEquals(3, Paper.by_shelf("unacquired").count()) self.assertEquals(3, Paper.by_shelf("read").count()) self.assertEquals(2, Paper.by_shelf("unread").count())
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)