Beispiel #1
0
    def POST(self, work_id):
        """
        Add a work (or a work and an edition) to a bookshelf.

        GET params:
        - edition_id str (optional)
        - action str: e.g. "add", "remove"
        - redir bool: if patron not logged in, redirect back to page after login
        - bookshelf_id int: which bookshelf? e.g. the ID for "want to read"?
        - dont_remove bool: if book exists & action== "add", don't try removal

        :param str work_id: e.g. OL123W
        :rtype: json
        :return: a list of bookshelves_affected
        """
        from openlibrary.core.models import Bookshelves

        user = accounts.get_current_user()
        i = web.input(edition_id=None,
                      action="add",
                      redir=False,
                      bookshelf_id=None,
                      dont_remove=False)
        key = i.edition_id if i.edition_id else ('/works/OL%sW' % work_id)

        if not user:
            raise web.seeother('/account/login?redirect=%s' % key)

        username = user.key.split('/')[2]
        current_status = Bookshelves.get_users_read_status_of_work(
            username, work_id)

        try:
            bookshelf_id = int(i.bookshelf_id)
            shelf_ids = Bookshelves.PRESET_BOOKSHELVES.values()
            if bookshelf_id != -1 and bookshelf_id not in shelf_ids:
                raise ValueError
        except (TypeError, ValueError):
            return delegate.RawText(json.dumps({'error': 'Invalid bookshelf'}),
                                    content_type="application/json")

        if (not i.dont_remove
            ) and bookshelf_id == current_status or bookshelf_id == -1:
            work_bookshelf = Bookshelves.remove(username=username,
                                                work_id=work_id,
                                                bookshelf_id=current_status)

        else:
            edition_id = int(
                i.edition_id.split('/')[2][2:-1]) if i.edition_id else None
            work_bookshelf = Bookshelves.add(username=username,
                                             bookshelf_id=bookshelf_id,
                                             work_id=work_id,
                                             edition_id=edition_id)

        if i.redir:
            raise web.seeother(key)
        return delegate.RawText(json.dumps(
            {'bookshelves_affected': work_bookshelf}),
                                content_type="application/json")
Beispiel #2
0
    def POST(self, work_id):
        from openlibrary.core.models import Bookshelves

        user = accounts.get_current_user()
        i = web.input(edition_id=None,
                      action="add",
                      redir=False,
                      bookshelf_id=None)
        key = i.edition_id if i.edition_id else ('/works/OL%sW' % work_id)

        if not user:
            raise web.seeother('/account/login?redirect=%s' % key)

        username = user.key.split('/')[2]
        current_status = Bookshelves.get_users_read_status_of_work(
            username, work_id)

        try:
            bookshelf_id = int(i.bookshelf_id)
            shelf_ids = Bookshelves.PRESET_BOOKSHELVES.values()
            if bookshelf_id != -1 and bookshelf_id not in shelf_ids:
                raise ValueError
        except ValueError:
            return delegate.RawText(simplejson.dumps(
                {'error': 'Invalid bookshelf'}),
                                    content_type="application/json")

        if bookshelf_id == current_status or bookshelf_id == -1:
            work_bookshelf = Bookshelves.remove(username=username,
                                                work_id=work_id,
                                                bookshelf_id=current_status)

        else:
            edition_id = int(
                i.edition_id.split('/')[2][2:-1]) if i.edition_id else None
            work_bookshelf = Bookshelves.add(username=username,
                                             bookshelf_id=bookshelf_id,
                                             work_id=work_id,
                                             edition_id=edition_id)

        if i.redir:
            raise web.seeother(key)
        return delegate.RawText(simplejson.dumps(
            {'bookshelves_affected': work_bookshelf}),
                                content_type="application/json")
Beispiel #3
0
    def GET(self, work_id):
        from openlibrary.core.models import Bookshelves

        result = {'counts': {}}
        counts = Bookshelves.get_num_users_by_bookshelf_by_work_id(work_id)
        for (shelf_name, shelf_id) in Bookshelves.PRESET_BOOKSHELVES_JSON.items():
            result['counts'][shelf_name] = counts.get(shelf_id, 0)

        return simplejson.dumps(result)