Statistics
| Branch: | Tag: | Revision:

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

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