1 from django
.shortcuts
import render
, get_object_or_404
2 from django
.urls
import reverse
3 from django
.http
import HttpResponseRedirect
4 from django
.contrib
.auth
import logout
5 from django
.contrib
import messages
6 from django
.forms
import ValidationError
7 from django
.utils
.translation
import gettext
as _
8 from django
.contrib
.auth
.decorators
import login_required
9 from django
.contrib
.contenttypes
.models
import ContentType
10 from django
.contrib
.sites
.requests
import RequestSite
11 from django
.views
.decorators
.vary
import vary_on_cookie
12 from django
.views
.decorators
.cache
import never_cache
, cache_control
13 from django
.utils
.decorators
import method_decorator
14 from django
.views
.generic
.base
import View
15 from django
.utils
.html
import strip_tags
17 from mygpo
.podcasts
.models
import Podcast
18 from mygpo
.usersettings
.models
import UserSettings
19 from mygpo
.decorators
import allowed_methods
20 from mygpo
.web
.forms
import UserAccountForm
, ProfileForm
21 from mygpo
.web
.utils
import normalize_twitter
22 from mygpo
.users
.settings
import PUBLIC_SUB_USER
, PUBLIC_SUB_PODCAST
23 from mygpo
.users
.models
import UserProfile
28 @cache_control(private
=True)
29 @allowed_methods(["GET", "POST"])
32 if request
.method
== "GET":
34 site
= RequestSite(request
)
35 userpage_token
= request
.user
.profile
.get_token("userpage_token")
37 profile_form
= ProfileForm(
39 "twitter": request
.user
.profile
.twitter
,
40 "about": request
.user
.profile
.about
,
44 form
= UserAccountForm(
46 "email": request
.user
.email
,
47 "public": request
.user
.profile
.settings
.get_wksetting(PUBLIC_SUB_USER
),
57 "profile_form": profile_form
,
58 "userpage_token": userpage_token
,
63 form
= UserAccountForm(request
.POST
)
65 if not form
.is_valid():
68 "Oops! Something went wrong. Please double-check the data you entered."
72 if form
.cleaned_data
["password_current"]:
73 if not request
.user
.check_password(form
.cleaned_data
["password_current"]):
74 raise ValueError("Current password is incorrect")
76 request
.user
.set_password(form
.cleaned_data
["password1"])
78 request
.user
.email
= form
.cleaned_data
["email"]
82 except Exception as ex
:
83 # TODO: which exception?
84 messages
.error(request
, str(ex
))
86 messages
.success(request
, "Account updated")
88 except (ValueError, ValidationError
) as e
:
89 messages
.error(request
, str(e
))
91 return render(request
, "account.html", {"form": form
})
94 class ProfileView(View
):
95 """Updates the public profile and redirects back to the account view"""
97 def post(self
, request
):
100 form
= ProfileForm(request
.POST
)
102 if not form
.is_valid():
105 "Oops! Something went wrong. Please double-check the data you entered."
109 # Retrieve user profile for request associated user
110 user_profile
= get_object_or_404(UserProfile
, user
=request
.user
)
111 user_profile
.twitter
= normalize_twitter(form
.cleaned_data
["twitter"])
112 user_profile
.about
= strip_tags(form
.cleaned_data
["about"])
115 messages
.success(request
, _("Data updated"))
117 return HttpResponseRedirect(reverse("account") + "#profile")
120 class AccountRemoveGoogle(View
):
121 """Removes the connected Google account"""
123 @method_decorator(login_required
)
124 def post(self
, request
):
125 request
.user
.google_email
= None
127 messages
.success(request
, _("Your account has been disconnected"))
128 return HttpResponseRedirect(reverse("account"))
133 @allowed_methods(["GET", "POST"])
134 def delete_account(request
):
136 if request
.method
== "GET":
137 return render(request
, "delete_account.html")
140 user
.is_active
= False
143 return render(request
, "deleted_account.html")
146 class DefaultPrivacySettings(View
):
150 @method_decorator(login_required
)
151 @method_decorator(never_cache
)
152 def post(self
, request
):
153 settings
= request
.user
.profile
.settings
154 settings
.set_setting(PUBLIC_SUB_USER
.name
, self
.public
)
156 return HttpResponseRedirect(reverse("privacy"))
159 class PodcastPrivacySettings(View
):
163 @method_decorator(login_required
)
164 @method_decorator(never_cache
)
165 def post(self
, request
, podcast_id
):
166 podcast
= Podcast
.objects
.get(id=podcast_id
)
168 settings
, created
= UserSettings
.objects
.get_or_create(
170 content_type
=ContentType
.objects
.get_for_model(podcast
),
171 object_id
=podcast
.pk
,
174 settings
.set_wksetting(PUBLIC_SUB_PODCAST
, self
.public
)
176 return HttpResponseRedirect(reverse("privacy"))
181 def privacy(request
):
182 site
= RequestSite(request
)
186 Podcast
.objects
.filter(subscription__user
=user
)
188 .prefetch_related("slugs")
190 private
= UserSettings
.objects
.get_private_podcasts(user
)
193 for podcast
in podcasts
:
195 subscriptions
.append((podcast
, podcast
in private
))
201 "private_subscriptions": not request
.user
.profile
.settings
.get_wksetting(
204 "subscriptions": subscriptions
,
205 "domain": site
.domain
,
211 @cache_control(private
=True)
214 site
= RequestSite(request
)
218 if "public_subscriptions" in request
.GET
:
219 user
.profile
.subscriptions_token
= ""
222 elif "private_subscriptions" in request
.GET
:
223 user
.profile
.create_new_token("subscriptions_token")
226 token
= user
.profile
.get_token("subscriptions_token")
228 return render(request
, "share.html", {"site": site
, "token": token
})