diff --git a/circle/dashboard/forms.py b/circle/dashboard/forms.py index 12d5542..bc3c83e 100644 --- a/circle/dashboard/forms.py +++ b/circle/dashboard/forms.py @@ -1107,6 +1107,19 @@ class MyProfileForm(forms.ModelForm): return value +class UnsubscribeForm(forms.ModelForm): + + class Meta: + fields = ('email_notifications', ) + model = Profile + + @property + def helper(self): + helper = FormHelper() + helper.add_input(Submit("submit", _("Change language"))) + return helper + + class CirclePasswordChangeForm(PasswordChangeForm): @property diff --git a/circle/dashboard/tasks/local_periodic_tasks.py b/circle/dashboard/tasks/local_periodic_tasks.py index 1a5f6ce..70032b5 100644 --- a/circle/dashboard/tasks/local_periodic_tasks.py +++ b/circle/dashboard/tasks/local_periodic_tasks.py @@ -26,6 +26,7 @@ from django.utils.translation import ungettext, override from manager.mancelery import celery from ..models import Notification +from ..views import UnsubscribeFormView logger = logging.getLogger(__name__) @@ -50,6 +51,9 @@ def send_email_notifications(): context = {'user': user.profile, 'messages': msgs, 'url': (settings.DJANGO_URL.rstrip("/") + reverse("dashboard.views.notifications")), + 'unsub': (settings.DJANGO_URL.rstrip("/") + reverse( + "dashboard.views.unsubscribe", + args=[UnsubscribeFormView.get_token(user)])), 'site': settings.COMPANY_NAME} subject = settings.EMAIL_SUBJECT_PREFIX + ungettext( "%d new notification", diff --git a/circle/dashboard/templates/dashboard/notifications/email.txt b/circle/dashboard/templates/dashboard/notifications/email.txt index b7cfdd2..50d7d34 100644 --- a/circle/dashboard/templates/dashboard/notifications/email.txt +++ b/circle/dashboard/templates/dashboard/notifications/email.txt @@ -11,3 +11,6 @@ -- {{site}} CIRCLE Cloud + +{% trans "You can change your subscription without logging in:" %} +{{unsub}} diff --git a/circle/dashboard/templates/dashboard/unsubscribe.html b/circle/dashboard/templates/dashboard/unsubscribe.html new file mode 100644 index 0000000..e76071c --- /dev/null +++ b/circle/dashboard/templates/dashboard/unsubscribe.html @@ -0,0 +1,21 @@ +{% extends "dashboard/base.html" %} +{% load crispy_forms_tags %} +{% load i18n %} + +{% block content %} +<div class="body-content"> + <div class="panel panel-default" style="margin-top: 60px;"> + <div class="panel-heading"> + <h3 class="no-margin"> + {% trans "Subscription settings" %} + </h3> + </div> + <div class="panel-body"> + <form method="POST" action=""> + {% csrf_token %} + {% crispy form %} + </form> + </div> + </div> +</div> +{% endblock %} diff --git a/circle/dashboard/urls.py b/circle/dashboard/urls.py index 88e779a..0d03d4c 100644 --- a/circle/dashboard/urls.py +++ b/circle/dashboard/urls.py @@ -35,7 +35,7 @@ from .views import ( TemplateChoose, UserCreationView, get_vm_screenshot, - ProfileView, toggle_use_gravatar, + ProfileView, toggle_use_gravatar, UnsubscribeFormView, ) urlpatterns = patterns( @@ -140,6 +140,8 @@ urlpatterns = patterns( url(r'^profile/$', MyPreferencesView.as_view(), name="dashboard.views.profile-preferences"), + url(r'^subscribe/(?P<token>.*)/$', UnsubscribeFormView.as_view(), + name="dashboard.views.unsubscribe"), url(r'^profile/(?P<username>[^/]+)/$', ProfileView.as_view(), name="dashboard.views.profile"), url(r'^profile/(?P<username>[^/]+)/use_gravatar/$', toggle_use_gravatar), diff --git a/circle/dashboard/views.py b/circle/dashboard/views.py index b81ebf1..42cd395 100644 --- a/circle/dashboard/views.py +++ b/circle/dashboard/views.py @@ -59,7 +59,7 @@ from braces.views._access import AccessMixin from .forms import ( CircleAuthenticationForm, DiskAddForm, HostForm, LeaseForm, MyProfileForm, NodeForm, TemplateForm, TraitForm, VmCustomizeForm, GroupCreateForm, - UserCreationForm, GroupProfileUpdateForm, + UserCreationForm, GroupProfileUpdateForm, UnsubscribeForm, CirclePasswordChangeForm ) @@ -2605,6 +2605,26 @@ class MyPreferencesView(UpdateView): return self.render_to_response(context) +class UnsubscribeFormView(SuccessMessageMixin, UpdateView): + model = Profile + form_class = UnsubscribeForm + template_name = "dashboard/unsubscribe.html" + success_message = _("Successfully modified subscription.") + + @classmethod + def get_salt(cls): + return unicode(cls) + + @classmethod + def get_token(cls, user): + return signing.dumps(user.pk, salt=cls.get_salt()) + + def get_object(self, queryset=None): + pk = signing.loads(self.kwargs['token'], salt=self.get_salt(), + max_age=48*3600) + return (queryset or self.get_queryset()).get(user_id=pk) + + def set_language_cookie(request, response, lang=None): if lang is None: try: