Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / views / im.py @ f6ff3033

History | View | Annotate | Download (34.7 kB)

1 1808f7bc Giorgos Korfiatis
# Copyright 2011, 2012, 2013 GRNET S.A. All rights reserved.
2 176023aa Kostas Papadimitriou
#
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 176023aa Kostas Papadimitriou
#
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 176023aa Kostas Papadimitriou
#
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 176023aa Kostas Papadimitriou
#
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 176023aa Kostas Papadimitriou
#
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 64cd4730 Antony Chazapis
import logging
35 9a06d96f Olga Brani
import inflect
36 9a06d96f Olga Brani
37 9a06d96f Olga Brani
engine = inflect.engine()
38 64cd4730 Antony Chazapis
39 0905ccd2 Sofia Papagiannaki
from urllib import quote
40 a5cef8d0 Kostas Papadimitriou
41 c630fee6 Kostas Papadimitriou
from django.shortcuts import get_object_or_404
42 890b0eaf Sofia Papagiannaki
from django.contrib import messages
43 0be81d73 Sofia Papagiannaki
from django.contrib.auth.models import User
44 01ac12d5 Sofia Papagiannaki
from django.core.urlresolvers import reverse
45 01ac12d5 Sofia Papagiannaki
from django.db import transaction
46 3e0a032d Sofia Papagiannaki
from django.http import HttpResponse, HttpResponseRedirect, Http404
47 01ac12d5 Sofia Papagiannaki
from django.shortcuts import redirect
48 01ac12d5 Sofia Papagiannaki
from django.utils.translation import ugettext as _
49 3e0a032d Sofia Papagiannaki
from django.core.exceptions import PermissionDenied
50 9a06d96f Olga Brani
from django.views.decorators.http import require_http_methods
51 96b44c04 Sofia Papagiannaki
from django.utils import simplejson as json
52 1ca02d1a Kostas Papadimitriou
from django.template import RequestContext
53 1ca02d1a Kostas Papadimitriou
54 1ca02d1a Kostas Papadimitriou
from synnefo_branding import utils as branding
55 1ca02d1a Kostas Papadimitriou
from synnefo_branding import settings as branding_settings
56 734107ef Kostas Papadimitriou
57 c630fee6 Kostas Papadimitriou
import astakos.im.messages as astakos_messages
58 c630fee6 Kostas Papadimitriou
59 e7cb4085 Kostas Papadimitriou
from astakos.im import activation_backends
60 3e0a032d Sofia Papagiannaki
from astakos.im.models import AstakosUser, ApprovalTerms, EmailChange, \
61 bea584e1 Giorgos Korfiatis
    AstakosUserAuthProvider, PendingThirdPartyUser, Component
62 3e0a032d Sofia Papagiannaki
from astakos.im.util import get_context, prepare_response, get_query, \
63 3e0a032d Sofia Papagiannaki
    restrict_next
64 3e0a032d Sofia Papagiannaki
from astakos.im.forms import LoginForm, InvitationForm, FeedbackForm, \
65 3e0a032d Sofia Papagiannaki
    SignApprovalTermsForm, EmailChangeForm
66 f7400729 Kostas Papadimitriou
from astakos.im.forms import ExtendedProfileForm as ProfileForm
67 1ca02d1a Kostas Papadimitriou
from synnefo.lib.services import get_public_endpoint
68 3e0a032d Sofia Papagiannaki
from astakos.im.functions import send_feedback, logout as auth_logout, \
69 3e0a032d Sofia Papagiannaki
    invite as invite_func
70 e7cb4085 Kostas Papadimitriou
from astakos.im import settings
71 8998f09a Sofia Papagiannaki
from astakos.im import presentation
72 9d20fe23 Kostas Papadimitriou
from astakos.im import auth_providers as auth
73 4e03ba30 Kostas Papadimitriou
from astakos.im import quotas
74 70e11eaa Sofia Papagiannaki
from astakos.im.views.util import render_response, _resources_catalog
75 70e11eaa Sofia Papagiannaki
from astakos.im.views.decorators import cookie_fix, signed_terms_required,\
76 b6496f0c Kostas Papadimitriou
    required_auth_methods_assigned, valid_astakos_user_required, login_required
77 ae497612 Olga Brani
78 e015e9e6 Sofia Papagiannaki
logger = logging.getLogger(__name__)
79 e015e9e6 Sofia Papagiannaki
80 9a06d96f Olga Brani
81 9a06d96f Olga Brani
@require_http_methods(["GET", "POST"])
82 222305b7 Sofia Papagiannaki
@cookie_fix
83 270dd48d Sofia Papagiannaki
@signed_terms_required
84 1ecda536 Kostas Papadimitriou
def login(request, template_name='im/login.html', extra_context=None):
85 890b0eaf Sofia Papagiannaki
    """
86 1ecda536 Kostas Papadimitriou
    Renders login page.
87 176023aa Kostas Papadimitriou

88 890b0eaf Sofia Papagiannaki
    **Arguments**
89 176023aa Kostas Papadimitriou

90 1ecda536 Kostas Papadimitriou
    ``template_name``
91 dcf55476 Sofia Papagiannaki
        A custom login template to use. This is optional; if not specified,
92 1e685275 Sofia Papagiannaki
        this will default to ``im/login.html``.
93 176023aa Kostas Papadimitriou

94 890b0eaf Sofia Papagiannaki
    ``extra_context``
95 890b0eaf Sofia Papagiannaki
        An dictionary of variables to add to the template context.
96 890b0eaf Sofia Papagiannaki
    """
97 1ecda536 Kostas Papadimitriou
98 1f3b4b39 Sofia Papagiannaki
    extra_context = extra_context or {}
99 792c2f3b Olga Brani
100 f7cf5257 Kostas Papadimitriou
    third_party_token = request.GET.get('key', False)
101 f7cf5257 Kostas Papadimitriou
    if third_party_token:
102 f7cf5257 Kostas Papadimitriou
        messages.info(request, astakos_messages.AUTH_PROVIDER_LOGIN_TO_ADD)
103 f7cf5257 Kostas Papadimitriou
104 1ecda536 Kostas Papadimitriou
    if request.user.is_authenticated():
105 1ecda536 Kostas Papadimitriou
        return HttpResponseRedirect(reverse('landing'))
106 1ecda536 Kostas Papadimitriou
107 ef20ea07 Sofia Papagiannaki
    return render_response(
108 ef20ea07 Sofia Papagiannaki
        template_name,
109 1ecda536 Kostas Papadimitriou
        login_form=LoginForm(request=request),
110 1ecda536 Kostas Papadimitriou
        context_instance=get_context(request, extra_context)
111 ef20ea07 Sofia Papagiannaki
    )
112 5ce3ce4f Sofia Papagiannaki
113 64cd4730 Antony Chazapis
114 1ecda536 Kostas Papadimitriou
@require_http_methods(["GET", "POST"])
115 1ecda536 Kostas Papadimitriou
@cookie_fix
116 1ecda536 Kostas Papadimitriou
@signed_terms_required
117 1ecda536 Kostas Papadimitriou
def index(request, authenticated_redirect='landing',
118 1ecda536 Kostas Papadimitriou
          anonymous_redirect='login', extra_context=None):
119 1ecda536 Kostas Papadimitriou
    """
120 1ecda536 Kostas Papadimitriou
    If user is authenticated redirect to ``authenticated_redirect`` url.
121 1ecda536 Kostas Papadimitriou
    Otherwise redirects to ``anonymous_redirect`` url.
122 1ecda536 Kostas Papadimitriou

123 1ecda536 Kostas Papadimitriou
    """
124 1ecda536 Kostas Papadimitriou
    if request.user.is_authenticated():
125 1ecda536 Kostas Papadimitriou
        return HttpResponseRedirect(reverse(authenticated_redirect))
126 1ecda536 Kostas Papadimitriou
    return HttpResponseRedirect(reverse(anonymous_redirect))
127 1ecda536 Kostas Papadimitriou
128 1ecda536 Kostas Papadimitriou
129 0504f010 Kostas Papadimitriou
@require_http_methods(["POST"])
130 222305b7 Sofia Papagiannaki
@cookie_fix
131 0504f010 Kostas Papadimitriou
@valid_astakos_user_required
132 f6ff3033 Giorgos Korfiatis
@transaction.commit_on_success
133 0504f010 Kostas Papadimitriou
def update_token(request):
134 0504f010 Kostas Papadimitriou
    """
135 0504f010 Kostas Papadimitriou
    Update api token view.
136 0504f010 Kostas Papadimitriou
    """
137 f6ff3033 Giorgos Korfiatis
    user = AstakosUser.objects.select_for_update().get(id=request.user.id)
138 0504f010 Kostas Papadimitriou
    user.renew_token()
139 0504f010 Kostas Papadimitriou
    user.save()
140 0504f010 Kostas Papadimitriou
    messages.success(request, astakos_messages.TOKEN_UPDATED)
141 14c76abe Kostas Papadimitriou
    return HttpResponseRedirect(reverse('api_access'))
142 0504f010 Kostas Papadimitriou
143 0504f010 Kostas Papadimitriou
144 9a06d96f Olga Brani
@require_http_methods(["GET", "POST"])
145 222305b7 Sofia Papagiannaki
@cookie_fix
146 badcb2a9 Kostas Papadimitriou
@valid_astakos_user_required
147 554ef175 Christos Stavrakakis
@transaction.commit_on_success
148 aab4d540 Sofia Papagiannaki
def invite(request, template_name='im/invitations.html', extra_context=None):
149 890b0eaf Sofia Papagiannaki
    """
150 890b0eaf Sofia Papagiannaki
    Allows a user to invite somebody else.
151 176023aa Kostas Papadimitriou

152 8fb8d0cf Giorgos Korfiatis
    In case of GET request renders a form for providing the invitee
153 8fb8d0cf Giorgos Korfiatis
    information.
154 8fb8d0cf Giorgos Korfiatis
    In case of POST checks whether the user has not run out of invitations and
155 8fb8d0cf Giorgos Korfiatis
    then sends an invitation email to singup to the service.
156 176023aa Kostas Papadimitriou

157 554ef175 Christos Stavrakakis
    The number of the user invitations is going to be updated only if the email
158 554ef175 Christos Stavrakakis
    has been successfully sent.
159 176023aa Kostas Papadimitriou

160 2cbaacd5 Sofia Papagiannaki
    If the user isn't logged in, redirects to settings.LOGIN_URL.
161 176023aa Kostas Papadimitriou

162 890b0eaf Sofia Papagiannaki
    **Arguments**
163 176023aa Kostas Papadimitriou

164 890b0eaf Sofia Papagiannaki
    ``template_name``
165 890b0eaf Sofia Papagiannaki
        A custom template to use. This is optional; if not specified,
166 1e685275 Sofia Papagiannaki
        this will default to ``im/invitations.html``.
167 176023aa Kostas Papadimitriou

168 890b0eaf Sofia Papagiannaki
    ``extra_context``
169 890b0eaf Sofia Papagiannaki
        An dictionary of variables to add to the template context.
170 176023aa Kostas Papadimitriou

171 890b0eaf Sofia Papagiannaki
    **Template:**
172 176023aa Kostas Papadimitriou

173 1e685275 Sofia Papagiannaki
    im/invitations.html or ``template_name`` keyword argument.
174 176023aa Kostas Papadimitriou

175 890b0eaf Sofia Papagiannaki
    **Settings:**
176 176023aa Kostas Papadimitriou

177 890b0eaf Sofia Papagiannaki
    The view expectes the following settings are defined:
178 176023aa Kostas Papadimitriou

179 890b0eaf Sofia Papagiannaki
    * LOGIN_URL: login uri
180 890b0eaf Sofia Papagiannaki
    """
181 1f3b4b39 Sofia Papagiannaki
    extra_context = extra_context or {}
182 64cd4730 Antony Chazapis
    status = None
183 64cd4730 Antony Chazapis
    message = None
184 8f5a3a06 Sofia Papagiannaki
    form = InvitationForm()
185 5ce3ce4f Sofia Papagiannaki
186 18ffbee1 Sofia Papagiannaki
    inviter = request.user
187 64cd4730 Antony Chazapis
    if request.method == 'POST':
188 8f5a3a06 Sofia Papagiannaki
        form = InvitationForm(request.POST)
189 64cd4730 Antony Chazapis
        if inviter.invitations > 0:
190 8f5a3a06 Sofia Papagiannaki
            if form.is_valid():
191 554ef175 Christos Stavrakakis
                email = form.cleaned_data.get('username')
192 554ef175 Christos Stavrakakis
                realname = form.cleaned_data.get('realname')
193 554ef175 Christos Stavrakakis
                invite_func(inviter, email, realname)
194 554ef175 Christos Stavrakakis
                message = _(astakos_messages.INVITATION_SENT) % locals()
195 554ef175 Christos Stavrakakis
                messages.success(request, message)
196 64cd4730 Antony Chazapis
        else:
197 ae497612 Olga Brani
            message = _(astakos_messages.MAX_INVITATION_NUMBER_REACHED)
198 24406ae3 Sofia Papagiannaki
            messages.error(request, message)
199 176023aa Kostas Papadimitriou
200 a196eb7e Sofia Papagiannaki
    sent = [{'email': inv.username,
201 d6ae71a2 root
             'realname': inv.realname,
202 d6ae71a2 root
             'is_consumed': inv.is_consumed}
203 5ce3ce4f Sofia Papagiannaki
            for inv in request.user.invitations_sent.all()]
204 77e2ad52 root
    kwargs = {'inviter': inviter,
205 5ce3ce4f Sofia Papagiannaki
              'sent': sent}
206 0905ccd2 Sofia Papagiannaki
    context = get_context(request, extra_context, **kwargs)
207 0905ccd2 Sofia Papagiannaki
    return render_response(template_name,
208 5ce3ce4f Sofia Papagiannaki
                           invitation_form=form,
209 5ce3ce4f Sofia Papagiannaki
                           context_instance=context)
210 5ce3ce4f Sofia Papagiannaki
211 64cd4730 Antony Chazapis
212 9a06d96f Olga Brani
@require_http_methods(["GET", "POST"])
213 7763f5c6 Kostas Papadimitriou
@required_auth_methods_assigned()
214 7763f5c6 Kostas Papadimitriou
@login_required
215 7763f5c6 Kostas Papadimitriou
@cookie_fix
216 7763f5c6 Kostas Papadimitriou
@signed_terms_required
217 1ca02d1a Kostas Papadimitriou
def api_access_config(request, template_name='im/api_access_config.html',
218 1ca02d1a Kostas Papadimitriou
                      content_type='text/plain', extra_context=None,
219 1ca02d1a Kostas Papadimitriou
                      filename='.kamakirc'):
220 1ca02d1a Kostas Papadimitriou
221 1ca02d1a Kostas Papadimitriou
    if settings.KAMAKI_CONFIG_CLOUD_NAME:
222 1ca02d1a Kostas Papadimitriou
        cloud_name = settings.KAMAKI_CONFIG_CLOUD_NAME
223 1ca02d1a Kostas Papadimitriou
    else:
224 1ca02d1a Kostas Papadimitriou
        cloud_name = branding_settings.SERVICE_NAME.replace(' ', '_').lower()
225 1ca02d1a Kostas Papadimitriou
226 1ca02d1a Kostas Papadimitriou
    url = get_public_endpoint(settings.astakos_services, 'identity')
227 1ca02d1a Kostas Papadimitriou
228 1ca02d1a Kostas Papadimitriou
    context = {
229 1ca02d1a Kostas Papadimitriou
        'user': request.user,
230 1ca02d1a Kostas Papadimitriou
        'services': Component.catalog(),
231 1ca02d1a Kostas Papadimitriou
        'token_url': url,
232 1ca02d1a Kostas Papadimitriou
        'cloud_name': cloud_name
233 1ca02d1a Kostas Papadimitriou
    }
234 1ca02d1a Kostas Papadimitriou
235 1ca02d1a Kostas Papadimitriou
    extra_context = extra_context or {}
236 1ca02d1a Kostas Papadimitriou
    context.update(extra_context)
237 1ca02d1a Kostas Papadimitriou
    content = branding.render_to_string(template_name, context,
238 1ca02d1a Kostas Papadimitriou
                                        RequestContext(request))
239 967a99e1 Kostas Papadimitriou
    response = HttpResponse(content_type=content_type)
240 1ca02d1a Kostas Papadimitriou
    response.status_code = 200
241 1ca02d1a Kostas Papadimitriou
    response['Content-Disposition'] = 'attachment; filename="%s"' % filename
242 1ca02d1a Kostas Papadimitriou
    response.content = content
243 1ca02d1a Kostas Papadimitriou
    return response
244 1ca02d1a Kostas Papadimitriou
245 1ca02d1a Kostas Papadimitriou
246 1ca02d1a Kostas Papadimitriou
@required_auth_methods_assigned()
247 1ca02d1a Kostas Papadimitriou
@login_required
248 1ca02d1a Kostas Papadimitriou
@cookie_fix
249 1ca02d1a Kostas Papadimitriou
@signed_terms_required
250 7763f5c6 Kostas Papadimitriou
def api_access(request, template_name='im/api_access.html',
251 7763f5c6 Kostas Papadimitriou
               extra_context=None):
252 7763f5c6 Kostas Papadimitriou
    """
253 7763f5c6 Kostas Papadimitriou
    API access view.
254 7763f5c6 Kostas Papadimitriou
    """
255 7763f5c6 Kostas Papadimitriou
    context = {}
256 7763f5c6 Kostas Papadimitriou
257 3466f0a3 Kostas Papadimitriou
    url = get_public_endpoint(settings.astakos_services, 'identity')
258 7763f5c6 Kostas Papadimitriou
    context['services'] = Component.catalog()
259 3466f0a3 Kostas Papadimitriou
    context['token_url'] = url
260 31240d2c Olga Brani
    context['user'] = request.user
261 7763f5c6 Kostas Papadimitriou
    context['client_url'] = settings.API_CLIENT_URL
262 7763f5c6 Kostas Papadimitriou
263 7763f5c6 Kostas Papadimitriou
    if extra_context:
264 7763f5c6 Kostas Papadimitriou
        context.update(extra_context)
265 7763f5c6 Kostas Papadimitriou
    context_instance = get_context(request, context)
266 7763f5c6 Kostas Papadimitriou
    return render_response(template_name,
267 7763f5c6 Kostas Papadimitriou
                           context_instance=context_instance)
268 7763f5c6 Kostas Papadimitriou
269 7763f5c6 Kostas Papadimitriou
270 7763f5c6 Kostas Papadimitriou
@require_http_methods(["GET", "POST"])
271 9d20fe23 Kostas Papadimitriou
@required_auth_methods_assigned(allow_access=True)
272 890b0eaf Sofia Papagiannaki
@login_required
273 222305b7 Sofia Papagiannaki
@cookie_fix
274 270dd48d Sofia Papagiannaki
@signed_terms_required
275 f6ff3033 Giorgos Korfiatis
@transaction.commit_on_success
276 aab4d540 Sofia Papagiannaki
def edit_profile(request, template_name='im/profile.html', extra_context=None):
277 890b0eaf Sofia Papagiannaki
    """
278 890b0eaf Sofia Papagiannaki
    Allows a user to edit his/her profile.
279 176023aa Kostas Papadimitriou

280 890b0eaf Sofia Papagiannaki
    In case of GET request renders a form for displaying the user information.
281 ce86cd44 Sofia Papagiannaki
    In case of POST updates the user informantion and redirects to ``next``
282 ce86cd44 Sofia Papagiannaki
    url parameter if exists.
283 176023aa Kostas Papadimitriou

284 2cbaacd5 Sofia Papagiannaki
    If the user isn't logged in, redirects to settings.LOGIN_URL.
285 176023aa Kostas Papadimitriou

286 890b0eaf Sofia Papagiannaki
    **Arguments**
287 176023aa Kostas Papadimitriou

288 890b0eaf Sofia Papagiannaki
    ``template_name``
289 890b0eaf Sofia Papagiannaki
        A custom template to use. This is optional; if not specified,
290 1e685275 Sofia Papagiannaki
        this will default to ``im/profile.html``.
291 176023aa Kostas Papadimitriou

292 890b0eaf Sofia Papagiannaki
    ``extra_context``
293 890b0eaf Sofia Papagiannaki
        An dictionary of variables to add to the template context.
294 176023aa Kostas Papadimitriou

295 890b0eaf Sofia Papagiannaki
    **Template:**
296 176023aa Kostas Papadimitriou

297 1e685275 Sofia Papagiannaki
    im/profile.html or ``template_name`` keyword argument.
298 176023aa Kostas Papadimitriou

299 92defad4 Sofia Papagiannaki
    **Settings:**
300 176023aa Kostas Papadimitriou

301 92defad4 Sofia Papagiannaki
    The view expectes the following settings are defined:
302 176023aa Kostas Papadimitriou

303 92defad4 Sofia Papagiannaki
    * LOGIN_URL: login uri
304 890b0eaf Sofia Papagiannaki
    """
305 f6ff3033 Giorgos Korfiatis
306 f6ff3033 Giorgos Korfiatis
    request.user = AstakosUser.objects.select_for_update().\
307 f6ff3033 Giorgos Korfiatis
        get(id=request.user.id)
308 aab4d540 Sofia Papagiannaki
    extra_context = extra_context or {}
309 bf0c6de5 Sofia Papagiannaki
    form = ProfileForm(
310 bf0c6de5 Sofia Papagiannaki
        instance=request.user,
311 bf0c6de5 Sofia Papagiannaki
        session_key=request.session.session_key
312 bf0c6de5 Sofia Papagiannaki
    )
313 15efc749 Sofia Papagiannaki
    extra_context['next'] = request.GET.get('next')
314 64cd4730 Antony Chazapis
    if request.method == 'POST':
315 bf0c6de5 Sofia Papagiannaki
        form = ProfileForm(
316 bf0c6de5 Sofia Papagiannaki
            request.POST,
317 bf0c6de5 Sofia Papagiannaki
            instance=request.user,
318 bf0c6de5 Sofia Papagiannaki
            session_key=request.session.session_key
319 bf0c6de5 Sofia Papagiannaki
        )
320 890b0eaf Sofia Papagiannaki
        if form.is_valid():
321 64cd4730 Antony Chazapis
            try:
322 c301698f Sofia Papagiannaki
                prev_token = request.user.auth_token
323 3fbf7863 Kostas Papadimitriou
                user = form.save(request=request)
324 217994f8 Sofia Papagiannaki
                next = restrict_next(
325 217994f8 Sofia Papagiannaki
                    request.POST.get('next'),
326 8998f09a Sofia Papagiannaki
                    domain=settings.COOKIE_DOMAIN
327 217994f8 Sofia Papagiannaki
                )
328 ae497612 Olga Brani
                msg = _(astakos_messages.PROFILE_UPDATED)
329 24406ae3 Sofia Papagiannaki
                messages.success(request, msg)
330 2da6f56b Kostas Papadimitriou
331 2da6f56b Kostas Papadimitriou
                if form.email_changed:
332 2da6f56b Kostas Papadimitriou
                    msg = _(astakos_messages.EMAIL_CHANGE_REGISTERED)
333 2da6f56b Kostas Papadimitriou
                    messages.success(request, msg)
334 2da6f56b Kostas Papadimitriou
                if form.password_changed:
335 2da6f56b Kostas Papadimitriou
                    msg = _(astakos_messages.PASSWORD_CHANGED)
336 2da6f56b Kostas Papadimitriou
                    messages.success(request, msg)
337 2da6f56b Kostas Papadimitriou
338 f7400729 Kostas Papadimitriou
                if next:
339 f7400729 Kostas Papadimitriou
                    return redirect(next)
340 f7400729 Kostas Papadimitriou
                else:
341 f7400729 Kostas Papadimitriou
                    return redirect(reverse('edit_profile'))
342 890b0eaf Sofia Papagiannaki
            except ValueError, ve:
343 24406ae3 Sofia Papagiannaki
                messages.success(request, ve)
344 23447441 Sofia Papagiannaki
    elif request.method == "GET":
345 792c2f3b Olga Brani
        request.user.is_verified = True
346 792c2f3b Olga Brani
        request.user.save()
347 792c2f3b Olga Brani
348 792c2f3b Olga Brani
    # existing providers
349 9d20fe23 Kostas Papadimitriou
    user_providers = request.user.get_enabled_auth_providers()
350 9d20fe23 Kostas Papadimitriou
    user_disabled_providers = request.user.get_disabled_auth_providers()
351 792c2f3b Olga Brani
352 792c2f3b Olga Brani
    # providers that user can add
353 792c2f3b Olga Brani
    user_available_providers = request.user.get_available_auth_providers()
354 792c2f3b Olga Brani
355 bea584e1 Giorgos Korfiatis
    extra_context['services'] = Component.catalog().values()
356 0905ccd2 Sofia Papagiannaki
    return render_response(template_name,
357 e7cb4085 Kostas Papadimitriou
                           profile_form=form,
358 e7cb4085 Kostas Papadimitriou
                           user_providers=user_providers,
359 e7cb4085 Kostas Papadimitriou
                           user_disabled_providers=user_disabled_providers,
360 e7cb4085 Kostas Papadimitriou
                           user_available_providers=user_available_providers,
361 e7cb4085 Kostas Papadimitriou
                           context_instance=get_context(request,
362 8fb8d0cf Giorgos Korfiatis
                                                        extra_context))
363 5ce3ce4f Sofia Papagiannaki
364 64cd4730 Antony Chazapis
365 554ef175 Christos Stavrakakis
@transaction.commit_on_success
366 9a06d96f Olga Brani
@require_http_methods(["GET", "POST"])
367 222305b7 Sofia Papagiannaki
@cookie_fix
368 e7cb4085 Kostas Papadimitriou
def signup(request, template_name='im/signup.html', on_success='index',
369 e7cb4085 Kostas Papadimitriou
           extra_context=None, activation_backend=None):
370 890b0eaf Sofia Papagiannaki
    """
371 890b0eaf Sofia Papagiannaki
    Allows a user to create a local account.
372 176023aa Kostas Papadimitriou

373 b669d9c0 Sofia Papagiannaki
    In case of GET request renders a form for entering the user information.
374 890b0eaf Sofia Papagiannaki
    In case of POST handles the signup.
375 176023aa Kostas Papadimitriou

376 e7cb4085 Kostas Papadimitriou
    The user activation will be delegated to the backend specified by the
377 e7cb4085 Kostas Papadimitriou
    ``activation_backend`` keyword argument if present, otherwise to the
378 e7cb4085 Kostas Papadimitriou
    ``astakos.im.activation_backends.InvitationBackend`` if
379 e7cb4085 Kostas Papadimitriou
    settings.ASTAKOS_INVITATIONS_ENABLED is True or
380 e7cb4085 Kostas Papadimitriou
    ``astakos.im.activation_backends.SimpleBackend`` if not (see
381 e7cb4085 Kostas Papadimitriou
    activation_backends);
382 5ce3ce4f Sofia Papagiannaki

383 e7cb4085 Kostas Papadimitriou
    Upon successful user creation, if ``next`` url parameter is present the
384 e7cb4085 Kostas Papadimitriou
    user is redirected there otherwise renders the same page with a success
385 e7cb4085 Kostas Papadimitriou
    message.
386 5ce3ce4f Sofia Papagiannaki

387 8f5a3a06 Sofia Papagiannaki
    On unsuccessful creation, renders ``template_name`` with an error message.
388 5ce3ce4f Sofia Papagiannaki

389 890b0eaf Sofia Papagiannaki
    **Arguments**
390 5ce3ce4f Sofia Papagiannaki

391 8f5a3a06 Sofia Papagiannaki
    ``template_name``
392 8f5a3a06 Sofia Papagiannaki
        A custom template to render. This is optional;
393 1e685275 Sofia Papagiannaki
        if not specified, this will default to ``im/signup.html``.
394 176023aa Kostas Papadimitriou

395 890b0eaf Sofia Papagiannaki
    ``extra_context``
396 890b0eaf Sofia Papagiannaki
        An dictionary of variables to add to the template context.
397 176023aa Kostas Papadimitriou

398 43332a76 Kostas Papadimitriou
    ``on_success``
399 43332a76 Kostas Papadimitriou
        Resolvable view name to redirect on registration success.
400 43332a76 Kostas Papadimitriou

401 890b0eaf Sofia Papagiannaki
    **Template:**
402 5ce3ce4f Sofia Papagiannaki

403 8f5a3a06 Sofia Papagiannaki
    im/signup.html or ``template_name`` keyword argument.
404 890b0eaf Sofia Papagiannaki
    """
405 1f3b4b39 Sofia Papagiannaki
    extra_context = extra_context or {}
406 0d02a287 Sofia Papagiannaki
    if request.user.is_authenticated():
407 e7cb4085 Kostas Papadimitriou
        logger.info("%s already signed in, redirect to index",
408 e7cb4085 Kostas Papadimitriou
                    request.user.log_display)
409 e7cb4085 Kostas Papadimitriou
        return HttpResponseRedirect(reverse('index'))
410 5ce3ce4f Sofia Papagiannaki
411 0a569195 Sofia Papagiannaki
    provider = get_query(request).get('provider', 'local')
412 9d20fe23 Kostas Papadimitriou
    if not auth.get_provider(provider).get_create_policy:
413 e7cb4085 Kostas Papadimitriou
        logger.error("%s provider not available for signup", provider)
414 279d6e51 Olga Brani
        raise PermissionDenied
415 279d6e51 Olga Brani
416 e7cb4085 Kostas Papadimitriou
    instance = None
417 6c8a3f7c Sofia Papagiannaki
418 e7cb4085 Kostas Papadimitriou
    # user registered using third party provider
419 8ab484ea Kostas Papadimitriou
    third_party_token = request.REQUEST.get('third_party_token', None)
420 9d20fe23 Kostas Papadimitriou
    unverified = None
421 c1f65a1e Kostas Papadimitriou
    pending = None
422 c630fee6 Kostas Papadimitriou
    if third_party_token:
423 e7cb4085 Kostas Papadimitriou
        # retreive third party entry. This was created right after the initial
424 e7cb4085 Kostas Papadimitriou
        # third party provider handshake.
425 c630fee6 Kostas Papadimitriou
        pending = get_object_or_404(PendingThirdPartyUser,
426 c630fee6 Kostas Papadimitriou
                                    token=third_party_token)
427 9d20fe23 Kostas Papadimitriou
428 c630fee6 Kostas Papadimitriou
        provider = pending.provider
429 e7cb4085 Kostas Papadimitriou
430 e7cb4085 Kostas Papadimitriou
        # clone third party instance into the corresponding AstakosUser
431 c630fee6 Kostas Papadimitriou
        instance = pending.get_user_instance()
432 9d20fe23 Kostas Papadimitriou
        get_unverified = AstakosUserAuthProvider.objects.unverified
433 e7cb4085 Kostas Papadimitriou
434 e7cb4085 Kostas Papadimitriou
        # check existing unverified entries
435 9d20fe23 Kostas Papadimitriou
        unverified = get_unverified(pending.provider,
436 9d20fe23 Kostas Papadimitriou
                                    identifier=pending.third_party_identifier)
437 9d20fe23 Kostas Papadimitriou
438 9d20fe23 Kostas Papadimitriou
        if unverified and request.method == 'GET':
439 9d20fe23 Kostas Papadimitriou
            messages.warning(request, unverified.get_pending_registration_msg)
440 e7cb4085 Kostas Papadimitriou
            if unverified.user.moderated:
441 9d20fe23 Kostas Papadimitriou
                messages.warning(request,
442 9d20fe23 Kostas Papadimitriou
                                 unverified.get_pending_resend_activation_msg)
443 9d20fe23 Kostas Papadimitriou
            else:
444 9d20fe23 Kostas Papadimitriou
                messages.warning(request,
445 9d20fe23 Kostas Papadimitriou
                                 unverified.get_pending_moderation_msg)
446 8ab484ea Kostas Papadimitriou
447 e7cb4085 Kostas Papadimitriou
    # prepare activation backend based on current request
448 e7cb4085 Kostas Papadimitriou
    if not activation_backend:
449 e7cb4085 Kostas Papadimitriou
        activation_backend = activation_backends.get_backend()
450 e7cb4085 Kostas Papadimitriou
451 e7726e14 Kostas Papadimitriou
    form_kwargs = {'instance': instance, 'request': request}
452 e7cb4085 Kostas Papadimitriou
    if third_party_token:
453 e7cb4085 Kostas Papadimitriou
        form_kwargs['third_party_token'] = third_party_token
454 e7cb4085 Kostas Papadimitriou
455 c1f65a1e Kostas Papadimitriou
    if pending:
456 c1f65a1e Kostas Papadimitriou
        form_kwargs['initial'] = {
457 c1f65a1e Kostas Papadimitriou
            'first_name': pending.first_name,
458 c1f65a1e Kostas Papadimitriou
            'last_name': pending.last_name,
459 c1f65a1e Kostas Papadimitriou
            'email': pending.email
460 c1f65a1e Kostas Papadimitriou
        }
461 c1f65a1e Kostas Papadimitriou
462 e7cb4085 Kostas Papadimitriou
    form = activation_backend.get_signup_form(
463 e7cb4085 Kostas Papadimitriou
        provider, None, **form_kwargs)
464 9d20fe23 Kostas Papadimitriou
465 8f5a3a06 Sofia Papagiannaki
    if request.method == 'POST':
466 e7cb4085 Kostas Papadimitriou
        form = activation_backend.get_signup_form(
467 e7cb4085 Kostas Papadimitriou
            provider,
468 e7cb4085 Kostas Papadimitriou
            request.POST,
469 e7cb4085 Kostas Papadimitriou
            **form_kwargs)
470 e7cb4085 Kostas Papadimitriou
471 8f5a3a06 Sofia Papagiannaki
        if form.is_valid():
472 1808f7bc Giorgos Korfiatis
            user = form.create_user()
473 554ef175 Christos Stavrakakis
            result = activation_backend.handle_registration(user)
474 554ef175 Christos Stavrakakis
            if result.status == \
475 554ef175 Christos Stavrakakis
                    activation_backend.Result.PENDING_MODERATION:
476 554ef175 Christos Stavrakakis
                # user should be warned that his account is not active yet
477 554ef175 Christos Stavrakakis
                status = messages.WARNING
478 554ef175 Christos Stavrakakis
            else:
479 554ef175 Christos Stavrakakis
                status = messages.SUCCESS
480 554ef175 Christos Stavrakakis
            message = result.message
481 554ef175 Christos Stavrakakis
            activation_backend.send_result_notifications(result, user)
482 792c2f3b Olga Brani
483 554ef175 Christos Stavrakakis
            # commit user entry
484 554ef175 Christos Stavrakakis
            transaction.commit()
485 554ef175 Christos Stavrakakis
486 554ef175 Christos Stavrakakis
            if user and user.is_active:
487 554ef175 Christos Stavrakakis
                # activation backend directly activated the user
488 554ef175 Christos Stavrakakis
                # log him in
489 554ef175 Christos Stavrakakis
                next = request.POST.get('next', '')
490 554ef175 Christos Stavrakakis
                response = prepare_response(request, user, next=next)
491 554ef175 Christos Stavrakakis
                return response
492 554ef175 Christos Stavrakakis
493 554ef175 Christos Stavrakakis
            messages.add_message(request, status, message)
494 554ef175 Christos Stavrakakis
            return HttpResponseRedirect(reverse(on_success))
495 43332a76 Kostas Papadimitriou
496 8fb8d0cf Giorgos Korfiatis
    return render_response(
497 8fb8d0cf Giorgos Korfiatis
        template_name,
498 8fb8d0cf Giorgos Korfiatis
        signup_form=form,
499 8fb8d0cf Giorgos Korfiatis
        third_party_token=third_party_token,
500 8fb8d0cf Giorgos Korfiatis
        provider=provider,
501 8fb8d0cf Giorgos Korfiatis
        context_instance=get_context(request, extra_context))
502 64cd4730 Antony Chazapis
503 5ce3ce4f Sofia Papagiannaki
504 9a06d96f Olga Brani
@require_http_methods(["GET", "POST"])
505 9d20fe23 Kostas Papadimitriou
@required_auth_methods_assigned(allow_access=True)
506 890b0eaf Sofia Papagiannaki
@login_required
507 222305b7 Sofia Papagiannaki
@cookie_fix
508 270dd48d Sofia Papagiannaki
@signed_terms_required
509 8fb8d0cf Giorgos Korfiatis
def feedback(request, template_name='im/feedback.html',
510 8fb8d0cf Giorgos Korfiatis
             email_template_name='im/feedback_mail.txt', extra_context=None):
511 890b0eaf Sofia Papagiannaki
    """
512 890b0eaf Sofia Papagiannaki
    Allows a user to send feedback.
513 176023aa Kostas Papadimitriou

514 8fb8d0cf Giorgos Korfiatis
    In case of GET request renders a form for providing the feedback
515 8fb8d0cf Giorgos Korfiatis
    information.
516 890b0eaf Sofia Papagiannaki
    In case of POST sends an email to support team.
517 176023aa Kostas Papadimitriou

518 2cbaacd5 Sofia Papagiannaki
    If the user isn't logged in, redirects to settings.LOGIN_URL.
519 176023aa Kostas Papadimitriou

520 890b0eaf Sofia Papagiannaki
    **Arguments**
521 176023aa Kostas Papadimitriou

522 890b0eaf Sofia Papagiannaki
    ``template_name``
523 890b0eaf Sofia Papagiannaki
        A custom template to use. This is optional; if not specified,
524 1e685275 Sofia Papagiannaki
        this will default to ``im/feedback.html``.
525 176023aa Kostas Papadimitriou

526 890b0eaf Sofia Papagiannaki
    ``extra_context``
527 890b0eaf Sofia Papagiannaki
        An dictionary of variables to add to the template context.
528 176023aa Kostas Papadimitriou

529 890b0eaf Sofia Papagiannaki
    **Template:**
530 176023aa Kostas Papadimitriou

531 1e685275 Sofia Papagiannaki
    im/signup.html or ``template_name`` keyword argument.
532 176023aa Kostas Papadimitriou

533 890b0eaf Sofia Papagiannaki
    **Settings:**
534 176023aa Kostas Papadimitriou

535 92defad4 Sofia Papagiannaki
    * LOGIN_URL: login uri
536 890b0eaf Sofia Papagiannaki
    """
537 1f3b4b39 Sofia Papagiannaki
    extra_context = extra_context or {}
538 64cd4730 Antony Chazapis
    if request.method == 'GET':
539 890b0eaf Sofia Papagiannaki
        form = FeedbackForm()
540 890b0eaf Sofia Papagiannaki
    if request.method == 'POST':
541 890b0eaf Sofia Papagiannaki
        if not request.user:
542 890b0eaf Sofia Papagiannaki
            return HttpResponse('Unauthorized', status=401)
543 176023aa Kostas Papadimitriou
544 890b0eaf Sofia Papagiannaki
        form = FeedbackForm(request.POST)
545 890b0eaf Sofia Papagiannaki
        if form.is_valid():
546 e9083112 Sofia Papagiannaki
            msg = form.cleaned_data['feedback_msg']
547 8f5a3a06 Sofia Papagiannaki
            data = form.cleaned_data['feedback_data']
548 860bb9f7 Sofia Papagiannaki
            send_feedback(msg, data, request.user, email_template_name)
549 860bb9f7 Sofia Papagiannaki
            message = _(astakos_messages.FEEDBACK_SENT)
550 860bb9f7 Sofia Papagiannaki
            messages.success(request, message)
551 b9f8f48a Kostas Papadimitriou
            return HttpResponseRedirect(reverse('feedback'))
552 e7cb4085 Kostas Papadimitriou
553 890b0eaf Sofia Papagiannaki
    return render_response(template_name,
554 5ce3ce4f Sofia Papagiannaki
                           feedback_form=form,
555 e7cb4085 Kostas Papadimitriou
                           context_instance=get_context(request,
556 e7cb4085 Kostas Papadimitriou
                                                        extra_context))
557 5ce3ce4f Sofia Papagiannaki
558 15efc749 Sofia Papagiannaki
559 217994f8 Sofia Papagiannaki
@require_http_methods(["GET"])
560 222305b7 Sofia Papagiannaki
@cookie_fix
561 e7cb4085 Kostas Papadimitriou
def logout(request, template='registration/logged_out.html',
562 e7cb4085 Kostas Papadimitriou
           extra_context=None):
563 63ecdd20 Sofia Papagiannaki
    """
564 b2ffa772 Sofia Papagiannaki
    Wraps `django.contrib.auth.logout`.
565 63ecdd20 Sofia Papagiannaki
    """
566 1f3b4b39 Sofia Papagiannaki
    extra_context = extra_context or {}
567 7482228b Sofia Papagiannaki
    response = HttpResponse()
568 8e45d6fd Sofia Papagiannaki
    if request.user.is_authenticated():
569 8e45d6fd Sofia Papagiannaki
        email = request.user.email
570 8e45d6fd Sofia Papagiannaki
        auth_logout(request)
571 c630fee6 Kostas Papadimitriou
    else:
572 c630fee6 Kostas Papadimitriou
        response['Location'] = reverse('index')
573 c630fee6 Kostas Papadimitriou
        response.status_code = 301
574 c630fee6 Kostas Papadimitriou
        return response
575 c630fee6 Kostas Papadimitriou
576 217994f8 Sofia Papagiannaki
    next = restrict_next(
577 217994f8 Sofia Papagiannaki
        request.GET.get('next'),
578 8998f09a Sofia Papagiannaki
        domain=settings.COOKIE_DOMAIN
579 217994f8 Sofia Papagiannaki
    )
580 c630fee6 Kostas Papadimitriou
581 63ecdd20 Sofia Papagiannaki
    if next:
582 63ecdd20 Sofia Papagiannaki
        response['Location'] = next
583 63ecdd20 Sofia Papagiannaki
        response.status_code = 302
584 8998f09a Sofia Papagiannaki
    elif settings.LOGOUT_NEXT:
585 8998f09a Sofia Papagiannaki
        response['Location'] = settings.LOGOUT_NEXT
586 0d02a287 Sofia Papagiannaki
        response.status_code = 301
587 b2ffa772 Sofia Papagiannaki
    else:
588 8fb8d0cf Giorgos Korfiatis
        last_provider = request.COOKIES.get(
589 8fb8d0cf Giorgos Korfiatis
            'astakos_last_login_method', 'local')
590 9d20fe23 Kostas Papadimitriou
        provider = auth.get_provider(last_provider)
591 9d20fe23 Kostas Papadimitriou
        message = provider.get_logout_success_msg
592 9d20fe23 Kostas Papadimitriou
        extra = provider.get_logout_success_extra_msg
593 9d20fe23 Kostas Papadimitriou
        if extra:
594 8fb8d0cf Giorgos Korfiatis
            message += "<br />" + extra
595 d21d422a Kostas Papadimitriou
        messages.success(request, message)
596 c630fee6 Kostas Papadimitriou
        response['Location'] = reverse('index')
597 c630fee6 Kostas Papadimitriou
        response.status_code = 301
598 49df775e Sofia Papagiannaki
    return response
599 2126d85d Sofia Papagiannaki
600 5ce3ce4f Sofia Papagiannaki
601 9a06d96f Olga Brani
@require_http_methods(["GET", "POST"])
602 222305b7 Sofia Papagiannaki
@cookie_fix
603 554ef175 Christos Stavrakakis
@transaction.commit_on_success
604 3bb604eb Sofia Papagiannaki
def activate(request, greeting_email_template_name='im/welcome_email.txt',
605 3bb604eb Sofia Papagiannaki
             helpdesk_email_template_name='im/helpdesk_notification.txt'):
606 2126d85d Sofia Papagiannaki
    """
607 e7cb4085 Kostas Papadimitriou
    Activates the user identified by the ``auth`` request parameter, sends a
608 e7cb4085 Kostas Papadimitriou
    welcome email and renews the user token.
609 176023aa Kostas Papadimitriou

610 554ef175 Christos Stavrakakis
    The user state will be updated only if the email will be send successfully.
611 2126d85d Sofia Papagiannaki
    """
612 fb4ef6eb Kostas Papadimitriou
    token = request.GET.get('auth', None)
613 fb4ef6eb Kostas Papadimitriou
    next = request.GET.get('next', None)
614 fb4ef6eb Kostas Papadimitriou
615 fb4ef6eb Kostas Papadimitriou
    if not token:
616 fb4ef6eb Kostas Papadimitriou
        raise PermissionDenied
617 5ce3ce4f Sofia Papagiannaki
618 e7cb4085 Kostas Papadimitriou
    if request.user.is_authenticated():
619 e7cb4085 Kostas Papadimitriou
        message = _(astakos_messages.LOGGED_IN_WARNING)
620 24406ae3 Sofia Papagiannaki
        messages.error(request, message)
621 92a8abc9 Kostas Papadimitriou
        return HttpResponseRedirect(reverse('index'))
622 5ce3ce4f Sofia Papagiannaki
623 e7cb4085 Kostas Papadimitriou
    try:
624 f6ff3033 Giorgos Korfiatis
        user = AstakosUser.objects.select_for_update().\
625 f6ff3033 Giorgos Korfiatis
            get(verification_code=token)
626 e7cb4085 Kostas Papadimitriou
    except AstakosUser.DoesNotExist:
627 e7cb4085 Kostas Papadimitriou
        raise Http404
628 e7cb4085 Kostas Papadimitriou
629 e7cb4085 Kostas Papadimitriou
    if user.email_verified:
630 e7cb4085 Kostas Papadimitriou
        message = _(astakos_messages.ACCOUNT_ALREADY_VERIFIED)
631 89d959c9 Kostas Papadimitriou
        messages.error(request, message)
632 89d959c9 Kostas Papadimitriou
        return HttpResponseRedirect(reverse('index'))
633 89d959c9 Kostas Papadimitriou
634 554ef175 Christos Stavrakakis
    backend = activation_backends.get_backend()
635 554ef175 Christos Stavrakakis
    result = backend.handle_verification(user, token)
636 554ef175 Christos Stavrakakis
    backend.send_result_notifications(result, user)
637 fb4ef6eb Kostas Papadimitriou
    next = settings.ACTIVATION_REDIRECT_URL or next or reverse('index')
638 554ef175 Christos Stavrakakis
    if user.is_active:
639 554ef175 Christos Stavrakakis
        response = prepare_response(request, user, next, renew=True)
640 554ef175 Christos Stavrakakis
        messages.success(request, _(result.message))
641 e7cb4085 Kostas Papadimitriou
    else:
642 fb4ef6eb Kostas Papadimitriou
        response = HttpResponseRedirect(reverse('index'))
643 554ef175 Christos Stavrakakis
        messages.warning(request, _(result.message))
644 554ef175 Christos Stavrakakis
645 554ef175 Christos Stavrakakis
    return response
646 270dd48d Sofia Papagiannaki
647 5ce3ce4f Sofia Papagiannaki
648 3d08cdca Sofia Papagiannaki
@login_required
649 3d08cdca Sofia Papagiannaki
def _approval_terms_post(request, template_name, terms, extra_context):
650 3d08cdca Sofia Papagiannaki
    next = restrict_next(
651 3d08cdca Sofia Papagiannaki
        request.POST.get('next'),
652 3d08cdca Sofia Papagiannaki
        domain=settings.COOKIE_DOMAIN
653 3d08cdca Sofia Papagiannaki
    )
654 3d08cdca Sofia Papagiannaki
    if not next:
655 3d08cdca Sofia Papagiannaki
        next = reverse('index')
656 3d08cdca Sofia Papagiannaki
    form = SignApprovalTermsForm(request.POST, instance=request.user)
657 3d08cdca Sofia Papagiannaki
    if not form.is_valid():
658 3d08cdca Sofia Papagiannaki
        return render_response(template_name,
659 3d08cdca Sofia Papagiannaki
                               terms=terms,
660 3d08cdca Sofia Papagiannaki
                               approval_terms_form=form,
661 3d08cdca Sofia Papagiannaki
                               context_instance=get_context(request,
662 3d08cdca Sofia Papagiannaki
                                                            extra_context))
663 3d08cdca Sofia Papagiannaki
    user = form.save()
664 3d08cdca Sofia Papagiannaki
    return HttpResponseRedirect(next)
665 3d08cdca Sofia Papagiannaki
666 3d08cdca Sofia Papagiannaki
667 9a06d96f Olga Brani
@require_http_methods(["GET", "POST"])
668 222305b7 Sofia Papagiannaki
@cookie_fix
669 e7cb4085 Kostas Papadimitriou
def approval_terms(request, term_id=None,
670 e7cb4085 Kostas Papadimitriou
                   template_name='im/approval_terms.html', extra_context=None):
671 1f3b4b39 Sofia Papagiannaki
    extra_context = extra_context or {}
672 3d08cdca Sofia Papagiannaki
    terms_record = None
673 270dd48d Sofia Papagiannaki
    terms = None
674 270dd48d Sofia Papagiannaki
    if not term_id:
675 270dd48d Sofia Papagiannaki
        try:
676 3d08cdca Sofia Papagiannaki
            terms_record = ApprovalTerms.objects.order_by('-id')[0]
677 270dd48d Sofia Papagiannaki
        except IndexError:
678 270dd48d Sofia Papagiannaki
            pass
679 270dd48d Sofia Papagiannaki
    else:
680 270dd48d Sofia Papagiannaki
        try:
681 3d08cdca Sofia Papagiannaki
            terms_record = ApprovalTerms.objects.get(id=term_id)
682 aab4d540 Sofia Papagiannaki
        except ApprovalTerms.DoesNotExist, e:
683 270dd48d Sofia Papagiannaki
            pass
684 176023aa Kostas Papadimitriou
685 3d08cdca Sofia Papagiannaki
    if not terms_record:
686 ae497612 Olga Brani
        messages.error(request, _(astakos_messages.NO_APPROVAL_TERMS))
687 6ff7a7ca Sofia Papagiannaki
        return HttpResponseRedirect(reverse('index'))
688 64b5136c Sofia Papagiannaki
    try:
689 3d08cdca Sofia Papagiannaki
        f = open(terms_record.location, 'r')
690 64b5136c Sofia Papagiannaki
    except IOError:
691 64b5136c Sofia Papagiannaki
        messages.error(request, _(astakos_messages.GENERIC_ERROR))
692 64b5136c Sofia Papagiannaki
        return render_response(
693 e7cb4085 Kostas Papadimitriou
            template_name, context_instance=get_context(request,
694 e7cb4085 Kostas Papadimitriou
                                                        extra_context))
695 64b5136c Sofia Papagiannaki
696 270dd48d Sofia Papagiannaki
    terms = f.read()
697 176023aa Kostas Papadimitriou
698 270dd48d Sofia Papagiannaki
    if request.method == 'POST':
699 3d08cdca Sofia Papagiannaki
        return _approval_terms_post(request, template_name, terms,
700 3d08cdca Sofia Papagiannaki
                                    extra_context)
701 270dd48d Sofia Papagiannaki
    else:
702 586967c0 Sofia Papagiannaki
        form = None
703 fcf90160 Sofia Papagiannaki
        if request.user.is_authenticated() and not request.user.signed_terms:
704 586967c0 Sofia Papagiannaki
            form = SignApprovalTermsForm(instance=request.user)
705 270dd48d Sofia Papagiannaki
        return render_response(template_name,
706 5ce3ce4f Sofia Papagiannaki
                               terms=terms,
707 5ce3ce4f Sofia Papagiannaki
                               approval_terms_form=form,
708 e7cb4085 Kostas Papadimitriou
                               context_instance=get_context(request,
709 e7cb4085 Kostas Papadimitriou
                                                            extra_context))
710 5ce3ce4f Sofia Papagiannaki
711 270dd48d Sofia Papagiannaki
712 9a06d96f Olga Brani
@require_http_methods(["GET", "POST"])
713 222305b7 Sofia Papagiannaki
@cookie_fix
714 554ef175 Christos Stavrakakis
@transaction.commit_on_success
715 49790d9d Sofia Papagiannaki
def change_email(request, activation_key=None,
716 49790d9d Sofia Papagiannaki
                 email_template_name='registration/email_change_email.txt',
717 49790d9d Sofia Papagiannaki
                 form_template_name='registration/email_change_form.html',
718 49790d9d Sofia Papagiannaki
                 confirm_template_name='registration/email_change_done.html',
719 aab4d540 Sofia Papagiannaki
                 extra_context=None):
720 1f3b4b39 Sofia Papagiannaki
    extra_context = extra_context or {}
721 53e0b8fe Kostas Papadimitriou
722 e7cb4085 Kostas Papadimitriou
    if not settings.EMAILCHANGE_ENABLED:
723 17ad5d37 Kostas Papadimitriou
        raise PermissionDenied
724 17ad5d37 Kostas Papadimitriou
725 49790d9d Sofia Papagiannaki
    if activation_key:
726 49790d9d Sofia Papagiannaki
        try:
727 0b817216 Kostas Papadimitriou
            try:
728 0b817216 Kostas Papadimitriou
                email_change = EmailChange.objects.get(
729 0b817216 Kostas Papadimitriou
                    activation_key=activation_key)
730 0b817216 Kostas Papadimitriou
            except EmailChange.DoesNotExist:
731 0b817216 Kostas Papadimitriou
                logger.error("[change-email] Invalid or used activation "
732 0b817216 Kostas Papadimitriou
                             "code, %s", activation_key)
733 0b817216 Kostas Papadimitriou
                raise Http404
734 0b817216 Kostas Papadimitriou
735 8fb8d0cf Giorgos Korfiatis
            if (
736 8fb8d0cf Giorgos Korfiatis
                request.user.is_authenticated() and
737 8fb8d0cf Giorgos Korfiatis
                request.user == email_change.user or not
738 8fb8d0cf Giorgos Korfiatis
                request.user.is_authenticated()
739 8fb8d0cf Giorgos Korfiatis
            ):
740 0b817216 Kostas Papadimitriou
                user = EmailChange.objects.change_email(activation_key)
741 ae497612 Olga Brani
                msg = _(astakos_messages.EMAIL_CHANGED)
742 24406ae3 Sofia Papagiannaki
                messages.success(request, msg)
743 49790d9d Sofia Papagiannaki
                transaction.commit()
744 53e0b8fe Kostas Papadimitriou
                return HttpResponseRedirect(reverse('edit_profile'))
745 0b817216 Kostas Papadimitriou
            else:
746 0b817216 Kostas Papadimitriou
                logger.error("[change-email] Access from invalid user, %s %s",
747 0b817216 Kostas Papadimitriou
                             email_change.user, request.user.log_display)
748 0b817216 Kostas Papadimitriou
                raise PermissionDenied
749 49790d9d Sofia Papagiannaki
        except ValueError, e:
750 24406ae3 Sofia Papagiannaki
            messages.error(request, e)
751 53e0b8fe Kostas Papadimitriou
            transaction.rollback()
752 53e0b8fe Kostas Papadimitriou
            return HttpResponseRedirect(reverse('index'))
753 53e0b8fe Kostas Papadimitriou
754 49790d9d Sofia Papagiannaki
        return render_response(confirm_template_name,
755 e7cb4085 Kostas Papadimitriou
                               modified_user=user if 'user' in locals()
756 0a70d2c5 Christos Stavrakakis
                               else None,
757 0a70d2c5 Christos Stavrakakis
                               context_instance=get_context(request,
758 0a70d2c5 Christos Stavrakakis
                                                            extra_context))
759 5ce3ce4f Sofia Papagiannaki
760 49790d9d Sofia Papagiannaki
    if not request.user.is_authenticated():
761 49790d9d Sofia Papagiannaki
        path = quote(request.get_full_path())
762 6ff7a7ca Sofia Papagiannaki
        url = request.build_absolute_uri(reverse('index'))
763 49790d9d Sofia Papagiannaki
        return HttpResponseRedirect(url + '?next=' + path)
764 53e0b8fe Kostas Papadimitriou
765 53e0b8fe Kostas Papadimitriou
    # clean up expired email changes
766 53e0b8fe Kostas Papadimitriou
    if request.user.email_change_is_pending():
767 53e0b8fe Kostas Papadimitriou
        change = request.user.emailchanges.get()
768 53e0b8fe Kostas Papadimitriou
        if change.activation_key_expired():
769 53e0b8fe Kostas Papadimitriou
            change.delete()
770 53e0b8fe Kostas Papadimitriou
            transaction.commit()
771 53e0b8fe Kostas Papadimitriou
            return HttpResponseRedirect(reverse('email_change'))
772 53e0b8fe Kostas Papadimitriou
773 49790d9d Sofia Papagiannaki
    form = EmailChangeForm(request.POST or None)
774 49790d9d Sofia Papagiannaki
    if request.method == 'POST' and form.is_valid():
775 554ef175 Christos Stavrakakis
        ec = form.save(request, email_template_name, request)
776 554ef175 Christos Stavrakakis
        msg = _(astakos_messages.EMAIL_CHANGE_REGISTERED)
777 554ef175 Christos Stavrakakis
        messages.success(request, msg)
778 554ef175 Christos Stavrakakis
        transaction.commit()
779 554ef175 Christos Stavrakakis
        return HttpResponseRedirect(reverse('edit_profile'))
780 53e0b8fe Kostas Papadimitriou
781 53e0b8fe Kostas Papadimitriou
    if request.user.email_change_is_pending():
782 8fb8d0cf Giorgos Korfiatis
        messages.warning(request,
783 8fb8d0cf Giorgos Korfiatis
                         astakos_messages.PENDING_EMAIL_CHANGE_REQUEST)
784 53e0b8fe Kostas Papadimitriou
785 c0b26605 Sofia Papagiannaki
    return render_response(
786 c0b26605 Sofia Papagiannaki
        form_template_name,
787 c0b26605 Sofia Papagiannaki
        form=form,
788 c0b26605 Sofia Papagiannaki
        context_instance=get_context(request, extra_context)
789 c0b26605 Sofia Papagiannaki
    )
790 1f3b4b39 Sofia Papagiannaki
791 1f3b4b39 Sofia Papagiannaki
792 222305b7 Sofia Papagiannaki
@cookie_fix
793 f6ff3033 Giorgos Korfiatis
@transaction.commit_on_success
794 e7cb4085 Kostas Papadimitriou
def send_activation(request, user_id, template_name='im/login.html',
795 e7cb4085 Kostas Papadimitriou
                    extra_context=None):
796 792c2f3b Olga Brani
797 badcb2a9 Kostas Papadimitriou
    if request.user.is_authenticated():
798 e7cb4085 Kostas Papadimitriou
        return HttpResponseRedirect(reverse('index'))
799 badcb2a9 Kostas Papadimitriou
800 1f3b4b39 Sofia Papagiannaki
    extra_context = extra_context or {}
801 1f3b4b39 Sofia Papagiannaki
    try:
802 f6ff3033 Giorgos Korfiatis
        u = AstakosUser.objects.select_for_update().get(id=user_id)
803 1f3b4b39 Sofia Papagiannaki
    except AstakosUser.DoesNotExist:
804 c0b26605 Sofia Papagiannaki
        messages.error(request, _(astakos_messages.ACCOUNT_UNKNOWN))
805 1f3b4b39 Sofia Papagiannaki
    else:
806 e7cb4085 Kostas Papadimitriou
        if u.email_verified:
807 e7cb4085 Kostas Papadimitriou
            logger.warning("[resend activation] Account already verified: %s",
808 e7cb4085 Kostas Papadimitriou
                           u.log_display)
809 e7cb4085 Kostas Papadimitriou
810 e7cb4085 Kostas Papadimitriou
            messages.error(request,
811 e7cb4085 Kostas Papadimitriou
                           _(astakos_messages.ACCOUNT_ALREADY_VERIFIED))
812 e7cb4085 Kostas Papadimitriou
        else:
813 e7cb4085 Kostas Papadimitriou
            activation_backend = activation_backends.get_backend()
814 e7cb4085 Kostas Papadimitriou
            activation_backend.send_user_verification_email(u)
815 e7cb4085 Kostas Papadimitriou
            messages.success(request, astakos_messages.ACTIVATION_SENT)
816 ff81d0d9 Kostas Papadimitriou
817 ff81d0d9 Kostas Papadimitriou
    return HttpResponseRedirect(reverse('index'))
818 5ce3ce4f Sofia Papagiannaki
819 73fbaec4 Sofia Papagiannaki
820 73fbaec4 Sofia Papagiannaki
@require_http_methods(["GET"])
821 222305b7 Sofia Papagiannaki
@cookie_fix
822 badcb2a9 Kostas Papadimitriou
@valid_astakos_user_required
823 666c7490 Sofia Papagiannaki
def resource_usage(request):
824 e9ef5009 Kostas Papadimitriou
825 37d59b27 Kostas Papadimitriou
    resources_meta = presentation.RESOURCES
826 37d59b27 Kostas Papadimitriou
827 4e03ba30 Kostas Papadimitriou
    current_usage = quotas.get_user_quotas(request.user)
828 4e03ba30 Kostas Papadimitriou
    current_usage = json.dumps(current_usage['system'])
829 2dc27ac1 Giorgos Korfiatis
    resource_catalog, resource_groups = _resources_catalog()
830 37d59b27 Kostas Papadimitriou
    if resource_catalog is False:
831 37d59b27 Kostas Papadimitriou
        # on fail resource_groups contains the result object
832 37d59b27 Kostas Papadimitriou
        result = resource_groups
833 37d59b27 Kostas Papadimitriou
        messages.error(request, 'Unable to retrieve system resources: %s' %
834 37d59b27 Kostas Papadimitriou
                       result.reason)
835 37d59b27 Kostas Papadimitriou
836 4e03ba30 Kostas Papadimitriou
    resource_catalog = json.dumps(resource_catalog)
837 4e03ba30 Kostas Papadimitriou
    resource_groups = json.dumps(resource_groups)
838 37d59b27 Kostas Papadimitriou
    resources_order = json.dumps(resources_meta.get('resources_order'))
839 401089d8 Kostas Papadimitriou
840 666c7490 Sofia Papagiannaki
    return render_response('im/resource_usage.html',
841 3cbd5e47 Olga Brani
                           context_instance=get_context(request),
842 4e03ba30 Kostas Papadimitriou
                           resource_catalog=resource_catalog,
843 4e03ba30 Kostas Papadimitriou
                           resource_groups=resource_groups,
844 4e03ba30 Kostas Papadimitriou
                           resources_order=resources_order,
845 4e03ba30 Kostas Papadimitriou
                           current_usage=current_usage,
846 e7cb4085 Kostas Papadimitriou
                           token_cookie_name=settings.COOKIE_NAME,
847 4e03ba30 Kostas Papadimitriou
                           usage_update_interval=
848 e7cb4085 Kostas Papadimitriou
                           settings.USAGE_UPDATE_INTERVAL)
849 4e03ba30 Kostas Papadimitriou
850 9a06d96f Olga Brani
851 f432088a Kostas Papadimitriou
# TODO: action only on POST and user should confirm the removal
852 327a4c1e Olga Brani
@require_http_methods(["POST"])
853 222305b7 Sofia Papagiannaki
@cookie_fix
854 9d20fe23 Kostas Papadimitriou
@valid_astakos_user_required
855 d2633501 Kostas Papadimitriou
def remove_auth_provider(request, pk):
856 f432088a Kostas Papadimitriou
    try:
857 9d20fe23 Kostas Papadimitriou
        provider = request.user.auth_providers.get(pk=int(pk)).settings
858 f432088a Kostas Papadimitriou
    except AstakosUserAuthProvider.DoesNotExist:
859 f432088a Kostas Papadimitriou
        raise Http404
860 f432088a Kostas Papadimitriou
861 9d20fe23 Kostas Papadimitriou
    if provider.get_remove_policy:
862 9d20fe23 Kostas Papadimitriou
        messages.success(request, provider.get_removed_msg)
863 9d20fe23 Kostas Papadimitriou
        provider.remove_from_user()
864 d2633501 Kostas Papadimitriou
        return HttpResponseRedirect(reverse('edit_profile'))
865 d2633501 Kostas Papadimitriou
    else:
866 f432088a Kostas Papadimitriou
        raise PermissionDenied
867 5ebebb20 Olga Brani
868 792c2f3b Olga Brani
869 9d20fe23 Kostas Papadimitriou
@require_http_methods(["GET"])
870 9d20fe23 Kostas Papadimitriou
@required_auth_methods_assigned(allow_access=True)
871 9d20fe23 Kostas Papadimitriou
@login_required
872 222305b7 Sofia Papagiannaki
@cookie_fix
873 9d20fe23 Kostas Papadimitriou
@signed_terms_required
874 003d8fcf Olga Brani
def landing(request):
875 bea584e1 Giorgos Korfiatis
    context = {'services': Component.catalog(orderfor='dashboard')}
876 003d8fcf Olga Brani
    return render_response(
877 003d8fcf Olga Brani
        'im/landing.html',
878 0156e40c Kostas Papadimitriou
        context_instance=get_context(request), **context)
879 b87429e1 Olga Brani
880 ca5148f2 Kostas Papadimitriou
881 0be81d73 Sofia Papagiannaki
@cookie_fix
882 0be81d73 Sofia Papagiannaki
def get_menu(request, with_extra_links=False, with_signout=True):
883 0be81d73 Sofia Papagiannaki
    user = request.user
884 0be81d73 Sofia Papagiannaki
    index_url = reverse('index')
885 0be81d73 Sofia Papagiannaki
886 0be81d73 Sofia Papagiannaki
    if isinstance(user, User) and user.is_authenticated():
887 0be81d73 Sofia Papagiannaki
        l = []
888 0be81d73 Sofia Papagiannaki
        append = l.append
889 0be81d73 Sofia Papagiannaki
        item = MenuItem
890 0be81d73 Sofia Papagiannaki
        item.current_path = request.build_absolute_uri(request.path)
891 0be81d73 Sofia Papagiannaki
        append(item(url=request.build_absolute_uri(reverse('index')),
892 0be81d73 Sofia Papagiannaki
                    name=user.email))
893 0be81d73 Sofia Papagiannaki
        if with_extra_links:
894 0be81d73 Sofia Papagiannaki
            append(item(url=request.build_absolute_uri(reverse('landing')),
895 0be81d73 Sofia Papagiannaki
                        name="Overview"))
896 0be81d73 Sofia Papagiannaki
        if with_signout:
897 0be81d73 Sofia Papagiannaki
            append(item(url=request.build_absolute_uri(reverse('landing')),
898 0be81d73 Sofia Papagiannaki
                        name="Dashboard"))
899 0be81d73 Sofia Papagiannaki
        if with_extra_links:
900 8fb8d0cf Giorgos Korfiatis
            append(
901 8fb8d0cf Giorgos Korfiatis
                item(
902 8fb8d0cf Giorgos Korfiatis
                    url=request.build_absolute_uri(reverse('edit_profile')),
903 8fb8d0cf Giorgos Korfiatis
                    name="Profile"))
904 0be81d73 Sofia Papagiannaki
905 0be81d73 Sofia Papagiannaki
        if with_extra_links:
906 0be81d73 Sofia Papagiannaki
            if settings.INVITATIONS_ENABLED:
907 0be81d73 Sofia Papagiannaki
                append(item(url=request.build_absolute_uri(reverse('invite')),
908 0be81d73 Sofia Papagiannaki
                            name="Invitations"))
909 0be81d73 Sofia Papagiannaki
910 7763f5c6 Kostas Papadimitriou
            append(item(url=request.build_absolute_uri(reverse('api_access')),
911 7763f5c6 Kostas Papadimitriou
                        name="API access"))
912 7763f5c6 Kostas Papadimitriou
913 8fb8d0cf Giorgos Korfiatis
            append(
914 8fb8d0cf Giorgos Korfiatis
                item(
915 8fb8d0cf Giorgos Korfiatis
                    url=request.build_absolute_uri(reverse('resource_usage')),
916 8fb8d0cf Giorgos Korfiatis
                    name="Usage"))
917 0be81d73 Sofia Papagiannaki
918 0be81d73 Sofia Papagiannaki
            if settings.PROJECTS_VISIBLE:
919 8fb8d0cf Giorgos Korfiatis
                append(
920 8fb8d0cf Giorgos Korfiatis
                    item(
921 8fb8d0cf Giorgos Korfiatis
                        url=request.build_absolute_uri(
922 8fb8d0cf Giorgos Korfiatis
                            reverse('project_list')),
923 8fb8d0cf Giorgos Korfiatis
                        name="Projects"))
924 0be81d73 Sofia Papagiannaki
925 0be81d73 Sofia Papagiannaki
            append(item(url=request.build_absolute_uri(reverse('feedback')),
926 0be81d73 Sofia Papagiannaki
                        name="Contact"))
927 0be81d73 Sofia Papagiannaki
        if with_signout:
928 0be81d73 Sofia Papagiannaki
            append(item(url=request.build_absolute_uri(reverse('logout')),
929 0be81d73 Sofia Papagiannaki
                        name="Sign out"))
930 0be81d73 Sofia Papagiannaki
    else:
931 0be81d73 Sofia Papagiannaki
        l = [{'url': request.build_absolute_uri(index_url),
932 0be81d73 Sofia Papagiannaki
              'name': _("Sign in")}]
933 0be81d73 Sofia Papagiannaki
934 0be81d73 Sofia Papagiannaki
    callback = request.GET.get('callback', None)
935 0be81d73 Sofia Papagiannaki
    data = json.dumps(tuple(l))
936 0be81d73 Sofia Papagiannaki
    mimetype = 'application/json'
937 0be81d73 Sofia Papagiannaki
938 0be81d73 Sofia Papagiannaki
    if callback:
939 0be81d73 Sofia Papagiannaki
        mimetype = 'application/javascript'
940 0be81d73 Sofia Papagiannaki
        data = '%s(%s)' % (callback, data)
941 0be81d73 Sofia Papagiannaki
942 0be81d73 Sofia Papagiannaki
    return HttpResponse(content=data, mimetype=mimetype)
943 0be81d73 Sofia Papagiannaki
944 0be81d73 Sofia Papagiannaki
945 0be81d73 Sofia Papagiannaki
class MenuItem(dict):
946 0be81d73 Sofia Papagiannaki
    current_path = ''
947 0be81d73 Sofia Papagiannaki
948 0be81d73 Sofia Papagiannaki
    def __init__(self, *args, **kwargs):
949 0be81d73 Sofia Papagiannaki
        super(MenuItem, self).__init__(*args, **kwargs)
950 0be81d73 Sofia Papagiannaki
        if kwargs.get('url') or kwargs.get('submenu'):
951 0be81d73 Sofia Papagiannaki
            self.__set_is_active__()
952 0be81d73 Sofia Papagiannaki
953 0be81d73 Sofia Papagiannaki
    def __setitem__(self, key, value):
954 0be81d73 Sofia Papagiannaki
        super(MenuItem, self).__setitem__(key, value)
955 0be81d73 Sofia Papagiannaki
        if key in ('url', 'submenu'):
956 0be81d73 Sofia Papagiannaki
            self.__set_is_active__()
957 0be81d73 Sofia Papagiannaki
958 0be81d73 Sofia Papagiannaki
    def __set_is_active__(self):
959 0be81d73 Sofia Papagiannaki
        if self.get('is_active'):
960 0be81d73 Sofia Papagiannaki
            return
961 0be81d73 Sofia Papagiannaki
        if self.current_path.startswith(self.get('url')):
962 0be81d73 Sofia Papagiannaki
            self.__setitem__('is_active', True)
963 0be81d73 Sofia Papagiannaki
        else:
964 0be81d73 Sofia Papagiannaki
            submenu = self.get('submenu', ())
965 0be81d73 Sofia Papagiannaki
            current = (i for i in submenu if i.get('url') == self.current_path)
966 0be81d73 Sofia Papagiannaki
            try:
967 0be81d73 Sofia Papagiannaki
                current_node = current.next()
968 0be81d73 Sofia Papagiannaki
                if not current_node.get('is_active'):
969 0be81d73 Sofia Papagiannaki
                    current_node.__setitem__('is_active', True)
970 0be81d73 Sofia Papagiannaki
                self.__setitem__('is_active', True)
971 0be81d73 Sofia Papagiannaki
            except StopIteration:
972 0be81d73 Sofia Papagiannaki
                return
973 0be81d73 Sofia Papagiannaki
974 0be81d73 Sofia Papagiannaki
    def __setattribute__(self, name, value):
975 0be81d73 Sofia Papagiannaki
        super(MenuItem, self).__setattribute__(name, value)
976 0be81d73 Sofia Papagiannaki
        if name == 'current_path':
977 0be81d73 Sofia Papagiannaki
            self.__set_is_active__()
978 58350a50 Sofia Papagiannaki
979 58350a50 Sofia Papagiannaki
980 58350a50 Sofia Papagiannaki
def get_services(request):
981 58350a50 Sofia Papagiannaki
    callback = request.GET.get('callback', None)
982 58350a50 Sofia Papagiannaki
    mimetype = 'application/json'
983 bea584e1 Giorgos Korfiatis
    data = json.dumps(Component.catalog().values())
984 58350a50 Sofia Papagiannaki
985 58350a50 Sofia Papagiannaki
    if callback:
986 58350a50 Sofia Papagiannaki
        # Consume session messages. When get_services is loaded from an astakos
987 58350a50 Sofia Papagiannaki
        # page, messages should have already been consumed in the html
988 58350a50 Sofia Papagiannaki
        # response. When get_services is loaded from another domain/service we
989 58350a50 Sofia Papagiannaki
        # consume them here so that no stale messages to appear if user visits
990 58350a50 Sofia Papagiannaki
        # an astakos view later on.
991 58350a50 Sofia Papagiannaki
        # TODO: messages could be served to other services/sites in the dict
992 58350a50 Sofia Papagiannaki
        # response of get_services and/or get_menu. Services could handle those
993 58350a50 Sofia Papagiannaki
        # messages respectively.
994 58350a50 Sofia Papagiannaki
        messages_list = list(messages.get_messages(request))
995 58350a50 Sofia Papagiannaki
        mimetype = 'application/javascript'
996 58350a50 Sofia Papagiannaki
        data = '%s(%s)' % (callback, data)
997 58350a50 Sofia Papagiannaki
998 58350a50 Sofia Papagiannaki
    return HttpResponse(content=data, mimetype=mimetype)