Revision 13858d75 astakos/im/views.py

b/astakos/im/views.py
1 1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2
# 
2
#
3 3
# Redistribution and use in source and binary forms, with or
4 4
# without modification, are permitted provided that the following
5 5
# conditions are met:
6
# 
6
#
7 7
#   1. Redistributions of source code must retain the above
8 8
#      copyright notice, this list of conditions and the following
9 9
#      disclaimer.
10
# 
10
#
11 11
#   2. Redistributions in binary form must reproduce the above
12 12
#      copyright notice, this list of conditions and the following
13 13
#      disclaimer in the documentation and/or other materials
14 14
#      provided with the distribution.
15
# 
15
#
16 16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
......
25 25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 27
# POSSIBILITY OF SUCH DAMAGE.
28
# 
28
#
29 29
# The views and conclusions contained in the software and
30 30
# documentation are those of the authors and should not be
31 31
# interpreted as representing official policies, either expressed
......
81 81
def index(request, login_template_name='login.html', profile_template_name='profile.html', extra_context={}):
82 82
    """
83 83
    If there is logged on user renders the profile page otherwise renders login page.
84
    
84

  
85 85
    **Arguments**
86
    
86

  
87 87
    ``login_template_name``
88 88
        A custom login template to use. This is optional; if not specified,
89 89
        this will default to ``login.html``.
90
    
90

  
91 91
    ``profile_template_name``
92 92
        A custom profile template to use. This is optional; if not specified,
93 93
        this will default to ``profile.html``.
94
    
94

  
95 95
    ``extra_context``
96 96
        An dictionary of variables to add to the template context.
97
    
97

  
98 98
    **Template:**
99
    
99

  
100 100
    profile.html or login.html or ``template_name`` keyword argument.
101
    
101

  
102 102
    """
103 103
    template_name = login_template_name
104 104
    formclass = 'LoginForm'
......
139 139
def invite(request, template_name='invitations.html', extra_context={}):
140 140
    """
141 141
    Allows a user to invite somebody else.
142
    
142

  
143 143
    In case of GET request renders a form for providing the invitee information.
144 144
    In case of POST checks whether the user has not run out of invitations and then
145 145
    sends an invitation email to singup to the service.
146
    
146

  
147 147
    The view uses commit_manually decorator in order to ensure the number of the
148 148
    user invitations is going to be updated only if the email has been successfully sent.
149
    
149

  
150 150
    If the user isn't logged in, redirects to settings.LOGIN_URL.
151
    
151

  
152 152
    **Arguments**
153
    
153

  
154 154
    ``template_name``
155 155
        A custom template to use. This is optional; if not specified,
156 156
        this will default to ``invitations.html``.
157
    
157

  
158 158
    ``extra_context``
159 159
        An dictionary of variables to add to the template context.
160
    
160

  
161 161
    **Template:**
162
    
162

  
163 163
    invitations.html or ``template_name`` keyword argument.
164
    
164

  
165 165
    **Settings:**
166
    
166

  
167 167
    The view expectes the following settings are defined:
168
    
168

  
169 169
    * LOGIN_URL: login uri
170 170
    * SIGNUP_TARGET: Where users should signup with their invitation code
171 171
    * DEFAULT_CONTACT_EMAIL: service support email
......
174 174
    status = None
175 175
    message = None
176 176
    inviter = AstakosUser.objects.get(username = request.user.username)
177
    
177

  
178 178
    if request.method == 'POST':
179 179
        username = request.POST.get('uniq')
180 180
        realname = request.POST.get('realname')
181
        
181

  
182 182
        if inviter.invitations > 0:
183 183
            code = _generate_invitation_code()
184 184
            invitation, created = Invitation.objects.get_or_create(
185 185
                inviter=inviter,
186 186
                username=username,
187 187
                defaults={'code': code, 'realname': realname})
188
            
188

  
189 189
            try:
190 190
                baseurl = request.build_absolute_uri('/').rstrip('/')
191 191
                _send_invitation(request, baseurl, invitation)
......
203 203
            status = messages.ERROR
204 204
            message = _('No invitations left')
205 205
    messages.add_message(request, status, message)
206
    
206

  
207 207
    sent = [{'email': inv.username,
208 208
                 'realname': inv.realname,
209 209
                 'is_accepted': inv.is_accepted}
......
218 218
def edit_profile(request, template_name='profile.html', extra_context={}):
219 219
    """
220 220
    Allows a user to edit his/her profile.
221
    
221

  
222 222
    In case of GET request renders a form for displaying the user information.
223 223
    In case of POST updates the user informantion and redirects to ``next``
224 224
    url parameter if exists.
225
    
226
    If the user isn't logged in, redirects to settings.LOGIN_URL.  
227
    
225

  
226
    If the user isn't logged in, redirects to settings.LOGIN_URL.
227

  
228 228
    **Arguments**
229
    
229

  
230 230
    ``template_name``
231 231
        A custom template to use. This is optional; if not specified,
232 232
        this will default to ``profile.html``.
233
    
233

  
234 234
    ``extra_context``
235 235
        An dictionary of variables to add to the template context.
236
    
236

  
237 237
    **Template:**
238
    
238

  
239 239
    profile.html or ``template_name`` keyword argument.
240 240
    """
241 241
    form = ProfileForm(instance=request.user)
......
261 261
def signup(request, template_name='signup.html', extra_context={}, backend=None):
262 262
    """
263 263
    Allows a user to create a local account.
264
    
264

  
265 265
    In case of GET request renders a form for providing the user information.
266 266
    In case of POST handles the signup.
267
    
267

  
268 268
    The user activation will be delegated to the backend specified by the ``backend`` keyword argument
269 269
    if present, otherwise to the ``astakos.im.backends.InvitationBackend``
270 270
    if settings.INVITATIONS_ENABLED is True or ``astakos.im.backends.SimpleBackend`` if not
271 271
    (see backends);
272
    
272

  
273 273
    Upon successful user creation if ``next`` url parameter is present the user is redirected there
274 274
    otherwise renders the same page with a success message.
275
    
275

  
276 276
    On unsuccessful creation, renders the same page with an error message.
277
    
277

  
278 278
    **Arguments**
279
    
279

  
280 280
    ``template_name``
281 281
        A custom template to use. This is optional; if not specified,
282 282
        this will default to ``signup.html``.
283
    
283

  
284 284
    ``extra_context``
285 285
        An dictionary of variables to add to the template context.
286
    
286

  
287 287
    **Template:**
288
    
288

  
289 289
    signup.html or ``template_name`` keyword argument.
290 290
    """
291 291
    try:
......
309 309
                    if status == messages.SUCCESS:
310 310
                        if next:
311 311
                            return redirect(next)
312
                    messages.add_message(request, status, message)    
312
                    messages.add_message(request, status, message)
313 313
    except (Invitation.DoesNotExist, ValueError), e:
314 314
        messages.add_message(request, messages.ERROR, e)
315 315
        for provider in settings.IM_MODULES:
......
323 323
def send_feedback(request, template_name='feedback.html', email_template_name='feedback_mail.txt', extra_context={}):
324 324
    """
325 325
    Allows a user to send feedback.
326
    
326

  
327 327
    In case of GET request renders a form for providing the feedback information.
328 328
    In case of POST sends an email to support team.
329
    
330
    If the user isn't logged in, redirects to settings.LOGIN_URL.  
331
    
329

  
330
    If the user isn't logged in, redirects to settings.LOGIN_URL.
331

  
332 332
    **Arguments**
333
    
333

  
334 334
    ``template_name``
335 335
        A custom template to use. This is optional; if not specified,
336 336
        this will default to ``feedback.html``.
337
    
337

  
338 338
    ``extra_context``
339 339
        An dictionary of variables to add to the template context.
340
    
340

  
341 341
    **Template:**
342
    
342

  
343 343
    signup.html or ``template_name`` keyword argument.
344
    
344

  
345 345
    **Settings:**
346
    
346

  
347 347
    * DEFAULT_CONTACT_EMAIL: List of feedback recipients
348 348
    """
349 349
    if request.method == 'GET':
......
351 351
    if request.method == 'POST':
352 352
        if not request.user:
353 353
            return HttpResponse('Unauthorized', status=401)
354
        
354

  
355 355
        form = FeedbackForm(request.POST)
356 356
        if form.is_valid():
357 357
            sitename, sitedomain = get_current_site(request, use_https=request.is_secure())
......
362 362
                        'message': form.cleaned_data['feedback_msg'],
363 363
                        'data': form.cleaned_data['feedback_data'],
364 364
                        'request': request})
365
            
365

  
366 366
            try:
367 367
                send_mail(subject, content, from_email, recipient_list)
368 368
                message = _('Feedback successfully sent')
......
375 375
                           form = form,
376 376
                           context_instance = get_context(request, extra_context))
377 377

  
378
def create_user(request, form, backend=None, post_data={}, next = None, template_name='login.html', extra_context={}): 
378
def create_user(request, form, backend=None, post_data={}, next = None, template_name='login.html', extra_context={}):
379 379
    try:
380 380
        if not backend:
381 381
            backend = get_backend(request)
......
401 401
    response.delete_cookie(settings.COOKIE_NAME)
402 402
    response['Location'] = reverse('django.contrib.auth.views.logout')
403 403
    response.status_code = 302
404
    return response
404
    return response

Also available in: Unified diff