Mercurial > hatta-flask
changeset 2363:73826ed2d401
Add support for ReCAPTCHA
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Tue, 30 Oct 2012 09:49:57 +0100 |
| parents | 863b3720e327 |
| children | 972b31f5f9f5 |
| files | hatta/config.py hatta/page.py hatta/templates/edit_file.html hatta/templates/edit_text.html hatta/wiki.py |
| diffstat | 5 files changed, 52 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/hatta/config.py Sun Oct 14 22:32:45 2012 +0200 +++ b/hatta/config.py Tue Oct 30 09:49:57 2012 +0100 @@ -61,6 +61,12 @@ _add('-U', '--unix-eol', dest='unix_eol', action="store_true", help='Convert all text pages to UNIX-style CR newlines') +_add('', '--recaptcha-public-key', dest='recaptcha_public_key', + metavar='KEY', + help='A public key KEY for ReCAPTCHA service.') +_add('', '--recaptcha-private-key', dest='recaptcha_private_key', + metavar='KEY', + help='A private key KEY for ReCAPTCHA service.') class WikiConfig(object):
--- a/hatta/page.py Sun Oct 14 22:32:45 2012 +0200 +++ b/hatta/page.py Tue Oct 30 09:49:57 2012 +0100 @@ -19,6 +19,12 @@ except ImportError: pass +captcha = None +try: + from recaptcha.client import captcha +except ImportError: + pass + Image = None try: import Image @@ -237,7 +243,7 @@ dependencies.add(etag) return dependencies - def render_editor(self, preview=None): + def render_editor(self, preview=None, captcha_error=None): _ = self.wiki.gettext author = self.request.get_author() if self.title in self.storage: @@ -249,7 +255,13 @@ else: comment = _(u'uploaded') rev = -1 + if captcha and self.wiki.recaptcha_public_key: + recaptcha_html = captcha.displayhtml( + self.wiki.recaptcha_public_key, error=captcha_error) + else: + recaptcha_html = None return self.template('edit_file.html', comment=comment, + recaptcha_html=recaptcha_html, author=author, parent=rev) @@ -284,7 +296,7 @@ lines = self.storage.page_lines(f) return self.content_iter(lines) - def render_editor(self, preview=None): + def render_editor(self, preview=None, captcha_error=None): """Generate the HTML for the editor.""" _ = self.wiki.gettext @@ -306,8 +318,14 @@ if preview: lines = preview comment = self.request.form.get('comment', comment) + if captcha and self.wiki.recaptcha_public_key: + recaptcha_html = captcha.displayhtml( + self.wiki.recaptcha_public_key, error=captcha_error) + else: + recaptcha_html = None return self.template('edit_text.html', comment=comment, preview=preview, + recaptcha_html=recaptcha_html, author=author, parent=rev, lines=lines) def diff_content(self, from_text, to_text, message=u''):
--- a/hatta/templates/edit_file.html Sun Oct 14 22:32:45 2012 +0200 +++ b/hatta/templates/edit_file.html Tue Oct 30 09:49:57 2012 +0100 @@ -14,6 +14,9 @@ name="comment" value="{{ comment }}"></label> <label id="hatta-author">{{ _("Author") }} <input name="author" value="{{ author }}"></label> + {% if recaptcha_html %} + <div id="hatta-recaptcha">{{ recaptcha_html|safe }}</div> + {% endif %} <div id="hatta-buttons"> <input type="submit" name="save" value="{{ _("Save") }}"> <input type="submit" name="cancel" value="{{ _("Cancel") }}">
--- a/hatta/templates/edit_text.html Sun Oct 14 22:32:45 2012 +0200 +++ b/hatta/templates/edit_text.html Tue Oct 30 09:49:57 2012 +0100 @@ -12,6 +12,9 @@ name="comment" value="{{ comment }}"></label> <label id="hatta-author">{{ _("Author") }} <input name="author" value="{{ author }}"></label> + {% if recaptcha_html %} + <div id="hatta-recaptcha">{{ recaptcha_html|safe }}</div> + {% endif %} <div id="hatta-buttons"> <input type="submit" name="save" value="{{ _("Save") }}"> <input type="submit" name="preview" value="{{ _("Preview") }}">
--- a/hatta/wiki.py Sun Oct 14 22:32:45 2012 +0200 +++ b/hatta/wiki.py Tue Oct 30 09:49:57 2012 +0100 @@ -19,6 +19,12 @@ except ImportError: pass +captcha = None +try: + from recaptcha.client import captcha +except ImportError: + pass + import hatta from hatta import error from hatta import page @@ -246,6 +252,10 @@ self.subdirectories = self.config.get_bool('subdirectories', False) self.extension = self.config.get('extension', None) self.unix_eol = self.config.get_bool('unix_eol', False) + self.recaptcha_public_key = self.config.get( + 'recaptcha_public_key', None) + self.recaptcha_private_key = self.config.get( + 'recaptcha_private_key', None) if self.subdirectories: self.storage = storage.WikiSubdirectoryStorage(self.path, self.page_charset, self.gettext, self.unix_eol, self.extension, @@ -416,6 +426,14 @@ _(u'No preview for binaries.')))] return self.edit(request, title, preview=lines) elif request.form.get('save'): + if captcha and self.recaptcha_private_key: + response = captcha.submit( + request.form.get('recaptcha_challenge_field', ''), + request.form.get('recaptcha_response_field', ''), + self.recaptcha_private_key, request.remote_addr) + if not response.is_valid: + return self.edit(request, title, + captcha_error=response.error_code) comment = request.form.get("comment", "") author = request.get_author() text = request.form.get("text") @@ -462,13 +480,13 @@ return response @URL('/+edit/<title:title>', methods=['GET']) - def edit(self, request, title, preview=None): + def edit(self, request, title, preview=None, captcha_error=None): self._check_lock(title) exists = title in self.storage if exists: self.storage.reopen() page = self.get_page(request, title) - html = page.render_editor(preview) + html = page.render_editor(preview, captcha_error) if not exists: response = WikiResponse(html, mimetype="text/html", status='404 Not found')
