Mercurial > personas_backend
annotate PersonasBackend/personas/views.py @ 40:748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Mon, 03 Mar 2008 19:39:53 -0600 |
parents | bd1cdb15ef85 |
children | ced361f3d90b |
rev | line source |
---|---|
40
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
1 from django.http import HttpResponseRedirect |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
2 from django.template import RequestContext |
31
ada72ce61fc2
Added a trivial login page.
Atul Varma <varmaa@toolness.com>
parents:
27
diff
changeset
|
3 from django.contrib.auth.decorators import login_required |
39
bd1cdb15ef85
Added persona form submission validation, fixed some things in model logic; submitting forms successfully now properly redirects the user to an edit page.
Atul Varma <varmaa@toolness.com>
parents:
38
diff
changeset
|
4 from django.core.urlresolvers import reverse |
40
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
5 from django.shortcuts import render_to_response |
31
ada72ce61fc2
Added a trivial login page.
Atul Varma <varmaa@toolness.com>
parents:
27
diff
changeset
|
6 |
20
ccb027c6862a
Added simple JSON views that generate personas_categories.dat and personas_all.dat.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
7 from PersonasBackend.personas import models |
27
4439a83c5dc6
Added a trivial new/edit form using the django.newforms system.
Atul Varma <varmaa@toolness.com>
parents:
25
diff
changeset
|
8 from PersonasBackend.personas import forms |
20
ccb027c6862a
Added simple JSON views that generate personas_categories.dat and personas_all.dat.
Atul Varma <varmaa@toolness.com>
parents:
2
diff
changeset
|
9 |
25 | 10 def list_view( request ): |
40
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
11 return render_to_response( |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
12 "personas/list.html", |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
13 { "personas" : models.Persona.objects.all() }, |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
14 context_instance = RequestContext(request) |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
15 ) |
37
ad477306dd51
A bit of refactoring to view logic.
Atul Varma <varmaa@toolness.com>
parents:
36
diff
changeset
|
16 |
31
ada72ce61fc2
Added a trivial login page.
Atul Varma <varmaa@toolness.com>
parents:
27
diff
changeset
|
17 @login_required |
34
f83712466fe6
Added functionality to edit an existing persona, and a number of TODOs.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
18 def edit_view( request, persona_id=None ): |
f83712466fe6
Added functionality to edit an existing persona, and a number of TODOs.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
19 # TODO: Perform permissions check to see if user has |
f83712466fe6
Added functionality to edit an existing persona, and a number of TODOs.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
20 # the rights to edit the persona or create a new one. |
f83712466fe6
Added functionality to edit an existing persona, and a number of TODOs.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
21 if persona_id is None: |
f83712466fe6
Added functionality to edit an existing persona, and a number of TODOs.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
22 persona = None |
f83712466fe6
Added functionality to edit an existing persona, and a number of TODOs.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
23 else: |
f83712466fe6
Added functionality to edit an existing persona, and a number of TODOs.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
24 # TODO: Consider using get_object_or_404() here. |
f83712466fe6
Added functionality to edit an existing persona, and a number of TODOs.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
25 persona = models.Persona.objects.get( id=persona_id ) |
f83712466fe6
Added functionality to edit an existing persona, and a number of TODOs.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
26 |
33
2b5a8b3b8bef
It's now possible for normal end-users to create new personas.
Atul Varma <varmaa@toolness.com>
parents:
31
diff
changeset
|
27 if request.method == "POST": |
40
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
28 form = forms.PersonaForm( request.POST, instance=persona ) |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
29 if form.is_valid(): |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
30 newPersona = form.save( commit=False ) |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
31 newPersona.updater = request.user |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
32 newPersona.save() |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
33 if persona is None: |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
34 msgText = "Persona created successfully." |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
35 else: |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
36 msgText = "Persona edited successfully." |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
37 request.user.message_set.create( message = msgText ) |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
38 url = reverse("edit-persona", args=[newPersona.id]) |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
39 return HttpResponseRedirect( url ) |
33
2b5a8b3b8bef
It's now possible for normal end-users to create new personas.
Atul Varma <varmaa@toolness.com>
parents:
31
diff
changeset
|
40 else: |
40
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
41 form = forms.PersonaForm( instance=persona ) |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
42 |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
43 return render_to_response( |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
44 "personas/edit.html", |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
45 { "form" : form }, |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
46 context_instance = RequestContext(request) |
748547f73357
Refactored some view logic and added messaging capabilities, so that the end-user is properly notified when persona creation/editing is successful.
Atul Varma <varmaa@toolness.com>
parents:
39
diff
changeset
|
47 ) |