Merge pull request #793 from gpodder/remove-advertise
[mygpo.git] / mygpo / users / views / settings.py
blobd16d513606210d53a90bf3849e2d77b23937c7e7
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
26 @login_required
27 @vary_on_cookie
28 @cache_control(private=True)
29 @allowed_methods(["GET", "POST"])
30 def account(request):
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),
51 return render(
52 request,
53 "account.html",
55 "site": site,
56 "form": form,
57 "profile_form": profile_form,
58 "userpage_token": userpage_token,
62 try:
63 form = UserAccountForm(request.POST)
65 if not form.is_valid():
66 raise ValueError(
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"]
80 try:
81 request.user.save()
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):
98 user = request.user
100 form = ProfileForm(request.POST)
102 if not form.is_valid():
103 raise ValueError(
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"])
114 user_profile.save()
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
126 request.user.save()
127 messages.success(request, _("Your account has been disconnected"))
128 return HttpResponseRedirect(reverse("account"))
131 @login_required
132 @never_cache
133 @allowed_methods(["GET", "POST"])
134 def delete_account(request):
136 if request.method == "GET":
137 return render(request, "delete_account.html")
139 user = request.user
140 user.is_active = False
141 user.save()
142 logout(request)
143 return render(request, "deleted_account.html")
146 class DefaultPrivacySettings(View):
148 public = True
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)
155 settings.save()
156 return HttpResponseRedirect(reverse("privacy"))
159 class PodcastPrivacySettings(View):
161 public = True
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(
169 user=request.user,
170 content_type=ContentType.objects.get_for_model(podcast),
171 object_id=podcast.pk,
174 settings.set_wksetting(PUBLIC_SUB_PODCAST, self.public)
175 settings.save()
176 return HttpResponseRedirect(reverse("privacy"))
179 @login_required
180 @never_cache
181 def privacy(request):
182 site = RequestSite(request)
183 user = request.user
185 podcasts = (
186 Podcast.objects.filter(subscription__user=user)
187 .distinct("pk")
188 .prefetch_related("slugs")
190 private = UserSettings.objects.get_private_podcasts(user)
192 subscriptions = []
193 for podcast in podcasts:
195 subscriptions.append((podcast, podcast in private))
197 return render(
198 request,
199 "privacy.html",
201 "private_subscriptions": not request.user.profile.settings.get_wksetting(
202 PUBLIC_SUB_USER
204 "subscriptions": subscriptions,
205 "domain": site.domain,
210 @vary_on_cookie
211 @cache_control(private=True)
212 @login_required
213 def share(request):
214 site = RequestSite(request)
216 user = request.user
218 if "public_subscriptions" in request.GET:
219 user.profile.subscriptions_token = ""
220 user.profile.save()
222 elif "private_subscriptions" in request.GET:
223 user.profile.create_new_token("subscriptions_token")
224 user.profile.save()
226 token = user.profile.get_token("subscriptions_token")
228 return render(request, "share.html", {"site": site, "token": token})