daks / django-userthemes
A Django user themes application
Clone this repository (size: 66.6 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/daks/django-userthemes/
| commit 26: | a2d7cae914e0 |
| parent 25: | d8ec3a0bc6e7 |
| branch: | default |
eliminating CRLF on views.py
Changed (Δ124 bytes):
raw changeset »
userthemes/views.py (124 lines added, 124 lines removed)
Up to file-list userthemes/views.py:
1 |
#!/usr/bin/env python |
|
2 |
# -*- coding: utf-8 -*- |
|
3 |
# |
|
4 |
# Copyright (C) 2009 Éric Veiras Galisson. |
|
5 |
# |
|
6 |
# This program is free software: you can redistribute it and/or modify |
|
7 |
# it under the terms of the GNU General Public License as published by |
|
8 |
# the Free Software Foundation, either version 2 of the License, or |
|
9 |
# (at your option) any later version. |
|
10 |
# |
|
11 |
# This program is distributed in the hope that it will be useful, |
|
12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
# GNU General Public License for more details. |
|
15 |
# |
|
16 |
# You should have received a copy of the GNU General Public License |
|
17 |
# along with this program. If not, see http://www.gnu.org/licenses/. |
|
18 |
# |
|
19 |
""" |
|
20 |
Views for creating and editing user theme preference and for serving |
|
21 |
static content. |
|
22 |
""" |
|
23 |
from django.core.exceptions import ObjectDoesNotExist |
|
24 |
from django.http import HttpResponseRedirect |
|
25 |
from django.core.urlresolvers import reverse |
|
26 |
from django.shortcuts import render_to_response |
|
27 |
from django.template import RequestContext |
|
28 |
||
29 |
from userthemes.models import UserTheme |
|
30 |
from userthemes.forms import UserThemeForm |
|
31 |
from userthemes.middleware import get_current_user |
|
32 |
||
33 |
||
34 |
def create_usertheme(request, |
|
35 |
form_class=None, |
|
36 |
template_name='userthemes/create_usertheme.html', |
|
37 |
success_url=None): |
|
38 |
""" |
|
39 |
Create a user theme for the current user, if one doesn't already |
|
40 |
exist. |
|
41 |
||
42 |
If the user already has a UserTheme, a redirect will be issued to the |
|
43 |
:view:`userthemes.views.edit_usertheme` view. |
|
44 |
||
45 |
Optional arguments |
|
46 |
* form_class |
|
47 |
* template_name |
|
48 |
* success_url |
|
49 |
""" |
|
50 |
user_id = get_current_user().id |
|
51 |
||
52 |
if success_url is None: |
|
53 |
success_url = reverse('userthemes-edit') |
|
54 |
||
55 |
try: |
|
56 |
usertheme_obj = UserTheme.objects.get(user=user_id) |
|
57 |
return HttpResponseRedirect(reverse('userthemes-edit')) |
|
58 |
except ObjectDoesNotExist: |
|
59 |
pass |
|
60 |
||
61 |
if form_class is None: |
|
62 |
form_class = UserThemeForm |
|
63 |
||
64 |
if request.method == 'POST': |
|
65 |
form = form_class(request.POST, request.FILES) |
|
66 |
if form.is_valid(): |
|
67 |
usertheme_obj = form.save(commit=False) |
|
68 |
usertheme_obj.user_id = user_id |
|
69 |
usertheme_obj.save() |
|
70 |
response = HttpResponseRedirect(success_url) |
|
71 |
response.set_cookie("usertheme", form.cleaned_data['theme']) |
|
72 |
return response |
|
73 |
else: |
|
74 |
form = form_class() |
|
75 |
||
76 |
return render_to_response(template_name, |
|
77 |
{'form': form}, |
|
78 |
context_instance=RequestContext(request)) |
|
79 |
||
80 |
||
81 |
def edit_usertheme(request, |
|
82 |
form_class=None, |
|
83 |
template_name='userthemes/edit_usertheme.html', |
|
84 |
success_url=None): |
|
85 |
""" |
|
86 |
Edit the current user's theme. |
|
87 |
||
88 |
If the user does not already have a UserTheme, a redirect will be issued |
|
89 |
to the :view:`userthemes.views.create_usertheme` view. |
|
90 |
||
91 |
Optional arguments |
|
92 |
* form_class |
|
93 |
* template_name |
|
94 |
* success_url |
|
95 |
""" |
|
96 |
user_id = get_current_user().id |
|
97 |
||
98 |
if success_url is None: |
|
99 |
success_url = reverse('userthemes-edit') |
|
100 |
||
101 |
try: |
|
102 |
usertheme_obj = UserTheme.objects.get(user=user_id) |
|
103 |
except ObjectDoesNotExist: |
|
104 |
return HttpResponseRedirect(reverse('userthemes-create')) |
|
105 |
||
106 |
if form_class is None: |
|
107 |
form_class = UserThemeForm |
|
108 |
||
109 |
if request.method == 'POST': |
|
110 |
form = form_class(request.POST, request.FILES, instance=usertheme_obj) |
|
111 |
if form.is_valid(): |
|
112 |
usertheme_obj = form.save(commit=False) |
|
113 |
usertheme_obj.user_id = user_id |
|
114 |
usertheme_obj.save() |
|
115 |
response = HttpResponseRedirect(success_url) |
|
116 |
response.set_cookie("usertheme", form.cleaned_data['theme']) |
|
117 |
return response |
|
118 |
else: |
|
119 |
form = form_class() |
|
120 |
||
121 |
return render_to_response(template_name, |
|
122 |
{'form': form}, |
|
123 |
context_instance=RequestContext(request)) |
|
124 |
||
1 |
#!/usr/bin/env python |
|
2 |
# -*- coding: utf-8 -*- |
|
3 |
# |
|
4 |
# Copyright (C) 2009 Éric Veiras Galisson. |
|
5 |
# |
|
6 |
# This program is free software: you can redistribute it and/or modify |
|
7 |
# it under the terms of the GNU General Public License as published by |
|
8 |
# the Free Software Foundation, either version 2 of the License, or |
|
9 |
# (at your option) any later version. |
|
10 |
# |
|
11 |
# This program is distributed in the hope that it will be useful, |
|
12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
# GNU General Public License for more details. |
|
15 |
# |
|
16 |
# You should have received a copy of the GNU General Public License |
|
17 |
# along with this program. If not, see http://www.gnu.org/licenses/. |
|
18 |
# |
|
19 |
""" |
|
20 |
Views for creating and editing user theme preference and for serving |
|
21 |
static content. |
|
22 |
""" |
|
23 |
from django.core.exceptions import ObjectDoesNotExist |
|
24 |
from django.http import HttpResponseRedirect |
|
25 |
from django.core.urlresolvers import reverse |
|
26 |
from django.shortcuts import render_to_response |
|
27 |
from django.template import RequestContext |
|
28 |
||
29 |
from userthemes.models import UserTheme |
|
30 |
from userthemes.forms import UserThemeForm |
|
31 |
from userthemes.middleware import get_current_user |
|
32 |
||
33 |
||
34 |
def create_usertheme(request, |
|
35 |
form_class=None, |
|
36 |
template_name='userthemes/create_usertheme.html', |
|
37 |
success_url=None): |
|
38 |
""" |
|
39 |
Create a user theme for the current user, if one doesn't already |
|
40 |
exist. |
|
41 |
||
42 |
If the user already has a UserTheme, a redirect will be issued to the |
|
43 |
:view:`userthemes.views.edit_usertheme` view. |
|
44 |
||
45 |
Optional arguments |
|
46 |
* form_class |
|
47 |
* template_name |
|
48 |
* success_url |
|
49 |
""" |
|
50 |
user_id = get_current_user().id |
|
51 |
||
52 |
if success_url is None: |
|
53 |
success_url = reverse('userthemes-edit') |
|
54 |
||
55 |
try: |
|
56 |
usertheme_obj = UserTheme.objects.get(user=user_id) |
|
57 |
return HttpResponseRedirect(reverse('userthemes-edit')) |
|
58 |
except ObjectDoesNotExist: |
|
59 |
pass |
|
60 |
||
61 |
if form_class is None: |
|
62 |
form_class = UserThemeForm |
|
63 |
||
64 |
if request.method == 'POST': |
|
65 |
form = form_class(request.POST, request.FILES) |
|
66 |
if form.is_valid(): |
|
67 |
usertheme_obj = form.save(commit=False) |
|
68 |
usertheme_obj.user_id = user_id |
|
69 |
usertheme_obj.save() |
|
70 |
response = HttpResponseRedirect(success_url) |
|
71 |
response.set_cookie("usertheme", form.cleaned_data['theme']) |
|
72 |
return response |
|
73 |
else: |
|
74 |
form = form_class() |
|
75 |
||
76 |
return render_to_response(template_name, |
|
77 |
{'form': form}, |
|
78 |
context_instance=RequestContext(request)) |
|
79 |
||
80 |
||
81 |
def edit_usertheme(request, |
|
82 |
form_class=None, |
|
83 |
template_name='userthemes/edit_usertheme.html', |
|
84 |
success_url=None): |
|
85 |
""" |
|
86 |
Edit the current user's theme. |
|
87 |
||
88 |
If the user does not already have a UserTheme, a redirect will be issued |
|
89 |
to the :view:`userthemes.views.create_usertheme` view. |
|
90 |
||
91 |
Optional arguments |
|
92 |
* form_class |
|
93 |
* template_name |
|
94 |
* success_url |
|
95 |
""" |
|
96 |
user_id = get_current_user().id |
|
97 |
||
98 |
if success_url is None: |
|
99 |
success_url = reverse('userthemes-edit') |
|
100 |
||
101 |
try: |
|
102 |
usertheme_obj = UserTheme.objects.get(user=user_id) |
|
103 |
except ObjectDoesNotExist: |
|
104 |
return HttpResponseRedirect(reverse('userthemes-create')) |
|
105 |
||
106 |
if form_class is None: |
|
107 |
form_class = UserThemeForm |
|
108 |
||
109 |
if request.method == 'POST': |
|
110 |
form = form_class(request.POST, request.FILES, instance=usertheme_obj) |
|
111 |
if form.is_valid(): |
|
112 |
usertheme_obj = form.save(commit=False) |
|
113 |
usertheme_obj.user_id = user_id |
|
114 |
usertheme_obj.save() |
|
115 |
response = HttpResponseRedirect(success_url) |
|
116 |
response.set_cookie("usertheme", form.cleaned_data['theme']) |
|
117 |
return response |
|
118 |
else: |
|
119 |
form = form_class() |
|
120 |
||
121 |
return render_to_response(template_name, |
|
122 |
{'form': form}, |
|
123 |
context_instance=RequestContext(request)) |
|
124 |
