49 lines
2.0 KiB
Python
49 lines
2.0 KiB
Python
# combo - content management system
|
|
# Copyright (C) 2015 Entr'ouvert
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
# under the terms of the GNU Affero General Public License as published
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
from django.views.generic import FormView
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from django.contrib import messages
|
|
from django.http import HttpResponseRedirect
|
|
|
|
from .forms import NewslettersManageForm
|
|
from .models import NewslettersCell, SubscriptionsSaveError
|
|
|
|
class NewslettersView(FormView):
|
|
http_method_names = ['post']
|
|
form_class = NewslettersManageForm
|
|
|
|
def form_valid(self, form):
|
|
try:
|
|
form.save()
|
|
messages.info(self.request, _('Your subscriptions are successfully saved'))
|
|
except SubscriptionsSaveError:
|
|
messages.error(self.request, _('An error occured while saving your subscriptions. Please try later.'))
|
|
return super(NewslettersView, self).form_valid(form)
|
|
|
|
def get_form_kwargs(self):
|
|
kwargs = super(NewslettersView, self).get_form_kwargs()
|
|
self.instance = NewslettersCell.objects.get(pk=self.kwargs['pk'])
|
|
kwargs.update({'request': self.request, 'instance': self.instance})
|
|
return kwargs
|
|
|
|
def form_invalid(self, form):
|
|
messages.error(self.request, _('An error occured while saving your subscriptions. Please try later.'))
|
|
return HttpResponseRedirect(self.get_success_url())
|
|
|
|
def get_success_url(self):
|
|
return self.instance.page.get_online_url()
|