Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / target / local.py @ 674f9a52

History | View | Annotate | Download (3.4 kB)

1 aba1e498 Antony Chazapis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 64cd4730 Antony Chazapis
#
3 64cd4730 Antony Chazapis
# Redistribution and use in source and binary forms, with or
4 64cd4730 Antony Chazapis
# without modification, are permitted provided that the following
5 64cd4730 Antony Chazapis
# conditions are met:
6 64cd4730 Antony Chazapis
#
7 64cd4730 Antony Chazapis
#   1. Redistributions of source code must retain the above
8 64cd4730 Antony Chazapis
#      copyright notice, this list of conditions and the following
9 64cd4730 Antony Chazapis
#      disclaimer.
10 64cd4730 Antony Chazapis
#
11 64cd4730 Antony Chazapis
#   2. Redistributions in binary form must reproduce the above
12 64cd4730 Antony Chazapis
#      copyright notice, this list of conditions and the following
13 64cd4730 Antony Chazapis
#      disclaimer in the documentation and/or other materials
14 64cd4730 Antony Chazapis
#      provided with the distribution.
15 64cd4730 Antony Chazapis
#
16 64cd4730 Antony Chazapis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 64cd4730 Antony Chazapis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 64cd4730 Antony Chazapis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 64cd4730 Antony Chazapis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 64cd4730 Antony Chazapis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 64cd4730 Antony Chazapis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 64cd4730 Antony Chazapis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 64cd4730 Antony Chazapis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 64cd4730 Antony Chazapis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 64cd4730 Antony Chazapis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 64cd4730 Antony Chazapis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 64cd4730 Antony Chazapis
# POSSIBILITY OF SUCH DAMAGE.
28 64cd4730 Antony Chazapis
#
29 64cd4730 Antony Chazapis
# The views and conclusions contained in the software and
30 64cd4730 Antony Chazapis
# documentation are those of the authors and should not be
31 64cd4730 Antony Chazapis
# interpreted as representing official policies, either expressed
32 64cd4730 Antony Chazapis
# or implied, of GRNET S.A.
33 64cd4730 Antony Chazapis
34 0905ccd2 Sofia Papagiannaki
from django.shortcuts import render_to_response
35 0905ccd2 Sofia Papagiannaki
from django.template import RequestContext
36 890b0eaf Sofia Papagiannaki
from django.contrib import messages
37 0905ccd2 Sofia Papagiannaki
from django.utils.translation import ugettext as _
38 12550ed2 Sofia Papagiannaki
from django.views.decorators.csrf import csrf_exempt
39 9a06d96f Olga Brani
from django.views.decorators.http import require_http_methods
40 64cd4730 Antony Chazapis
41 c9e0b7e8 Sofia Papagiannaki
from astakos.im.util import prepare_response, get_query
42 63ecdd20 Sofia Papagiannaki
from astakos.im.views import requires_anonymous
43 5ed6816e Sofia Papagiannaki
from astakos.im.forms import LoginForm
44 672d445a Sofia Papagiannaki
from astakos.im.settings import RATELIMIT_RETRIES_ALLOWED
45 672d445a Sofia Papagiannaki
46 674f9a52 Sofia Papagiannaki
import astakos.im.messages as astakos_messages
47 674f9a52 Sofia Papagiannaki
48 672d445a Sofia Papagiannaki
from ratelimit.decorators import ratelimit
49 672d445a Sofia Papagiannaki
50 5ce3ce4f Sofia Papagiannaki
retries = RATELIMIT_RETRIES_ALLOWED - 1
51 5ce3ce4f Sofia Papagiannaki
rate = str(retries) + '/m'
52 5ce3ce4f Sofia Papagiannaki
53 64cd4730 Antony Chazapis
54 9a06d96f Olga Brani
@require_http_methods(["GET", "POST"])
55 12550ed2 Sofia Papagiannaki
@csrf_exempt
56 15efc749 Sofia Papagiannaki
@requires_anonymous
57 672d445a Sofia Papagiannaki
@ratelimit(field='username', method='POST', rate=rate)
58 1e685275 Sofia Papagiannaki
def login(request, on_failure='im/login.html'):
59 0905ccd2 Sofia Papagiannaki
    """
60 b90b602c Sofia Papagiannaki
    on_failure: the template name to render on login failure
61 0905ccd2 Sofia Papagiannaki
    """
62 672d445a Sofia Papagiannaki
    was_limited = getattr(request, 'limited', False)
63 aab4d540 Sofia Papagiannaki
    form = LoginForm(data=request.POST,
64 5ce3ce4f Sofia Papagiannaki
                     was_limited=was_limited,
65 9a06d96f Olga Brani
                     request=request)
66 c9e0b7e8 Sofia Papagiannaki
    next = get_query(request).get('next', '')
67 0905ccd2 Sofia Papagiannaki
    if not form.is_valid():
68 0905ccd2 Sofia Papagiannaki
        return render_to_response(on_failure,
69 5ce3ce4f Sofia Papagiannaki
                                  {'login_form': form,
70 5ce3ce4f Sofia Papagiannaki
                                   'next': next},
71 0905ccd2 Sofia Papagiannaki
                                  context_instance=RequestContext(request))
72 890b0eaf Sofia Papagiannaki
    # get the user from the cash
73 890b0eaf Sofia Papagiannaki
    user = form.user_cache
74 5ce3ce4f Sofia Papagiannaki
75 890b0eaf Sofia Papagiannaki
    message = None
76 0905ccd2 Sofia Papagiannaki
    if not user:
77 674f9a52 Sofia Papagiannaki
        message = _(astakos_messages.ACCOUNT_AUTHENTICATION_FAILED)
78 0905ccd2 Sofia Papagiannaki
    elif not user.is_active:
79 674f9a52 Sofia Papagiannaki
        message = _(astakos_messages.ACCOUNT_INACTIVE)
80 890b0eaf Sofia Papagiannaki
    if message:
81 24406ae3 Sofia Papagiannaki
        messages.error(request, message)
82 0905ccd2 Sofia Papagiannaki
        return render_to_response(on_failure,
83 5ce3ce4f Sofia Papagiannaki
                                  {'form': form},
84 0905ccd2 Sofia Papagiannaki
                                  context_instance=RequestContext(request))
85 5ce3ce4f Sofia Papagiannaki
86 64cd4730 Antony Chazapis
    return prepare_response(request, user, next)