Commit b20ebb00 by Őry Máté

dashboard: allow unsubscription with token

parent a30de002
......@@ -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
......
......@@ -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",
......
......@@ -11,3 +11,6 @@
--
{{site}} CIRCLE Cloud
{% trans "You can change your subscription without logging in:" %}
{{unsub}}
{% 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 %}
......@@ -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),
......
......@@ -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:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment