73 lines
2.7 KiB
Python
73 lines
2.7 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.contrib import messages
|
|
from django.http import HttpResponseRedirect
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from django.views.generic import FormView, TemplateView
|
|
|
|
from .forms import FamilyLinkForm
|
|
from .utils import link_family, unlink_family
|
|
|
|
ERROR_MESSAGES = {
|
|
100: _('Wrong credentials: make sure you typed them correctly.'),
|
|
101: _('This family account is blocked.'),
|
|
}
|
|
|
|
|
|
class FamilyLinkView(FormView):
|
|
form_class = FamilyLinkForm
|
|
template_name = 'family/link_form.html'
|
|
|
|
def get_success_url(self):
|
|
if self.request.META.get('HTTP_REFERER'):
|
|
return self.request.META['HTTP_REFERER']
|
|
# return to current location if no referer
|
|
return '.'
|
|
|
|
def form_valid(self, form):
|
|
response = link_family(
|
|
self.request.user, form.cleaned_data['family_id'], form.cleaned_data['family_code']
|
|
)
|
|
|
|
if not response.ok or response.json().get('err'):
|
|
error_code = response.json().get('err')
|
|
error_message = ERROR_MESSAGES.get(
|
|
error_code, _('Failed to link to family. Please check your credentials and retry later.')
|
|
)
|
|
messages.error(self.request, error_message)
|
|
else:
|
|
messages.info(self.request, _('Your account was successfully linked.'))
|
|
return super().form_valid(form)
|
|
|
|
|
|
class FamilyUnlinkView(TemplateView):
|
|
template_name = 'family/unlink_confirm.html'
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
response = unlink_family(request.user)
|
|
|
|
if not response.ok or response.json().get('err'):
|
|
messages.error(request, _('An error occured when unlinking.'))
|
|
else:
|
|
messages.info(request, _('Your account was successfully unlinked.'))
|
|
|
|
if self.request.META.get('HTTP_REFERER'):
|
|
return HttpResponseRedirect(self.request.META['HTTP_REFERER'])
|
|
# fallback to homepage if no referer
|
|
return HttpResponseRedirect('/')
|