Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (34.6 kB)

1 aba1e498 Antony Chazapis
# Copyright 2011-2012 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 0504f010 Kostas Papadimitriou
def update_token(request):
133 0504f010 Kostas Papadimitriou
    """
134 0504f010 Kostas Papadimitriou
    Update api token view.
135 0504f010 Kostas Papadimitriou
    """
136 0504f010 Kostas Papadimitriou
    user = request.user
137 0504f010 Kostas Papadimitriou
    user.renew_token()
138 0504f010 Kostas Papadimitriou
    user.save()
139 0504f010 Kostas Papadimitriou
    messages.success(request, astakos_messages.TOKEN_UPDATED)
140 14c76abe Kostas Papadimitriou
    return HttpResponseRedirect(reverse('api_access'))
141 0504f010 Kostas Papadimitriou
142 0504f010 Kostas Papadimitriou
143 9a06d96f Olga Brani
@require_http_methods(["GET", "POST"])
144 222305b7 Sofia Papagiannaki
@cookie_fix
145 badcb2a9 Kostas Papadimitriou
@valid_astakos_user_required
146 554ef175 Christos Stavrakakis
@transaction.commit_on_success
147 aab4d540 Sofia Papagiannaki
def invite(request, template_name='im/invitations.html', extra_context=None):
148 890b0eaf Sofia Papagiannaki
    """
149 890b0eaf Sofia Papagiannaki
    Allows a user to invite somebody else.
150 176023aa Kostas Papadimitriou

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

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

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

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

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

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

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

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

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

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

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

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

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

284 890b0eaf Sofia Papagiannaki
    **Arguments**
285 176023aa Kostas Papadimitriou

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

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

293 890b0eaf Sofia Papagiannaki
    **Template:**
294 176023aa Kostas Papadimitriou

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

297 92defad4 Sofia Papagiannaki
    **Settings:**
298 176023aa Kostas Papadimitriou

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

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

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

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

378 e7cb4085 Kostas Papadimitriou
    Upon successful user creation, if ``next`` url parameter is present the
379 e7cb4085 Kostas Papadimitriou
    user is redirected there otherwise renders the same page with a success
380 e7cb4085 Kostas Papadimitriou
    message.
381 5ce3ce4f Sofia Papagiannaki

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

384 890b0eaf Sofia Papagiannaki
    **Arguments**
385 5ce3ce4f Sofia Papagiannaki

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

390 890b0eaf Sofia Papagiannaki
    ``extra_context``
391 890b0eaf Sofia Papagiannaki
        An dictionary of variables to add to the template context.
392 176023aa Kostas Papadimitriou

393 43332a76 Kostas Papadimitriou
    ``on_success``
394 43332a76 Kostas Papadimitriou
        Resolvable view name to redirect on registration success.
395 43332a76 Kostas Papadimitriou

396 890b0eaf Sofia Papagiannaki
    **Template:**
397 5ce3ce4f Sofia Papagiannaki

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

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

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

523 890b0eaf Sofia Papagiannaki
    **Arguments**
524 176023aa Kostas Papadimitriou

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

529 890b0eaf Sofia Papagiannaki
    ``extra_context``
530 890b0eaf Sofia Papagiannaki
        An dictionary of variables to add to the template context.
531 176023aa Kostas Papadimitriou

532 890b0eaf Sofia Papagiannaki
    **Template:**
533 176023aa Kostas Papadimitriou

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

536 890b0eaf Sofia Papagiannaki
    **Settings:**
537 176023aa Kostas Papadimitriou

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

613 554ef175 Christos Stavrakakis
    The user state will be updated only if the email will be send successfully.
614 2126d85d Sofia Papagiannaki
    """
615 2126d85d Sofia Papagiannaki
    token = request.GET.get('auth')
616 2126d85d Sofia Papagiannaki
    next = request.GET.get('next')
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 a53f77ee Kostas Papadimitriou
        transaction.rollback()
622 92a8abc9 Kostas Papadimitriou
        return HttpResponseRedirect(reverse('index'))
623 5ce3ce4f Sofia Papagiannaki
624 e7cb4085 Kostas Papadimitriou
    try:
625 e7cb4085 Kostas Papadimitriou
        user = AstakosUser.objects.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 554ef175 Christos Stavrakakis
    next = settings.ACTIVATION_REDIRECT_URL or next
638 554ef175 Christos Stavrakakis
    response = HttpResponseRedirect(reverse('index'))
639 554ef175 Christos Stavrakakis
    if user.is_active:
640 554ef175 Christos Stavrakakis
        response = prepare_response(request, user, next, renew=True)
641 554ef175 Christos Stavrakakis
        messages.success(request, _(result.message))
642 e7cb4085 Kostas Papadimitriou
    else:
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 e7cb4085 Kostas Papadimitriou
def send_activation(request, user_id, template_name='im/login.html',
794 e7cb4085 Kostas Papadimitriou
                    extra_context=None):
795 792c2f3b Olga Brani
796 badcb2a9 Kostas Papadimitriou
    if request.user.is_authenticated():
797 e7cb4085 Kostas Papadimitriou
        return HttpResponseRedirect(reverse('index'))
798 badcb2a9 Kostas Papadimitriou
799 1f3b4b39 Sofia Papagiannaki
    extra_context = extra_context or {}
800 1f3b4b39 Sofia Papagiannaki
    try:
801 1f3b4b39 Sofia Papagiannaki
        u = AstakosUser.objects.get(id=user_id)
802 1f3b4b39 Sofia Papagiannaki
    except AstakosUser.DoesNotExist:
803 c0b26605 Sofia Papagiannaki
        messages.error(request, _(astakos_messages.ACCOUNT_UNKNOWN))
804 1f3b4b39 Sofia Papagiannaki
    else:
805 e7cb4085 Kostas Papadimitriou
        if u.email_verified:
806 e7cb4085 Kostas Papadimitriou
            logger.warning("[resend activation] Account already verified: %s",
807 e7cb4085 Kostas Papadimitriou
                           u.log_display)
808 e7cb4085 Kostas Papadimitriou
809 e7cb4085 Kostas Papadimitriou
            messages.error(request,
810 e7cb4085 Kostas Papadimitriou
                           _(astakos_messages.ACCOUNT_ALREADY_VERIFIED))
811 e7cb4085 Kostas Papadimitriou
        else:
812 e7cb4085 Kostas Papadimitriou
            activation_backend = activation_backends.get_backend()
813 e7cb4085 Kostas Papadimitriou
            activation_backend.send_user_verification_email(u)
814 e7cb4085 Kostas Papadimitriou
            messages.success(request, astakos_messages.ACTIVATION_SENT)
815 ff81d0d9 Kostas Papadimitriou
816 ff81d0d9 Kostas Papadimitriou
    return HttpResponseRedirect(reverse('index'))
817 5ce3ce4f Sofia Papagiannaki
818 73fbaec4 Sofia Papagiannaki
819 73fbaec4 Sofia Papagiannaki
@require_http_methods(["GET"])
820 222305b7 Sofia Papagiannaki
@cookie_fix
821 badcb2a9 Kostas Papadimitriou
@valid_astakos_user_required
822 666c7490 Sofia Papagiannaki
def resource_usage(request):
823 e9ef5009 Kostas Papadimitriou
824 37d59b27 Kostas Papadimitriou
    resources_meta = presentation.RESOURCES
825 37d59b27 Kostas Papadimitriou
826 4e03ba30 Kostas Papadimitriou
    current_usage = quotas.get_user_quotas(request.user)
827 4e03ba30 Kostas Papadimitriou
    current_usage = json.dumps(current_usage['system'])
828 37d59b27 Kostas Papadimitriou
    resource_catalog, resource_groups = _resources_catalog(for_usage=True)
829 37d59b27 Kostas Papadimitriou
    if resource_catalog is False:
830 37d59b27 Kostas Papadimitriou
        # on fail resource_groups contains the result object
831 37d59b27 Kostas Papadimitriou
        result = resource_groups
832 37d59b27 Kostas Papadimitriou
        messages.error(request, 'Unable to retrieve system resources: %s' %
833 37d59b27 Kostas Papadimitriou
                       result.reason)
834 37d59b27 Kostas Papadimitriou
835 4e03ba30 Kostas Papadimitriou
    resource_catalog = json.dumps(resource_catalog)
836 4e03ba30 Kostas Papadimitriou
    resource_groups = json.dumps(resource_groups)
837 37d59b27 Kostas Papadimitriou
    resources_order = json.dumps(resources_meta.get('resources_order'))
838 401089d8 Kostas Papadimitriou
839 666c7490 Sofia Papagiannaki
    return render_response('im/resource_usage.html',
840 3cbd5e47 Olga Brani
                           context_instance=get_context(request),
841 4e03ba30 Kostas Papadimitriou
                           resource_catalog=resource_catalog,
842 4e03ba30 Kostas Papadimitriou
                           resource_groups=resource_groups,
843 4e03ba30 Kostas Papadimitriou
                           resources_order=resources_order,
844 4e03ba30 Kostas Papadimitriou
                           current_usage=current_usage,
845 e7cb4085 Kostas Papadimitriou
                           token_cookie_name=settings.COOKIE_NAME,
846 4e03ba30 Kostas Papadimitriou
                           usage_update_interval=
847 e7cb4085 Kostas Papadimitriou
                           settings.USAGE_UPDATE_INTERVAL)
848 4e03ba30 Kostas Papadimitriou
849 9a06d96f Olga Brani
850 f432088a Kostas Papadimitriou
# TODO: action only on POST and user should confirm the removal
851 327a4c1e Olga Brani
@require_http_methods(["POST"])
852 222305b7 Sofia Papagiannaki
@cookie_fix
853 9d20fe23 Kostas Papadimitriou
@valid_astakos_user_required
854 d2633501 Kostas Papadimitriou
def remove_auth_provider(request, pk):
855 f432088a Kostas Papadimitriou
    try:
856 9d20fe23 Kostas Papadimitriou
        provider = request.user.auth_providers.get(pk=int(pk)).settings
857 f432088a Kostas Papadimitriou
    except AstakosUserAuthProvider.DoesNotExist:
858 f432088a Kostas Papadimitriou
        raise Http404
859 f432088a Kostas Papadimitriou
860 9d20fe23 Kostas Papadimitriou
    if provider.get_remove_policy:
861 9d20fe23 Kostas Papadimitriou
        messages.success(request, provider.get_removed_msg)
862 9d20fe23 Kostas Papadimitriou
        provider.remove_from_user()
863 d2633501 Kostas Papadimitriou
        return HttpResponseRedirect(reverse('edit_profile'))
864 d2633501 Kostas Papadimitriou
    else:
865 f432088a Kostas Papadimitriou
        raise PermissionDenied
866 5ebebb20 Olga Brani
867 792c2f3b Olga Brani
868 9d20fe23 Kostas Papadimitriou
@require_http_methods(["GET"])
869 9d20fe23 Kostas Papadimitriou
@required_auth_methods_assigned(allow_access=True)
870 9d20fe23 Kostas Papadimitriou
@login_required
871 222305b7 Sofia Papagiannaki
@cookie_fix
872 9d20fe23 Kostas Papadimitriou
@signed_terms_required
873 003d8fcf Olga Brani
def landing(request):
874 bea584e1 Giorgos Korfiatis
    context = {'services': Component.catalog(orderfor='dashboard')}
875 003d8fcf Olga Brani
    return render_response(
876 003d8fcf Olga Brani
        'im/landing.html',
877 0156e40c Kostas Papadimitriou
        context_instance=get_context(request), **context)
878 b87429e1 Olga Brani
879 ca5148f2 Kostas Papadimitriou
880 0be81d73 Sofia Papagiannaki
@cookie_fix
881 0be81d73 Sofia Papagiannaki
def get_menu(request, with_extra_links=False, with_signout=True):
882 0be81d73 Sofia Papagiannaki
    user = request.user
883 0be81d73 Sofia Papagiannaki
    index_url = reverse('index')
884 0be81d73 Sofia Papagiannaki
885 0be81d73 Sofia Papagiannaki
    if isinstance(user, User) and user.is_authenticated():
886 0be81d73 Sofia Papagiannaki
        l = []
887 0be81d73 Sofia Papagiannaki
        append = l.append
888 0be81d73 Sofia Papagiannaki
        item = MenuItem
889 0be81d73 Sofia Papagiannaki
        item.current_path = request.build_absolute_uri(request.path)
890 0be81d73 Sofia Papagiannaki
        append(item(url=request.build_absolute_uri(reverse('index')),
891 0be81d73 Sofia Papagiannaki
                    name=user.email))
892 0be81d73 Sofia Papagiannaki
        if with_extra_links:
893 0be81d73 Sofia Papagiannaki
            append(item(url=request.build_absolute_uri(reverse('landing')),
894 0be81d73 Sofia Papagiannaki
                        name="Overview"))
895 0be81d73 Sofia Papagiannaki
        if with_signout:
896 0be81d73 Sofia Papagiannaki
            append(item(url=request.build_absolute_uri(reverse('landing')),
897 0be81d73 Sofia Papagiannaki
                        name="Dashboard"))
898 0be81d73 Sofia Papagiannaki
        if with_extra_links:
899 8fb8d0cf Giorgos Korfiatis
            append(
900 8fb8d0cf Giorgos Korfiatis
                item(
901 8fb8d0cf Giorgos Korfiatis
                    url=request.build_absolute_uri(reverse('edit_profile')),
902 8fb8d0cf Giorgos Korfiatis
                    name="Profile"))
903 0be81d73 Sofia Papagiannaki
904 0be81d73 Sofia Papagiannaki
        if with_extra_links:
905 0be81d73 Sofia Papagiannaki
            if settings.INVITATIONS_ENABLED:
906 0be81d73 Sofia Papagiannaki
                append(item(url=request.build_absolute_uri(reverse('invite')),
907 0be81d73 Sofia Papagiannaki
                            name="Invitations"))
908 0be81d73 Sofia Papagiannaki
909 7763f5c6 Kostas Papadimitriou
            append(item(url=request.build_absolute_uri(reverse('api_access')),
910 7763f5c6 Kostas Papadimitriou
                        name="API access"))
911 7763f5c6 Kostas Papadimitriou
912 8fb8d0cf Giorgos Korfiatis
            append(
913 8fb8d0cf Giorgos Korfiatis
                item(
914 8fb8d0cf Giorgos Korfiatis
                    url=request.build_absolute_uri(reverse('resource_usage')),
915 8fb8d0cf Giorgos Korfiatis
                    name="Usage"))
916 0be81d73 Sofia Papagiannaki
917 0be81d73 Sofia Papagiannaki
            if settings.PROJECTS_VISIBLE:
918 8fb8d0cf Giorgos Korfiatis
                append(
919 8fb8d0cf Giorgos Korfiatis
                    item(
920 8fb8d0cf Giorgos Korfiatis
                        url=request.build_absolute_uri(
921 8fb8d0cf Giorgos Korfiatis
                            reverse('project_list')),
922 8fb8d0cf Giorgos Korfiatis
                        name="Projects"))
923 0be81d73 Sofia Papagiannaki
924 0be81d73 Sofia Papagiannaki
            append(item(url=request.build_absolute_uri(reverse('feedback')),
925 0be81d73 Sofia Papagiannaki
                        name="Contact"))
926 0be81d73 Sofia Papagiannaki
        if with_signout:
927 0be81d73 Sofia Papagiannaki
            append(item(url=request.build_absolute_uri(reverse('logout')),
928 0be81d73 Sofia Papagiannaki
                        name="Sign out"))
929 0be81d73 Sofia Papagiannaki
    else:
930 0be81d73 Sofia Papagiannaki
        l = [{'url': request.build_absolute_uri(index_url),
931 0be81d73 Sofia Papagiannaki
              'name': _("Sign in")}]
932 0be81d73 Sofia Papagiannaki
933 0be81d73 Sofia Papagiannaki
    callback = request.GET.get('callback', None)
934 0be81d73 Sofia Papagiannaki
    data = json.dumps(tuple(l))
935 0be81d73 Sofia Papagiannaki
    mimetype = 'application/json'
936 0be81d73 Sofia Papagiannaki
937 0be81d73 Sofia Papagiannaki
    if callback:
938 0be81d73 Sofia Papagiannaki
        mimetype = 'application/javascript'
939 0be81d73 Sofia Papagiannaki
        data = '%s(%s)' % (callback, data)
940 0be81d73 Sofia Papagiannaki
941 0be81d73 Sofia Papagiannaki
    return HttpResponse(content=data, mimetype=mimetype)
942 0be81d73 Sofia Papagiannaki
943 0be81d73 Sofia Papagiannaki
944 0be81d73 Sofia Papagiannaki
class MenuItem(dict):
945 0be81d73 Sofia Papagiannaki
    current_path = ''
946 0be81d73 Sofia Papagiannaki
947 0be81d73 Sofia Papagiannaki
    def __init__(self, *args, **kwargs):
948 0be81d73 Sofia Papagiannaki
        super(MenuItem, self).__init__(*args, **kwargs)
949 0be81d73 Sofia Papagiannaki
        if kwargs.get('url') or kwargs.get('submenu'):
950 0be81d73 Sofia Papagiannaki
            self.__set_is_active__()
951 0be81d73 Sofia Papagiannaki
952 0be81d73 Sofia Papagiannaki
    def __setitem__(self, key, value):
953 0be81d73 Sofia Papagiannaki
        super(MenuItem, self).__setitem__(key, value)
954 0be81d73 Sofia Papagiannaki
        if key in ('url', 'submenu'):
955 0be81d73 Sofia Papagiannaki
            self.__set_is_active__()
956 0be81d73 Sofia Papagiannaki
957 0be81d73 Sofia Papagiannaki
    def __set_is_active__(self):
958 0be81d73 Sofia Papagiannaki
        if self.get('is_active'):
959 0be81d73 Sofia Papagiannaki
            return
960 0be81d73 Sofia Papagiannaki
        if self.current_path.startswith(self.get('url')):
961 0be81d73 Sofia Papagiannaki
            self.__setitem__('is_active', True)
962 0be81d73 Sofia Papagiannaki
        else:
963 0be81d73 Sofia Papagiannaki
            submenu = self.get('submenu', ())
964 0be81d73 Sofia Papagiannaki
            current = (i for i in submenu if i.get('url') == self.current_path)
965 0be81d73 Sofia Papagiannaki
            try:
966 0be81d73 Sofia Papagiannaki
                current_node = current.next()
967 0be81d73 Sofia Papagiannaki
                if not current_node.get('is_active'):
968 0be81d73 Sofia Papagiannaki
                    current_node.__setitem__('is_active', True)
969 0be81d73 Sofia Papagiannaki
                self.__setitem__('is_active', True)
970 0be81d73 Sofia Papagiannaki
            except StopIteration:
971 0be81d73 Sofia Papagiannaki
                return
972 0be81d73 Sofia Papagiannaki
973 0be81d73 Sofia Papagiannaki
    def __setattribute__(self, name, value):
974 0be81d73 Sofia Papagiannaki
        super(MenuItem, self).__setattribute__(name, value)
975 0be81d73 Sofia Papagiannaki
        if name == 'current_path':
976 0be81d73 Sofia Papagiannaki
            self.__set_is_active__()
977 58350a50 Sofia Papagiannaki
978 58350a50 Sofia Papagiannaki
979 58350a50 Sofia Papagiannaki
def get_services(request):
980 58350a50 Sofia Papagiannaki
    callback = request.GET.get('callback', None)
981 58350a50 Sofia Papagiannaki
    mimetype = 'application/json'
982 bea584e1 Giorgos Korfiatis
    data = json.dumps(Component.catalog().values())
983 58350a50 Sofia Papagiannaki
984 58350a50 Sofia Papagiannaki
    if callback:
985 58350a50 Sofia Papagiannaki
        # Consume session messages. When get_services is loaded from an astakos
986 58350a50 Sofia Papagiannaki
        # page, messages should have already been consumed in the html
987 58350a50 Sofia Papagiannaki
        # response. When get_services is loaded from another domain/service we
988 58350a50 Sofia Papagiannaki
        # consume them here so that no stale messages to appear if user visits
989 58350a50 Sofia Papagiannaki
        # an astakos view later on.
990 58350a50 Sofia Papagiannaki
        # TODO: messages could be served to other services/sites in the dict
991 58350a50 Sofia Papagiannaki
        # response of get_services and/or get_menu. Services could handle those
992 58350a50 Sofia Papagiannaki
        # messages respectively.
993 58350a50 Sofia Papagiannaki
        messages_list = list(messages.get_messages(request))
994 58350a50 Sofia Papagiannaki
        mimetype = 'application/javascript'
995 58350a50 Sofia Papagiannaki
        data = '%s(%s)' % (callback, data)
996 58350a50 Sofia Papagiannaki
997 58350a50 Sofia Papagiannaki
    return HttpResponse(content=data, mimetype=mimetype)