remove unnecessary imports
[mygpo.git] / mygpo / web / views / settings.py
blobb89546cfc63b3da35083924e278e311eee3dbba7
2 # This file is part of my.gpodder.org.
4 # my.gpodder.org is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or (at your
7 # option) any later version.
9 # my.gpodder.org is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
12 # License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with my.gpodder.org. If not, see <http://www.gnu.org/licenses/>.
18 from django.shortcuts import render_to_response
19 from django.contrib.auth import logout
20 from django.template import RequestContext
21 from mygpo.api.models import Podcast, Subscription, SubscriptionMeta
22 from mygpo.web.models import SecurityToken
23 from mygpo.web.forms import UserAccountForm
24 from django.forms import ValidationError
25 from django.utils.translation import ugettext as _
26 from mygpo.decorators import manual_gc, allowed_methods
27 from django.contrib.auth.decorators import login_required
28 from django.contrib.sites.models import Site
31 @manual_gc
32 @login_required
33 @allowed_methods(['GET', 'POST'])
34 def account(request):
35 success = False
36 error_message = ''
38 if request.method == 'GET':
40 form = UserAccountForm({
41 'email': request.user.email,
42 'public': request.user.get_profile().public_profile
45 return render_to_response('account.html', {
46 'form': form,
47 }, context_instance=RequestContext(request))
49 try:
50 form = UserAccountForm(request.POST)
52 if not form.is_valid():
53 raise ValueError(_('Oops! Something went wrong. Please double-check the data you entered.'))
55 if form.cleaned_data['password_current']:
56 if not request.user.check_password(form.cleaned_data['password_current']):
57 raise ValueError('Current password is incorrect')
59 request.user.set_password(form.cleaned_data['password1'])
61 request.user.email = form.cleaned_data['email']
62 request.user.save()
63 request.user.get_profile().save()
65 success = True
67 except ValueError, e:
68 success = False
69 error_message = e
71 except ValidationError, e:
72 success = False
73 error_message = e
75 return render_to_response('account.html', {
76 'form': form,
77 'success': success,
78 'error_message': error_message
79 }, context_instance=RequestContext(request))
82 @manual_gc
83 @login_required
84 @allowed_methods(['GET', 'POST'])
85 def delete_account(request):
87 if request.method == 'GET':
88 return render_to_response('delete_account.html', context_instance=RequestContext(request))
90 profile = request.user.get_profile()
91 profile.deleted = True
92 profile.save()
94 request.user.is_active = False
95 request.user.save()
96 logout(request)
97 return render_to_response('delete_account.html', {
98 'success': True
99 }, context_instance=RequestContext(request))
102 @manual_gc
103 @login_required
104 @allowed_methods(['GET'])
105 def privacy(request):
107 if 'private_subscriptions' in request.GET:
108 request.user.get_profile().settings['public_profile'] = False
109 request.user.get_profile().save()
111 elif 'public_subscriptions' in request.GET:
112 request.user.get_profile().settings['public_profile'] = True
113 request.user.get_profile().save()
115 if 'exclude' in request.GET:
116 id = request.GET['exclude']
117 try:
118 podcast = Podcast.objects.get(pk=id)
119 sm, c = SubscriptionMeta.objects.get_or_create(user=request.user, podcast=podcast, defaults={'public': False})
121 if not c:
122 sm.settings['public_subscription'] = False
123 sm.save()
125 except Podcast.DoesNotExist:
126 pass
128 if 'include' in request.GET:
129 id = request.GET['include']
130 try:
131 podcast = Podcast.objects.get(pk=id)
132 sm, c = SubscriptionMeta.objects.get_or_create(user=request.user, podcast=podcast, defaults={'public': True})
134 if not c:
135 sm.settings['public_subscription'] = True
136 sm.save()
138 except Podcast.DoesNotExist:
139 pass
141 subscriptions = [s for s in Subscription.objects.filter(user=request.user)]
142 included_subscriptions = set([s.podcast for s in subscriptions if s.get_meta().public])
143 excluded_subscriptions = set([s.podcast for s in subscriptions if not s.get_meta().public])
145 return render_to_response('privacy.html', {
146 'public_subscriptions': request.user.get_profile().public_profile,
147 'included_subscriptions': included_subscriptions,
148 'excluded_subscriptions': excluded_subscriptions,
149 }, context_instance=RequestContext(request))
152 @manual_gc
153 @login_required
154 def share(request):
155 site = Site.objects.get_current()
156 token, c = SecurityToken.objects.get_or_create(user=request.user, object='subscriptions', action='r')
159 if 'public_subscriptions' in request.GET:
160 token.token = ''
161 token.save()
163 elif 'private_subscriptions' in request.GET:
164 token.random_token()
165 token.save()
167 return render_to_response('share.html', {
168 'site': site,
169 'token': token.token,
170 }, context_instance=RequestContext(request))