extensions-website/users/views/settings.py
Oleg Komarov bd3a17a19b Notification emails (#80)
Implementation for #72 based on https://github.com/justquick/django-activity-stream/

Summary:
- create an Action object, specifying a corresponding Extension object as a `target` and other relevant objects (Rating, ApprovalActivity, AbuseReport) as `action_object` (search code for `action.send` to see where it happens)
  - there is one exception when we don't have an `action_object` - when we submit a new extension for a review, but this could be restructured as creating a first ApprovalActivity item, TODO revisit this
- use `follow` provided by actstream to specify relations between users and extensions, use different flags (author, moderator, reviewer) to explicitly manage those relations; proactively call the `follow` to make sure that users are subscribed before an Action interesting to them is created;
  - one downside here is that each moderator needs to follow each extensions individually, but this potentially allows to explicitly unsubscribe from activity on extensions that are not interesting to a given user
- introduce `VERB2FLAGS` mapping to define when a given Action needs to generate a Notification for a follower of a particular type (=flag) based on the Action's verb
- process Notification records by a new `send_notifications` management command that sends emails
- add a profile setting to disable notification emails

First iteration includes only internal (`@blender.org`) emails.

If you have a DB with some preexisting data, you need to run `./manage.py ensure_followers` command to retroactively create expected follow relations.

Next steps (out of scope for this PR):
- refine notification texts: current `Verb` usage may be not grammatical and is not covered by i18n
- UI: templates for showing notifications in user profile, marking notifications as read, unread counter in the header (there is some views code in this PR, but it it not surfaced to the users yet)
- remove the internal email check

Co-authored-by: Anna Sirota <railla@noreply.localhost>
Reviewed-on: #80
Reviewed-by: Anna Sirota <railla@noreply.localhost>
2024-04-18 16:11:18 +02:00

40 lines
1.4 KiB
Python

"""User profile pages."""
from django.contrib.auth import get_user_model
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.views.generic import TemplateView
from django.views.generic.edit import FormView
from users.forms import SubscribeNotificationEmailsForm
User = get_user_model()
class ProfileView(LoginRequiredMixin, TemplateView):
"""Template view for the profile settings."""
template_name = 'users/settings/profile.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['subscribe_notification_emails_form'] = SubscribeNotificationEmailsForm(
{'subscribe': not self.request.user.is_subscribed_to_notification_emails},
)
return context
class DeleteView(LoginRequiredMixin, TemplateView):
"""Template view where account deletion can be requested."""
template_name = 'users/settings/delete.html'
class SubscribeNotificationEmailsView(LoginRequiredMixin, FormView):
form_class = SubscribeNotificationEmailsForm
success_url = reverse_lazy('users:my-profile')
def form_valid(self, form):
self.request.user.is_subscribed_to_notification_emails = form.cleaned_data['subscribe']
self.request.user.save(update_fields={'is_subscribed_to_notification_emails'})
return super().form_valid(form)