コード例 #1
0
    def POST(self, key):
        i = web.input(v=None, _method="GET")

        if spamcheck.is_spam():
            return render_template(
                "message.html", "Oops", 'Something went wrong. Please try again later.'
            )

        recap = get_recaptcha()
        if recap and not recap.validate():
            return render_template(
                "message.html",
                'Recaptcha solution was incorrect',
                'Please <a href="javascript:history.back()">go back</a> and try again.',
            )
        v = i.v and safeint(i.v, None)
        edition = web.ctx.site.get(key, v)

        if edition is None:
            raise web.notfound()
        if edition.works:
            work = edition.works[0]
        else:
            work = None

        add = (
            edition.revision == 1
            and work
            and work.revision == 1
            and work.edition_count == 1
        )

        try:
            helper = SaveBookHelper(work, edition)
            helper.save(web.input())

            if add:
                add_flash_message("info", utils.get_message("flash_book_added"))
            else:
                add_flash_message("info", utils.get_message("flash_book_updated"))

            raise safe_seeother(edition.url())
        except ClientException as e:
            add_flash_message('error', e.args[-1] or e.json)
            return self.GET(key)
        except ValidationException as e:
            add_flash_message('error', str(e))
            return self.GET(key)
コード例 #2
0
ファイル: addbook.py プロジェクト: lephemere/openlibrary
    def POST(self, key):
        i = web.input(v=None, _method="GET")

        if spamcheck.is_spam():
            return render_template(
                "message.html", "Oops",
                'Something went wrong. Please try again later.')

        recap = get_recaptcha()

        if recap and not recap.validate():
            return render_template(
                "message.html",
                'Recaptcha solution was incorrect',
                'Please <a href="javascript:history.back()">go back</a> and try again.',
            )

        v = i.v and safeint(i.v, None)
        work = web.ctx.site.get(key, v)
        if work is None:
            raise web.notfound()

        try:
            helper = SaveBookHelper(work, None)
            helper.save(web.input())
            add_flash_message("info", utils.get_message("flash_work_updated"))
            raise safe_seeother(work.url())
        except (ClientException, ValidationException) as e:
            add_flash_message('error', str(e))
            return self.GET(key)
コード例 #3
0
    def no_match(self, saveutil, i):
        """
        Action to take when no matches are found.
        Creates and saves both a Work and Edition.
        Redirects the user to the work/edition edit page
        in `add-work` mode.

        :param DocSaveHelper saveutil:
        :param web.utils.Storage i:
        :rtype: None
        """
        # Any new author has been created and added to
        # saveutil, and author_key added to i
        work = new_doc("/type/work",
            title=i.title,
            authors=i.authors
        )

        edition = self._make_edition(work, i)

        saveutil.save(work)
        saveutil.save(edition)

        comment = utils.get_message("comment_add_book")
        saveutil.commit(action="add-book", comment=comment)

        raise web.seeother(edition.url("/edit?mode=add-work"))
コード例 #4
0
ファイル: addbook.py プロジェクト: lephemere/openlibrary
    def work_match(self, saveutil, work, i):
        """
        Action for when a work, but not edition, is matched.
        Saves a new edition of work, created form the formdata i.
        Redirects the user to the newly created edition page in edit
        mode to add more details.

        :param DocSaveHelper saveutil:
        :param Work work: the matched work for this book
        :param web.utils.Storage i: user supplied book formdata
        :rtype: None
        """
        edition = self._make_edition(work, i)

        saveutil.save(edition)
        comment = utils.get_message("comment_add_book")
        saveutil.commit(comment=comment, action="add-book")

        raise safe_seeother(edition.url("/edit?mode=add-book"))