gvacommon/gvacommon/viewmixins.py
Jan Dittberner 8dda40a363 Make gvacommon distributable
This commit performs a lot of changes to make gvacommon properly
distributable:

- add a setup.py
- move code into a module directory
- add a __version__
- add a README.rst and AGPLv3 license file (COPYING)
- specify the licensing in file headers
- move tests into a submodule of gvacommon and split tests for different
  modules into separate test modules
- get rid of django-braces dependency
- adapt setup.cfg to the new directory structure
- ignore build and dist directories
2016-01-29 13:40:12 +01:00

60 lines
1.9 KiB
Python

#
# gvacommon - common parts of gnuviechadmin
# Copyright (C) 2015-2016 Jan Dittberner
#
# 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/>.
#
"""
This module defines mixins for gnuviechadmin views.
"""
from __future__ import unicode_literals
from django.http import HttpResponseForbidden
from django.utils.translation import ugettext as _
from django.contrib.auth.mixins import LoginRequiredMixin
class StaffOrSelfLoginRequiredMixin(LoginRequiredMixin):
"""
Mixin that makes sure that a user is logged in and matches the current
customer or is a staff user.
"""
def dispatch(self, request, *args, **kwargs):
if (
request.user.is_staff or
request.user == self.get_customer_object()
):
return super(StaffOrSelfLoginRequiredMixin, self).dispatch(
request, *args, **kwargs
)
return HttpResponseForbidden(
_('You are not allowed to view this page.')
)
def get_customer_object(self):
"""
Views based on this mixin have to implement this method to return
the customer that must be an object of the same class as the
django.contrib.auth user type.
:return: customer
:rtype: settings.AUTH_USER_MODEL
"""
raise NotImplementedError(
"subclass has to implement get_customer_object")