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
19 from django
.core
.urlresolvers
import reverse
20 from django
.http
import HttpResponseRedirect
21 from django
.contrib
.auth
import logout
22 from django
.contrib
import messages
23 from django
.forms
import ValidationError
24 from django
.utils
.translation
import ugettext
as _
25 from django
.contrib
.auth
.decorators
import login_required
26 from django
.contrib
.contenttypes
.models
import ContentType
27 from django
.contrib
.sites
.requests
import RequestSite
28 from django
.views
.decorators
.vary
import vary_on_cookie
29 from django
.views
.decorators
.cache
import never_cache
, cache_control
30 from django
.utils
.decorators
import method_decorator
31 from django
.views
.generic
.base
import View
32 from django
.utils
.html
import strip_tags
34 from mygpo
.podcasts
.models
import Podcast
35 from mygpo
.usersettings
.models
import UserSettings
36 from mygpo
.decorators
import allowed_methods
37 from mygpo
.web
.forms
import UserAccountForm
, ProfileForm
, FlattrForm
38 from mygpo
.web
.utils
import normalize_twitter
39 from mygpo
.flattr
import Flattr
40 from mygpo
.users
.settings
import PUBLIC_SUB_USER
, PUBLIC_SUB_PODCAST
, \
41 FLATTR_TOKEN
, FLATTR_AUTO
, FLATTR_MYGPO
, FLATTR_USERNAME
46 @cache_control(private
=True)
47 @allowed_methods(['GET', 'POST'])
50 if request
.method
== 'GET':
52 site
= RequestSite(request
)
53 flattr
= Flattr(request
.user
, site
.domain
, request
.is_secure())
54 userpage_token
= request
.user
.profile
.get_token('userpage_token')
56 profile_form
= ProfileForm({
57 'twitter': request
.user
.profile
.twitter
,
58 'about': request
.user
.profile
.about
,
61 form
= UserAccountForm({
62 'email': request
.user
.email
,
63 'public': request
.user
.profile
.settings
.get_wksetting(PUBLIC_SUB_USER
)
66 flattr_form
= FlattrForm({
67 'enable': request
.user
.profile
.settings
.get_wksetting(FLATTR_AUTO
),
68 'token': request
.user
.profile
.settings
.get_wksetting(FLATTR_TOKEN
),
69 'flattr_mygpo': request
.user
.profile
.settings
.get_wksetting(FLATTR_MYGPO
),
70 'username': request
.user
.profile
.settings
.get_wksetting(FLATTR_USERNAME
),
73 return render(request
, 'account.html', {
76 'profile_form': profile_form
,
77 'flattr_form': flattr_form
,
79 'userpage_token': userpage_token
,
83 form
= UserAccountForm(request
.POST
)
85 if not form
.is_valid():
86 raise ValueError(_('Oops! Something went wrong. Please double-check the data you entered.'))
88 if form
.cleaned_data
['password_current']:
89 if not request
.user
.check_password(form
.cleaned_data
['password_current']):
90 raise ValueError('Current password is incorrect')
92 request
.user
.set_password(form
.cleaned_data
['password1'])
94 request
.user
.email
= form
.cleaned_data
['email']
98 except Exception as ex
:
99 # TODO: which exception?
100 messages
.error(request
, str(ex
))
102 messages
.success(request
, 'Account updated')
104 except (ValueError, ValidationError
) as e
:
105 messages
.error(request
, str(e
))
107 return render(request
, 'account.html', {
112 class ProfileView(View
):
113 """ Updates the public profile and redirects back to the account view """
115 def post(self
, request
):
118 form
= ProfileForm(request
.POST
)
120 if not form
.is_valid():
121 raise ValueError(_('Oops! Something went wrong. Please double-check the data you entered.'))
123 request
.user
.twitter
= normalize_twitter(form
.cleaned_data
['twitter'])
124 request
.user
.about
= strip_tags(form
.cleaned_data
['about'])
127 messages
.success(request
, _('Data updated'))
129 return HttpResponseRedirect(reverse('account') + '#profile')
132 class FlattrSettingsView(View
):
133 """ Updates Flattr settings and redirects back to the Account page """
135 def post(self
, request
):
138 form
= FlattrForm(request
.POST
)
140 if not form
.is_valid():
141 raise ValueError('asdf')
143 auto_flattr
= form
.cleaned_data
.get('enable', False)
144 flattr_mygpo
= form
.cleaned_data
.get('flattr_mygpo', False)
145 username
= form
.cleaned_data
.get('username', '')
147 settings
= user
.profile
.settings
148 settings
.set_wksetting(FLATTR_AUTO
, auto_flattr
)
149 settings
.set_wksetting(FLATTR_MYGPO
, flattr_mygpo
)
150 settings
.set_wksetting(FLATTR_USERNAME
, username
)
153 return HttpResponseRedirect(reverse('account') + '#flattr')
156 class FlattrLogout(View
):
157 """ Removes Flattr authentication token """
159 def get(self
, request
):
161 settings
= user
.profile
.settings
162 settings
.set_wksetting(FLATTR_AUTO
, False)
163 settings
.set_wksetting(FLATTR_TOKEN
, False)
164 settings
.set_wksetting(FLATTR_MYGPO
, False)
166 return HttpResponseRedirect(reverse('account') + '#flattr')
169 class FlattrTokenView(View
):
170 """ Callback for the Flattr authentication
172 Updates the user's Flattr token and redirects back to the account page """
174 @method_decorator(login_required
)
175 def get(self
, request
):
178 site
= RequestSite(request
)
179 flattr
= Flattr(user
, site
.domain
, request
.is_secure())
181 url
= request
.build_absolute_uri()
182 token
= flattr
.process_retrieved_code(url
)
184 messages
.success(request
, _('Authentication successful'))
185 settings
= user
.profile
.settings
186 settings
.set_wksetting(FLATTR_TOKEN
, token
)
190 messages
.error(request
, _('Authentication failed. Try again later'))
192 return HttpResponseRedirect(reverse('account') + '#flattr')
195 class AccountRemoveGoogle(View
):
196 """ Removes the connected Google account """
198 @method_decorator(login_required
)
199 def post(self
, request
):
200 request
.user
.google_email
= None
202 messages
.success(request
, _('Your account has been disconnected'))
203 return HttpResponseRedirect(reverse('account'))
208 @allowed_methods(['GET', 'POST'])
209 def delete_account(request
):
211 if request
.method
== 'GET':
212 return render(request
, 'delete_account.html')
215 user
.is_active
= False
219 return render(request
, 'deleted_account.html')
223 class DefaultPrivacySettings(View
):
227 @method_decorator(login_required
)
228 @method_decorator(never_cache
)
229 def post(self
, request
):
230 settings
= request
.user
.profile
.settings
231 settings
.set_setting(PUBLIC_SUB_USER
.name
, self
.public
)
233 return HttpResponseRedirect(reverse('privacy'))
236 class PodcastPrivacySettings(View
):
240 @method_decorator(login_required
)
241 @method_decorator(never_cache
)
242 def post(self
, request
, podcast_id
):
243 podcast
= Podcast
.objects
.get(id=podcast_id
)
245 settings
, created
= UserSettings
.objects
.get_or_create(
247 content_type
=ContentType
.objects
.get_for_model(podcast
),
248 object_id
=podcast
.pk
,
251 settings
.set_wksetting(PUBLIC_SUB_PODCAST
, self
.public
)
253 return HttpResponseRedirect(reverse('privacy'))
258 def privacy(request
):
259 site
= RequestSite(request
)
262 podcasts
= Podcast
.objects
.filter(subscription__user
=user
)\
264 private
= UserSettings
.objects
.get_private_podcasts(user
)
267 for podcast
in podcasts
:
269 subscriptions
.append( (podcast
, podcast
in private
) )
271 return render(request
, 'privacy.html', {
272 'private_subscriptions': not request
.user
.profile
.settings
.get_wksetting(PUBLIC_SUB_USER
),
273 'subscriptions': subscriptions
,
274 'domain': site
.domain
,
279 @cache_control(private
=True)
282 site
= RequestSite(request
)
286 if 'public_subscriptions' in request
.GET
:
287 user
.profile
.subscriptions_token
= ''
290 elif 'private_subscriptions' in request
.GET
:
291 user
.profile
.create_new_token('subscriptions_token')
294 token
= user
.profile
.get_token('subscriptions_token')
296 return render(request
, 'share.html', {