Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.3 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 672d445a Sofia Papagiannaki
from ratelimit.decorators import ratelimit
47 672d445a Sofia Papagiannaki
48 5ce3ce4f Sofia Papagiannaki
retries = RATELIMIT_RETRIES_ALLOWED - 1
49 5ce3ce4f Sofia Papagiannaki
rate = str(retries) + '/m'
50 5ce3ce4f Sofia Papagiannaki
51 64cd4730 Antony Chazapis
52 9a06d96f Olga Brani
@require_http_methods(["GET", "POST"])
53 12550ed2 Sofia Papagiannaki
@csrf_exempt
54 15efc749 Sofia Papagiannaki
@requires_anonymous
55 672d445a Sofia Papagiannaki
@ratelimit(field='username', method='POST', rate=rate)
56 1e685275 Sofia Papagiannaki
def login(request, on_failure='im/login.html'):
57 0905ccd2 Sofia Papagiannaki
    """
58 b90b602c Sofia Papagiannaki
    on_failure: the template name to render on login failure
59 0905ccd2 Sofia Papagiannaki
    """
60 672d445a Sofia Papagiannaki
    was_limited = getattr(request, 'limited', False)
61 aab4d540 Sofia Papagiannaki
    form = LoginForm(data=request.POST,
62 5ce3ce4f Sofia Papagiannaki
                     was_limited=was_limited,
63 9a06d96f Olga Brani
                     request=request)
64 c9e0b7e8 Sofia Papagiannaki
    next = get_query(request).get('next', '')
65 0905ccd2 Sofia Papagiannaki
    if not form.is_valid():
66 0905ccd2 Sofia Papagiannaki
        return render_to_response(on_failure,
67 5ce3ce4f Sofia Papagiannaki
                                  {'login_form': form,
68 5ce3ce4f Sofia Papagiannaki
                                   'next': next},
69 0905ccd2 Sofia Papagiannaki
                                  context_instance=RequestContext(request))
70 890b0eaf Sofia Papagiannaki
    # get the user from the cash
71 890b0eaf Sofia Papagiannaki
    user = form.user_cache
72 5ce3ce4f Sofia Papagiannaki
73 890b0eaf Sofia Papagiannaki
    message = None
74 0905ccd2 Sofia Papagiannaki
    if not user:
75 0905ccd2 Sofia Papagiannaki
        message = _('Cannot authenticate account')
76 0905ccd2 Sofia Papagiannaki
    elif not user.is_active:
77 0905ccd2 Sofia Papagiannaki
        message = _('Inactive account')
78 890b0eaf Sofia Papagiannaki
    if message:
79 24406ae3 Sofia Papagiannaki
        messages.error(request, message)
80 0905ccd2 Sofia Papagiannaki
        return render_to_response(on_failure,
81 5ce3ce4f Sofia Papagiannaki
                                  {'form': form},
82 0905ccd2 Sofia Papagiannaki
                                  context_instance=RequestContext(request))
83 5ce3ce4f Sofia Papagiannaki
84 64cd4730 Antony Chazapis
    return prepare_response(request, user, next)