Reorganize package structure

This commit reorganizes the package structure. The gvaldap.settings
modules have been merged. The gvaldap.ldaptasks module has been move up
one level to have task names without the gvaldap prefix.

isort control instructions have been added to setup.cfg.
This commit is contained in:
Jan Dittberner 2020-03-03 12:20:13 +01:00
parent 427fdd9c03
commit 34f788e099
17 changed files with 434 additions and 532 deletions

View File

@ -2,3 +2,7 @@
This is the gvaldap project module. This is the gvaldap project module.
""" """
__version__ = "0.6.0.dev1" __version__ = "0.6.0.dev1"
from ldaptasks.celery import app as celery_app
__all__ = ("celery_app",)

View File

@ -1,23 +0,0 @@
"""
This module defines the Celery_ app for gvaldap.
.. _Celery: http://www.celeryproject.org/
"""
import os
from celery import Celery
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE',
'gvaldap.settings.production')
app = Celery('gvaldap')
def get_installed_apps():
return settings.INSTALLED_APPS
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(get_installed_apps)

315
gvaldap/gvaldap/settings.py Normal file
View File

@ -0,0 +1,315 @@
# -*- coding: utf-8 -*-
# pymode:lint_ignore=E501
"""
Common settings and globals.
"""
from os.path import abspath, basename, dirname, join, normpath
from sys import path
from gvacommon.settings_utils import get_env_variable
# ######### PATH CONFIGURATION
# Absolute filesystem path to the Django project directory:
DJANGO_ROOT = dirname(dirname(abspath(__file__)))
# Absolute filesystem path to the top-level project folder:
SITE_ROOT = dirname(DJANGO_ROOT)
# Site name:
SITE_NAME = basename(DJANGO_ROOT)
# Add our project to our pythonpath, this way we don't need to type our project
# name in our dotted import paths:
path.append(DJANGO_ROOT)
# ######### END PATH CONFIGURATION
# ######### DEBUG CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#debug
DEBUG = get_env_variable("GVALDAP_DEBUG", bool, False)
# ######### MANAGER CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#admins
ADMINS = (
(get_env_variable("GVALDAP_ADMIN_NAME"), get_env_variable("GVALDAP_ADMIN_EMAIL")),
)
# See: https://docs.djangoproject.com/en/dev/ref/settings/#managers
MANAGERS = ADMINS
# ######### END MANAGER CONFIGURATION
# ######### DATABASE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": normpath(join(DJANGO_ROOT, "default.db")),
"USER": "",
"PASSWORD": "",
"HOST": "",
"PORT": "",
},
"ldap": {
"ENGINE": "ldapdb.backends.ldap",
"NAME": get_env_variable("GVALDAP_LDAP_URL"),
"USER": get_env_variable("GVALDAP_LDAP_USER"),
"PASSWORD": get_env_variable("GVALDAP_LDAP_PASSWORD"),
},
}
DATABASE_ROUTERS = ["ldapdb.router.Router"]
# ######### END DATABASE CONFIGURATION
# ######### GENERAL CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#time-zone
TIME_ZONE = "Europe/Berlin"
# See: https://docs.djangoproject.com/en/dev/ref/settings/#language-code
LANGUAGE_CODE = "en-us"
# See: https://docs.djangoproject.com/en/dev/ref/settings/#site-id
SITE_ID = 1
# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-i18n
USE_I18N = True
# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-l10n
USE_L10N = True
# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-tz
USE_TZ = True
# ######### END GENERAL CONFIGURATION
# ######### MEDIA CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-root
MEDIA_ROOT = normpath(join(SITE_ROOT, "media"))
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-url
MEDIA_URL = "/media/"
# ######### END MEDIA CONFIGURATION
# ######### STATIC FILE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = "/static/"
# See:
# https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders # noqa
STATICFILES_FINDERS = ("django.contrib.staticfiles.finders.AppDirectoriesFinder",)
# ######### END STATIC FILE CONFIGURATION
# ######### SECRET CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
# Note: This key should only be used for development and testing.
SECRET_KEY = get_env_variable("GVALDAP_SECRETKEY")
# ######### END SECRET CONFIGURATION
# ######### FIXTURE CONFIGURATION
# See:
# https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-FIXTURE_DIRS # noqa
FIXTURE_DIRS = (normpath(join(SITE_ROOT, "fixtures")),)
# ######### END FIXTURE CONFIGURATION
# ######### TEMPLATE CONFIGURATION
# See: https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-TEMPLATES # noqa
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [normpath(join(SITE_ROOT, "templates"))],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.contrib.auth.context_processors.auth",
"django.template.context_processors.debug",
"django.template.context_processors.i18n",
"django.template.context_processors.media",
"django.template.context_processors.static",
"django.template.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"django.template.context_processors.request",
]
},
}
]
# ######### END TEMPLATE CONFIGURATION
# ######### MIDDLEWARE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#middleware-classes
MIDDLEWARE = (
# Default Django middleware.
"django.middleware.common.CommonMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.locale.LocaleMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
)
# ######### END MIDDLEWARE CONFIGURATION
# ######### URL CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#root-urlconf
ROOT_URLCONF = "%s.urls" % SITE_NAME
# ######### END URL CONFIGURATION
# ######### TEST RUNNER CONFIGURATION
TEST_RUNNER = "django.test.runner.DiscoverRunner"
# ######### END TEST RUNNER CONFIGURATION
# ######### APP CONFIGURATION
DJANGO_APPS = (
# Default Django apps:
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.sites",
"django.contrib.messages",
"django.contrib.staticfiles",
# Admin panel and documentation:
"django.contrib.admin",
)
# Apps specific for this project go here.
LOCAL_APPS = ("ldapentities", "ldaptasks")
# See: https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS
# ######### END APP CONFIGURATION
# ######### LOGGING CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#logging
# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"verbose": {
"format": "%(levelname)s %(asctime)s %(name)s "
"%(module)s:%(lineno)d %(process)d %(thread)d %(message)s"
},
"simple": {"format": "%(levelname)s %(name)s:%(lineno)d %(message)s"},
},
"filters": {"require_debug_false": {"()": "django.utils.log.RequireDebugFalse"}},
"handlers": {
"mail_admins": {
"level": "ERROR",
"filters": ["require_debug_false"],
"class": "django.utils.log.AdminEmailHandler",
}
},
"loggers": {
"django.request": {
"handlers": ["mail_admins"],
"level": "ERROR",
"propagate": True,
}
},
}
# ######### END LOGGING CONFIGURATION
# ######### WSGI CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#wsgi-application
WSGI_APPLICATION = "%s.wsgi.application" % SITE_NAME
# ######### END WSGI CONFIGURATION
# ######### LDAP SETTINGS
GROUP_BASE_DN = get_env_variable("GVALDAP_BASEDN_GROUP")
USER_BASE_DN = get_env_variable("GVALDAP_BASEDN_USER")
# ######### END LDAP SETTINGS
# ######### CELERY CONFIGURATION
CELERY_BROKER_URL = get_env_variable("GVALDAP_BROKER_URL")
CELERY_RESULT_BACKEND = get_env_variable("GVALDAP_RESULTS_REDIS_URL")
CELERY_TASK_RESULT_EXPIRES = None
CELERY_ROUTES = ("gvacommon.celeryrouters.GvaRouter",)
CELERY_TIMEZONE = "Europe/Berlin"
CELERY_ENABLE_UTC = True
CELERY_ACCEPT_CONTENT = ["json"]
CELERY_TASK_SERIALIZER = "json"
CELERY_RESULT_SERIALIZER = "json"
# ######### END CELERY CONFIGURATION
if DEBUG:
TEMPLATES[0]["OPTIONS"]["debug"] = DEBUG
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
CACHES = {"default": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"}}
INSTALLED_APPS += ("debug_toolbar",)
MIDDLEWARE += ("debug_toolbar.middleware.DebugToolbarMiddleware",)
LOGGING["handlers"].update(
{
"console": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"formatter": "simple",
}
}
)
LOGGING["loggers"].update(
dict(
[
(key, {"handlers": ["console"], "level": "DEBUG", "propagate": True})
for key in ["ldapentities", "ldaptasks"]
]
)
)
INTERNAL_IPS = get_env_variable("GVALDAP_INTERNAL_IPS", str, "127.0.0.1").split(",")
else:
ALLOWED_HOSTS = get_env_variable("GVALDAP_ALLOWED_HOSTS").split(",")
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_SUBJECT_PREFIX = "[%s] " % SITE_NAME
DEFAULT_FROM_EMAIL = get_env_variable("GVALDAP_ADMIN_EMAIL")
SERVER_EMAIL = get_env_variable("GVALDAP_SERVER_EMAIL")
if get_env_variable("GVALDAP_TEST", bool, False):
PASSWORD_HASHERS = ("django.contrib.auth.hashers.MD5PasswordHasher",)
# ######### IN-MEMORY TEST DATABASE
DATABASES["default"] = {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
"USER": "",
"PASSWORD": "",
"HOST": "",
"PORT": "",
}
LOGGING["handlers"].update(
{
"console": {
"level": "ERROR",
"class": "logging.StreamHandler",
"formatter": "simple",
}
}
)
LOGGING["loggers"].update(
dict(
[
(key, {"handlers": ["console"], "level": "ERROR", "propagate": True})
for key in ["ldapentities", "ldaptasks"]
]
)
)
CELERY_BROKER_URL = CELERY_BROKER_URL + "_test"
CELERY_RESULT_PERSISTENT = False

View File

@ -1,3 +0,0 @@
"""
This module contains settings for various environments.
"""

View File

@ -1,278 +0,0 @@
# -*- coding: utf-8 -*-
# pymode:lint_ignore=E501
"""
Common settings and globals.
"""
from os.path import abspath, basename, dirname, join, normpath
from sys import path
from gvacommon.settings_utils import get_env_variable
# ######### PATH CONFIGURATION
# Absolute filesystem path to the Django project directory:
DJANGO_ROOT = dirname(dirname(abspath(__file__)))
# Absolute filesystem path to the top-level project folder:
SITE_ROOT = dirname(DJANGO_ROOT)
# Site name:
SITE_NAME = basename(DJANGO_ROOT)
# Add our project to our pythonpath, this way we don't need to type our project
# name in our dotted import paths:
path.append(DJANGO_ROOT)
# ######### END PATH CONFIGURATION
# ######### DEBUG CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#debug
DEBUG = False
# ######### MANAGER CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#admins
ADMINS = (
(get_env_variable('GVALDAP_ADMIN_NAME'),
get_env_variable('GVALDAP_ADMIN_EMAIL')),
)
# See: https://docs.djangoproject.com/en/dev/ref/settings/#managers
MANAGERS = ADMINS
# ######### END MANAGER CONFIGURATION
# ######### DATABASE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': normpath(join(DJANGO_ROOT, 'default.db')),
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
},
'ldap': {
'ENGINE': 'ldapdb.backends.ldap',
'NAME': get_env_variable('GVALDAP_LDAP_URL'),
'USER': get_env_variable('GVALDAP_LDAP_USER'),
'PASSWORD': get_env_variable('GVALDAP_LDAP_PASSWORD'),
}
}
DATABASE_ROUTERS = ['ldapdb.router.Router']
# ######### END DATABASE CONFIGURATION
# ######### GENERAL CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#time-zone
TIME_ZONE = 'Europe/Berlin'
# See: https://docs.djangoproject.com/en/dev/ref/settings/#language-code
LANGUAGE_CODE = 'en-us'
# See: https://docs.djangoproject.com/en/dev/ref/settings/#site-id
SITE_ID = 1
# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-i18n
USE_I18N = True
# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-l10n
USE_L10N = True
# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-tz
USE_TZ = True
# ######### END GENERAL CONFIGURATION
# ######### MEDIA CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-root
MEDIA_ROOT = normpath(join(SITE_ROOT, 'media'))
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-url
MEDIA_URL = '/media/'
# ######### END MEDIA CONFIGURATION
# ######### STATIC FILE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = '/static/'
# See:
# https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders # noqa
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
# ######### END STATIC FILE CONFIGURATION
# ######### SECRET CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
# Note: This key should only be used for development and testing.
SECRET_KEY = get_env_variable('GVALDAP_SECRETKEY')
# ######### END SECRET CONFIGURATION
# ######### SITE CONFIGURATION
# Hosts/domain names that are valid for this site
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = []
# ######### END SITE CONFIGURATION
# ######### FIXTURE CONFIGURATION
# See:
# https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-FIXTURE_DIRS # noqa
FIXTURE_DIRS = (
normpath(join(SITE_ROOT, 'fixtures')),
)
# ######### END FIXTURE CONFIGURATION
# ######### TEMPLATE CONFIGURATION
# See: https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-TEMPLATES # noqa
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
normpath(join(SITE_ROOT, 'templates')),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.request',
],
},
},
]
# ######### END TEMPLATE CONFIGURATION
# ######### MIDDLEWARE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#middleware-classes
MIDDLEWARE = (
# Default Django middleware.
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
# ######### END MIDDLEWARE CONFIGURATION
# ######### URL CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#root-urlconf
ROOT_URLCONF = '%s.urls' % SITE_NAME
# ######### END URL CONFIGURATION
# ######### TEST RUNNER CONFIGURATION
TEST_RUNNER = 'django.test.runner.DiscoverRunner'
# ######### END TEST RUNNER CONFIGURATION
# ######### APP CONFIGURATION
DJANGO_APPS = (
# Default Django apps:
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Admin panel and documentation:
'django.contrib.admin',
)
# Apps specific for this project go here.
LOCAL_APPS = (
'ldapentities',
'gvaldap.ldaptasks',
)
# See: https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS
# ######### END APP CONFIGURATION
# ######### LOGGING CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#logging
# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(name)s '
'%(module)s:%(lineno)d %(process)d %(thread)d %(message)s',
},
'simple': {
'format': '%(levelname)s %(name)s:%(lineno)d %(message)s',
},
},
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}
# ######### END LOGGING CONFIGURATION
# ######### WSGI CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#wsgi-application
WSGI_APPLICATION = '%s.wsgi.application' % SITE_NAME
# ######### END WSGI CONFIGURATION
# ######### LDAP SETTINGS
GROUP_BASE_DN = get_env_variable('GVALDAP_BASEDN_GROUP')
USER_BASE_DN = get_env_variable('GVALDAP_BASEDN_USER')
# ######### END LDAP SETTINGS
# ######### CELERY CONFIGURATION
BROKER_URL = get_env_variable('GVALDAP_BROKER_URL')
CELERY_RESULT_BACKEND = get_env_variable('GVALDAP_RESULTS_REDIS_URL')
CELERY_TASK_RESULT_EXPIRES = None
CELERY_ROUTES = (
'gvacommon.celeryrouters.GvaRouter',
)
CELERY_TIMEZONE = 'Europe/Berlin'
CELERY_ENABLE_UTC = True
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
# ######### END CELERY CONFIGURATION

View File

@ -1,64 +0,0 @@
# -*- python -*-
# pymode:lint_ignore=W0401,E501
"""
Development settings and globals based on :py:mod:`gvaldap.settings.base`.
"""
from __future__ import absolute_import
# use import * to import all settings from base
from .base import * # NOQA
# ######### DEBUG CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#debug
DEBUG = True
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-debug
TEMPLATES[0]['OPTIONS']['debug'] = DEBUG
# ######### END DEBUG CONFIGURATION
# ######### EMAIL CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
# ######### END EMAIL CONFIGURATION
# ######### CACHE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#caches
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
}
}
# ######### END CACHE CONFIGURATION
# ######### TOOLBAR CONFIGURATION
# See: http://django-debug-toolbar.readthedocs.org/en/latest/installation.html#explicit-setup # noqa
INSTALLED_APPS += (
'debug_toolbar',
)
MIDDLEWARE += (
'debug_toolbar.middleware.DebugToolbarMiddleware',
)
LOGGING['handlers'].update({
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple',
}
})
LOGGING['loggers'].update(dict(
[(key, {'handlers': ['console'], 'level': 'DEBUG', 'propagate': True, })
for key in ['ldapentities', 'ldaptasks']]))
DEBUG_TOOLBAR_PATCH_SETTINGS = False
# http://django-debug-toolbar.readthedocs.org/en/latest/installation.html
INTERNAL_IPS = ('127.0.0.1', '10.0.2.2')
# ######### END TOOLBAR CONFIGURATION

View File

@ -1,35 +0,0 @@
# -*- python -*-
# pymode:lint_ignore=W0401,E501
"""
Production settings and globals based on :py:mod:`gvaldap.settings.base`.
"""
from __future__ import absolute_import
# use import * to import all settings from base
from .base import * # NOQA
# ######### HOST CONFIGURATION
# See: https://docs.djangoproject.com/en/1.5/releases/1.5/#allowed-hosts-required-in-production # noqa
ALLOWED_HOSTS = get_env_variable('GVALDAP_ALLOWED_HOSTS').split(',')
# ######### END HOST CONFIGURATION
# ######### EMAIL CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# See: https://docs.djangoproject.com/en/dev/ref/settings/#email-subject-prefix
EMAIL_SUBJECT_PREFIX = '[%s] ' % SITE_NAME
# See: https://docs.djangoproject.com/en/dev/ref/settings/#default-from-email
DEFAULT_FROM_EMAIL = get_env_variable('GVALDAP_ADMIN_EMAIL')
# See: https://docs.djangoproject.com/en/dev/ref/settings/#server-email
SERVER_EMAIL = get_env_variable('GVALDAP_SERVER_EMAIL')
# ######### END EMAIL CONFIGURATION
# ######### CACHE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#caches
# CACHES = {}
# ######### END CACHE CONFIGURATION

View File

@ -1,35 +0,0 @@
# pymode:lint_ignore=W0401
"""
Test settings based on :py:mod:`gvaldap.settings.base`.
"""
from __future__ import absolute_import
# use import * to import all settings from base
from .base import * # NOQA
PASSWORD_HASHERS = (
'django.contrib.auth.hashers.MD5PasswordHasher',
)
# ######### IN-MEMORY TEST DATABASE
DATABASES['default'] = {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
"USER": "",
"PASSWORD": "",
"HOST": "",
"PORT": "",
}
LOGGING['handlers'].update({
'console': {
'level': 'ERROR',
'class': 'logging.StreamHandler',
'formatter': 'simple',
}
})
LOGGING['loggers'].update(dict(
[(key, {'handlers': ['console'], 'level': 'ERROR', 'propagate': True, })
for key in ['ldapentities', 'ldaptasks']]))
BROKER_URL = BROKER_URL + '_test'
CELERY_RESULT_PERSISTENT = False

View File

@ -1,14 +0,0 @@
"""
This module implements tests for :py:mod:`gvaldap.celery`.
"""
from unittest import TestCase
from gvaldap.celery import get_installed_apps
from django.conf import settings
class GetInstalledAppsTest(TestCase):
def test_get_installed_apps(self):
self.assertEqual(get_installed_apps(), settings.INSTALLED_APPS)

View File

@ -17,6 +17,11 @@ import os
from os.path import abspath, dirname from os.path import abspath, dirname
from sys import path from sys import path
# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application # noqa
SITE_ROOT = dirname(dirname(abspath(__file__))) SITE_ROOT = dirname(dirname(abspath(__file__)))
path.append(SITE_ROOT) path.append(SITE_ROOT)
@ -24,12 +29,8 @@ path.append(SITE_ROOT)
# if running multiple sites in the same mod_wsgi process. To fix this, use # if running multiple sites in the same mod_wsgi process. To fix this, use
# mod_wsgi daemon mode with each site in its own daemon process, or use # mod_wsgi daemon mode with each site in its own daemon process, or use
# os.environ["DJANGO_SETTINGS_MODULE"] = "jajaja.settings" # os.environ["DJANGO_SETTINGS_MODULE"] = "jajaja.settings"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gvaldap.settings.production") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gvaldap.settings")
# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application # noqa
application = get_wsgi_application() application = get_wsgi_application()
# Apply WSGI middleware here. # Apply WSGI middleware here.

View File

@ -0,0 +1,17 @@
"""
This module defines the Celery_ app for gvaldap.
.. _Celery: http://www.celeryproject.org/
"""
import os
from celery import Celery
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gvaldap.settings")
app = Celery("ldaptasks")
app.config_from_object("django.conf:settings", namespace="CELERY")
app.autodiscover_tasks()

View File

@ -8,16 +8,14 @@ This module defines `Celery`_ tasks to manage LDAP entities.
from __future__ import absolute_import from __future__ import absolute_import
from copy import deepcopy from copy import deepcopy
from django.core.exceptions import ObjectDoesNotExist
from celery import shared_task from celery import shared_task
from celery.utils.log import get_task_logger
from celery.exceptions import Reject from celery.exceptions import Reject
from celery.utils.log import get_task_logger
from ldapentities.models import ( from django.core.exceptions import ObjectDoesNotExist
LdapGroup,
LdapUser,
)
from ldapentities.models import LdapGroup, LdapUser
_LOGGER = get_task_logger(__name__) _LOGGER = get_task_logger(__name__)
@ -43,20 +41,20 @@ def create_ldap_group(groupname, gid, description):
try: try:
ldapgroup = LdapGroup.objects.get(name=groupname) ldapgroup = LdapGroup.objects.get(name=groupname)
_LOGGER.info( _LOGGER.info(
'LDAP group %s with groupname %s already exists', "LDAP group %s with groupname %s already exists", ldapgroup.dn, groupname
ldapgroup.dn, groupname) )
ldapgroup.gid = gid ldapgroup.gid = gid
except LdapGroup.DoesNotExist: except LdapGroup.DoesNotExist:
ldapgroup = LdapGroup(gid=gid, name=groupname) ldapgroup = LdapGroup(gid=gid, name=groupname)
_LOGGER.info('created LDAP group %s', ldapgroup.dn) _LOGGER.info("created LDAP group %s", ldapgroup.dn)
ldapgroup.description = description ldapgroup.description = description
ldapgroup.save() ldapgroup.save()
_LOGGER.info('set description of LDAP group %s', ldapgroup.dn) _LOGGER.info("set description of LDAP group %s", ldapgroup.dn)
return { return {
'groupname': groupname, "groupname": groupname,
'gid': gid, "gid": gid,
'description': description, "description": description,
'group_dn': ldapgroup.dn, "group_dn": ldapgroup.dn,
} }
@ -89,14 +87,14 @@ def create_ldap_user(username, uid, gid, gecos, homedir, shell, password):
try: try:
ldapuser = LdapUser.objects.get(username=username) ldapuser = LdapUser.objects.get(username=username)
_LOGGER.info( _LOGGER.info(
'LDAP user %s with username %s already exists', "LDAP user %s with username %s already exists", ldapuser.dn, username
ldapuser.dn, username) )
except LdapUser.DoesNotExist: except LdapUser.DoesNotExist:
ldapuser = LdapUser(username=username) ldapuser = LdapUser(username=username)
try: try:
ldapgroup = LdapGroup.objects.get(gid=gid) ldapgroup = LdapGroup.objects.get(gid=gid)
except ObjectDoesNotExist as exc: except ObjectDoesNotExist as exc:
_LOGGER.error('LDAP group with gid %d does not exist', gid) _LOGGER.error("LDAP group with gid %d does not exist", gid)
raise Reject(exc, requeue=False) raise Reject(exc, requeue=False)
ldapuser.uid = uid ldapuser.uid = uid
ldapuser.group = gid ldapuser.group = gid
@ -107,27 +105,27 @@ def create_ldap_user(username, uid, gid, gecos, homedir, shell, password):
ldapuser.common_name = username ldapuser.common_name = username
if password is not None: if password is not None:
ldapuser.set_password(password) ldapuser.set_password(password)
_LOGGER.info('set password for LDAP user %s', ldapuser.dn) _LOGGER.info("set password for LDAP user %s", ldapuser.dn)
ldapuser.save() ldapuser.save()
_LOGGER.info('LDAP user %s created', ldapuser.dn) _LOGGER.info("LDAP user %s created", ldapuser.dn)
if ldapuser.username in ldapgroup.members: if ldapuser.username in ldapgroup.members:
_LOGGER.info( _LOGGER.info(
'LDAP user %s is already member of LDAP group %s', "LDAP user %s is already member of LDAP group %s", ldapuser.dn, ldapgroup.dn
ldapuser.dn, ldapgroup.dn) )
else: else:
ldapgroup.members.append(ldapuser.username) ldapgroup.members.append(ldapuser.username)
ldapgroup.save() ldapgroup.save()
_LOGGER.info( _LOGGER.info(
'LDAP user %s has been added to LDAP group %s', "LDAP user %s has been added to LDAP group %s", ldapuser.dn, ldapgroup.dn
ldapuser.dn, ldapgroup.dn) )
return { return {
'username': username, "username": username,
'uid': uid, "uid": uid,
'gid': gid, "gid": gid,
'gecos': gecos, "gecos": gecos,
'homedir': homedir, "homedir": homedir,
'shell': shell, "shell": shell,
'user_dn': ldapuser.dn, "user_dn": ldapuser.dn,
} }
@ -145,16 +143,16 @@ def set_ldap_user_password(username, password):
:rtype: dict :rtype: dict
""" """
retval = {'username': username, 'password_set': False} retval = {"username": username, "password_set": False}
try: try:
ldapuser = LdapUser.objects.get(username=username) ldapuser = LdapUser.objects.get(username=username)
except LdapUser.DoesNotExist: except LdapUser.DoesNotExist:
_LOGGER.info('there is no LDAP user with username %s', username) _LOGGER.info("there is no LDAP user with username %s", username)
return retval return retval
ldapuser.set_password(password) ldapuser.set_password(password)
ldapuser.save() ldapuser.save()
_LOGGER.info("set new password for LDAP user %s", ldapuser.dn) _LOGGER.info("set new password for LDAP user %s", ldapuser.dn)
retval['password_set'] = True retval["password_set"] = True
return retval return retval
@ -175,27 +173,31 @@ def add_ldap_user_to_group(self, username, groupname):
:rtype: dict :rtype: dict
""" """
retval = {'username': username, 'groupname': groupname, 'added': False} retval = {"username": username, "groupname": groupname, "added": False}
try: try:
ldapgroup = LdapGroup.objects.get(name=groupname) ldapgroup = LdapGroup.objects.get(name=groupname)
ldapuser = LdapUser.objects.get(username=username) ldapuser = LdapUser.objects.get(username=username)
except LdapGroup.DoesNotExist: except LdapGroup.DoesNotExist:
_LOGGER.error('LDAP group with groupname %s does not exist', groupname) _LOGGER.error("LDAP group with groupname %s does not exist", groupname)
except LdapUser.DoesNotExist as exc: except LdapUser.DoesNotExist as exc:
_LOGGER.error('LDAP user with username %s does not exist', username) _LOGGER.error("LDAP user with username %s does not exist", username)
self.retry(exc=exc, time_limit=5) self.retry(exc=exc, time_limit=5)
else: else:
if ldapuser.username not in ldapgroup.members: if ldapuser.username not in ldapgroup.members:
ldapgroup.members.append(ldapuser.username) ldapgroup.members.append(ldapuser.username)
ldapgroup.save() ldapgroup.save()
_LOGGER.info( _LOGGER.info(
'LDAP user %s has been added to LDAP group %s', "LDAP user %s has been added to LDAP group %s",
ldapuser.username, ldapgroup.dn) ldapuser.username,
ldapgroup.dn,
)
else: else:
_LOGGER.info( _LOGGER.info(
'LDAP user %s is already in LDAP group %s', "LDAP user %s is already in LDAP group %s",
ldapuser.username, ldapgroup.dn) ldapuser.username,
retval['added'] = True ldapgroup.dn,
)
retval["added"] = True
return retval return retval
@ -212,26 +214,28 @@ def remove_ldap_user_from_group(username, groupname):
:rtype: dict :rtype: dict
""" """
retval = {'username': username, 'groupname': groupname, 'removed': False} retval = {"username": username, "groupname": groupname, "removed": False}
try: try:
ldapgroup = LdapGroup.objects.get(name=groupname) ldapgroup = LdapGroup.objects.get(name=groupname)
ldapuser = LdapUser.objects.get(username=username) ldapuser = LdapUser.objects.get(username=username)
except LdapGroup.DoesNotExist: except LdapGroup.DoesNotExist:
_LOGGER.error('LDAP group with groupname %s does not exist', groupname) _LOGGER.error("LDAP group with groupname %s does not exist", groupname)
except LdapUser.DoesNotExist: except LdapUser.DoesNotExist:
_LOGGER.error('LDAP user with username %s does not exist', username) _LOGGER.error("LDAP user with username %s does not exist", username)
else: else:
if ldapuser.username in ldapgroup.members: if ldapuser.username in ldapgroup.members:
ldapgroup.members.remove(ldapuser.username) ldapgroup.members.remove(ldapuser.username)
_LOGGER.info( _LOGGER.info(
'removed LDAP user %s from LDAP group %s', "removed LDAP user %s from LDAP group %s", ldapuser.dn, ldapgroup.dn
ldapuser.dn, ldapgroup.dn) )
ldapgroup.save() ldapgroup.save()
retval['removed'] = True retval["removed"] = True
else: else:
_LOGGER.info( _LOGGER.info(
'LDAP user %s is not a member of LDAP group %s', "LDAP user %s is not a member of LDAP group %s",
ldapuser.dn, ldapgroup.dn) ldapuser.dn,
ldapgroup.dn,
)
return retval return retval
@ -255,29 +259,31 @@ def delete_ldap_user(username, *args, **kwargs):
positions in the task chain positions in the task chain
""" """
retval = {'username': username, 'deleted': False} retval = {"username": username, "deleted": False}
try: try:
ldapuser = LdapUser.objects.get(username=username) ldapuser = LdapUser.objects.get(username=username)
except LdapUser.DoesNotExist: except LdapUser.DoesNotExist:
_LOGGER.info('there is no LDAP user with username %s', username) _LOGGER.info("there is no LDAP user with username %s", username)
else: else:
try: try:
ldapgroup = LdapGroup.objects.get(gid=ldapuser.group) ldapgroup = LdapGroup.objects.get(gid=ldapuser.group)
except LdapGroup.DoesNotExist: except LdapGroup.DoesNotExist:
_LOGGER.info( _LOGGER.info(
'LDAP group %s of LDAP user %s does not exist', "LDAP group %s of LDAP user %s does not exist",
ldapuser.group, ldapuser.dn) ldapuser.group,
ldapuser.dn,
)
else: else:
if ldapuser.username in ldapgroup.members: if ldapuser.username in ldapgroup.members:
ldapgroup.members.remove(ldapuser.username) ldapgroup.members.remove(ldapuser.username)
ldapgroup.save() ldapgroup.save()
_LOGGER.info( _LOGGER.info(
'removed LDAP user %s from LDAP group %s', "removed LDAP user %s from LDAP group %s", ldapuser.dn, ldapgroup.dn
ldapuser.dn, ldapgroup.dn) )
userdn = ldapuser.dn userdn = ldapuser.dn
ldapuser.delete() ldapuser.delete()
_LOGGER.info('deleted LDAP user %s', userdn) _LOGGER.info("deleted LDAP user %s", userdn)
retval['deleted'] = True retval["deleted"] = True
return retval return retval
@ -295,7 +301,7 @@ def delete_ldap_user_chained(previous_result, *args, **kwargs):
:rtype: dict :rtype: dict
""" """
username = previous_result['username'] username = previous_result["username"]
retval = deepcopy(previous_result) retval = deepcopy(previous_result)
retval.update(delete_ldap_user(username)) retval.update(delete_ldap_user(username))
return retval return retval
@ -313,22 +319,23 @@ def delete_ldap_group_if_empty(groupname):
:rtype: dict :rtype: dict
""" """
retval = {'groupname': groupname, 'deleted': False} retval = {"groupname": groupname, "deleted": False}
try: try:
ldapgroup = LdapGroup.objects.get(name=groupname) ldapgroup = LdapGroup.objects.get(name=groupname)
except LdapGroup.DoesNotExist: except LdapGroup.DoesNotExist:
_LOGGER.info('LDAP group with groupname %s does not exist', groupname) _LOGGER.info("LDAP group with groupname %s does not exist", groupname)
else: else:
if len(ldapgroup.members) == 0: if len(ldapgroup.members) == 0:
groupdn = ldapgroup.dn groupdn = ldapgroup.dn
ldapgroup.delete() ldapgroup.delete()
_LOGGER.info( _LOGGER.info("deleted LDAP group %s", groupdn)
'deleted LDAP group %s', groupdn) retval["deleted"] = True
retval['deleted'] = True
else: else:
_LOGGER.info( _LOGGER.info(
'LDAP group %s has not been deleted. It still has %d members', "LDAP group %s has not been deleted. It still has %d members",
ldapgroup.dn, len(ldapgroup.members)) ldapgroup.dn,
len(ldapgroup.members),
)
return retval return retval
@ -344,14 +351,14 @@ def delete_ldap_group(groupname):
:rtype: dict :rtype: dict
""" """
retval = {'groupname': groupname, 'deleted': False} retval = {"groupname": groupname, "deleted": False}
try: try:
ldapgroup = LdapGroup.objects.get(name=groupname) ldapgroup = LdapGroup.objects.get(name=groupname)
except LdapGroup.DoesNotExist: except LdapGroup.DoesNotExist:
_LOGGER.info('LDAP group with name %s does not exist', groupname) _LOGGER.info("LDAP group with name %s does not exist", groupname)
else: else:
groupdn = ldapgroup.dn groupdn = ldapgroup.dn
ldapgroup.delete() ldapgroup.delete()
_LOGGER.info('deleted LDAP group %s', groupdn) _LOGGER.info("deleted LDAP group %s", groupdn)
retval['deleted'] = True retval["deleted"] = True
return retval return retval

View File

@ -3,12 +3,13 @@ This module provides tests for :py:mod:`ldaptasks.tasks`.
""" """
import volatildap import volatildap
from django.conf import settings
from django.test import TestCase
from celery.exceptions import Reject from celery.exceptions import Reject
from django.conf import settings
from django.test import TestCase
from ldapentities.models import LdapUser from ldapentities.models import LdapUser
from gvaldap.ldaptasks.tasks import ( from ldaptasks.tasks import (
add_ldap_user_to_group, add_ldap_user_to_group,
create_ldap_group, create_ldap_group,
create_ldap_user, create_ldap_user,

View File

@ -3,7 +3,7 @@ import os
import sys import sys
if __name__ == "__main__": if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gvaldap.settings.local") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gvaldap.settings")
from django.core.management import execute_from_command_line from django.core.management import execute_from_command_line

View File

@ -5,12 +5,21 @@ exclude = migrations
exclude = migrations exclude = migrations
[coverage:run] [coverage:run]
source = gvaldap,ldapentities,gvaldap.ldaptasks source = gvaldap,ldapentities,ldaptasks
branch = True branch = True
omit = */migrations/*,*/tests/*.py,*/tests.py,gvaldap.py
relative_files = True
[coverage:report] [coverage:report]
omit = */migrations/*,*/tests/*.py,*/tests.py,gvaldap/settings/local.py,gvaldap/settings/production.py
show_missing = True show_missing = True
[coverage:html] [coverage:html]
directory = ../coverage-report directory = ../coverage-report
[isort]
multi_line_output = 3
line_length = 88
known_django = django
known_third_party = celery,volatildap
include_trailing_comma = True
sections = FUTURE,STDLIB,THIRDPARTY,DJANGO,FIRSTPARTY,LOCALFOLDER