2017-09-10 14:53:02 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
2017-06-25 22:50:55 +00:00
|
|
|
# Re2o un logiciel d'administration développé initiallement au rezometz. Il
|
|
|
|
# se veut agnostique au réseau considéré, de manière à être installable en
|
|
|
|
# quelques clics.
|
|
|
|
#
|
|
|
|
# Copyright © 2017 Gabriel Détraz
|
2019-09-29 14:02:28 +00:00
|
|
|
# Copyright © 2017 Lara Kermarec
|
2017-06-25 22:50:55 +00:00
|
|
|
# Copyright © 2017 Augustin Lemesle
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2017-10-14 04:03:53 +00:00
|
|
|
"""
|
2020-05-30 09:22:55 +00:00
|
|
|
Models defining the preferences for users, machines, emails, general settings
|
|
|
|
etc.
|
2017-10-14 04:03:53 +00:00
|
|
|
"""
|
2017-09-10 23:29:24 +00:00
|
|
|
from __future__ import unicode_literals
|
2019-01-20 23:54:02 +00:00
|
|
|
import os
|
2017-06-25 22:50:55 +00:00
|
|
|
|
2018-01-29 02:53:49 +00:00
|
|
|
from django.utils.functional import cached_property
|
2018-07-03 16:34:10 +00:00
|
|
|
from django.utils import timezone
|
2017-06-25 22:50:55 +00:00
|
|
|
from django.db import models
|
2018-04-14 18:55:24 +00:00
|
|
|
from django.db.models.signals import post_save
|
2018-01-30 05:33:56 +00:00
|
|
|
from django.dispatch import receiver
|
2018-01-30 22:07:43 +00:00
|
|
|
from django.core.cache import cache
|
2018-06-26 20:27:33 +00:00
|
|
|
from django.forms import ValidationError
|
2018-07-26 19:53:32 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2018-07-30 15:00:41 +00:00
|
|
|
|
2018-04-14 18:55:24 +00:00
|
|
|
import machines.models
|
2018-07-09 19:05:50 +00:00
|
|
|
|
2019-01-20 23:54:02 +00:00
|
|
|
from re2o.mixins import AclMixin, RevMixin
|
2018-07-10 23:07:31 +00:00
|
|
|
from re2o.aes_field import AESEncryptedField
|
2018-07-03 16:34:10 +00:00
|
|
|
|
2018-07-10 23:07:31 +00:00
|
|
|
from datetime import timedelta
|
2017-06-25 22:50:55 +00:00
|
|
|
|
2018-04-13 23:42:22 +00:00
|
|
|
|
2020-04-22 17:35:45 +00:00
|
|
|
class PreferencesModel(models.Model):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Base object for the Preferences objects.
|
|
|
|
|
|
|
|
Defines methods to handle the cache of the settings (they should not change
|
|
|
|
a lot).
|
|
|
|
"""
|
2020-04-22 17:35:45 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def set_in_cache(cls):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Save the preferences in a server-side cache."""
|
2020-04-22 17:35:45 +00:00
|
|
|
instance, _created = cls.objects.get_or_create()
|
|
|
|
cache.set(cls().__class__.__name__.lower(), instance, None)
|
|
|
|
return instance
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_cached_value(cls, key):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Get the preferences from the server-side cache."""
|
2020-04-22 17:35:45 +00:00
|
|
|
instance = cache.get(cls().__class__.__name__.lower())
|
|
|
|
if instance is None:
|
|
|
|
instance = cls.set_in_cache()
|
|
|
|
return getattr(instance, key)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
|
2018-03-28 15:39:23 +00:00
|
|
|
class OptionalUser(AclMixin, PreferencesModel):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""User preferences: telephone number requirement, user balance activation,
|
|
|
|
creation of users by everyone etc.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
is_tel_mandatory: whether indicating a telephone number is mandatory.
|
|
|
|
gpg_fingerprint: whether GPG fingerprints are enabled.
|
|
|
|
all_can_create_club: whether all users can create a club.
|
|
|
|
all_can_create_adherent: whether all users can create a member.
|
|
|
|
shell_default: the default shell for users connecting to machines
|
|
|
|
managed by the organisation.
|
|
|
|
self_change_shell: whether users can edit their shell.
|
|
|
|
self_change_pseudo: whether users can edit their pseudo (username).
|
|
|
|
self_room_policy: whether users can edit the policy of their room.
|
|
|
|
local_email_accounts_enabled: whether local email accounts are enabled.
|
|
|
|
local_email_domain: the domain used for local email accounts.
|
|
|
|
max_email_address: the maximum number of local email addresses allowed
|
|
|
|
for a standard user.
|
|
|
|
delete_notyetactive: the number of days before deleting not yet active
|
|
|
|
users.
|
|
|
|
disable_emailnotyetconfirmed: the number of days before disabling users
|
|
|
|
with not yet verified email address.
|
|
|
|
self_adhesion: whether users can create their account themselves.
|
|
|
|
all_users_active: whether newly created users are active.
|
|
|
|
allow_set_password_during_user_creation: whether users can set their
|
|
|
|
password directly when creating their account.
|
|
|
|
allow_archived_connexion: whether archived users can connect on the web
|
|
|
|
interface.
|
|
|
|
"""
|
2017-08-24 19:36:59 +00:00
|
|
|
|
2020-04-19 01:16:55 +00:00
|
|
|
DISABLED = "DISABLED"
|
|
|
|
ONLY_INACTIVE = "ONLY_INACTIVE"
|
|
|
|
ALL_ROOM = "ALL_ROOM"
|
|
|
|
ROOM_POLICY = (
|
|
|
|
(DISABLED, _("Users can't select their room")),
|
2020-04-22 17:35:45 +00:00
|
|
|
(
|
|
|
|
ONLY_INACTIVE,
|
|
|
|
_(
|
|
|
|
"Users can only select a room occupied by a user with a disabled connection."
|
|
|
|
),
|
|
|
|
),
|
2020-04-19 01:16:55 +00:00
|
|
|
(ALL_ROOM, _("Users can select all rooms")),
|
|
|
|
)
|
|
|
|
|
2017-06-25 22:50:55 +00:00
|
|
|
is_tel_mandatory = models.BooleanField(default=True)
|
|
|
|
gpg_fingerprint = models.BooleanField(default=True)
|
2018-02-22 17:14:20 +00:00
|
|
|
all_can_create_club = models.BooleanField(
|
2019-11-04 16:55:03 +00:00
|
|
|
default=False, help_text=_("Users can create a club.")
|
2018-02-22 17:14:20 +00:00
|
|
|
)
|
|
|
|
all_can_create_adherent = models.BooleanField(
|
2019-11-04 16:55:03 +00:00
|
|
|
default=False, help_text=_("Users can create a member.")
|
2017-11-21 04:47:05 +00:00
|
|
|
)
|
2018-03-24 20:19:52 +00:00
|
|
|
shell_default = models.OneToOneField(
|
2019-11-04 16:55:03 +00:00
|
|
|
"users.ListShell", on_delete=models.PROTECT, blank=True, null=True
|
2018-03-24 20:19:52 +00:00
|
|
|
)
|
2018-08-13 17:36:57 +00:00
|
|
|
self_change_shell = models.BooleanField(
|
2019-11-04 16:55:03 +00:00
|
|
|
default=False, help_text=_("Users can edit their shell.")
|
2018-08-13 17:36:57 +00:00
|
|
|
)
|
2020-04-23 10:07:15 +00:00
|
|
|
self_change_pseudo = models.BooleanField(
|
|
|
|
default=True, help_text=_("Users can edit their pseudo.")
|
|
|
|
)
|
2020-04-19 01:16:55 +00:00
|
|
|
self_room_policy = models.CharField(
|
|
|
|
max_length=32,
|
|
|
|
choices=ROOM_POLICY,
|
|
|
|
default="DISABLED",
|
2020-04-22 17:35:45 +00:00
|
|
|
help_text=_("Policy on self users room edition"),
|
2020-04-15 17:55:35 +00:00
|
|
|
)
|
2018-07-30 15:00:41 +00:00
|
|
|
local_email_accounts_enabled = models.BooleanField(
|
2019-11-04 16:55:03 +00:00
|
|
|
default=False, help_text=_("Enable local email accounts for users.")
|
2018-06-29 16:20:25 +00:00
|
|
|
)
|
2018-07-30 15:00:41 +00:00
|
|
|
local_email_domain = models.CharField(
|
2018-08-05 16:48:35 +00:00
|
|
|
max_length=32,
|
|
|
|
default="@example.org",
|
2019-11-16 14:07:59 +00:00
|
|
|
help_text=_("Domain to use for local email accounts."),
|
2018-06-26 20:27:33 +00:00
|
|
|
)
|
2018-08-01 11:06:25 +00:00
|
|
|
max_email_address = models.IntegerField(
|
2018-08-05 16:48:35 +00:00
|
|
|
default=15,
|
2019-11-16 14:07:59 +00:00
|
|
|
help_text=_("Maximum number of local email addresses for a standard user."),
|
2018-06-30 12:46:35 +00:00
|
|
|
)
|
2018-10-11 13:15:16 +00:00
|
|
|
delete_notyetactive = models.IntegerField(
|
|
|
|
default=15,
|
2020-04-22 17:35:45 +00:00
|
|
|
help_text=_("Not yet active users will be deleted after this number of days."),
|
2018-10-11 13:15:16 +00:00
|
|
|
)
|
2020-04-17 15:03:54 +00:00
|
|
|
disable_emailnotyetconfirmed = models.IntegerField(
|
2020-04-16 21:14:19 +00:00
|
|
|
default=2,
|
|
|
|
help_text=_(
|
2020-04-17 15:03:54 +00:00
|
|
|
"Users with an email address not yet confirmed will be disabled after this number of days."
|
2020-04-16 21:14:19 +00:00
|
|
|
),
|
|
|
|
)
|
2018-10-14 22:44:37 +00:00
|
|
|
self_adhesion = models.BooleanField(
|
2019-11-04 16:55:03 +00:00
|
|
|
default=False, help_text=_("A new user can create their account on Re2o.")
|
2018-10-14 22:44:37 +00:00
|
|
|
)
|
2019-01-05 17:32:54 +00:00
|
|
|
all_users_active = models.BooleanField(
|
|
|
|
default=False,
|
2019-11-04 16:55:03 +00:00
|
|
|
help_text=_(
|
|
|
|
"If True, all new created and connected users are active."
|
|
|
|
" If False, only when a valid registration has been paid."
|
|
|
|
),
|
2019-01-05 17:32:54 +00:00
|
|
|
)
|
2020-04-16 17:16:33 +00:00
|
|
|
allow_set_password_during_user_creation = models.BooleanField(
|
|
|
|
default=False,
|
|
|
|
help_text=_(
|
|
|
|
"If True, users have the choice to receive an email containing"
|
|
|
|
" a link to reset their password during creation, or to directly"
|
|
|
|
" set their password in the page."
|
|
|
|
" If False, an email is always sent."
|
|
|
|
),
|
|
|
|
)
|
2019-09-09 10:03:27 +00:00
|
|
|
allow_archived_connexion = models.BooleanField(
|
2019-11-04 16:55:03 +00:00
|
|
|
default=False, help_text=_("If True, archived users are allowed to connect.")
|
2019-09-09 10:03:27 +00:00
|
|
|
)
|
2017-06-25 22:50:55 +00:00
|
|
|
|
2017-12-31 20:43:59 +00:00
|
|
|
class Meta:
|
2019-11-16 14:07:59 +00:00
|
|
|
permissions = (("view_optionaluser", _("Can view the user preferences")),)
|
|
|
|
verbose_name = _("user preferences")
|
2017-12-31 20:43:59 +00:00
|
|
|
|
2018-06-26 20:27:33 +00:00
|
|
|
def clean(self):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Check the email extension."""
|
2018-07-30 15:00:41 +00:00
|
|
|
if self.local_email_domain[0] != "@":
|
2019-11-16 14:07:59 +00:00
|
|
|
raise ValidationError(_("Email domain must begin with @."))
|
2018-07-30 15:00:41 +00:00
|
|
|
|
2017-10-14 04:03:53 +00:00
|
|
|
|
2018-01-31 03:10:10 +00:00
|
|
|
@receiver(post_save, sender=OptionalUser)
|
2018-04-15 01:00:05 +00:00
|
|
|
def optionaluser_post_save(**kwargs):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Write in the cache."""
|
2019-11-04 16:55:03 +00:00
|
|
|
user_pref = kwargs["instance"]
|
2018-01-31 03:10:10 +00:00
|
|
|
user_pref.set_in_cache()
|
|
|
|
|
|
|
|
|
2018-03-28 15:39:23 +00:00
|
|
|
class OptionalMachine(AclMixin, PreferencesModel):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Machines preferences: maximum number of machines per user, IPv6
|
|
|
|
activation etc.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
password_machine: whether password per machine is enabled.
|
|
|
|
max_lambdauser_interfaces: the maximum number of interfaces allowed for
|
|
|
|
a standard user.
|
|
|
|
max_lambdauser_aliases: the maximum number of aliases allowed for a
|
|
|
|
standard user.
|
|
|
|
ipv6_mode: whether IPv6 mode is enabled.
|
|
|
|
create_machine: whether creation of machine is enabled.
|
|
|
|
default_dns_ttl: the default TTL for CNAME, A and AAAA records.
|
|
|
|
"""
|
2017-08-24 19:36:59 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
SLAAC = "SLAAC"
|
|
|
|
DHCPV6 = "DHCPV6"
|
|
|
|
DISABLED = "DISABLED"
|
2018-01-29 02:53:49 +00:00
|
|
|
CHOICE_IPV6 = (
|
2019-11-16 14:07:59 +00:00
|
|
|
(SLAAC, _("Automatic configuration by RA")),
|
|
|
|
(DHCPV6, _("IP addresses assignment by DHCPv6")),
|
2018-08-05 16:48:35 +00:00
|
|
|
(DISABLED, _("Disabled")),
|
2018-01-29 02:53:49 +00:00
|
|
|
)
|
|
|
|
|
2017-06-25 22:50:55 +00:00
|
|
|
password_machine = models.BooleanField(default=False)
|
|
|
|
max_lambdauser_interfaces = models.IntegerField(default=10)
|
|
|
|
max_lambdauser_aliases = models.IntegerField(default=10)
|
2019-11-04 16:55:03 +00:00
|
|
|
ipv6_mode = models.CharField(max_length=32, choices=CHOICE_IPV6, default="DISABLED")
|
|
|
|
create_machine = models.BooleanField(default=True)
|
2019-10-02 20:48:33 +00:00
|
|
|
default_dns_ttl = models.PositiveIntegerField(
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("default Time To Live (TTL) for CNAME, A and AAAA records"),
|
2019-10-02 20:48:33 +00:00
|
|
|
default=172800, # 2 days
|
|
|
|
)
|
2018-01-29 02:53:49 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def ipv6(self):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Check if the IPv6 mode is enabled."""
|
2019-11-04 16:55:03 +00:00
|
|
|
return not self.get_cached_value("ipv6_mode") == "DISABLED"
|
2018-01-30 22:07:43 +00:00
|
|
|
|
2017-12-31 20:43:59 +00:00
|
|
|
class Meta:
|
2019-11-16 14:07:59 +00:00
|
|
|
permissions = (("view_optionalmachine", _("Can view the machine preferences")),)
|
|
|
|
verbose_name = _("machine preferences")
|
2017-12-31 20:43:59 +00:00
|
|
|
|
2017-10-14 04:03:53 +00:00
|
|
|
|
2018-01-30 05:33:56 +00:00
|
|
|
@receiver(post_save, sender=OptionalMachine)
|
2018-04-15 01:00:05 +00:00
|
|
|
def optionalmachine_post_save(**kwargs):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Synchronise IPv6 mode and write in the cache."""
|
2019-11-04 16:55:03 +00:00
|
|
|
machine_pref = kwargs["instance"]
|
2018-01-31 03:10:10 +00:00
|
|
|
machine_pref.set_in_cache()
|
2018-01-30 05:33:56 +00:00
|
|
|
if machine_pref.ipv6_mode != "DISABLED":
|
|
|
|
for interface in machines.models.Interface.objects.all():
|
|
|
|
interface.sync_ipv6()
|
|
|
|
|
|
|
|
|
2018-03-28 15:39:23 +00:00
|
|
|
class OptionalTopologie(AclMixin, PreferencesModel):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Configuration of switches: automatic provision, RADIUS mode, default
|
|
|
|
VLANs etc.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
switchs_web_management: whether web management for automatic provision
|
|
|
|
is enabled.
|
|
|
|
switchs_web_management_ssl: whether SSL web management is required.
|
|
|
|
switchs_rest_management: whether REST management for automatic
|
|
|
|
provision is enabled.
|
|
|
|
switchs_ip_type: the IP range for the management of switches.
|
|
|
|
switchs_provision: the provision mode for switches to get their
|
|
|
|
configuration.
|
|
|
|
sftp_login: the SFTP login for switches.
|
|
|
|
sftp_pass: the SFTP password for switches.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
|
|
|
MACHINE = "MACHINE"
|
|
|
|
DEFINED = "DEFINED"
|
2017-09-02 13:20:44 +00:00
|
|
|
CHOICE_RADIUS = (
|
2018-08-05 16:48:35 +00:00
|
|
|
(MACHINE, _("On the IP range's VLAN of the machine")),
|
2020-04-22 17:35:45 +00:00
|
|
|
(DEFINED, _('Preset in "VLAN for machines accepted by RADIUS"')),
|
2017-09-02 13:20:44 +00:00
|
|
|
)
|
2019-11-16 14:07:59 +00:00
|
|
|
CHOICE_PROVISION = (("sftp", "SFTP"), ("tftp", "TFTP"))
|
2017-06-25 22:50:55 +00:00
|
|
|
|
2018-07-10 22:16:35 +00:00
|
|
|
switchs_web_management = models.BooleanField(
|
|
|
|
default=False,
|
2019-11-16 14:07:59 +00:00
|
|
|
help_text=_("Web management, activated in case of automatic provision."),
|
2018-07-10 22:16:35 +00:00
|
|
|
)
|
|
|
|
switchs_web_management_ssl = models.BooleanField(
|
|
|
|
default=False,
|
2019-11-04 16:55:03 +00:00
|
|
|
help_text=_(
|
|
|
|
"SSL web management, make sure that a certificate is"
|
2019-11-16 14:07:59 +00:00
|
|
|
" installed on the switch."
|
2019-11-04 16:55:03 +00:00
|
|
|
),
|
2018-10-14 22:44:37 +00:00
|
|
|
)
|
2018-07-10 22:16:35 +00:00
|
|
|
switchs_rest_management = models.BooleanField(
|
|
|
|
default=False,
|
2019-11-16 14:07:59 +00:00
|
|
|
help_text=_("REST management, activated in case of automatic provision."),
|
2018-07-10 22:16:35 +00:00
|
|
|
)
|
|
|
|
switchs_ip_type = models.OneToOneField(
|
2019-11-04 16:55:03 +00:00
|
|
|
"machines.IpType",
|
2018-07-10 22:16:35 +00:00
|
|
|
on_delete=models.PROTECT,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
2019-11-16 14:07:59 +00:00
|
|
|
help_text=_("IP range for the management of switches."),
|
2018-07-10 22:16:35 +00:00
|
|
|
)
|
2018-07-12 15:33:26 +00:00
|
|
|
switchs_provision = models.CharField(
|
|
|
|
max_length=32,
|
|
|
|
choices=CHOICE_PROVISION,
|
2019-11-04 16:55:03 +00:00
|
|
|
default="tftp",
|
2019-11-16 14:07:59 +00:00
|
|
|
help_text=_("Provision of configuration mode for switches."),
|
2018-07-12 15:33:26 +00:00
|
|
|
)
|
|
|
|
sftp_login = models.CharField(
|
2019-11-16 14:07:59 +00:00
|
|
|
max_length=32, null=True, blank=True, help_text=_("SFTP login for switches.")
|
2018-07-12 15:33:26 +00:00
|
|
|
)
|
|
|
|
sftp_pass = AESEncryptedField(
|
2019-11-16 14:07:59 +00:00
|
|
|
max_length=63, null=True, blank=True, help_text=_("SFTP password.")
|
2018-07-12 15:33:26 +00:00
|
|
|
)
|
2018-07-10 22:16:35 +00:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def provisioned_switchs(self):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Get the list of provisioned switches."""
|
2018-07-10 22:16:35 +00:00
|
|
|
from topologie.models import Switch
|
2019-11-04 16:55:03 +00:00
|
|
|
|
|
|
|
return Switch.objects.filter(automatic_provision=True).order_by(
|
|
|
|
"interface__domain__name"
|
|
|
|
)
|
2017-09-10 16:16:59 +00:00
|
|
|
|
2018-10-14 22:44:37 +00:00
|
|
|
@cached_property
|
2018-07-11 17:46:13 +00:00
|
|
|
def switchs_management_interface(self):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Get the interface that the switch has to contact to get its
|
|
|
|
configuration.
|
|
|
|
"""
|
2018-07-11 17:46:13 +00:00
|
|
|
if self.switchs_ip_type:
|
|
|
|
from machines.models import Role, Interface
|
2019-11-04 16:55:03 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
Interface.objects.filter(
|
|
|
|
machine__interface__in=Role.interface_for_roletype(
|
|
|
|
"switch-conf-server"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.filter(machine_type__ip_type=self.switchs_ip_type)
|
|
|
|
.first()
|
|
|
|
)
|
2018-07-11 17:46:13 +00:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2018-10-14 22:44:37 +00:00
|
|
|
@cached_property
|
2018-07-11 17:46:13 +00:00
|
|
|
def switchs_management_interface_ip(self):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Get the IPv4 address of the interface that the switch has to contact
|
|
|
|
to get its configuration.
|
|
|
|
"""
|
2018-07-11 17:46:13 +00:00
|
|
|
if not self.switchs_management_interface:
|
|
|
|
return None
|
|
|
|
return self.switchs_management_interface.ipv4
|
|
|
|
|
2018-07-12 15:33:26 +00:00
|
|
|
@cached_property
|
|
|
|
def switchs_management_sftp_creds(self):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Get the switch credentials for SFTP provisioning."""
|
2018-07-12 15:33:26 +00:00
|
|
|
if self.sftp_login and self.sftp_pass:
|
2019-11-04 16:55:03 +00:00
|
|
|
return {"login": self.sftp_login, "pass": self.sftp_pass}
|
2018-07-12 15:33:26 +00:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def switchs_management_utils(self):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Get the dictionary of IP addresses for the configuration of
|
|
|
|
switches.
|
|
|
|
"""
|
2018-07-12 15:33:26 +00:00
|
|
|
from machines.models import Role, Ipv6List, Interface
|
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
def return_ips_dict(interfaces):
|
|
|
|
return {
|
|
|
|
"ipv4": [str(interface.ipv4) for interface in interfaces],
|
|
|
|
"ipv6": Ipv6List.objects.filter(interface__in=interfaces).values_list(
|
|
|
|
"ipv6", flat=True
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
ntp_servers = Role.all_interfaces_for_roletype("ntp-server").filter(
|
|
|
|
machine_type__ip_type=self.switchs_ip_type
|
|
|
|
)
|
|
|
|
log_servers = Role.all_interfaces_for_roletype("log-server").filter(
|
|
|
|
machine_type__ip_type=self.switchs_ip_type
|
|
|
|
)
|
|
|
|
radius_servers = Role.all_interfaces_for_roletype("radius-server").filter(
|
|
|
|
machine_type__ip_type=self.switchs_ip_type
|
|
|
|
)
|
2018-07-12 15:33:26 +00:00
|
|
|
dhcp_servers = Role.all_interfaces_for_roletype("dhcp-server")
|
2019-11-04 16:55:03 +00:00
|
|
|
dns_recursive_servers = Role.all_interfaces_for_roletype(
|
|
|
|
"dns-recursive-server"
|
|
|
|
).filter(machine_type__ip_type=self.switchs_ip_type)
|
2018-07-12 15:33:26 +00:00
|
|
|
subnet = None
|
|
|
|
subnet6 = None
|
|
|
|
if self.switchs_ip_type:
|
2019-11-04 16:55:03 +00:00
|
|
|
subnet = (
|
|
|
|
self.switchs_ip_type.ip_net_full_info
|
|
|
|
or self.switchs_ip_type.ip_set_full_info[0]
|
|
|
|
)
|
2018-07-12 15:33:26 +00:00
|
|
|
subnet6 = self.switchs_ip_type.ip6_set_full_info
|
2019-11-04 16:55:03 +00:00
|
|
|
return {
|
|
|
|
"ntp_servers": return_ips_dict(ntp_servers),
|
|
|
|
"log_servers": return_ips_dict(log_servers),
|
|
|
|
"radius_servers": return_ips_dict(radius_servers),
|
|
|
|
"dhcp_servers": return_ips_dict(dhcp_servers),
|
|
|
|
"dns_recursive_servers": return_ips_dict(dns_recursive_servers),
|
|
|
|
"subnet": subnet,
|
|
|
|
"subnet6": subnet6,
|
|
|
|
}
|
2018-07-12 15:33:26 +00:00
|
|
|
|
2018-07-10 23:37:08 +00:00
|
|
|
@cached_property
|
|
|
|
def provision_switchs_enabled(self):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Check if all automatic provisioning settings are OK."""
|
2019-11-04 16:55:03 +00:00
|
|
|
return bool(
|
|
|
|
self.provisioned_switchs
|
|
|
|
and self.switchs_ip_type
|
|
|
|
and SwitchManagementCred.objects.filter(default_switch=True).exists()
|
|
|
|
and self.switchs_management_interface_ip
|
|
|
|
and bool(
|
|
|
|
self.switchs_provision != "sftp" or self.switchs_management_sftp_creds
|
|
|
|
)
|
2017-12-31 20:43:59 +00:00
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
|
|
|
|
class Meta:
|
2020-04-22 17:35:45 +00:00
|
|
|
permissions = (
|
|
|
|
("view_optionaltopologie", _("Can view the topology preferences")),
|
|
|
|
)
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name = _("topology preferences")
|
2017-12-31 20:43:59 +00:00
|
|
|
|
2017-06-25 22:50:55 +00:00
|
|
|
|
2018-01-31 03:10:10 +00:00
|
|
|
@receiver(post_save, sender=OptionalTopologie)
|
2018-04-15 01:00:05 +00:00
|
|
|
def optionaltopologie_post_save(**kwargs):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Write in the cache."""
|
2019-11-04 16:55:03 +00:00
|
|
|
topologie_pref = kwargs["instance"]
|
2018-01-31 03:10:10 +00:00
|
|
|
topologie_pref.set_in_cache()
|
|
|
|
|
|
|
|
|
2018-07-10 23:07:31 +00:00
|
|
|
class RadiusKey(AclMixin, models.Model):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Class of a RADIUS key.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
radius_key: the encrypted RADIUS key.
|
|
|
|
comment: a comment related to the key.
|
|
|
|
default_switch: bool, True if the key is to be used by default on
|
|
|
|
switches and False otherwise.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2019-11-16 14:07:59 +00:00
|
|
|
radius_key = AESEncryptedField(max_length=255, help_text=_("RADIUS key."))
|
2018-07-10 23:07:31 +00:00
|
|
|
comment = models.CharField(
|
2019-11-16 14:07:59 +00:00
|
|
|
max_length=255, null=True, blank=True, help_text=_("Comment for this key.")
|
2018-07-10 23:07:31 +00:00
|
|
|
)
|
|
|
|
default_switch = models.BooleanField(
|
2019-11-16 14:07:59 +00:00
|
|
|
default=False, help_text=_("Default key for switches.")
|
2018-07-10 23:07:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
2019-11-04 16:55:03 +00:00
|
|
|
permissions = (("view_radiuskey", _("Can view a RADIUS key object")),)
|
2019-01-08 23:39:47 +00:00
|
|
|
verbose_name = _("RADIUS key")
|
|
|
|
verbose_name_plural = _("RADIUS keys")
|
2018-07-10 23:07:31 +00:00
|
|
|
|
2019-10-10 13:43:01 +00:00
|
|
|
def clean(self):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Check if there is a unique default RADIUS key."""
|
2019-10-10 13:43:01 +00:00
|
|
|
if RadiusKey.objects.filter(default_switch=True).count() > 1:
|
2019-11-16 14:07:59 +00:00
|
|
|
raise ValidationError(_("Default RADIUS key for switches already exists."))
|
2019-10-10 13:43:01 +00:00
|
|
|
|
2018-07-10 23:40:06 +00:00
|
|
|
def __str__(self):
|
2019-01-08 23:39:47 +00:00
|
|
|
return _("RADIUS key ") + str(self.id) + " " + str(self.comment)
|
2018-07-10 23:40:06 +00:00
|
|
|
|
2018-07-10 23:07:31 +00:00
|
|
|
|
2018-07-11 00:19:29 +00:00
|
|
|
class SwitchManagementCred(AclMixin, models.Model):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Class of a switch management credentials, for rest management.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
management_id: the login used to connect to switches.
|
|
|
|
management_pass: the encrypted password used to connect to switches.
|
|
|
|
default_switch: bool, True if the credentials are to be used by default
|
|
|
|
on switches and False otherwise.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2019-11-16 14:07:59 +00:00
|
|
|
management_id = models.CharField(max_length=63, help_text=_("Switch login."))
|
|
|
|
management_pass = AESEncryptedField(max_length=63, help_text=_("Password."))
|
2018-07-11 00:19:29 +00:00
|
|
|
default_switch = models.BooleanField(
|
2019-11-16 14:07:59 +00:00
|
|
|
default=True, unique=True, help_text=_("Default credentials for switches.")
|
2018-07-11 00:19:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
permissions = (
|
2019-11-04 16:55:03 +00:00
|
|
|
(
|
|
|
|
"view_switchmanagementcred",
|
2019-11-16 14:07:59 +00:00
|
|
|
_("Can view a switch management credentials object"),
|
2019-11-04 16:55:03 +00:00
|
|
|
),
|
2018-07-11 00:19:29 +00:00
|
|
|
)
|
2019-01-08 23:39:47 +00:00
|
|
|
verbose_name = _("switch management credentials")
|
2018-07-11 00:19:29 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2019-01-08 23:39:47 +00:00
|
|
|
return _("Switch login ") + str(self.management_id)
|
2018-07-11 00:19:29 +00:00
|
|
|
|
|
|
|
|
2018-07-10 23:07:31 +00:00
|
|
|
class Reminder(AclMixin, models.Model):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Reminder of membership's end preferences: email messages, number of days
|
|
|
|
before sending emails.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
days: the number of days before the membership's end to send the
|
|
|
|
reminder.
|
|
|
|
message: the content of the reminder.
|
2018-07-10 23:07:31 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
days = models.IntegerField(
|
|
|
|
default=7,
|
|
|
|
unique=True,
|
2019-11-16 14:07:59 +00:00
|
|
|
help_text=_("Delay between the email and the membership's end."),
|
2018-07-10 23:07:31 +00:00
|
|
|
)
|
2019-07-12 16:27:12 +00:00
|
|
|
message = models.TextField(
|
2018-07-10 23:07:31 +00:00
|
|
|
default="",
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
2019-11-16 14:07:59 +00:00
|
|
|
help_text=_("Message displayed specifically for this reminder."),
|
2018-07-10 23:07:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
2019-11-04 16:55:03 +00:00
|
|
|
permissions = (("view_reminder", _("Can view a reminder object")),)
|
2019-01-08 23:39:47 +00:00
|
|
|
verbose_name = _("reminder")
|
|
|
|
verbose_name_plural = _("reminders")
|
2018-07-10 23:07:31 +00:00
|
|
|
|
|
|
|
def users_to_remind(self):
|
|
|
|
from re2o.utils import all_has_access
|
2019-11-04 16:55:03 +00:00
|
|
|
|
|
|
|
date = timezone.now().replace(minute=0, hour=0)
|
2018-07-10 23:07:31 +00:00
|
|
|
futur_date = date + timedelta(days=self.days)
|
2019-11-04 16:55:03 +00:00
|
|
|
users = all_has_access(futur_date).exclude(
|
|
|
|
pk__in=all_has_access(futur_date + timedelta(days=1))
|
|
|
|
)
|
2018-07-10 23:07:31 +00:00
|
|
|
return users
|
|
|
|
|
|
|
|
|
2018-03-28 15:39:23 +00:00
|
|
|
class GeneralOption(AclMixin, PreferencesModel):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""General preferences: number of search results per page, website name
|
|
|
|
etc.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
general_message_fr: general message displayed on the French version of
|
|
|
|
the website (e.g. in case of maintenance).
|
|
|
|
general_message_en: general message displayed on the English version of
|
|
|
|
the website (e.g. in case of maintenance).
|
|
|
|
search_display_page: number of results displayed (in each category)
|
|
|
|
when searching.
|
|
|
|
pagination_number: number of items per page (standard size).
|
|
|
|
pagination_large_number: number of items per page (large size).
|
|
|
|
req_expire_hrs: number of hours before expiration of the reset password
|
|
|
|
link.
|
|
|
|
site_name: website name.
|
|
|
|
email_from: email address for automatic emailing.
|
|
|
|
main_site_url: main site URL.
|
|
|
|
GTU_sum_up: summary of the General Terms of Use.
|
|
|
|
GTU: file, General Terms of Use.
|
|
|
|
"""
|
2017-08-24 19:36:59 +00:00
|
|
|
|
2018-08-11 13:24:58 +00:00
|
|
|
general_message_fr = models.TextField(
|
2017-12-16 03:01:39 +00:00
|
|
|
default="",
|
|
|
|
blank=True,
|
2019-11-04 16:55:03 +00:00
|
|
|
help_text=_(
|
|
|
|
"General message displayed on the French version of the"
|
2019-11-16 14:07:59 +00:00
|
|
|
" website (e.g. in case of maintenance)."
|
2019-11-04 16:55:03 +00:00
|
|
|
),
|
2018-08-11 13:24:58 +00:00
|
|
|
)
|
|
|
|
general_message_en = models.TextField(
|
|
|
|
default="",
|
|
|
|
blank=True,
|
2019-11-04 16:55:03 +00:00
|
|
|
help_text=_(
|
|
|
|
"General message displayed on the English version of the"
|
2019-11-16 14:07:59 +00:00
|
|
|
" website (e.g. in case of maintenance)."
|
2019-11-04 16:55:03 +00:00
|
|
|
),
|
2017-12-16 03:01:39 +00:00
|
|
|
)
|
2017-06-25 22:50:55 +00:00
|
|
|
search_display_page = models.IntegerField(default=15)
|
|
|
|
pagination_number = models.IntegerField(default=25)
|
|
|
|
pagination_large_number = models.IntegerField(default=8)
|
2017-08-26 14:04:51 +00:00
|
|
|
req_expire_hrs = models.IntegerField(default=48)
|
|
|
|
site_name = models.CharField(max_length=32, default="Re2o")
|
2018-03-21 21:58:31 +00:00
|
|
|
email_from = models.EmailField(default="www-data@example.com")
|
2018-11-15 13:35:26 +00:00
|
|
|
main_site_url = models.URLField(max_length=255, default="http://re2o.example.org")
|
2019-11-04 16:55:03 +00:00
|
|
|
GTU_sum_up = models.TextField(default="", blank=True)
|
|
|
|
GTU = models.FileField(upload_to="", default="", null=True, blank=True)
|
2017-06-25 22:50:55 +00:00
|
|
|
|
2017-12-31 20:43:59 +00:00
|
|
|
class Meta:
|
2019-11-16 14:07:59 +00:00
|
|
|
permissions = (("view_generaloption", _("Can view the general preferences")),)
|
|
|
|
verbose_name = _("general preferences")
|
2017-12-31 20:43:59 +00:00
|
|
|
|
2017-10-14 04:03:53 +00:00
|
|
|
|
2018-01-31 03:10:10 +00:00
|
|
|
@receiver(post_save, sender=GeneralOption)
|
2018-04-15 01:00:05 +00:00
|
|
|
def generaloption_post_save(**kwargs):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Write in the cache."""
|
2019-11-04 16:55:03 +00:00
|
|
|
general_pref = kwargs["instance"]
|
2018-01-31 03:10:10 +00:00
|
|
|
general_pref.set_in_cache()
|
|
|
|
|
|
|
|
|
2018-03-28 15:39:23 +00:00
|
|
|
class Service(AclMixin, models.Model):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Service displayed on the home page.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
name: the name of the service.
|
|
|
|
url: the URL of the service.
|
|
|
|
description: the description of the service.
|
|
|
|
image: an image to illustrate the service (e.g. logo).
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2017-08-24 19:36:59 +00:00
|
|
|
name = models.CharField(max_length=32)
|
|
|
|
url = models.URLField()
|
|
|
|
description = models.TextField()
|
2019-11-04 16:55:03 +00:00
|
|
|
image = models.ImageField(upload_to="logo", blank=True)
|
2017-08-24 19:36:59 +00:00
|
|
|
|
2017-12-31 20:43:59 +00:00
|
|
|
class Meta:
|
2019-11-16 14:07:59 +00:00
|
|
|
permissions = (("view_service", _("Can view the service preferences")),)
|
2018-08-05 16:48:35 +00:00
|
|
|
verbose_name = _("service")
|
2019-11-04 16:55:03 +00:00
|
|
|
verbose_name_plural = _("services")
|
2017-12-31 20:43:59 +00:00
|
|
|
|
2017-08-25 02:35:49 +00:00
|
|
|
def __str__(self):
|
|
|
|
return str(self.name)
|
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2018-06-30 17:19:40 +00:00
|
|
|
class MailContact(AclMixin, models.Model):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Contact email address with a comment.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
address: the contact email address.
|
|
|
|
commentary: a comment used to describe the contact email address.
|
|
|
|
"""
|
2018-06-30 17:19:40 +00:00
|
|
|
|
|
|
|
address = models.EmailField(
|
2019-11-16 14:07:59 +00:00
|
|
|
default="contact@example.org", help_text=_("Contact email address.")
|
2018-06-30 17:19:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
commentary = models.CharField(
|
2019-11-04 16:55:03 +00:00
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
help_text=_("Description of the associated email address."),
|
|
|
|
max_length=256,
|
2018-06-30 17:19:40 +00:00
|
|
|
)
|
|
|
|
|
2018-07-01 08:49:47 +00:00
|
|
|
@cached_property
|
|
|
|
def get_name(self):
|
|
|
|
return self.address.split("@")[0]
|
|
|
|
|
2018-06-30 17:19:40 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
2018-08-05 16:48:35 +00:00
|
|
|
("view_mailcontact", _("Can view a contact email address object")),
|
2018-06-30 17:19:40 +00:00
|
|
|
)
|
2018-08-05 16:48:35 +00:00
|
|
|
verbose_name = _("contact email address")
|
|
|
|
verbose_name_plural = _("contact email addresses")
|
2018-06-30 17:19:40 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2019-11-04 16:55:03 +00:00
|
|
|
return self.address
|
|
|
|
|
2018-06-30 17:19:40 +00:00
|
|
|
|
2019-09-21 21:10:28 +00:00
|
|
|
class Mandate(RevMixin, AclMixin, models.Model):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Mandate, documenting who was the president of the organisation at a
|
|
|
|
given time.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
president: User, the president during the mandate.
|
|
|
|
start_date: datetime, the date when the mandate started.
|
|
|
|
end_date: datetime, the date when the mandate ended.
|
|
|
|
"""
|
|
|
|
|
2019-09-21 21:10:28 +00:00
|
|
|
class Meta:
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name = _("mandate")
|
|
|
|
verbose_name_plural = _("mandates")
|
|
|
|
permissions = (("view_mandate", _("Can view a mandate object")),)
|
2019-09-21 21:10:28 +00:00
|
|
|
|
|
|
|
president = models.ForeignKey(
|
2019-11-04 16:55:03 +00:00
|
|
|
"users.User",
|
2019-09-21 21:10:28 +00:00
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("president of the association"),
|
|
|
|
help_text=_("Displayed on subscription vouchers."),
|
2019-09-21 21:10:28 +00:00
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
start_date = models.DateTimeField(verbose_name=_("start date"))
|
|
|
|
end_date = models.DateTimeField(verbose_name=_("end date"), blank=True, null=True)
|
2019-09-21 21:10:28 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_mandate(cls, date=timezone.now):
|
2020-04-29 12:23:12 +00:00
|
|
|
""""Get the mandate taking place at the given date.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
date: the date used to find the mandate (default: timezone.now).
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The mandate related to the given date.
|
|
|
|
"""
|
2019-09-21 21:10:28 +00:00
|
|
|
if callable(date):
|
|
|
|
date = date()
|
2019-11-04 16:55:03 +00:00
|
|
|
mandate = (
|
|
|
|
cls.objects.exclude(end_date__lte=date).order_by("start_date").first()
|
|
|
|
or cls.objects.order_by("start_date").last()
|
|
|
|
)
|
2019-09-23 22:40:22 +00:00
|
|
|
if not mandate:
|
2019-11-04 16:55:03 +00:00
|
|
|
raise cls.DoesNotExist(
|
2020-04-22 17:35:45 +00:00
|
|
|
_(
|
|
|
|
"No mandates have been created. Please go to the preferences page to create one."
|
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
)
|
2019-09-23 22:40:22 +00:00
|
|
|
return mandate
|
2019-09-23 20:49:59 +00:00
|
|
|
|
2019-09-21 21:10:28 +00:00
|
|
|
def is_over(self):
|
|
|
|
return self.end_date is None
|
2019-10-02 20:48:33 +00:00
|
|
|
|
2019-09-23 22:40:22 +00:00
|
|
|
def __str__(self):
|
2019-11-04 16:55:03 +00:00
|
|
|
return str(self.president) + " " + str(self.start_date.year)
|
2019-09-23 22:40:22 +00:00
|
|
|
|
2017-10-14 04:03:53 +00:00
|
|
|
|
2018-03-28 15:39:23 +00:00
|
|
|
class AssoOption(AclMixin, PreferencesModel):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Information about the organisation: name, address, SIRET number etc.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
name: the name of the organisation.
|
|
|
|
siret: the SIRET number of the organisation.
|
|
|
|
adresse1: the first line of the organisation's address, e.g. street and
|
|
|
|
number.
|
|
|
|
adresse2: the second line of the organisation's address, e.g. city and
|
|
|
|
postal code.
|
|
|
|
contact: contact email address.
|
|
|
|
telephone: contact telephone number.
|
|
|
|
pseudo: short name of the organisation.
|
|
|
|
utilisateur_asso: the user used to manage the organisation.
|
|
|
|
description: the description of the organisation.
|
|
|
|
"""
|
2017-08-24 19:36:59 +00:00
|
|
|
|
2017-10-14 04:03:53 +00:00
|
|
|
name = models.CharField(
|
2019-11-04 16:55:03 +00:00
|
|
|
default=_("Networking organisation school Something"), max_length=256
|
2017-10-14 04:03:53 +00:00
|
|
|
)
|
2017-08-24 19:36:59 +00:00
|
|
|
siret = models.CharField(default="00000000000000", max_length=32)
|
2018-08-05 16:48:35 +00:00
|
|
|
adresse1 = models.CharField(default=_("Threadneedle Street"), max_length=128)
|
|
|
|
adresse2 = models.CharField(default=_("London EC2R 8AH"), max_length=128)
|
2017-08-24 19:36:59 +00:00
|
|
|
contact = models.EmailField(default="contact@example.org")
|
|
|
|
telephone = models.CharField(max_length=15, default="0000000000")
|
2018-08-05 16:48:35 +00:00
|
|
|
pseudo = models.CharField(default=_("Organisation"), max_length=32)
|
2017-10-14 04:03:53 +00:00
|
|
|
utilisateur_asso = models.OneToOneField(
|
2019-11-04 16:55:03 +00:00
|
|
|
"users.User", on_delete=models.PROTECT, blank=True, null=True
|
2018-03-18 01:14:45 +00:00
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
description = models.TextField(null=True, blank=True)
|
2018-01-15 00:06:54 +00:00
|
|
|
|
2017-12-31 20:43:59 +00:00
|
|
|
class Meta:
|
2019-11-16 14:07:59 +00:00
|
|
|
permissions = (("view_assooption", _("Can view the organisation preferences")),)
|
|
|
|
verbose_name = _("organisation preferences")
|
2017-12-31 20:43:59 +00:00
|
|
|
|
2017-09-08 20:53:13 +00:00
|
|
|
|
2018-01-31 03:10:10 +00:00
|
|
|
@receiver(post_save, sender=AssoOption)
|
2018-04-15 01:00:05 +00:00
|
|
|
def assooption_post_save(**kwargs):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Write in the cache."""
|
2019-11-04 16:55:03 +00:00
|
|
|
asso_pref = kwargs["instance"]
|
2018-01-31 03:10:10 +00:00
|
|
|
asso_pref.set_in_cache()
|
|
|
|
|
|
|
|
|
2018-04-16 16:22:51 +00:00
|
|
|
class HomeOption(AclMixin, PreferencesModel):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Social networks displayed on the home page (supports only Facebook and
|
|
|
|
Twitter).
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
facebook_url: URL of the Facebook account.
|
|
|
|
twitter_url: URL of the Twitter account.
|
|
|
|
twitter_account_name: name of the Twitter account.
|
|
|
|
"""
|
2018-04-16 03:28:27 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
facebook_url = models.URLField(null=True, blank=True)
|
|
|
|
twitter_url = models.URLField(null=True, blank=True)
|
|
|
|
twitter_account_name = models.CharField(max_length=32, null=True, blank=True)
|
2018-04-16 03:28:27 +00:00
|
|
|
|
|
|
|
class Meta:
|
2019-11-16 14:07:59 +00:00
|
|
|
permissions = (("view_homeoption", _("Can view the homepage preferences")),)
|
|
|
|
verbose_name = _("homepage preferences")
|
2018-04-16 03:28:27 +00:00
|
|
|
|
|
|
|
|
2018-04-16 16:22:51 +00:00
|
|
|
@receiver(post_save, sender=HomeOption)
|
|
|
|
def homeoption_post_save(**kwargs):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Write in the cache."""
|
2019-11-04 16:55:03 +00:00
|
|
|
home_pref = kwargs["instance"]
|
2018-04-16 16:22:51 +00:00
|
|
|
home_pref.set_in_cache()
|
2018-04-16 03:28:27 +00:00
|
|
|
|
|
|
|
|
2018-03-28 15:39:23 +00:00
|
|
|
class MailMessageOption(AclMixin, models.Model):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Welcome email messages preferences.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
welcome_mail_fr: the text of the welcome email in French.
|
|
|
|
welcome_mail_en: the text of the welcome email in English.
|
|
|
|
"""
|
2017-09-08 20:53:13 +00:00
|
|
|
|
2019-10-08 16:27:09 +00:00
|
|
|
welcome_mail_fr = models.TextField(
|
2019-11-16 14:07:59 +00:00
|
|
|
default="", blank=True, help_text=_("Welcome email in French.")
|
2019-10-08 16:27:09 +00:00
|
|
|
)
|
|
|
|
welcome_mail_en = models.TextField(
|
2019-11-16 14:07:59 +00:00
|
|
|
default="", blank=True, help_text=_("Welcome email in English.")
|
2019-10-08 16:27:09 +00:00
|
|
|
)
|
2017-12-08 23:41:16 +00:00
|
|
|
|
2017-12-31 20:43:59 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
2019-11-16 14:07:59 +00:00
|
|
|
("view_mailmessageoption", _("Can view the email message preferences")),
|
2017-12-31 20:43:59 +00:00
|
|
|
)
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name = _("email message preferences")
|
2018-08-05 16:48:35 +00:00
|
|
|
|
2018-09-02 14:53:21 +00:00
|
|
|
|
2019-09-09 15:04:30 +00:00
|
|
|
class RadiusAttribute(RevMixin, AclMixin, models.Model):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""RADIUS attributes preferences.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
attribute: the name of the RADIUS attribute.
|
|
|
|
value: the value of the RADIUS attribute.
|
|
|
|
comment: the comment to document the attribute.
|
|
|
|
"""
|
|
|
|
|
2019-09-09 15:04:30 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("RADIUS attribute")
|
|
|
|
verbose_name_plural = _("RADIUS attributes")
|
|
|
|
|
|
|
|
attribute = models.CharField(
|
|
|
|
max_length=255,
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("attribute"),
|
|
|
|
help_text=_("See https://freeradius.org/rfc/attributes.html."),
|
2019-09-09 15:04:30 +00:00
|
|
|
)
|
2019-11-16 14:07:59 +00:00
|
|
|
value = models.CharField(max_length=255, verbose_name=_("value"))
|
2019-09-09 15:04:30 +00:00
|
|
|
comment = models.TextField(
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("comment"),
|
2019-09-09 15:04:30 +00:00
|
|
|
help_text=_("Use this field to document this attribute."),
|
|
|
|
blank=True,
|
2019-11-04 16:55:03 +00:00
|
|
|
default="",
|
2019-09-09 15:04:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self):
|
2019-11-04 16:55:03 +00:00
|
|
|
return " ".join([self.attribute, "=", self.value])
|
2019-09-09 15:59:43 +00:00
|
|
|
|
2019-09-09 15:04:30 +00:00
|
|
|
|
2018-12-05 11:28:07 +00:00
|
|
|
class RadiusOption(AclMixin, PreferencesModel):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""RADIUS preferences.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
radius_general_policy: the general RADIUS policy (MACHINE or DEFINED).
|
|
|
|
unknown_machine: the RADIUS policy for unknown machines.
|
|
|
|
unknown_machine_vlan: the VLAN for unknown machines if not rejected.
|
|
|
|
unknown_machine_attributes: the answer attributes for unknown machines.
|
|
|
|
unknown_port: the RADIUS policy for unknown ports.
|
|
|
|
unknown_port_vlan: the VLAN for unknown ports if not rejected;
|
|
|
|
unknown_port_attributes: the answer attributes for unknown ports.
|
|
|
|
unknown_room: the RADIUS policy for machines connecting from
|
|
|
|
unregistered rooms (relevant for ports with STRICT RADIUS mode).
|
|
|
|
unknown_room_vlan: the VLAN for unknown rooms if not rejected.
|
|
|
|
unknown_room_attributes: the answer attributes for unknown rooms.
|
|
|
|
non_member: the RADIUS policy for non members.
|
|
|
|
non_member_vlan: the VLAN for non members if not rejected.
|
|
|
|
non_member_attributes: the answer attributes for non members.
|
|
|
|
banned: the RADIUS policy for banned users.
|
|
|
|
banned_vlan: the VLAN for banned users if not rejected.
|
|
|
|
banned_attributes: the answer attributes for banned users.
|
|
|
|
vlan_decision_ok: the VLAN for accepted machines.
|
|
|
|
ok_attributes: the answer attributes for accepted machines.
|
|
|
|
"""
|
|
|
|
|
2018-09-02 14:53:21 +00:00
|
|
|
class Meta:
|
2019-01-08 23:39:47 +00:00
|
|
|
verbose_name = _("RADIUS policy")
|
|
|
|
verbose_name_plural = _("RADIUS policies")
|
2018-09-02 14:53:21 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
MACHINE = "MACHINE"
|
|
|
|
DEFINED = "DEFINED"
|
2018-09-02 14:53:21 +00:00
|
|
|
CHOICE_RADIUS = (
|
|
|
|
(MACHINE, _("On the IP range's VLAN of the machine")),
|
2020-04-22 17:35:45 +00:00
|
|
|
(DEFINED, _('Preset in "VLAN for machines accepted by RADIUS"')),
|
2018-09-02 14:53:21 +00:00
|
|
|
)
|
2019-11-04 16:55:03 +00:00
|
|
|
REJECT = "REJECT"
|
|
|
|
SET_VLAN = "SET_VLAN"
|
2018-12-02 16:03:27 +00:00
|
|
|
CHOICE_POLICY = (
|
2019-01-08 23:39:47 +00:00
|
|
|
(REJECT, _("Reject the machine")),
|
2019-11-04 16:55:03 +00:00
|
|
|
(SET_VLAN, _("Place the machine on the VLAN")),
|
2018-12-02 16:03:27 +00:00
|
|
|
)
|
2018-09-02 14:53:21 +00:00
|
|
|
radius_general_policy = models.CharField(
|
2019-11-04 16:55:03 +00:00
|
|
|
max_length=32, choices=CHOICE_RADIUS, default="DEFINED"
|
2018-09-02 14:53:21 +00:00
|
|
|
)
|
2018-12-02 16:03:27 +00:00
|
|
|
unknown_machine = models.CharField(
|
|
|
|
max_length=32,
|
|
|
|
choices=CHOICE_POLICY,
|
|
|
|
default=REJECT,
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("policy for unknown machines"),
|
2018-09-02 14:53:21 +00:00
|
|
|
)
|
2018-12-02 16:03:27 +00:00
|
|
|
unknown_machine_vlan = models.ForeignKey(
|
2019-11-04 16:55:03 +00:00
|
|
|
"machines.Vlan",
|
2018-09-02 14:53:21 +00:00
|
|
|
on_delete=models.PROTECT,
|
2019-11-04 16:55:03 +00:00
|
|
|
related_name="unknown_machine_vlan",
|
2018-12-02 16:03:27 +00:00
|
|
|
blank=True,
|
|
|
|
null=True,
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("unknown machines VLAN"),
|
|
|
|
help_text=_("VLAN for unknown machines if not rejected."),
|
2018-12-02 16:03:27 +00:00
|
|
|
)
|
2019-09-09 15:04:30 +00:00
|
|
|
unknown_machine_attributes = models.ManyToManyField(
|
|
|
|
RadiusAttribute,
|
2019-11-04 16:55:03 +00:00
|
|
|
related_name="unknown_machine_attribute",
|
2019-09-09 15:04:30 +00:00
|
|
|
blank=True,
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("unknown machines attributes"),
|
2019-09-09 15:04:30 +00:00
|
|
|
help_text=_("Answer attributes for unknown machines."),
|
|
|
|
)
|
2018-12-02 16:03:27 +00:00
|
|
|
unknown_port = models.CharField(
|
|
|
|
max_length=32,
|
|
|
|
choices=CHOICE_POLICY,
|
|
|
|
default=REJECT,
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("policy for unknown ports"),
|
2018-09-02 14:53:21 +00:00
|
|
|
)
|
2018-12-02 16:03:27 +00:00
|
|
|
unknown_port_vlan = models.ForeignKey(
|
2019-11-04 16:55:03 +00:00
|
|
|
"machines.Vlan",
|
2018-09-02 14:53:21 +00:00
|
|
|
on_delete=models.PROTECT,
|
2019-11-04 16:55:03 +00:00
|
|
|
related_name="unknown_port_vlan",
|
2018-12-02 16:03:27 +00:00
|
|
|
blank=True,
|
|
|
|
null=True,
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("unknown ports VLAN"),
|
|
|
|
help_text=_("VLAN for unknown ports if not rejected."),
|
2018-12-02 16:03:27 +00:00
|
|
|
)
|
2019-09-09 15:04:30 +00:00
|
|
|
unknown_port_attributes = models.ManyToManyField(
|
|
|
|
RadiusAttribute,
|
2019-11-04 16:55:03 +00:00
|
|
|
related_name="unknown_port_attribute",
|
2019-09-09 15:04:30 +00:00
|
|
|
blank=True,
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("unknown ports attributes"),
|
2019-09-09 15:04:30 +00:00
|
|
|
help_text=_("Answer attributes for unknown ports."),
|
|
|
|
)
|
2018-12-02 16:03:27 +00:00
|
|
|
unknown_room = models.CharField(
|
|
|
|
max_length=32,
|
|
|
|
choices=CHOICE_POLICY,
|
|
|
|
default=REJECT,
|
2019-11-04 16:55:03 +00:00
|
|
|
verbose_name=_(
|
|
|
|
"Policy for machines connecting from unregistered rooms"
|
|
|
|
" (relevant on ports with STRICT RADIUS mode)"
|
|
|
|
),
|
2018-09-02 14:53:21 +00:00
|
|
|
)
|
2018-12-02 16:03:27 +00:00
|
|
|
unknown_room_vlan = models.ForeignKey(
|
2019-11-04 16:55:03 +00:00
|
|
|
"machines.Vlan",
|
|
|
|
related_name="unknown_room_vlan",
|
2018-09-02 14:53:21 +00:00
|
|
|
on_delete=models.PROTECT,
|
2018-12-02 16:03:27 +00:00
|
|
|
blank=True,
|
|
|
|
null=True,
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("unknown rooms VLAN"),
|
|
|
|
help_text=_("VLAN for unknown rooms if not rejected."),
|
2018-12-02 16:03:27 +00:00
|
|
|
)
|
2019-09-09 15:04:30 +00:00
|
|
|
unknown_room_attributes = models.ManyToManyField(
|
|
|
|
RadiusAttribute,
|
2019-11-04 16:55:03 +00:00
|
|
|
related_name="unknown_room_attribute",
|
2019-09-09 15:04:30 +00:00
|
|
|
blank=True,
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("unknown rooms attributes"),
|
2019-09-09 15:04:30 +00:00
|
|
|
help_text=_("Answer attributes for unknown rooms."),
|
|
|
|
)
|
2018-12-02 16:03:27 +00:00
|
|
|
non_member = models.CharField(
|
|
|
|
max_length=32,
|
|
|
|
choices=CHOICE_POLICY,
|
|
|
|
default=REJECT,
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("policy for non members"),
|
2018-09-02 14:53:21 +00:00
|
|
|
)
|
2018-12-02 16:03:27 +00:00
|
|
|
non_member_vlan = models.ForeignKey(
|
2019-11-04 16:55:03 +00:00
|
|
|
"machines.Vlan",
|
|
|
|
related_name="non_member_vlan",
|
2018-09-02 14:53:21 +00:00
|
|
|
on_delete=models.PROTECT,
|
2018-12-02 16:03:27 +00:00
|
|
|
blank=True,
|
|
|
|
null=True,
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("non members VLAN"),
|
|
|
|
help_text=_("VLAN for non members if not rejected."),
|
2018-12-02 16:03:27 +00:00
|
|
|
)
|
2019-09-09 15:04:30 +00:00
|
|
|
non_member_attributes = models.ManyToManyField(
|
|
|
|
RadiusAttribute,
|
2019-11-04 16:55:03 +00:00
|
|
|
related_name="non_member_attribute",
|
2019-09-09 15:04:30 +00:00
|
|
|
blank=True,
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("non members attributes"),
|
2019-09-09 15:04:30 +00:00
|
|
|
help_text=_("Answer attributes for non members."),
|
|
|
|
)
|
2018-12-02 16:03:27 +00:00
|
|
|
banned = models.CharField(
|
|
|
|
max_length=32,
|
|
|
|
choices=CHOICE_POLICY,
|
|
|
|
default=REJECT,
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("policy for banned users"),
|
2018-12-02 16:03:27 +00:00
|
|
|
)
|
|
|
|
banned_vlan = models.ForeignKey(
|
2019-11-04 16:55:03 +00:00
|
|
|
"machines.Vlan",
|
|
|
|
related_name="banned_vlan",
|
2018-12-02 16:03:27 +00:00
|
|
|
on_delete=models.PROTECT,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("banned users VLAN"),
|
|
|
|
help_text=_("VLAN for banned users if not rejected."),
|
2018-09-02 14:53:21 +00:00
|
|
|
)
|
2019-09-09 15:04:30 +00:00
|
|
|
banned_attributes = models.ManyToManyField(
|
|
|
|
RadiusAttribute,
|
2019-11-04 16:55:03 +00:00
|
|
|
related_name="banned_attribute",
|
2019-09-09 15:04:30 +00:00
|
|
|
blank=True,
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("banned users attributes"),
|
2019-09-09 15:04:30 +00:00
|
|
|
help_text=_("Answer attributes for banned users."),
|
|
|
|
)
|
2018-09-02 14:53:21 +00:00
|
|
|
vlan_decision_ok = models.OneToOneField(
|
2019-11-04 16:55:03 +00:00
|
|
|
"machines.Vlan",
|
2018-09-02 14:53:21 +00:00
|
|
|
on_delete=models.PROTECT,
|
2019-11-04 16:55:03 +00:00
|
|
|
related_name="vlan_ok_option",
|
2018-09-02 14:53:21 +00:00
|
|
|
blank=True,
|
2019-11-04 16:55:03 +00:00
|
|
|
null=True,
|
2018-09-02 14:53:21 +00:00
|
|
|
)
|
2019-09-09 15:04:30 +00:00
|
|
|
ok_attributes = models.ManyToManyField(
|
|
|
|
RadiusAttribute,
|
2019-11-04 16:55:03 +00:00
|
|
|
related_name="ok_attribute",
|
2019-09-09 15:04:30 +00:00
|
|
|
blank=True,
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("accepted users attributes"),
|
2019-09-09 15:04:30 +00:00
|
|
|
help_text=_("Answer attributes for accepted users."),
|
|
|
|
)
|
2018-09-02 14:53:21 +00:00
|
|
|
|
2019-09-09 15:59:43 +00:00
|
|
|
@classmethod
|
2019-09-10 17:11:14 +00:00
|
|
|
def get_attributes(cls, name, attribute_kwargs={}):
|
2019-09-09 15:59:43 +00:00
|
|
|
return (
|
2019-11-04 16:55:03 +00:00
|
|
|
(str(attribute.attribute), str(attribute.value % attribute_kwargs))
|
2019-09-09 15:59:43 +00:00
|
|
|
for attribute in cls.get_cached_value(name).all()
|
|
|
|
)
|
|
|
|
|
2019-01-03 21:34:45 +00:00
|
|
|
|
2019-01-20 17:53:25 +00:00
|
|
|
def default_invoice():
|
2019-01-20 23:54:02 +00:00
|
|
|
tpl, _ = DocumentTemplate.objects.get_or_create(
|
2019-11-04 16:55:03 +00:00
|
|
|
name="Re2o default invoice", template="templates/default_invoice.tex"
|
2019-01-20 17:53:25 +00:00
|
|
|
)
|
|
|
|
return tpl.id
|
|
|
|
|
|
|
|
|
|
|
|
def default_voucher():
|
2019-01-20 23:54:02 +00:00
|
|
|
tpl, _ = DocumentTemplate.objects.get_or_create(
|
2019-11-04 16:55:03 +00:00
|
|
|
name="Re2o default voucher", template="templates/default_voucher.tex"
|
2019-01-20 17:53:25 +00:00
|
|
|
)
|
|
|
|
return tpl.id
|
|
|
|
|
|
|
|
|
2019-01-03 21:34:45 +00:00
|
|
|
class CotisationsOption(AclMixin, PreferencesModel):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Subscription preferences.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
invoice_template: the template for invoices.
|
|
|
|
voucher_template: the template for vouchers.
|
|
|
|
send_voucher_mail: whether the voucher is sent by email when the
|
|
|
|
invoice is controlled.
|
|
|
|
"""
|
|
|
|
|
2019-01-03 21:34:45 +00:00
|
|
|
class Meta:
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name = _("subscription preferences")
|
2019-01-03 21:34:45 +00:00
|
|
|
|
|
|
|
invoice_template = models.OneToOneField(
|
2019-11-04 16:55:03 +00:00
|
|
|
"preferences.DocumentTemplate",
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("template for invoices"),
|
2019-01-03 21:34:45 +00:00
|
|
|
related_name="invoice_template",
|
|
|
|
on_delete=models.PROTECT,
|
2019-01-20 17:53:25 +00:00
|
|
|
default=default_invoice,
|
2019-01-03 21:34:45 +00:00
|
|
|
)
|
2019-01-05 18:45:21 +00:00
|
|
|
voucher_template = models.OneToOneField(
|
2019-11-04 16:55:03 +00:00
|
|
|
"preferences.DocumentTemplate",
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("template for subscription vouchers"),
|
2019-01-05 18:45:21 +00:00
|
|
|
related_name="voucher_template",
|
|
|
|
on_delete=models.PROTECT,
|
2019-01-20 17:53:25 +00:00
|
|
|
default=default_voucher,
|
2019-01-05 18:45:21 +00:00
|
|
|
)
|
2019-01-20 18:08:11 +00:00
|
|
|
send_voucher_mail = models.BooleanField(
|
2019-11-16 14:07:59 +00:00
|
|
|
verbose_name=_("send voucher by email when the invoice is controlled"),
|
2019-11-04 16:55:03 +00:00
|
|
|
help_text=_(
|
2019-11-16 14:07:59 +00:00
|
|
|
"Be careful, if no mandate is defined on the preferences page,"
|
|
|
|
" errors will be triggered when generating vouchers."
|
2019-11-04 16:55:03 +00:00
|
|
|
),
|
2019-01-20 18:08:11 +00:00
|
|
|
default=False,
|
|
|
|
)
|
2019-01-20 23:54:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DocumentTemplate(RevMixin, AclMixin, models.Model):
|
|
|
|
"""Represent a template in order to create documents such as invoice or
|
|
|
|
subscription voucher.
|
2020-04-29 12:23:12 +00:00
|
|
|
|
|
|
|
Attributes:
|
|
|
|
template: file, the template used to create documents.
|
|
|
|
name: the name of the template.
|
2019-01-20 23:54:02 +00:00
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
|
|
|
|
template = models.FileField(upload_to="templates/", verbose_name=_("template"))
|
|
|
|
name = models.CharField(max_length=125, verbose_name=_("name"), unique=True)
|
2019-01-20 23:54:02 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("document template")
|
|
|
|
verbose_name_plural = _("document templates")
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.name)
|
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2019-01-20 23:54:02 +00:00
|
|
|
@receiver(models.signals.post_delete, sender=DocumentTemplate)
|
|
|
|
def auto_delete_file_on_delete(sender, instance, **kwargs):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Delete the tempalte file from filesystem when the related
|
|
|
|
DocumentTemplate object is deleted.
|
2019-01-20 23:54:02 +00:00
|
|
|
"""
|
|
|
|
if instance.template:
|
|
|
|
if os.path.isfile(instance.template.path):
|
|
|
|
os.remove(instance.template.path)
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(models.signals.pre_save, sender=DocumentTemplate)
|
|
|
|
def auto_delete_file_on_change(sender, instance, **kwargs):
|
2020-04-29 12:23:12 +00:00
|
|
|
"""Delete the previous file from filesystem when the related
|
|
|
|
DocumentTemplate object is updated with new file.
|
2019-01-20 23:54:02 +00:00
|
|
|
"""
|
|
|
|
if not instance.pk:
|
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
|
|
old_file = DocumentTemplate.objects.get(pk=instance.pk).template
|
|
|
|
except DocumentTemplate.DoesNotExist:
|
|
|
|
return False
|
|
|
|
|
|
|
|
new_file = instance.template
|
|
|
|
if not old_file == new_file:
|
|
|
|
if os.path.isfile(old_file.path):
|
|
|
|
os.remove(old_file.path)
|