Exemple #1
0
def get_recaptcha():
    def recaptcha_exempt():
        """Check to see if account is an admin, or more than two years old."""
        user = web.ctx.site.get_user()
        account = user and user.get_account()

        if not (user and account):
            return False

        if account.has_tag(
                "trusted-user") or user.is_admin() or user.is_librarian():
            return True

        create_dt = account.creation_time()
        now_dt = datetime.datetime.utcnow()
        delta = now_dt - create_dt
        return delta.days > 30

    def is_plugin_enabled(name):
        plugin_names = delegate.get_plugins()
        return name in plugin_names or "openlibrary.plugins." + name in plugin_names

    if is_plugin_enabled('recaptcha') and not recaptcha_exempt():
        public_key = config.plugin_recaptcha.public_key
        private_key = config.plugin_recaptcha.private_key
        return recaptcha.Recaptcha(public_key, private_key)
    else:
        return None
Exemple #2
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_plugin_active = is_plugin_enabled('recaptcha')
        if recap_plugin_active:
            public_key = config.plugin_recaptcha.public_key
            private_key = config.plugin_recaptcha.private_key
            recap = recaptcha.Recaptcha(public_key, private_key)

            if not recap.validate():
                return '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 web.seeother(work.url())
        except (ClientException, ValidationException), e:
            add_flash_message('error', str(e))
            return self.GET(key)
Exemple #3
0
    def POST(self):
        i = web.input('email', 'password', 'username', agreement="no")
        i.displayname = i.get('displayname') or i.username

        recap_plugin_active = 'recaptcha' in config.get('plugins')
        if recap_plugin_active:
            public_key = config.plugin_recaptcha.public_key
            private_key = config.plugin_recaptcha.private_key
            recap = recaptcha.Recaptcha(public_key, private_key)

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

        f = forms.Register()

        if not f.validates(i):
            return render['account/create'](f)

        if i.agreement != "yes":
            f.note = utils.get_error("account_create_tos_not_selected")
            return render['account/create'](f)

        try:
            accounts.register(username=i.username,
                              email=i.email,
                              password=i.password,
                              displayname=i.displayname)
        except ClientException, e:
            f.note = str(e)
            return render['account/create'](f)
Exemple #4
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_plugin_active = is_plugin_enabled('recaptcha')

        #check to see if account is more than two years old
        old_user = False
        user = web.ctx.site.get_user()
        account = user and user.get_account()
        if account:
            create_dt = account.creation_time()
            now_dt = datetime.datetime.utcnow()
            delta = now_dt - create_dt
            if delta.days > 365 * 2:
                old_user = True

        if recap_plugin_active and not old_user:
            public_key = config.plugin_recaptcha.public_key
            private_key = config.plugin_recaptcha.private_key
            recap = recaptcha.Recaptcha(public_key, private_key)

            if not recap.validate():
                return '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 web.seeother(edition.url())
        except (ClientException, ValidationException), e:
            add_flash_message('error', str(e))
            return self.GET(key)
Exemple #5
0
    def GET(self, key):
        i = web.input(v=None)
        v = i.v and safeint(i.v, None)

        if not web.ctx.site.can_write(key):
            return render_template("permission_denied", web.ctx.fullpath,
                                   "Permission denied to edit " + key + ".")

        edition = web.ctx.site.get(key, v)
        if edition is None:
            raise web.notfound()

        work = edition.works and edition.works[0]

        if not work:
            # HACK: create dummy work when work is not available to make edit form work
            work = web.ctx.site.new(
                '', {
                    'key':
                    '',
                    'type': {
                        'key': '/type/work'
                    },
                    'title':
                    edition.title,
                    'authors': [{
                        'type': '/type/author_role',
                        'author': {
                            'key': a['key']
                        }
                    } for a in edition.get('authors', [])]
                })

        recap_plugin_active = is_plugin_enabled('recaptcha')

        #check to see if account is more than two years old
        old_user = False
        user = web.ctx.site.get_user()
        account = user and user.get_account()
        if account:
            create_dt = account.creation_time()
            now_dt = datetime.datetime.utcnow()
            delta = now_dt - create_dt
            if delta.days > 365 * 2:
                old_user = True

        if recap_plugin_active and not old_user:
            public_key = config.plugin_recaptcha.public_key
            private_key = config.plugin_recaptcha.private_key
            recap = recaptcha.Recaptcha(public_key, private_key)
        else:
            recap = None

        return render_template('books/edit', work, edition, recaptcha=recap)
Exemple #6
0
    def GET(self):
        f = forms.Register()

        recap_plugin_active = 'recaptcha' in config.get('plugins')
        if recap_plugin_active:
            public_key = config.plugin_recaptcha.public_key
            private_key = config.plugin_recaptcha.private_key
            recap = recaptcha.Recaptcha(public_key, private_key)
        else:
            recap = None

        return render['account/create'](f, recaptcha=recap)
Exemple #7
0
    def POST(self):
        i = web.input(title="",
                      author_name="",
                      author_key="",
                      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.')

        recap_plugin_active = is_plugin_enabled('recaptcha')
        if recap_plugin_active and not web.ctx.site.get_user():
            public_key = config.plugin_recaptcha.public_key
            private_key = config.plugin_recaptcha.private_key
            recap = recaptcha.Recaptcha(public_key, private_key)

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

        saveutil = DocSaveHelper()

        match = self.find_matches(saveutil, i)

        if i._test == "true" and not isinstance(match, list):
            if match:
                return 'Matched <a href="%s">%s</a>' % (match.key, match.key)
            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
            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)
Exemple #8
0
    def GET(self):

        if not self.has_permission():
            return render_template("permission_denied", "/books/add", "Permission denied to add a book to Open Library.")

        i = web.input(work=None, author=None)
        work = i.work and web.ctx.site.get(i.work)
        author = i.author and web.ctx.site.get(i.author)

        recap_plugin_active = 'recaptcha' in config.get('plugins')
        if recap_plugin_active:
            public_key = config.plugin_recaptcha.public_key
            private_key = config.plugin_recaptcha.private_key
            recap = recaptcha.Recaptcha(public_key, private_key)
        else:
            recap = None

        return render_template('books/add', work=work, author=author, recaptcha=recap)
Exemple #9
0
    def GET(self, key):
        i = web.input(v=None, _method="GET")
        v = i.v and safeint(i.v, None)

        if not web.ctx.site.can_write(key):
            return render_template("permission_denied", web.ctx.fullpath, "Permission denied to edit " + key + ".")

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

        recap_plugin_active = 'recaptcha' in config.get('plugins')
        if recap_plugin_active:
            public_key = config.plugin_recaptcha.public_key
            private_key = config.plugin_recaptcha.private_key
            recap = recaptcha.Recaptcha(public_key, private_key)
        else:
            recap = None

        return render_template('books/edit', work, recaptcha=recap)
def get_recaptcha():
    def is_old_user():
        """Check to see if account is more than two years old."""
        user = web.ctx.site.get_user()
        account = user and user.get_account()
        if not account:
            return False
        create_dt = account.creation_time()
        now_dt = datetime.datetime.utcnow()
        delta = now_dt - create_dt
        return delta.days > 365 * 2

    def is_plugin_enabled(name):
        plugin_names = delegate.get_plugins()
        return name in plugin_names or "openlibrary.plugins." + name in plugin_names

    if is_plugin_enabled('recaptcha') and not is_old_user():
        public_key = config.plugin_recaptcha.public_key
        private_key = config.plugin_recaptcha.private_key
        recap = recaptcha.Recaptcha(public_key, private_key)
    else:
        recap = None
    return recap
Exemple #11
0
 def get_recap(self):
     if self.is_plugin_enabled('recaptcha'):
         public_key = config.plugin_recaptcha.public_key
         private_key = config.plugin_recaptcha.private_key
         return recaptcha.Recaptcha(public_key, private_key)