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
This commit is contained in:
parent
c94f20f83f
commit
8dda40a363
14 changed files with 896 additions and 78 deletions
18
gvacommon/__init__.py
Normal file
18
gvacommon/__init__.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
#
|
||||
# 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/>.
|
||||
#
|
||||
__version__ = '0.2.dev1'
|
31
gvacommon/celeryrouters.py
Normal file
31
gvacommon/celeryrouters.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
#
|
||||
# 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/>.
|
||||
#
|
||||
from __future__ import unicode_literals
|
||||
|
||||
|
||||
class GvaRouter(object):
|
||||
|
||||
def route_for_task(self, task, args=None, kwargs=None):
|
||||
for route in ['ldap', 'file', 'mysql', 'pgsql', 'web']:
|
||||
if route in task:
|
||||
return {
|
||||
'exchange': route,
|
||||
'exchange_type': 'direct',
|
||||
'queue': route,
|
||||
}
|
||||
return None
|
0
gvacommon/tests/__init__.py
Normal file
0
gvacommon/tests/__init__.py
Normal file
68
gvacommon/tests/test_celeryrouters.py
Normal file
68
gvacommon/tests/test_celeryrouters.py
Normal file
|
@ -0,0 +1,68 @@
|
|||
#
|
||||
# gvacommon - common parts of gnuviechadmin
|
||||
# Copyright (C) 2014-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/>.
|
||||
#
|
||||
"""
|
||||
Tests for gvacommon celery router code.
|
||||
|
||||
"""
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
import unittest
|
||||
|
||||
from gvacommon.celeryrouters import GvaRouter
|
||||
|
||||
|
||||
class GvaRouteTest(unittest.TestCase):
|
||||
|
||||
def test_route_for_task_ldap(self):
|
||||
router = GvaRouter()
|
||||
route = router.route_for_task('ldap')
|
||||
self.assertEqual(
|
||||
route,
|
||||
{'exchange': 'ldap', 'exchange_type': 'direct', 'queue': 'ldap'})
|
||||
|
||||
def test_route_for_task_file(self):
|
||||
router = GvaRouter()
|
||||
route = router.route_for_task('file')
|
||||
self.assertEqual(
|
||||
route,
|
||||
{'exchange': 'file', 'exchange_type': 'direct', 'queue': 'file'})
|
||||
|
||||
def test_route_for_task_mysql(self):
|
||||
router = GvaRouter()
|
||||
route = router.route_for_task('mysql')
|
||||
self.assertEqual(
|
||||
route,
|
||||
{'exchange': 'mysql', 'exchange_type': 'direct', 'queue': 'mysql'})
|
||||
|
||||
def test_route_for_task_pgsql(self):
|
||||
router = GvaRouter()
|
||||
route = router.route_for_task('pgsql')
|
||||
self.assertEqual(
|
||||
route,
|
||||
{'exchange': 'pgsql', 'exchange_type': 'direct', 'queue': 'pgsql'})
|
||||
|
||||
def test_route_for_task_web(self):
|
||||
router = GvaRouter()
|
||||
route = router.route_for_task('web')
|
||||
self.assertEqual(
|
||||
route,
|
||||
{'exchange': 'web', 'exchange_type': 'direct', 'queue': 'web'})
|
||||
|
||||
def test_route_for_task_other(self):
|
||||
router = GvaRouter()
|
||||
route = router.route_for_task('unknown')
|
||||
self.assertIsNone(route)
|
118
gvacommon/tests/test_viewmixins.py
Normal file
118
gvacommon/tests/test_viewmixins.py
Normal file
|
@ -0,0 +1,118 @@
|
|||
#
|
||||
# gvacommon - common parts of gnuviechadmin
|
||||
# Copyright (C) 2014-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/>.
|
||||
#
|
||||
"""
|
||||
Tests for gvacommon code.
|
||||
|
||||
"""
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
import unittest
|
||||
|
||||
try:
|
||||
from unittest.Mock import MagicMock
|
||||
except ImportError:
|
||||
from mock import MagicMock
|
||||
|
||||
import django
|
||||
from django.db import connection
|
||||
from django.http import HttpResponseForbidden
|
||||
from django.conf import settings
|
||||
from django.views.generic import View
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
if not settings.configured:
|
||||
settings.configure(
|
||||
DEBUG=True,
|
||||
ROOT_URLCONF='tests',
|
||||
TEMPLATE_DIRS=['.'],
|
||||
DATABASES={
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': 'test',
|
||||
}
|
||||
},
|
||||
INSTALLED_APPS=(
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.auth',),
|
||||
)
|
||||
|
||||
django.setup()
|
||||
|
||||
urlpatterns = []
|
||||
|
||||
|
||||
class StaffOrSelfLoginRequiredMixinTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
connection.creation.create_test_db()
|
||||
User = get_user_model()
|
||||
User.objects.create_superuser(
|
||||
username='admin', password='admin', email='test@example.org')
|
||||
User.objects.create_user(username='test')
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
connection.creation.destroy_test_db()
|
||||
|
||||
def setUp(self):
|
||||
from gvacommon.viewmixins import StaffOrSelfLoginRequiredMixin
|
||||
|
||||
class SubjectView(StaffOrSelfLoginRequiredMixin, View):
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
return "success"
|
||||
|
||||
def get_customer_object(self):
|
||||
return get_user_model().objects.get(username='test')
|
||||
|
||||
self.subject = SubjectView.as_view()
|
||||
|
||||
def test_dispatch_anonymous(self):
|
||||
from django.contrib.auth.models import AnonymousUser
|
||||
request = MagicMock(method='GET')
|
||||
request.user = AnonymousUser()
|
||||
result = self.subject(request)
|
||||
self.assertIsInstance(result, HttpResponseForbidden)
|
||||
|
||||
def test_dispatch_staff_user(self):
|
||||
request = MagicMock(method='GET')
|
||||
request.user = get_user_model().objects.get(username='admin')
|
||||
result = self.subject(request)
|
||||
self.assertEqual(result, "success")
|
||||
|
||||
def test_dispatch_customer_user(self):
|
||||
request = MagicMock(method='GET')
|
||||
request.user = get_user_model().objects.get(username='test')
|
||||
result = self.subject(request)
|
||||
self.assertEqual(result, "success")
|
||||
|
||||
def test_dispatch_other_user(self):
|
||||
request = MagicMock(method='GET')
|
||||
request.user = get_user_model().objects.create_user('other')
|
||||
result = self.subject(request)
|
||||
self.assertIsInstance(result, HttpResponseForbidden)
|
||||
|
||||
def test_get_customer_object_not_implemented(self):
|
||||
from gvacommon.viewmixins import StaffOrSelfLoginRequiredMixin
|
||||
|
||||
class IncompleteView(StaffOrSelfLoginRequiredMixin, View):
|
||||
pass
|
||||
|
||||
view = IncompleteView()
|
||||
with self.assertRaises(NotImplementedError):
|
||||
view.get_customer_object()
|
59
gvacommon/viewmixins.py
Normal file
59
gvacommon/viewmixins.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
#
|
||||
# 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")
|
Loading…
Add table
Add a link
Reference in a new issue