Exemplo n.º 1
0
    def test_search(self):
        issue, created = Issue.from_dict(
            {
                "issue": "1/2020",
            },
            self.magazine,
        )
        self.assertTrue(created)
        self.assertIsNotNone(issue.id)

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

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

        self.assertEquals(3, Issue.objects.all().count())
        self.assertEquals(1, Issue.search("Stuff 2020").count())
        self.assertEquals(2, Issue.search("Stuff 2021").count())
Exemplo n.º 2
0
    def test_delete(self):
        magazine, created = Magazine.from_dict({"name": "Cool"})
        self.assertTrue(created)
        self.assertIsNotNone(magazine.id)

        deleted = magazine.delete()
        self.assertIsNone(magazine.id)
        self.assertEquals((1, {"magazines.Magazine": 1}), deleted)

        magazine, created = Magazine.from_dict({
            "name":
            "Example",
            "feed": {
                "url": "https://example.com/feed"
            },
            "links": [{
                "url": "https://example.com"
            }],
        })
        self.assertTrue(created)
        self.assertIsNotNone(magazine.id)

        deleted = magazine.delete()
        self.assertIsNone(magazine.id)
        self.assertEquals(
            (
                4,
                {
                    "magazines.Magazine": 1,
                    "magazines.Magazine_links": 1,
                    "links.Link": 2,
                },
            ),
            deleted,
        )

        magazine, created = Magazine.from_dict({"name": "Cool"})
        self.assertTrue(created)
        self.assertIsNotNone(magazine.id)
        issue, created = Issue.from_dict({"issue": "1"}, magazine)
        self.assertTrue(created)
        self.assertIsNotNone(issue.id)
        issue, created = Issue.from_dict({"issue": "2"}, magazine)
        self.assertTrue(created)
        self.assertIsNotNone(issue.id)

        deleted = magazine.delete()
        self.assertIsNone(magazine.id)
        self.assertEquals(
            (
                3,
                {
                    "magazines.Magazine": 1,
                    "magazines.Issue": 2,
                },
            ),
            deleted,
        )
Exemplo n.º 3
0
    def test_edit(self):
        issue, created = Issue.from_dict({"issue": "1-2021"}, self.magazine)
        self.assertTrue(created)
        self.assertIsNotNone(issue.id)

        issue.edit("issue", "1/2021")
        self.assertEquals("1/2021", issue.issue)

        self.assertIsNone(issue.publishing_date)
        issue.edit("publishing_date",
                   datetime.strptime("2021-02-01", "%Y-%m-%d").date())
        self.assertEquals(
            datetime.strptime("2021-02-01", "%Y-%m-%d").date(),
            issue.publishing_date)

        self.assertEquals(0, issue.languages.count())
        issue.edit("language", "Deutsch")
        self.assertEquals(1, issue.languages.count())
        self.assertEquals("Deutsch", issue.languages.first().name)

        issue.edit("language", "English")
        self.assertEquals(2, issue.languages.count())
        self.assertEquals("English", issue.languages.last().name)

        issue.edit("language", "Deutsch")
        self.assertEquals(1, issue.languages.count())
        self.assertEquals("English", issue.languages.first().name)

        self.assertEquals(0, issue.links.count())
        issue.edit("link", "http://example.com")
        self.assertEquals(1, issue.links.count())
        self.assertEquals("http://example.com", issue.links.first().link)

        issue.edit("link", "http://example.org")
        self.assertEquals(2, issue.links.count())
        self.assertEquals("http://example.org", issue.links.last().link)

        issue.edit("link", "http://example.com")
        self.assertEquals(1, issue.links.count())
        self.assertEquals("http://example.org", issue.links.first().link)

        with NamedTemporaryFile() as f:
            f.write(b"Lorem ipsum dolorem")

            issue.edit("file", f.name)
            self.assertEquals(1, issue.files.count())
            self.assertEquals(
                {
                    "path":
                    os.path.join(
                        settings.MEDIA_ROOT,
                        "magazines",
                        str(self.magazine.pk),
                        str(issue.pk),
                        os.path.basename(f.name),
                    )
                },
                issue.files.first().to_dict(),
            )
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
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="=")
Exemplo n.º 6
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)
Exemplo n.º 7
0
    def test_get(self):
        for obj in [self.edition, self.issue, self.paper]:
            read, created = Read.from_dict({"finished": "2021-01-01"}, obj)
            self.assertTrue(created)
            self.assertIsNotNone(read.pk)

            if type(obj) == Edition:
                read2 = Read.get(obj.book.title)
            elif type(obj) == Issue:
                read2 = Read.get(f"{obj.magazine.name} {obj.issue}")
            elif type(obj) == Paper:
                read2 = Read.get(obj.title)
            self.assertIsNotNone(read2)
            self.assertEquals(read, read2)

            read2 = read.get(str(read.pk))
            self.assertIsNotNone(read2)
            self.assertEquals(read, read2)

            if type(obj) == Edition:
                edition, created = Edition.from_dict({"title": "Old stuff"},
                                                     obj.book)
                self.assertTrue(created)
                self.assertIsNotNone(edition.id)

                read, created = Read.from_dict(
                    {
                        "started": "2021-01-10",
                        "finished": "2021-01-31"
                    }, edition)
                self.assertTrue(created)
                self.assertIsNotNone(read.pk)

                read2 = read.get(str(read.pk), editions=edition)
                self.assertIsNotNone(read2)
                self.assertEquals(read, read2)
            elif type(obj) == Issue:
                issue, created = Issue.from_dict({"issue": "2/2021"},
                                                 obj.magazine)
                self.assertTrue(created)
                self.assertIsNotNone(issue.id)

                read, created = Read.from_dict(
                    {
                        "started": "2021-01-10",
                        "finished": "2021-01-31"
                    }, issue)
                self.assertTrue(created)
                self.assertIsNotNone(read.pk)

                read2 = read.get(str(read.pk), issues=issue)
                self.assertIsNotNone(read2)
                self.assertEquals(read, read2)
            elif type(obj) == Paper:
                paper, created = Paper.from_dict({"title": "Old stuff"})
                self.assertTrue(created)
                self.assertIsNotNone(paper.id)

                read, created = Read.from_dict(
                    {
                        "started": "2021-01-10",
                        "finished": "2021-01-31"
                    }, paper)
                self.assertTrue(created)
                self.assertIsNotNone(read.pk)

                read2 = read.get(str(read.pk), papers=paper)
                self.assertIsNotNone(read2)
                self.assertEquals(read, read2)
Exemplo n.º 8
0
    def test_search(self):
        for obj in [self.edition, self.issue, self.paper]:
            acquisition, created = Acquisition.from_dict(
                {"date": "2021-01-01"}, obj)
            self.assertTrue(created)
            self.assertIsNotNone(acquisition.pk)

            acquisition, created = Acquisition.from_dict(
                {
                    "date": "2021-01-10",
                    "price": 10.99
                }, obj)
            self.assertTrue(created)
            self.assertIsNotNone(acquisition.pk)

            acquisition, created = Acquisition.from_dict({"price": 1.99}, obj)
            self.assertTrue(created)
            self.assertIsNotNone(acquisition.pk)

            if type(obj) == Edition:
                edition, created = Edition.from_dict({}, obj.book)
                self.assertTrue(created)
                self.assertIsNotNone(edition.id)

                acquisition, created = Acquisition.from_dict(
                    {
                        "date": "2021-01-10",
                        "price": 1.99
                    }, edition)
                self.assertTrue(created)
                self.assertIsNotNone(acquisition.pk)

                self.assertEquals(4, Acquisition.objects.all().count())
                self.assertEquals(4, Acquisition.search("example").count())
                self.assertEquals(
                    1,
                    Acquisition.search("4", editions=edition).count())
            elif type(obj) == Issue:
                issue, created = Issue.from_dict({"issue": "2/2021"},
                                                 obj.magazine)
                self.assertTrue(created)
                self.assertIsNotNone(issue.id)

                acquisition, created = Acquisition.from_dict(
                    {
                        "date": "2021-01-10",
                        "price": 1.99
                    }, issue)
                self.assertTrue(created)
                self.assertIsNotNone(acquisition.pk)

                self.assertEquals(8, Acquisition.objects.all().count())
                self.assertEquals(8, Acquisition.search("example").count())
                self.assertEquals(
                    1,
                    Acquisition.search("8", issues=issue).count())
            elif type(obj) == Paper:
                paper, created = Paper.from_dict({"title": "Old stuff"})
                self.assertTrue(created)
                self.assertIsNotNone(paper.id)

                acquisition, created = Acquisition.from_dict(
                    {
                        "date": "2021-01-10",
                        "price": 1.99
                    }, paper)
                self.assertTrue(created)
                self.assertIsNotNone(acquisition.pk)

                self.assertEquals(12, Acquisition.objects.all().count())
                self.assertEquals(3, Acquisition.search("cool").count())
                self.assertEquals(
                    1,
                    Acquisition.search("12", papers=paper).count())
Exemplo n.º 9
0
    def test_get(self):
        for obj in [self.edition, self.issue, self.paper]:
            acquisition, created = Acquisition.from_dict(
                {"date": "2021-01-01"}, obj)
            self.assertTrue(created)
            self.assertIsNotNone(acquisition.pk)

            if type(obj) == Edition:
                acquisition2 = Acquisition.get(obj.book.title)
            elif type(obj) == Issue:
                acquisition2 = Acquisition.get(
                    f"{obj.magazine.name} {obj.issue}")
            elif type(obj) == Paper:
                acquisition2 = Acquisition.get(obj.title)
            self.assertIsNotNone(acquisition2)
            self.assertEquals(acquisition, acquisition2)

            acquisition2 = Acquisition.get(str(acquisition.pk))
            self.assertIsNotNone(acquisition2)
            self.assertEquals(acquisition, acquisition2)

            if type(obj) == Edition:
                edition, created = Edition.from_dict({"title": "Old stuff"},
                                                     obj.book)
                self.assertTrue(created)
                self.assertIsNotNone(edition.id)

                acquisition, created = Acquisition.from_dict(
                    {
                        "date": "2021-01-10",
                        "price": 1.99
                    }, edition)
                self.assertTrue(created)
                self.assertIsNotNone(acquisition.pk)

                acquisition2 = Acquisition.get(str(acquisition.pk),
                                               editions=edition)
                self.assertIsNotNone(acquisition2)
                self.assertEquals(acquisition, acquisition2)
            elif type(obj) == Issue:
                issue, created = Issue.from_dict({"issue": "2/2021"},
                                                 obj.magazine)
                self.assertTrue(created)
                self.assertIsNotNone(issue.id)

                acquisition, created = Acquisition.from_dict(
                    {
                        "date": "2021-01-10",
                        "price": 1.99
                    }, issue)
                self.assertTrue(created)
                self.assertIsNotNone(acquisition.pk)

                acquisition2 = Acquisition.get(str(acquisition.pk),
                                               issues=issue)
                self.assertIsNotNone(acquisition2)
                self.assertEquals(acquisition, acquisition2)
            elif type(obj) == Paper:
                paper, created = Paper.from_dict({"title": "Old stuff"})
                self.assertTrue(created)
                self.assertIsNotNone(paper.id)

                acquisition, created = Acquisition.from_dict(
                    {
                        "date": "2021-01-10",
                        "price": 1.99
                    }, paper)
                self.assertTrue(created)
                self.assertIsNotNone(acquisition.pk)

                acquisition2 = Acquisition.get(str(acquisition.pk),
                                               papers=paper)
                self.assertIsNotNone(acquisition2)
                self.assertEquals(acquisition, acquisition2)
Exemplo n.º 10
0
    def test_save(self):
        issue = Issue(issue="1/2021")
        issue.magazine = self.magazine
        issue.save()
        self.assertIsNotNone(issue.id)

        issue = Issue(issue="2/2021")
        issue.magazine = self.magazine
        issue.publishing_date = datetime.strptime("2021-02-01",
                                                  "%Y-%m-%d").date()
        issue.save()
        self.assertIsNotNone(issue.id)

        file = File(
            file=SimpleUploadedFile("test.txt", b"Lorem ipsum dolorem"))
        file.save()
        self.assertIsNotNone(file.id)
        self.assertEquals("files/test.txt", file.file.name)

        issue.files.add(file)
        issue.save()
        self.assertEquals("magazines/1/2/test.txt",
                          issue.files.first().file.name)
Exemplo n.º 11
0
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)
Exemplo n.º 12
0
    def test_delete(self):
        issue, created = Issue.from_dict({"issue": "1-2021"}, self.magazine)
        self.assertTrue(created)
        self.assertIsNotNone(issue.id)

        deleted = issue.delete()
        self.assertIsNone(issue.id)
        self.assertEquals((1, {"magazines.Issue": 1}), deleted)

        issue, created = Issue.from_dict(
            {
                "issue": "1/2021",
                "publishing_date": "2021-03-01",
                "languages": [{
                    "name": "Englisch"
                }],
                "links": [{
                    "url": "https://example.com"
                }],
                "acquisitions": [{
                    "date": "2021-03-01",
                    "price": 10.0
                }],
                "reads": [{
                    "started": "2021-03-01",
                    "finished": "2021-03-31"
                }],
            },
            self.magazine,
        )
        self.assertTrue(created)
        self.assertIsNotNone(issue.id)

        deleted = issue.delete()
        self.assertIsNone(issue.id)
        self.assertEquals(
            (
                6,
                {
                    "magazines.Issue": 1,
                    "links.Link": 1,
                    "magazines.Issue_languages": 1,
                    "magazines.Issue_links": 1,
                    "shelves.Acquisition": 1,
                    "shelves.Read": 1,
                },
            ),
            deleted,
        )

        with NamedTemporaryFile() as f:
            f.write(b"Lorem ipsum dolorem")

            issue, created = Issue.from_dict(
                {
                    "issue": "3/2021",
                    "publishing_date": "2021-03-01",
                    "languages": [{
                        "name": "Englisch"
                    }],
                    "links": [{
                        "url": "https://example.com"
                    }],
                    "files": [{
                        "path": f.name
                    }],
                    "acquisitions": [{
                        "date": "2021-03-01",
                        "price": 10.0
                    }],
                    "reads": [{
                        "started": "2021-03-01",
                        "finished": "2021-03-31"
                    }],
                },
                self.magazine,
            )
            self.assertTrue(created)
            self.assertIsNotNone(issue.id)

            deleted = issue.delete()
            self.assertIsNone(issue.id)
            self.assertEquals(
                (
                    7,
                    {
                        "magazines.Issue": 1,
                        "links.Link": 1,
                        "files.File": 1,
                        "magazines.Issue_languages": 1,
                        "magazines.Issue_links": 1,
                        "shelves.Acquisition": 1,
                        "shelves.Read": 1,
                    },
                ),
                deleted,
            )
Exemplo n.º 13
0
    def test_from_to_dict(self):
        issue, created = Issue.from_dict({"issue": "1/2021"}, self.magazine)
        self.assertTrue(created)
        self.assertIsNotNone(issue.id)
        self.assertEquals(
            {
                "issue": "1/2021",
                "publishing_date": None,
                "cover_image": None,
                "languages": None,
                "links": None,
                "files": None,
                "acquisitions": None,
                "reads": None,
            },
            issue.to_dict(),
        )
        self.assertEquals((issue, False),
                          Issue.from_dict(issue.to_dict(), self.magazine))

        issue, created = Issue.from_dict(
            {
                "issue": "2/2021",
                "publishing_date": "2021-02-01",
                "languages": [{
                    "name": "Englisch"
                }],
                "links": [{
                    "url": "https://example.com"
                }],
                "acquisitions": [{
                    "date": "2021-01-02",
                    "price": 10.0
                }],
                "reads": [{
                    "started": "2021-01-02",
                    "finished": "2021-01-03"
                }],
            },
            self.magazine,
        )
        self.assertTrue(created)
        self.assertIsNotNone(issue.id)
        self.assertEquals(
            {
                "issue": "2/2021",
                "publishing_date": "2021-02-01",
                "cover_image": None,
                "languages": [{
                    "name": "Englisch"
                }],
                "files": None,
                "links": [{
                    "url": "https://example.com"
                }],
                "acquisitions": [{
                    "date": "2021-01-02",
                    "price": 10.0
                }],
                "reads": [{
                    "started": "2021-01-02",
                    "finished": "2021-01-03"
                }],
            },
            issue.to_dict(),
        )
        self.assertEquals(
            (issue, False),
            Issue.from_dict(
                {
                    "issue": "2/2021",
                    "publishing_date": "2021-02-01",
                    "languages": [{
                        "name": "Englisch"
                    }],
                    "links": [{
                        "url": "https://example.com"
                    }],
                    "acquisitions": [{
                        "date": "2021-02-01",
                        "price": 10.0
                    }],
                    "reads": [{
                        "started": "2021-02-01",
                        "finished": "2021-02-28"
                    }],
                },
                self.magazine,
            ),
        )

        with NamedTemporaryFile() as f:
            f.write(b"Lorem ipsum dolorem")

            issue, created = Issue.from_dict(
                {
                    "issue": "3/2021",
                    "publishing_date": "2021-03-01",
                    "languages": [{
                        "name": "Englisch"
                    }],
                    "links": [{
                        "url": "https://example.com"
                    }],
                    "files": [{
                        "path": f.name
                    }],
                    "acquisitions": [{
                        "date": "2021-03-01",
                        "price": 10.0
                    }],
                    "reads": [{
                        "started": "2021-03-01",
                        "finished": "2021-03-31"
                    }],
                },
                self.magazine,
            )
            self.assertTrue(created)
            self.assertIsNotNone(issue.id)
            self.assertEquals(
                {
                    "issue":
                    "3/2021",
                    "publishing_date":
                    "2021-03-01",
                    "cover_image":
                    None,
                    "languages": [{
                        "name": "Englisch"
                    }],
                    "files": [{
                        "path":
                        os.path.join(
                            settings.MEDIA_ROOT,
                            "magazines",
                            str(self.magazine.pk),
                            str(issue.pk),
                            os.path.basename(f.name),
                        )
                    }],
                    "links": [{
                        "url": "https://example.com"
                    }],
                    "acquisitions": [{
                        "date": "2021-03-01",
                        "price": 10.0
                    }],
                    "reads": [{
                        "started": "2021-03-01",
                        "finished": "2021-03-31"
                    }],
                },
                issue.to_dict(),
            )
Exemplo n.º 14
0
    def test_by_shelf(self):
        issue, created = Issue.from_dict(
            {
                "issue": "1/2021",
            },
            self.magazine,
        )
        self.assertTrue(created)
        self.assertIsNotNone(issue.id)

        issue, created = Issue.from_dict(
            {
                "issue": "2/2021",
                "acquisitions": [{
                    "date": "2021-01-02",
                    "price": 10.0
                }],
            },
            self.magazine,
        )
        self.assertTrue(created)
        self.assertIsNotNone(issue.id)

        issue, created = Issue.from_dict(
            {
                "issue": "3/2021",
                "reads": [{
                    "started": "2021-01-02",
                    "finished": "2021-01-03"
                }],
            },
            self.magazine,
        )
        self.assertTrue(created)
        self.assertIsNotNone(issue.id)

        issue, created = Issue.from_dict(
            {
                "issue": "4/2021",
                "reads": [{
                    "started": "2021-01-02",
                    "finished": "2021-01-03"
                }],
            },
            self.magazine,
        )
        self.assertTrue(created)
        self.assertIsNotNone(issue.id)

        issue, created = Issue.from_dict(
            {
                "issue": "5/2021",
                "acquisitions": [{
                    "date": "2021-01-02",
                    "price": 10.0
                }],
                "reads": [{
                    "started": "2021-01-02",
                    "finished": "2021-01-03"
                }],
            },
            self.magazine,
        )
        self.assertTrue(created)
        self.assertIsNotNone(issue.id)

        self.assertEquals(2, issue.by_shelf("acquired").count())
        self.assertEquals(3, issue.by_shelf("unacquired").count())
        self.assertEquals(3, issue.by_shelf("read").count())
        self.assertEquals(2, issue.by_shelf("unread").count())
Exemplo n.º 15
0
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)
Exemplo n.º 16
0
    def test_search(self):
        for obj in [self.edition, self.issue, self.paper]:
            read, created = Read.from_dict({"started": "2021-01-01"}, obj)
            self.assertTrue(created)
            self.assertIsNotNone(read.pk)

            read, created = Read.from_dict(
                {
                    "date": "2021-01-10",
                    "finished": "2021-04-15"
                }, obj)
            self.assertTrue(created)
            self.assertIsNotNone(read.pk)

            read, created = Read.from_dict({"finished": "2021-03-18"}, obj)
            self.assertTrue(created)
            self.assertIsNotNone(read.pk)

            if type(obj) == Edition:
                edition, created = Edition.from_dict({}, obj.book)
                self.assertTrue(created)
                self.assertIsNotNone(edition.id)

                read, created = Read.from_dict(
                    {
                        "date": "2021-01-10",
                        "finished": "2021-01-31"
                    }, edition)
                self.assertTrue(created)
                self.assertIsNotNone(read.pk)

                self.assertEquals(4, Read.objects.all().count())
                self.assertEquals(4, Read.search("example").count())
                self.assertEquals(1,
                                  Read.search("4", editions=edition).count())
            elif type(obj) == Issue:
                issue, created = Issue.from_dict({"issue": "2/2021"},
                                                 obj.magazine)
                self.assertTrue(created)
                self.assertIsNotNone(issue.id)

                read, created = Read.from_dict(
                    {
                        "date": "2021-01-10",
                        "finished": "2021-01-31"
                    }, issue)
                self.assertTrue(created)
                self.assertIsNotNone(read.pk)

                self.assertEquals(8, Read.objects.all().count())
                self.assertEquals(8, Read.search("example").count())
                self.assertEquals(1, Read.search("8", issues=issue).count())
            elif type(obj) == Paper:
                paper, created = Paper.from_dict({"title": "Old stuff"})
                self.assertTrue(created)
                self.assertIsNotNone(paper.id)

                read, created = Read.from_dict(
                    {
                        "date": "2021-01-10",
                        "finished": "2021-01-31"
                    }, paper)
                self.assertTrue(created)
                self.assertIsNotNone(read.pk)

                self.assertEquals(12, Read.objects.all().count())
                self.assertEquals(3, Read.search("cool").count())
                self.assertEquals(1, Read.search("12", papers=paper).count())
Exemplo n.º 17
0
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,
            )
Exemplo n.º 18
0
    def test_print(self):
        issue, created = Issue.from_dict(
            {
                "issue": "1/2021",
                "publishing_date": "2021-01-01",
                "languages": [{
                    "name": "Englisch"
                }],
            },
            self.magazine,
        )
        self.assertTrue(created)
        self.assertIsNotNone(issue.id)

        with StringIO() as cout:
            issue.print(cout)
            self.assertEquals(
                "Field                            Value                              "
                +
                "                                \n=================================="
                +
                "==================================================================\n"
                +
                "Id                               1                                  "
                +
                "                                \n__________________________________"
                +
                "__________________________________________________________________\n"
                +
                "Magazine                         1: Stuff                           "
                +
                "                                \n__________________________________"
                +
                "__________________________________________________________________\n"
                +
                "Issue                            1/2021                             "
                +
                "                                \n__________________________________"
                +
                "__________________________________________________________________\n"
                +
                "Publishing date                  2021-01-01                         "
                +
                "                                \n__________________________________"
                +
                "__________________________________________________________________\n"
                +
                "Cover                                                               "
                +
                "                                \n__________________________________"
                +
                "__________________________________________________________________\n"
                +
                "Languages                        1: Englisch                        "
                +
                "                                \n__________________________________"
                +
                "__________________________________________________________________\n"
                +
                "Files                                                               "
                +
                "                                \n__________________________________"
                +
                "__________________________________________________________________\n"
                +
                "Links                                                               "
                +
                "                                \n__________________________________"
                +
                "__________________________________________________________________\n"
                +
                "Acquisitions                                                        "
                +
                "                                \n__________________________________"
                +
                "__________________________________________________________________\n"
                +
                "Read                                                                "
                +
                "                                \n__________________________________"
                +
                "__________________________________________________________________\n",
                cout.getvalue(),
            )