Exemplo n.º 1
0
 def test_update_collision(self):
     existing_book = {
         "username": "******",
         "work_id": "2",
         "edition_id": "2",
         "bookshelf_id": "1",
     }
     self.db.insert("bookshelves_books", **existing_book)
     assert len(list(self.db.select("bookshelves_books"))) == 2
     Bookshelves.update_work_id(self.source_book['work_id'],
                                existing_book['work_id'])
     assert len(
         list(
             self.db.select(
                 "bookshelves_books",
                 where={
                     "username": "******",
                     "work_id": "2",
                     "edition_id": "2"
                 },
             ))), "failed to update 1 to 2"
     assert not len(
         list(
             self.db.select(
                 "bookshelves_books",
                 where={
                     "username": "******",
                     "work_id": "1",
                     "edition_id": "1"
                 },
             ))), "old work_id 1 present"
Exemplo n.º 2
0
    def main(self, test=False):
        params = web.input(key='', test='')

        # Provide an escape hatch to let POST requests preview
        if test is False and params.test:
            test = True

        summary = {'key': params.key, 'redirect_chain': [], 'resolved_key': None}
        if params.key:
            redirect_chain = Work.get_redirect_chain(params.key)
            summary['redirect_chain'] = [
                {
                    "key": thing.key,
                    "occurrences": {},
                    "updates": {}
                } for thing in redirect_chain
            ]
            summary['resolved_key'] = redirect_chain[-1].key

            for r in summary['redirect_chain']:
                olid = r['key'].split('/')[-1][2:-1]
                new_olid = summary['resolved_key'].split('/')[-1][2:-1]

                # count reading log entries
                r['occurrences']['readinglog'] = len(
                    Bookshelves.get_works_shelves(olid))
                r['occurrences']['ratings'] = len(
                    Ratings.get_all_works_ratings(olid))
                r['occurrences']['booknotes'] = len(
                    Booknotes.get_booknotes_for_work(olid))
                r['occurrences']['observations'] = len(
                    Observations.get_observations_for_work(olid))

                # track updates
                r['updates']['readinglog'] = Bookshelves.update_work_id(
                    olid, new_olid, _test=test
                )
                r['updates']['ratings'] = Ratings.update_work_id(
                    olid, new_olid, _test=test
                )
                r['updates']['booknotes'] = Booknotes.update_work_id(
                    olid, new_olid, _test=test
                )
                r['updates']['observations'] = Observations.update_work_id(
                    olid, new_olid, _test=test
                )

        return delegate.RawText(
            json.dumps(summary), content_type="application/json")
Exemplo n.º 3
0
    def resolve_redirect_chain(cls,
                               work_key: str,
                               test: bool = False) -> dict[str, Any]:
        summary: dict[str, Any] = {
            'key': work_key,
            'redirect_chain': [],
            'resolved_key': None,
        }
        redirect_chain = cls.get_redirect_chain(work_key)
        summary['redirect_chain'] = [{
            "key": thing.key,
            "occurrences": {},
            "updates": {}
        } for thing in redirect_chain]
        summary['resolved_key'] = redirect_chain[-1].key

        for r in summary['redirect_chain']:
            olid = r['key'].split('/')[-1][2:-1]  # 'OL1234x' --> '1234'
            new_olid = summary['resolved_key'].split('/')[-1][2:-1]

            # count reading log entries
            r['occurrences']['readinglog'] = len(
                Bookshelves.get_works_shelves(olid))
            r['occurrences']['ratings'] = len(
                Ratings.get_all_works_ratings(olid))
            r['occurrences']['booknotes'] = len(
                Booknotes.get_booknotes_for_work(olid))
            r['occurrences']['observations'] = len(
                Observations.get_observations_for_work(olid))

            # track updates
            r['updates']['readinglog'] = Bookshelves.update_work_id(olid,
                                                                    new_olid,
                                                                    _test=test)
            r['updates']['ratings'] = Ratings.update_work_id(olid,
                                                             new_olid,
                                                             _test=test)
            r['updates']['booknotes'] = Booknotes.update_work_id(olid,
                                                                 new_olid,
                                                                 _test=test)
            r['updates']['observations'] = Observations.update_work_id(
                olid, new_olid, _test=test)
        return summary
Exemplo n.º 4
0
 def test_update_simple(self):
     assert len(list(self.db.select("bookshelves_books"))) == 1
     Bookshelves.update_work_id(self.source_book['work_id'], "2")