Mercurial > personas_backend
annotate PersonasBackend/personas/views.py @ 46:f92d5de36a4d
Added a TODO.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Tue, 04 Mar 2008 11:48:32 -0600 |
parents | 6cde01f6012a |
children | 7a20574bedc8 |
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 |
42
6cde01f6012a
Resolved a TODO involving get_object_or_404().
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
5 from django.shortcuts import render_to_response, get_object_or_404 |
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", |
46 | 13 # TODO: Show only the personas that the currently |
14 # logged-in user has access to viewing. | |
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
|
15 { "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
|
16 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
|
17 ) |
37
ad477306dd51
A bit of refactoring to view logic.
Atul Varma <varmaa@toolness.com>
parents:
36
diff
changeset
|
18 |
41
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
19 def _rename_file( filedict, persona ): |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
20 import os |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
21 import time |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
22 |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
23 # TODO: This feels hacky. It might be cleaner to just rename the |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
24 # file after the commit is made or something. |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
25 |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
26 origExt = os.path.splitext( filedict["filename"] )[1] |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
27 # TODO: Normalize origExt somehow? |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
28 if persona: |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
29 filename = "persona_%d_rev_%d%s" % ( |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
30 persona.id, |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
31 persona.revision + 1, |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
32 origExt |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
33 ) |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
34 else: |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
35 filename = "new_persona_%Y_%m_%d_%H_%M_%S" + origExt |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
36 filename = time.strftime( filename ) |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
37 filedict["filename"] = filename |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
38 |
31
ada72ce61fc2
Added a trivial login page.
Atul Varma <varmaa@toolness.com>
parents:
27
diff
changeset
|
39 @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
|
40 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
|
41 # 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
|
42 # 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
|
43 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
|
44 persona = None |
f83712466fe6
Added functionality to edit an existing persona, and a number of TODOs.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
45 else: |
42
6cde01f6012a
Resolved a TODO involving get_object_or_404().
Atul Varma <varmaa@toolness.com>
parents:
41
diff
changeset
|
46 persona = get_object_or_404( models.Persona, id=persona_id ) |
34
f83712466fe6
Added functionality to edit an existing persona, and a number of TODOs.
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
47 |
33
2b5a8b3b8bef
It's now possible for normal end-users to create new personas.
Atul Varma <varmaa@toolness.com>
parents:
31
diff
changeset
|
48 if request.method == "POST": |
41
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
49 for img_name in request.FILES: |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
50 _rename_file( request.FILES[img_name], persona ) |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
51 form = forms.PersonaForm( request.POST, request.FILES, |
ced361f3d90b
Uploading of header/footer images now works.
Atul Varma <varmaa@toolness.com>
parents:
40
diff
changeset
|
52 instance=persona ) |
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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 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
|
64 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
|
65 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
|
66 |
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
|
67 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
|
68 "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
|
69 { "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
|
70 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
|
71 ) |