Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / widgets.py @ 67cf14bf

History | View | Annotate | Download (2.8 kB)

1 db7fecd9 Sofia Papagiannaki
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 ee1de5e9 Kostas Papadimitriou
#
3 db7fecd9 Sofia Papagiannaki
# Redistribution and use in source and binary forms, with or
4 db7fecd9 Sofia Papagiannaki
# without modification, are permitted provided that the following
5 db7fecd9 Sofia Papagiannaki
# conditions are met:
6 ee1de5e9 Kostas Papadimitriou
#
7 db7fecd9 Sofia Papagiannaki
#   1. Redistributions of source code must retain the above
8 db7fecd9 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
9 db7fecd9 Sofia Papagiannaki
#      disclaimer.
10 ee1de5e9 Kostas Papadimitriou
#
11 db7fecd9 Sofia Papagiannaki
#   2. Redistributions in binary form must reproduce the above
12 db7fecd9 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
13 db7fecd9 Sofia Papagiannaki
#      disclaimer in the documentation and/or other materials
14 db7fecd9 Sofia Papagiannaki
#      provided with the distribution.
15 ee1de5e9 Kostas Papadimitriou
#
16 db7fecd9 Sofia Papagiannaki
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 db7fecd9 Sofia Papagiannaki
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 db7fecd9 Sofia Papagiannaki
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 db7fecd9 Sofia Papagiannaki
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 db7fecd9 Sofia Papagiannaki
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 db7fecd9 Sofia Papagiannaki
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 db7fecd9 Sofia Papagiannaki
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 db7fecd9 Sofia Papagiannaki
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 db7fecd9 Sofia Papagiannaki
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 db7fecd9 Sofia Papagiannaki
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 db7fecd9 Sofia Papagiannaki
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 db7fecd9 Sofia Papagiannaki
# POSSIBILITY OF SUCH DAMAGE.
28 ee1de5e9 Kostas Papadimitriou
#
29 db7fecd9 Sofia Papagiannaki
# The views and conclusions contained in the software and
30 db7fecd9 Sofia Papagiannaki
# documentation are those of the authors and should not be
31 db7fecd9 Sofia Papagiannaki
# interpreted as representing official policies, either expressed
32 db7fecd9 Sofia Papagiannaki
# or implied, of GRNET S.A.
33 db7fecd9 Sofia Papagiannaki
34 db7fecd9 Sofia Papagiannaki
import recaptcha.client.captcha as captcha
35 ee1de5e9 Kostas Papadimitriou
36 db7fecd9 Sofia Papagiannaki
from django import forms
37 ee1de5e9 Kostas Papadimitriou
from django.utils.safestring import mark_safe
38 ee1de5e9 Kostas Papadimitriou
from django.utils import simplejson as json
39 734107ef Kostas Papadimitriou
from synnefo_branding.utils import render_to_string
40 db7fecd9 Sofia Papagiannaki
41 989b28f3 Kostas Papadimitriou
from astakos.im.settings import RECAPTCHA_PUBLIC_KEY, RECAPTCHA_OPTIONS, \
42 5ce3ce4f Sofia Papagiannaki
    RECAPTCHA_USE_SSL
43 5ce3ce4f Sofia Papagiannaki
44 db7fecd9 Sofia Papagiannaki
45 db7fecd9 Sofia Papagiannaki
class RecaptchaWidget(forms.Widget):
46 db7fecd9 Sofia Papagiannaki
    """ A Widget which "renders" the output of captcha.displayhtml """
47 db7fecd9 Sofia Papagiannaki
    def render(self, *args, **kwargs):
48 ee1de5e9 Kostas Papadimitriou
        conf = RECAPTCHA_OPTIONS
49 ee1de5e9 Kostas Papadimitriou
        recaptcha_conf = ('<script type="text/javascript">'
50 5ce3ce4f Sofia Papagiannaki
                          'var RecaptchaOptions = %s'
51 5ce3ce4f Sofia Papagiannaki
                          '</script>') % json.dumps(conf)
52 5ce3ce4f Sofia Papagiannaki
        custom_widget_html = render_to_string("im/captcha.html",
53 5ce3ce4f Sofia Papagiannaki
                                              {'conf': 'Bob'})
54 5ce3ce4f Sofia Papagiannaki
        return mark_safe(recaptcha_conf +
55 5ce3ce4f Sofia Papagiannaki
                         custom_widget_html +
56 5ce3ce4f Sofia Papagiannaki
                         captcha.displayhtml(RECAPTCHA_PUBLIC_KEY,
57 5ce3ce4f Sofia Papagiannaki
                                             use_ssl=RECAPTCHA_USE_SSL))
58 5ce3ce4f Sofia Papagiannaki
59 db7fecd9 Sofia Papagiannaki
60 db7fecd9 Sofia Papagiannaki
class DummyWidget(forms.Widget):
61 db7fecd9 Sofia Papagiannaki
    """
62 db7fecd9 Sofia Papagiannaki
    A dummy Widget class for a placeholder input field which will
63 db7fecd9 Sofia Papagiannaki
    be created by captcha.displayhtml
64 db7fecd9 Sofia Papagiannaki

65 db7fecd9 Sofia Papagiannaki
    """
66 db7fecd9 Sofia Papagiannaki
    # make sure that labels are not displayed either
67 5ce3ce4f Sofia Papagiannaki
    is_hidden = True
68 5ce3ce4f Sofia Papagiannaki
69 db7fecd9 Sofia Papagiannaki
    def render(self, *args, **kwargs):
70 ee1de5e9 Kostas Papadimitriou
        return ''