Commit 4c984a39 authored by chirac's avatar chirac Committed by Gabriel Detraz
Browse files

Black on files

parent 0d575366
......@@ -45,7 +45,11 @@ from django.utils.translation import ugettext_lazy as _
from django.shortcuts import get_object_or_404
from re2o.field_permissions import FieldPermissionFormMixin
from re2o.mixins import FormRevMixin, AutocompleteModelMixin, AutocompleteMultipleModelMixin
from re2o.mixins import (
FormRevMixin,
AutocompleteModelMixin,
AutocompleteMultipleModelMixin,
)
from .models import (
Article,
Paiement,
......@@ -80,12 +84,8 @@ class FactureForm(FieldPermissionFormMixin, FormRevMixin, ModelForm):
model = Facture
fields = "__all__"
widgets = {
"user": AutocompleteModelMixin(
url="/users/user-autocomplete",
),
"banque": AutocompleteModelMixin(
url="/cotisations/banque-autocomplete",
),
"user": AutocompleteModelMixin(url="/users/user-autocomplete"),
"banque": AutocompleteModelMixin(url="/cotisations/banque-autocomplete"),
}
def clean(self):
......
......@@ -47,10 +47,7 @@ CHOICES_ACTION_TYPE = (
("all", _("All")),
)
CHOICES_TYPE = (
("ip", _("IPv4")),
("mac", _("MAC address")),
)
CHOICES_TYPE = (("ip", _("IPv4")), ("mac", _("MAC address")))
def all_classes(module):
......@@ -88,14 +85,11 @@ def classes_for_action_type(action_type):
users.models.User.__name__,
users.models.Adherent.__name__,
users.models.Club.__name__,
users.models.EMailAddress.__name__
users.models.EMailAddress.__name__,
]
if action_type == "machines":
return [
machines.models.Machine.__name__,
machines.models.Interface.__name__
]
return [machines.models.Machine.__name__, machines.models.Interface.__name__]
if action_type == "subscriptions":
return all_classes(cotisations.models)
......@@ -115,6 +109,7 @@ def classes_for_action_type(action_type):
class ActionsSearchForm(Form):
"""Form used to do an advanced search through the logs."""
user = forms.ModelChoiceField(
label=_("Performed by"),
queryset=users.models.User.objects.all(),
......@@ -143,13 +138,10 @@ class ActionsSearchForm(Form):
class MachineHistorySearchForm(Form):
"""Form used to do a search through the machine histories."""
q = forms.CharField(
label=_("Search"),
max_length=100,
)
q = forms.CharField(label=_("Search"), max_length=100)
t = forms.CharField(
label=_("Search type"),
widget=forms.Select(choices=CHOICES_TYPE)
label=_("Search type"), widget=forms.Select(choices=CHOICES_TYPE)
)
s = forms.DateField(required=False, label=_("Start date"))
e = forms.DateField(required=False, label=_("End date"))
......
......@@ -40,7 +40,11 @@ from django.forms import ModelForm, Form
from django.utils.translation import ugettext_lazy as _
from re2o.field_permissions import FieldPermissionFormMixin
from re2o.mixins import FormRevMixin, AutocompleteModelMixin, AutocompleteMultipleModelMixin
from re2o.mixins import (
FormRevMixin,
AutocompleteModelMixin,
AutocompleteMultipleModelMixin,
)
from .models import (
Domain,
Machine,
......@@ -71,11 +75,7 @@ class EditMachineForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
class Meta:
model = Machine
fields = "__all__"
widgets = {
"user": AutocompleteModelMixin(
url="/users/user-autocomplete",
),
}
widgets = {"user": AutocompleteModelMixin(url="/users/user-autocomplete")}
def __init__(self, *args, **kwargs):
prefix = kwargs.pop("prefix", self.Meta.model.__name__)
......@@ -97,17 +97,16 @@ class EditInterfaceForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
model = Interface
fields = ["machine", "machine_type", "ipv4", "mac_address", "details"]
widgets = {
"machine": AutocompleteModelMixin(
url="/machines/machine-autocomplete",
),
"machine": AutocompleteModelMixin(url="/machines/machine-autocomplete"),
"machine_type": AutocompleteModelMixin(
url="/machines/machinetype-autocomplete",
url="/machines/machinetype-autocomplete"
),
"ipv4": AutocompleteModelMixin(
url="/machines/iplist-autocomplete", forward=['machine_type'],
url="/machines/iplist-autocomplete",
forward=["machine_type"],
attrs={
'data-placeholder': 'Automatic assigment. Type to choose specific ip.',
}
"data-placeholder": "Automatic assigment. Type to choose specific ip."
},
),
}
......@@ -159,9 +158,7 @@ class AliasForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
model = Domain
fields = ["name", "extension", "ttl"]
widgets = {
"extension": AutocompleteModelMixin(
url="/machines/extension-autocomplete",
),
"extension": AutocompleteModelMixin(url="/machines/extension-autocomplete")
}
def __init__(self, *args, **kwargs):
......@@ -213,9 +210,7 @@ class MachineTypeForm(FormRevMixin, ModelForm):
model = MachineType
fields = ["name", "ip_type"]
widgets = {
"ip_type": AutocompleteModelMixin(
url="/machines/iptype-autocomplete",
),
"ip_type": AutocompleteModelMixin(url="/machines/iptype-autocomplete")
}
def __init__(self, *args, **kwargs):
......@@ -252,14 +247,10 @@ class IpTypeForm(FormRevMixin, ModelForm):
model = IpType
fields = "__all__"
widgets = {
"vlan": AutocompleteModelMixin(
url="/machines/vlan-autocomplete",
),
"extension": AutocompleteModelMixin(
url="/machines/extension-autocomplete",
),
"vlan": AutocompleteModelMixin(url="/machines/vlan-autocomplete"),
"extension": AutocompleteModelMixin(url="/machines/extension-autocomplete"),
"ouverture_ports": AutocompleteModelMixin(
url="/machines/ouvertureportlist-autocomplete",
url="/machines/ouvertureportlist-autocomplete"
),
}
......@@ -392,12 +383,8 @@ class MxForm(FormRevMixin, ModelForm):
model = Mx
fields = ["zone", "priority", "name", "ttl"]
widgets = {
"zone": AutocompleteModelMixin(
url="/machines/extension-autocomplete",
),
"name": AutocompleteModelMixin(
url="/machines/domain-autocomplete",
),
"zone": AutocompleteModelMixin(url="/machines/extension-autocomplete"),
"name": AutocompleteModelMixin(url="/machines/domain-autocomplete"),
}
def __init__(self, *args, **kwargs):
......@@ -435,12 +422,8 @@ class NsForm(FormRevMixin, ModelForm):
model = Ns
fields = ["zone", "ns", "ttl"]
widgets = {
"zone": AutocompleteModelMixin(
url="/machines/extension-autocomplete",
),
"ns": AutocompleteModelMixin(
url="/machines/domain-autocomplete",
),
"zone": AutocompleteModelMixin(url="/machines/extension-autocomplete"),
"ns": AutocompleteModelMixin(url="/machines/domain-autocomplete"),
}
def __init__(self, *args, **kwargs):
......@@ -476,9 +459,7 @@ class TxtForm(FormRevMixin, ModelForm):
model = Txt
fields = "__all__"
widgets = {
"zone": AutocompleteModelMixin(
url="/machines/extension-autocomplete",
),
"zone": AutocompleteModelMixin(url="/machines/extension-autocomplete")
}
def __init__(self, *args, **kwargs):
......@@ -511,9 +492,7 @@ class DNameForm(FormRevMixin, ModelForm):
model = DName
fields = "__all__"
widgets = {
"zone": AutocompleteModelMixin(
url="/machines/extension-autocomplete",
),
"zone": AutocompleteModelMixin(url="/machines/extension-autocomplete")
}
def __init__(self, *args, **kwargs):
......@@ -546,12 +525,8 @@ class SrvForm(FormRevMixin, ModelForm):
model = Srv
fields = "__all__"
widgets = {
"extension": AutocompleteModelMixin(
url="/machines/extension-autocomplete",
),
"target": AutocompleteModelMixin(
url="/machines/domain-autocomplete",
),
"extension": AutocompleteModelMixin(url="/machines/extension-autocomplete"),
"target": AutocompleteModelMixin(url="/machines/domain-autocomplete"),
}
def __init__(self, *args, **kwargs):
......@@ -585,10 +560,10 @@ class NasForm(FormRevMixin, ModelForm):
fields = "__all__"
widgets = {
"nas_type": AutocompleteModelMixin(
url="/machines/machinetype-autocomplete",
url="/machines/machinetype-autocomplete"
),
"machine_type": AutocompleteModelMixin(
url="/machines/machinetype-autocomplete",
url="/machines/machinetype-autocomplete"
),
}
......@@ -623,8 +598,8 @@ class RoleForm(FormRevMixin, ModelForm):
fields = "__all__"
widgets = {
"servers": AutocompleteMultipleModelMixin(
url="/machines/interface-autocomplete",
),
url="/machines/interface-autocomplete"
)
}
def __init__(self, *args, **kwargs):
......@@ -661,8 +636,8 @@ class ServiceForm(FormRevMixin, ModelForm):
fields = "__all__"
widgets = {
"servers": AutocompleteMultipleModelMixin(
url="/machines/interface-autocomplete",
),
url="/machines/interface-autocomplete"
)
}
def __init__(self, *args, **kwargs):
......@@ -750,8 +725,8 @@ class EditOuverturePortConfigForm(FormRevMixin, ModelForm):
fields = ["port_lists"]
widgets = {
"port_lists": AutocompleteMultipleModelMixin(
url="/machines/ouvertureportlist-autocomplete",
),
url="/machines/ouvertureportlist-autocomplete"
)
}
def __init__(self, *args, **kwargs):
......
......@@ -43,7 +43,7 @@ from .models import (
Extension,
Domain,
OuverturePortList,
IpList
IpList,
)
from re2o.views import AutocompleteViewMixin
......@@ -66,15 +66,15 @@ class IpTypeAutocomplete(AutocompleteViewMixin):
class ExtensionAutocomplete(AutocompleteViewMixin):
obj_type = Extension
obj_type = Extension
class DomainAutocomplete(AutocompleteViewMixin):
obj_type = Domain
obj_type = Domain
class OuverturePortListAutocomplete(AutocompleteViewMixin):
obj_type = OuverturePortList
obj_type = OuverturePortList
class InterfaceAutocomplete(AutocompleteViewMixin):
......@@ -84,8 +84,7 @@ class InterfaceAutocomplete(AutocompleteViewMixin):
def filter_results(self):
if self.q:
self.query_set = self.query_set.filter(
Q(domain__name__icontains=self.q)
| Q(machine__name__icontains=self.q)
Q(domain__name__icontains=self.q) | Q(machine__name__icontains=self.q)
)
......@@ -94,11 +93,11 @@ class IpListAutocomplete(AutocompleteViewMixin):
# Precision on search to add annotations so search behaves more like users expect it to
def filter_results(self):
machine_type = self.forwarded.get('machine_type', None)
machine_type = self.forwarded.get("machine_type", None)
self.query_set = self.query_set.filter(interface__isnull=True)
if machine_type:
self.query_set = self.query_set.filter(ip_type__machinetype__id=machine_type)
if self.q:
self.query_set = self.query_set.filter(
Q(ipv4__startswith=self.q)
ip_type__machinetype__id=machine_type
)
if self.q:
self.query_set = self.query_set.filter(Q(ipv4__startswith=self.q))
......@@ -29,7 +29,6 @@ from django.utils.translation import ugettext as _
from dal import autocomplete
class RevMixin(object):
"""A mixin to subclass the save and delete function of a model
to enforce the versioning of the object before those actions
......@@ -260,6 +259,7 @@ class AutocompleteModelMixin(autocomplete.ModelSelect2):
""" A mixin subclassing django-autocomplete-light's Select2 model to pass default options
See https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html#passing-options-to-select2
"""
def __init__(self, *args, **kwargs):
select2_attrs = kwargs.get("attrs", {})
kwargs["attrs"] = self.fill_default_select2_attrs(select2_attrs)
......@@ -271,9 +271,11 @@ class AutocompleteModelMixin(autocomplete.ModelSelect2):
See https://select2.org/configuration/options-api
"""
# By default, only trigger autocompletion after 3 characters have been typed
#attrs["data-minimum-input-length"] = attrs.get("data-minimum-input-length", 3)
# attrs["data-minimum-input-length"] = attrs.get("data-minimum-input-length", 3)
# If there are less than 10 results, just show all of them (no need to autocomplete)
attrs["data-minimum-results-for-search"] = attrs.get("data-minimum-results-for-search", 10)
attrs["data-minimum-results-for-search"] = attrs.get(
"data-minimum-results-for-search", 10
)
return attrs
......@@ -281,6 +283,7 @@ class AutocompleteMultipleModelMixin(autocomplete.ModelSelect2Multiple):
""" A mixin subclassing django-autocomplete-light's Select2 model to pass default options
See https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html#passing-options-to-select2
"""
def __init__(self, *args, **kwargs):
select2_attrs = kwargs.get("attrs", {})
kwargs["attrs"] = self.fill_default_select2_attrs(select2_attrs)
......@@ -292,8 +295,9 @@ class AutocompleteMultipleModelMixin(autocomplete.ModelSelect2Multiple):
See https://select2.org/configuration/options-api
"""
# By default, only trigger autocompletion after 3 characters have been typed
#attrs["data-minimum-input-length"] = attrs.get("data-minimum-input-length", 3)
# attrs["data-minimum-input-length"] = attrs.get("data-minimum-input-length", 3)
# If there are less than 10 results, just show all of them (no need to autocomplete)
attrs["data-minimum-results-for-search"] = attrs.get("data-minimum-results-for-search", 10)
attrs["data-minimum-results-for-search"] = attrs.get(
"data-minimum-results-for-search", 10
)
return attrs
......@@ -193,5 +193,5 @@ class AutocompleteViewMixin(LoginRequiredMixin, autocomplete.Select2QuerySetView
self.filter_results()
else:
if self.q:
self.query_set = self.query_set.filter(**{ self.query_filter: self.q})
self.query_set = self.query_set.filter(**{self.query_filter: self.q})
return self.query_set
......@@ -37,7 +37,11 @@ from django.utils.translation import ugettext_lazy as _
from machines.models import Interface
from machines.forms import EditMachineForm, NewMachineForm
from re2o.mixins import FormRevMixin, AutocompleteModelMixin, AutocompleteMultipleModelMixin
from re2o.mixins import (
FormRevMixin,
AutocompleteModelMixin,
AutocompleteMultipleModelMixin,
)
from .models import (
Port,
......@@ -63,20 +67,14 @@ class PortForm(FormRevMixin, ModelForm):
model = Port
fields = "__all__"
widgets = {
"switch": AutocompleteModelMixin(
url="/topologie/switch-autocomplete",
),
"room": AutocompleteModelMixin(
url="/topologie/room-autocomplete",
),
"switch": AutocompleteModelMixin(url="/topologie/switch-autocomplete"),
"room": AutocompleteModelMixin(url="/topologie/room-autocomplete"),
"machine_interface": AutocompleteModelMixin(
url="/machine/machine-autocomplete",
),
"related": AutocompleteModelMixin(
url="/topologie/port-autocomplete",
url="/machine/machine-autocomplete"
),
"related": AutocompleteModelMixin(url="/topologie/port-autocomplete"),
"custom_profile": AutocompleteModelMixin(
url="/topologie/portprofile-autocomplete",
url="/topologie/portprofile-autocomplete"
),
}
......@@ -184,11 +182,9 @@ class EditSwitchForm(EditMachineForm):
fields = "__all__"
widgets = {
"switchbay": AutocompleteModelMixin(
url="/topologie/switchbay-autocomplete",
),
"user": AutocompleteModelMixin(
url="/users/user-autocomplete",
url="/topologie/switchbay-autocomplete"
),
"user": AutocompleteModelMixin(url="/users/user-autocomplete"),
}
......@@ -206,9 +202,7 @@ class EditRoomForm(FormRevMixin, ModelForm):
model = Room
fields = "__all__"
widgets = {
"building": AutocompleteModelMixin(
url="/topologie/building-autocomplete",
),
"building": AutocompleteModelMixin(url="/topologie/building-autocomplete")
}
def __init__(self, *args, **kwargs):
......@@ -229,7 +223,7 @@ class EditModelSwitchForm(FormRevMixin, ModelForm):
members = forms.ModelMultipleChoiceField(
Switch.objects.all(),
widget=AutocompleteMultipleModelMixin(url="/topologie/switch-autocomplete"),
required=False
required=False,
)
class Meta:
......@@ -274,9 +268,7 @@ class EditSwitchBayForm(FormRevMixin, ModelForm):
model = SwitchBay
fields = "__all__"
widgets = {
"building": AutocompleteModelMixin(
url="/topologie/building-autocomplete",
),
"building": AutocompleteModelMixin(url="/topologie/building-autocomplete")
}
def __init__(self, *args, **kwargs):
......@@ -324,11 +316,9 @@ class EditPortProfileForm(FormRevMixin, ModelForm):
fields = "__all__"
widgets = {
"vlan_tagged": AutocompleteMultipleModelMixin(
url="/machines/vlan-autocomplete",
),
"vlan_untagged": AutocompleteModelMixin(
url="/machines/vlan-autocomplete",
url="/machines/vlan-autocomplete"
),
"vlan_untagged": AutocompleteModelMixin(url="/machines/vlan-autocomplete"),
}
def __init__(self, *args, **kwargs):
......
......@@ -34,15 +34,7 @@ from __future__ import unicode_literals
from django.db.models import Q, Value, CharField
from django.db.models.functions import Concat
from .models import (
Room,
Dormitory,
Building,
Switch,
PortProfile,
Port,
SwitchBay,
)
from .models import Room, Dormitory, Building, Switch, PortProfile, Port, SwitchBay
from re2o.views import AutocompleteViewMixin
......@@ -55,11 +47,27 @@ class RoomAutocomplete(AutocompleteViewMixin):
# Suppose we have a dorm named Dorm, a building name B, and rooms from 001 - 999
# Comments explain what we try to match
self.query_set = self.query_set.annotate(
full_name=Concat("building__name", Value(" "), "name"), # Match when the user searches "B 001"
full_name=Concat(
"building__name", Value(" "), "name"
), # Match when the user searches "B 001"
full_name_stuck=Concat("building__name", "name"), # Match "B001"
dorm_name=Concat("building__dormitory__name", Value(" "), "name"), # Match "Dorm 001"
dorm_full_name=Concat("building__dormitory__name", Value(" "), "building__name", Value(" "), "name"), # Match "Dorm B 001"
dorm_full_colon_name=Concat("building__dormitory__name", Value(" : "), "building__name", Value(" "), "name"), # Match "Dorm : B 001" (see Room's full_name property)
dorm_name=Concat(
"building__dormitory__name", Value(" "), "name"
), # Match "Dorm 001"
dorm_full_name=Concat(
"building__dormitory__name",
Value(" "),
"building__name",
Value(" "),
"name",
), # Match "Dorm B 001"
dorm_full_colon_name=Concat(
"building__dormitory__name",
Value(" : "),
"building__name",
Value(" "),
"name",
), # Match "Dorm : B 001" (see Room's full_name property)
).all()
if self.q:
......@@ -89,8 +97,7 @@ class BuildingAutocomplete(AutocompleteViewMixin):
if self.q:
self.query_set = self.query_set.filter(
Q(full_name__icontains=self.q)
| Q(full_name_colon__icontains=self.q)
Q(full_name__icontains=self.q) | Q(full_name_colon__icontains=self.q)
)
......@@ -106,9 +113,13 @@ class PortAutocomplete(AutocompleteViewMixin):
# We want to enter the switch name, not just the port number
# Because we're concatenating a CharField and an Integer, we have to sepcify the output_field
self.query_set = self.query_set.annotate(
full_name=Concat("switch__name", Value(" "), "port", output_field=CharField()),
full_name=Concat(
"switch__name", Value(" "), "port", output_field=CharField()
),
full_name_stuck=Concat("switch__name", "port", output_field=CharField()),
full_name_dash=Concat("switch__name", Value(" - "), "port", output_field=CharField()),
full_name_dash=Concat(
"switch__name", Value(" - "), "port", output_field=CharField()
),
).all()
if self.q:
......@@ -126,9 +137,19 @@ class SwitchBayAutocomplete(AutocompleteViewMixin):
def filter_results(self):
# Comments explain what we try to match
self.query_set = self.query_set.annotate(
full_name=Concat("building__name", Value(" "), "name"), # Match when the user searches ""
dorm_name=Concat("building__dormitory__name", Value(" "), "name"), # Match "Dorm Local Sud"
dorm_full_name=Concat("building__dormitory__name", Value(" "), "building__name", Value(" "), "name"), # Match "Dorm J Local Sud"
full_name=Concat(
"building__name", Value(" "), "name"
), # Match when the user searches ""
dorm_name=Concat(
"building__dormitory__name", Value(" "), "name"
), # Match "Dorm Local Sud"
dorm_full_name=Concat(
"building__dormitory__name",
Value(" "),
"building__name",
Value(" "),
"name",
), # Match "Dorm J Local Sud"
).all()
if self.q:
......
......@@ -46,7 +46,10 @@ from os import walk, path
from django import forms
from django.forms import ModelForm, Form
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from django.contrib.auth.password_validation import validate_password, password_validators_help_text_html
from django.contrib.auth.password_validation import (
validate_password,
password_validators_help_text_html,
)
from django.core.validators import MinLengthValidator
from django.conf import settings
from django.utils import timezone
......@@ -60,7 +63,11 @@ from topologie.models import Port
from preferences.models import OptionalUser
from re2o.utils import remove_user_room
from re2o.base import get_input_formats_help_text
from re2o.mixins import FormRevMixin, AutocompleteMultipleModelMixin, AutocompleteModelMixin
from re2o.mixins import (
FormRevMixin,
AutocompleteMultipleModelMixin,
AutocompleteModelMixin,
)
from re2o.field_permissions import FieldPermissionFormMixin
from preferences.models import GeneralOption
......@@ -156,14 +163,10 @@ class ServiceUserAdminForm(FormRevMixin, forms.ModelForm):
"""
password1 = forms.CharField(
label=_("Password"),
widget=forms.PasswordInput,
max_length=255,
label=_("Password"), widget=forms.PasswordInput, max_length=255
)
password2 = forms.CharField(
label=_("Password confirmation"),
widget=forms.PasswordInput,
max_length=255,
label=_("Password confirmation"), widget=forms.PasswordInput, max_length=255
)
def __init__(self, *args, **kwargs):
......@@ -215,6 +218,7 @@ class PassForm(FormRevMixin, FieldPermissionFormMixin, forms.ModelForm):
DjangoForm : Inherit from basic django form
"""
selfpasswd = forms.CharField(
label=_("Current password"), max_length=255, widget=forms.PasswordInput
)
......@@ -222,12 +226,10 @@ class PassForm(FormRevMixin, FieldPermissionFormMixin, forms.ModelForm):
label=_("New password"),
max_length=255,
widget=forms.PasswordInput,