Esempio n. 1
0
 def process_input(self, i):
     i = utils.unflatten(i)
     if 'author' in i:
         author = trim_doc(i.author)
         alternate_names = author.get('alternate_names', None) or ''
         author.alternate_names = [name.strip() for name in alternate_names.replace("\n", ";").split(';') if name.strip()]
         author.links = author.get('links') or []
         return author
Esempio n. 2
0
 def POST(self):
     from openlibrary.plugins.upstream.utils import unflatten
     i = unflatten(web.input())
     
     page = web.ctx.get("/admin/block") or web.ctx.site.new("/admin/block", {"key": "/admin/block", "type": "/type/object"})
     ips = [d for d in i.ips if d.get('ip')]
     page.ips = ips
     page._save("update blocked IPs")
     add_flash_message("info", "Saved!")
     raise web.seeother("/admin/block")
Esempio n. 3
0
    def POST(self):
        i = web.input(
            title="",
            publisher="",
            publish_date="",
            id_name="",
            id_value="",
            _test="false",
        )

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

        if not web.ctx.site.get_user():
            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.',
                )

        i = utils.unflatten(i)
        saveutil = DocSaveHelper()
        created_author = saveutil.create_authors_from_form_data(
            i.authors, i.author_names, _test=i._test == 'true'
        )
        match = None if created_author else self.find_matches(i)

        if i._test == 'true' and not isinstance(match, list):
            if match:
                return f'Matched <a href="{match.key}">{match.key}</a>'
            else:
                return 'No match found'

        if isinstance(match, list):
            # multiple matches
            return render_template('books/check', i, match)

        elif match and match.key.startswith('/books'):
            # work match and edition match, match is an Edition
            return self.work_edition_match(match)

        elif match and match.key.startswith('/works'):
            # work match but not edition
            work = match
            return self.work_match(saveutil, work, i)
        else:
            # no match
            return self.no_match(saveutil, i)
Esempio n. 4
0
    def save(self, formdata):
        """
        Update work and edition documents according to the specified formdata.
        :param web.storage formdata:
        :rtype: None
        """
        comment = formdata.pop('_comment', '')

        user = accounts.get_current_user()
        delete = (user and (user.is_admin() or user.is_librarian())
                  and formdata.pop('_delete', ''))

        formdata = utils.unflatten(formdata)
        work_data, edition_data = self.process_input(formdata)

        self.process_new_fields(formdata)

        saveutil = DocSaveHelper()

        if delete:
            if self.edition:
                self.delete(self.edition.key, comment=comment)

            if self.work and self.work.edition_count == 0:
                self.delete(self.work.key, comment=comment)
            return

        just_editing_work = edition_data is None
        if work_data:
            # Create any new authors that were added
            saveutil.create_authors_from_form_data(
                work_data.get("authors") or [],
                formdata.get('authors') or [])

            if not just_editing_work:
                # Handle orphaned editions
                new_work_key = (edition_data.get('works') or [{
                    'key': None
                }])[0]['key']
                if self.work is None and (new_work_key is None
                                          or new_work_key == '__new__'):
                    # i.e. not moving to another work, create empty work
                    self.work = self.new_work(self.edition)
                    edition_data.works = [{'key': self.work.key}]
                    work_data.key = self.work.key
                elif self.work is not None and new_work_key is None:
                    # we're trying to create an orphan; let's not do that
                    edition_data.works = [{'key': self.work.key}]

            if self.work is not None:
                self.work.update(work_data)
                saveutil.save(self.work)

        if self.edition and edition_data:
            # Create a new work if so desired
            new_work_key = (edition_data.get('works') or [{
                'key': None
            }])[0]['key']
            if new_work_key == "__new__" and self.work is not None:
                self.work = self.new_work(self.edition)
                edition_data.works = [{'key': self.work.key}]
                saveutil.save(self.work)

            identifiers = edition_data.pop('identifiers', [])
            self.edition.set_identifiers(identifiers)

            classifications = edition_data.pop('classifications', [])
            self.edition.set_classifications(classifications)

            self.edition.set_physical_dimensions(
                edition_data.pop('physical_dimensions', None))
            self.edition.set_weight(edition_data.pop('weight', None))
            self.edition.set_toc_text(edition_data.pop('table_of_contents',
                                                       ''))

            if edition_data.pop('translation', None) != 'yes':
                edition_data.translation_of = None
                edition_data.translated_from = None

            if 'contributors' not in edition_data:
                self.edition.contributors = []

            self.edition.update(edition_data)
            saveutil.save(self.edition)

        saveutil.commit(comment=comment, action="edit-book")