Fix deprecation warnings

This commit is contained in:
Jan Dittberner 2023-02-18 19:07:33 +01:00
parent b3588b5e6c
commit 0f18e59d67
6 changed files with 67 additions and 75 deletions

View file

@ -24,10 +24,11 @@ SITE_NAME = basename(DJANGO_ROOT)
# ######### END PATH CONFIGURATION # ######### END PATH CONFIGURATION
GVA_ENVIRONMENT = get_env_variable("GVA_ENVIRONMENT", default="prod")
# ######### DEBUG CONFIGURATION # ######### DEBUG CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#debug # See: https://docs.djangoproject.com/en/dev/ref/settings/#debug
DEBUG = False DEBUG = (GVA_ENVIRONMENT == "local")
# ######### END DEBUG CONFIGURATION # ######### END DEBUG CONFIGURATION
@ -351,8 +352,6 @@ GVA_LINK_PHPPGADMIN = get_env_variable(
) )
# ######### END CUSTOM APP CONFIGURATION # ######### END CUSTOM APP CONFIGURATION
GVA_ENVIRONMENT = get_env_variable("GVA_ENVIRONMENT", default="prod")
# ######### STATIC FILE CONFIGURATION # ######### STATIC FILE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-root # See: https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = "/srv/gva/static/" STATIC_ROOT = "/srv/gva/static/"
@ -362,11 +361,19 @@ def show_debug_toolbar(request):
return DEBUG and GVA_ENVIRONMENT == "local" return DEBUG and GVA_ENVIRONMENT == "local"
if GVA_ENVIRONMENT == "local": # ######### TOOLBAR CONFIGURATION
# ######### DEBUG CONFIGURATION # See: http://django-debug-toolbar.readthedocs.org/en/latest/installation.html#explicit-setup # noqa
# See: https://docs.djangoproject.com/en/dev/ref/settings/#debug INSTALLED_APPS += ("debug_toolbar",)
DEBUG = True
MIDDLEWARE += ["debug_toolbar.middleware.DebugToolbarMiddleware"]
DEBUG_TOOLBAR_CONFIG = {
"SHOW_TOOLBAR_CALLBACK": "gnuviechadmin.settings.show_debug_toolbar"
}
# ######### END TOOLBAR CONFIGURATION
if GVA_ENVIRONMENT == "local":
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-debug # See: https://docs.djangoproject.com/en/dev/ref/settings/#template-debug
TEMPLATES[0]["OPTIONS"]["debug"] = DEBUG TEMPLATES[0]["OPTIONS"]["debug"] = DEBUG
# ######### END DEBUG CONFIGURATION # ######### END DEBUG CONFIGURATION
@ -381,12 +388,6 @@ if GVA_ENVIRONMENT == "local":
CACHES = {"default": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"}} CACHES = {"default": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"}}
# ######### END CACHE CONFIGURATION # ######### 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( LOGGING["handlers"].update(
{ {
"console": { "console": {
@ -419,13 +420,6 @@ if GVA_ENVIRONMENT == "local":
] ]
) )
) )
DEBUG_TOOLBAR_PATCH_SETTINGS = False
DEBUG_TOOLBAR_CONFIG = {
"SHOW_TOOLBAR_CALLBACK": "gnuviechadmin.settings.show_debug_toolbar"
}
# ######### END TOOLBAR CONFIGURATION
elif GVA_ENVIRONMENT == "test": elif GVA_ENVIRONMENT == "test":
ALLOWED_HOSTS = ["localhost"] ALLOWED_HOSTS = ["localhost"]
PASSWORD_HASHERS = ("django.contrib.auth.hashers.MD5PasswordHasher",) PASSWORD_HASHERS = ("django.contrib.auth.hashers.MD5PasswordHasher",)

View file

@ -1,11 +1,11 @@
from __future__ import absolute_import from __future__ import absolute_import
import debug_toolbar
from django.conf.urls import include, url from django.conf.urls import include, url
from django.conf import settings
from django.contrib import admin from django.contrib import admin
from django.contrib.flatpages import views from django.contrib.flatpages import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.urls import path
admin.autodiscover() admin.autodiscover()
@ -28,9 +28,7 @@ urlpatterns = [
# Uncomment the next line to serve media files in dev. # Uncomment the next line to serve media files in dev.
# urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) # urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG: # pragma: no cover urlpatterns += staticfiles_urlpatterns()
import debug_toolbar urlpatterns += [
path('__debug__/', include(debug_toolbar.urls)),
urlpatterns = [ ]
url(r'^__debug__/', include(debug_toolbar.urls)),
] + staticfiles_urlpatterns() + urlpatterns

View file

@ -7,24 +7,20 @@ from __future__ import unicode_literals
from django.db import models from django.db import models
from django.utils.encoding import python_2_unicode_compatible from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from passlib.hash import sha512_crypt
from model_utils.models import TimeStampedModel from model_utils.models import TimeStampedModel
from passlib.hash import sha512_crypt
from domains.models import MailDomain from domains.models import MailDomain
from fileservertasks.tasks import create_file_mailbox, delete_file_mailbox
from osusers.models import User as OsUser from osusers.models import User as OsUser
from fileservertasks.tasks import (
create_file_mailbox,
delete_file_mailbox,
)
class ActivateAbleMixin(models.Model): class ActivateAbleMixin(models.Model):
""" """
Mixin for model classes that can be active or inactive. Mixin for model classes that can be active or inactive.
""" """
active = models.BooleanField(default=True) active = models.BooleanField(default=True)
class Meta: class Meta:
@ -51,10 +47,8 @@ class MailboxManager(models.Manager):
mailboxformat = "{0}p{1:02d}" mailboxformat = "{0}p{1:02d}"
mailboxname = mailboxformat.format(osuser.username, count) mailboxname = mailboxformat.format(osuser.username, count)
for box in self.values('username').filter(osuser=osuser).order_by( for box in self.values("username").filter(osuser=osuser).order_by("username"):
'username' if box["username"] == mailboxname:
):
if box['username'] == mailboxname:
count += 1 count += 1
mailboxname = mailboxformat.format(osuser.username, count) mailboxname = mailboxformat.format(osuser.username, count)
else: else:
@ -69,9 +63,10 @@ class MailboxManager(models.Manager):
""" """
return self.filter( return self.filter(
models.Q(mailaddressmailbox__isnull=True) | models.Q(mailaddressmailbox__isnull=True)
models.Q(mailaddressmailbox__mailaddress=mailaddress), | models.Q(mailaddressmailbox__mailaddress=mailaddress),
active=True, osuser=osuser, active=True,
osuser=osuser,
) )
def unused(self, osuser): def unused(self, osuser):
@ -82,7 +77,8 @@ class MailboxManager(models.Manager):
""" """
return self.filter( return self.filter(
mailaddressmailbox__isnull=True, mailaddressmailbox__isnull=True,
active=True, osuser=osuser, active=True,
osuser=osuser,
) )
def create_mailbox(self, osuser, password=None, commit=True): def create_mailbox(self, osuser, password=None, commit=True):
@ -97,7 +93,8 @@ class MailboxManager(models.Manager):
""" """
mailbox = self.create( mailbox = self.create(
osuser=osuser, username=self.get_next_mailbox_name(osuser)) osuser=osuser, username=self.get_next_mailbox_name(osuser)
)
if password is not None: if password is not None:
mailbox.set_password(password) mailbox.set_password(password)
return mailbox return mailbox
@ -109,6 +106,7 @@ class Mailbox(ActivateAbleMixin, TimeStampedModel):
This is the model class for a mailbox. This is the model class for a mailbox.
""" """
osuser = models.ForeignKey(OsUser, on_delete=models.CASCADE) osuser = models.ForeignKey(OsUser, on_delete=models.CASCADE)
username = models.CharField(max_length=128, unique=True) username = models.CharField(max_length=128, unique=True)
password = models.CharField(max_length=255) password = models.CharField(max_length=255)
@ -116,9 +114,9 @@ class Mailbox(ActivateAbleMixin, TimeStampedModel):
objects = MailboxManager() objects = MailboxManager()
class Meta: class Meta:
ordering = ['osuser', 'username'] ordering = ["osuser", "username"]
verbose_name = _('Mailbox') verbose_name = _("Mailbox")
verbose_name_plural = _('Mailboxes') verbose_name_plural = _("Mailboxes")
def set_password(self, password): def set_password(self, password):
""" """
@ -127,7 +125,7 @@ class Mailbox(ActivateAbleMixin, TimeStampedModel):
:param str password: the clear text password :param str password: the clear text password
""" """
self.password = sha512_crypt.encrypt(password) self.password = sha512_crypt.hash(password)
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
# TODO: refactor to use signals # TODO: refactor to use signals
@ -144,11 +142,9 @@ class Mailbox(ActivateAbleMixin, TimeStampedModel):
Get a list of mail addresses assigned to this mailbox. Get a list of mail addresses assigned to this mailbox.
""" """
addrs = [ addrs = [mbadr.mailaddress for mbadr in self.mailaddressmailbox_set.all()]
mbadr.mailaddress for mbadr in
self.mailaddressmailbox_set.all()
]
return addrs return addrs
mailaddresses = property(get_mailaddresses) mailaddresses = property(get_mailaddresses)
def __str__(self): def __str__(self):
@ -161,15 +157,17 @@ class MailAddress(ActivateAbleMixin, TimeStampedModel, models.Model):
This is the model class for a mail address. This is the model class for a mail address.
""" """
localpart = models.CharField(_('local part'), max_length=128)
localpart = models.CharField(_("local part"), max_length=128)
domain = models.ForeignKey( domain = models.ForeignKey(
MailDomain, verbose_name=_('domain'), on_delete=models.CASCADE) MailDomain, verbose_name=_("domain"), on_delete=models.CASCADE
)
class Meta: class Meta:
ordering = ['domain', 'localpart'] ordering = ["domain", "localpart"]
unique_together = ('localpart', 'domain') unique_together = ("localpart", "domain")
verbose_name = _('Mail address') verbose_name = _("Mail address")
verbose_name_plural = _('Mail addresses') verbose_name_plural = _("Mail addresses")
def __str__(self): def __str__(self):
return "{0}@{1}".format(self.localpart, self.domain) return "{0}@{1}".format(self.localpart, self.domain)
@ -220,8 +218,7 @@ class MailAddress(ActivateAbleMixin, TimeStampedModel, models.Model):
if MailAddressMailbox.objects.filter(mailaddress=self).exists(): if MailAddressMailbox.objects.filter(mailaddress=self).exists():
mabox = MailAddressMailbox.objects.get(mailaddress=self) mabox = MailAddressMailbox.objects.get(mailaddress=self)
mabox.delete() mabox.delete()
forwards = MailAddressForward.objects.filter( forwards = MailAddressForward.objects.filter(mailaddress=self).all()
mailaddress=self).all()
for item in forwards: for item in forwards:
if item.target not in addresses: if item.target not in addresses:
item.delete() item.delete()
@ -250,14 +247,19 @@ class MailAddressMailbox(TimeStampedModel, models.Model):
This is the model class to assign a mail address to a mailbox. This is the model class to assign a mail address to a mailbox.
""" """
mailaddress = models.OneToOneField( mailaddress = models.OneToOneField(
MailAddress, verbose_name=_('mailaddress'), primary_key=True, MailAddress,
on_delete=models.CASCADE) verbose_name=_("mailaddress"),
primary_key=True,
on_delete=models.CASCADE,
)
mailbox = models.ForeignKey( mailbox = models.ForeignKey(
Mailbox, verbose_name=_('mailbox'), on_delete=models.CASCADE) Mailbox, verbose_name=_("mailbox"), on_delete=models.CASCADE
)
class Meta: class Meta:
unique_together = ('mailaddress', 'mailbox') unique_together = ("mailaddress", "mailbox")
def __str__(self): def __str__(self):
return self.mailbox.username return self.mailbox.username
@ -268,8 +270,9 @@ class MailAddressForward(TimeStampedModel, models.Model):
This is a model class to map mail addresses to forwarding addresses. This is a model class to map mail addresses to forwarding addresses.
""" """
mailaddress = models.ForeignKey(MailAddress, on_delete=models.CASCADE) mailaddress = models.ForeignKey(MailAddress, on_delete=models.CASCADE)
target = models.EmailField(max_length=254) target = models.EmailField(max_length=254)
class Meta: class Meta:
unique_together = ('mailaddress', 'target') unique_together = ("mailaddress", "target")

View file

@ -3,22 +3,19 @@ This module defines the database models of operating system users.
""" """
import base64 import base64
from datetime import date
import logging import logging
import os import os
from datetime import date
from django.db import models, transaction
from django.conf import settings from django.conf import settings
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db import models, transaction
from django.dispatch import Signal from django.dispatch import Signal
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from model_utils.models import TimeStampedModel from model_utils.models import TimeStampedModel
from passlib.hash import sha512_crypt from passlib.hash import sha512_crypt
from passlib.utils import generate_password from passlib.pwd import genword
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -150,7 +147,7 @@ class UserManager(models.Manager):
If username is None the result of :py:meth:`get_next_username` is used. If username is None the result of :py:meth:`get_next_username` is used.
If password is None a new password will be generated using passlib's If password is None a new password will be generated using passlib's
:py:func:`generate_password`. :py:func:`genword`.
:param customer: Django User instance this user is associated to :param customer: Django User instance this user is associated to
:param str username: the username or None :param str username: the username or None
@ -166,7 +163,7 @@ class UserManager(models.Manager):
if username is None: if username is None:
username = self.get_next_username() username = self.get_next_username()
if password is None: if password is None:
password = generate_password() password = genword(entropy=128)
homedir = os.path.join(settings.OSUSER_HOME_BASEPATH, username) homedir = os.path.join(settings.OSUSER_HOME_BASEPATH, username)
group = Group.objects.create(groupname=username, gid=gid) group = Group.objects.create(groupname=username, gid=gid)
user = self.create( user = self.create(

View file

@ -1,4 +1,4 @@
{% load staticfiles i18n account %}<!DOCTYPE html> {% load static i18n account %}<!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">

View file

@ -1,5 +1,5 @@
""" """
This module defines Celery_ tasks to manage website configurations. This module defines Celery tasks to manage website configurations.
""" """
from __future__ import absolute_import from __future__ import absolute_import