8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-23 15:33:45 +00:00

Just blacked everything.

This commit is contained in:
Hugo Levy-Falk 2020-12-28 20:59:35 +01:00
parent 70b492c315
commit a1c08c4e96
4 changed files with 48 additions and 55 deletions

View file

@ -278,8 +278,7 @@ class AutodetectACLPermission(permissions.BasePermission):
# they have read permissions to see 403, or not, and simply see # they have read permissions to see 403, or not, and simply see
# a 404 response. # a 404 response.
SAFE_METHODS = ("GET", "OPTIONS", "HEAD", SAFE_METHODS = ("GET", "OPTIONS", "HEAD", "POST", "PUT", "PATCH", "DELETE")
"POST", "PUT", "PATCH", "DELETE")
if request.method in SAFE_METHODS: if request.method in SAFE_METHODS:
# Read permissions already checked and failed, no need # Read permissions already checked and failed, no need

View file

@ -96,8 +96,7 @@ def edit_options_template_function(request, section, forms, models):
return redirect(reverse("preferences:display-options")) return redirect(reverse("preferences:display-options"))
options_instance, _created = model.objects.get_or_create() options_instance, _created = model.objects.get_or_create()
_is_allowed_to_edit, msg, permissions = options_instance.can_edit( _is_allowed_to_edit, msg, permissions = options_instance.can_edit(request.user)
request.user)
if not _is_allowed_to_edit: if not _is_allowed_to_edit:
messages.error(request, acl_error_message(msg, permissions)) messages.error(request, acl_error_message(msg, permissions))
return redirect(reverse("index")) return redirect(reverse("index"))
@ -151,7 +150,8 @@ def display_options(request):
optionnal_templates_list = [ optionnal_templates_list = [
app.preferences.views.aff_preferences(request) app.preferences.views.aff_preferences(request)
for app in optionnal_apps for app in optionnal_apps
if hasattr(app, "preferences") and hasattr(app.preferences.views, "aff_preferences") if hasattr(app, "preferences")
and hasattr(app.preferences.views, "aff_preferences")
] ]
return form( return form(
@ -302,8 +302,7 @@ def add_radiuskey(request):
@can_edit(RadiusKey) @can_edit(RadiusKey)
def edit_radiuskey(request, radiuskey_instance, **_kwargs): def edit_radiuskey(request, radiuskey_instance, **_kwargs):
"""View used to edit RADIUS keys.""" """View used to edit RADIUS keys."""
radiuskey = RadiusKeyForm(request.POST or None, radiuskey = RadiusKeyForm(request.POST or None, instance=radiuskey_instance)
instance=radiuskey_instance)
if radiuskey.is_valid(): if radiuskey.is_valid():
radiuskey.save() radiuskey.save()
messages.success(request, _("The RADIUS key was edited.")) messages.success(request, _("The RADIUS key was edited."))
@ -346,11 +345,13 @@ def add_switchmanagementcred(request):
switchmanagementcred = SwitchManagementCredForm(request.POST or None) switchmanagementcred = SwitchManagementCredForm(request.POST or None)
if switchmanagementcred.is_valid(): if switchmanagementcred.is_valid():
switchmanagementcred.save() switchmanagementcred.save()
messages.success(request, _( messages.success(request, _("The switch management credentials were added."))
"The switch management credentials were added."))
return redirect(reverse("preferences:display-options")) return redirect(reverse("preferences:display-options"))
return form( return form(
{"preferenceform": switchmanagementcred, "action_name": _("Add"), }, {
"preferenceform": switchmanagementcred,
"action_name": _("Add"),
},
"preferences/preferences.html", "preferences/preferences.html",
request, request,
) )
@ -364,8 +365,7 @@ def edit_switchmanagementcred(request, switchmanagementcred_instance, **_kwargs)
) )
if switchmanagementcred.is_valid(): if switchmanagementcred.is_valid():
switchmanagementcred.save() switchmanagementcred.save()
messages.success(request, _( messages.success(request, _("The switch management credentials were edited."))
"The switch management credentials were edited."))
return redirect(reverse("preferences:display-options")) return redirect(reverse("preferences:display-options"))
return form( return form(
{"preferenceform": switchmanagementcred, "action_name": _("Edit")}, {"preferenceform": switchmanagementcred, "action_name": _("Edit")},
@ -414,7 +414,10 @@ def add_mailcontact(request):
messages.success(request, _("The contact email address was created.")) messages.success(request, _("The contact email address was created."))
return redirect(reverse("preferences:display-options")) return redirect(reverse("preferences:display-options"))
return form( return form(
{"preferenceform": mailcontact, "action_name": _("Add"), }, {
"preferenceform": mailcontact,
"action_name": _("Add"),
},
"preferences/preferences.html", "preferences/preferences.html",
request, request,
) )
@ -442,14 +445,12 @@ def edit_mailcontact(request, mailcontact_instance, **_kwargs):
@can_delete_set(MailContact) @can_delete_set(MailContact)
def del_mailcontact(request, instances): def del_mailcontact(request, instances):
"""View used to delete one or several contact email addresses.""" """View used to delete one or several contact email addresses."""
mailcontacts = DelMailContactForm( mailcontacts = DelMailContactForm(request.POST or None, instances=instances)
request.POST or None, instances=instances)
if mailcontacts.is_valid(): if mailcontacts.is_valid():
mailcontacts_dels = mailcontacts.cleaned_data["mailcontacts"] mailcontacts_dels = mailcontacts.cleaned_data["mailcontacts"]
for mailcontacts_del in mailcontacts_dels: for mailcontacts_del in mailcontacts_dels:
mailcontacts_del.delete() mailcontacts_del.delete()
messages.success(request, _( messages.success(request, _("The contact email adress was deleted."))
"The contact email adress was deleted."))
return redirect(reverse("preferences:display-options")) return redirect(reverse("preferences:display-options"))
return form( return form(
{"preferenceform": mailcontacts, "action_name": _("Delete")}, {"preferenceform": mailcontacts, "action_name": _("Delete")},

View file

@ -44,13 +44,11 @@ def acl_error_message(msg, permissions):
"""Create an error message for msg and permissions.""" """Create an error message for msg and permissions."""
if permissions is None: if permissions is None:
return msg return msg
groups = ", ".join( groups = ", ".join([g.name for g in get_group_having_permission(*permissions)])
[g.name for g in get_group_having_permission(*permissions)])
message = msg or _("You don't have the right to edit this option.") message = msg or _("You don't have the right to edit this option.")
if groups: if groups:
return ( return (
message + message + _("You need to be a member of one of these groups: %s.") % groups
_("You need to be a member of one of these groups: %s.") % groups
) )
else: else:
return message + _("No group has the %s permission(s)!") % " or ".join( return message + _("No group has the %s permission(s)!") % " or ".join(
@ -181,8 +179,7 @@ ModelC)
# `wrapper` inside the `decorator` function, you need to read some # `wrapper` inside the `decorator` function, you need to read some
#  documentation on decorators ! #  documentation on decorators !
def decorator(view): def decorator(view):
"""The decorator to use on a specific view """The decorator to use on a specific view"""
"""
def wrapper(request, *args, **kwargs): def wrapper(request, *args, **kwargs):
"""The wrapper used for a specific request""" """The wrapper used for a specific request"""
@ -198,8 +195,7 @@ ModelC)
# and store it to pass it to the view. # and store it to pass it to the view.
if on_instance: if on_instance:
try: try:
target = target.get_instance( target = target.get_instance(target_id, *args, **kwargs)
target_id, *args, **kwargs)
instances.append(target) instances.append(target)
except target.DoesNotExist: except target.DoesNotExist:
# A non existing instance is a valid reason to deny # A non existing instance is a valid reason to deny
@ -245,11 +241,9 @@ ModelC)
# Store the messages at the right place. # Store the messages at the right place.
for can, msg, permissions in process_target(target, fields, target_id): for can, msg, permissions in process_target(target, fields, target_id):
if not can: if not can:
error_messages.append( error_messages.append(acl_error_message(msg, permissions))
acl_error_message(msg, permissions))
elif msg: elif msg:
warning_messages.append( warning_messages.append(acl_error_message(msg, permissions))
acl_error_message(msg, permissions))
# Display the warning messages # Display the warning messages
if not api: if not api:
@ -264,18 +258,24 @@ ModelC)
for msg in error_messages: for msg in error_messages:
messages.error( messages.error(
request, request,
msg or _( msg or _("You don't have the right to access this menu."),
"You don't have the right to access this menu."),
) )
# And redirect the user to the right place. # And redirect the user to the right place.
if request.user.id is not None: if request.user.id is not None:
if not api: if not api:
return redirect( return redirect(
reverse("users:profil", kwargs={ reverse(
"userid": str(request.user.id)}) "users:profil", kwargs={"userid": str(request.user.id)}
)
) )
else: else:
return Response(data={"errors": error_messages, "warning": warning_messages}, status=403) return Response(
data={
"errors": error_messages,
"warning": warning_messages,
},
status=403,
)
else: else:
return redirect(reverse("index")) return redirect(reverse("index"))
return view(request, *chain(instances, args), **kwargs) return view(request, *chain(instances, args), **kwargs)
@ -326,12 +326,10 @@ def can_delete_set(model):
If none of them, return an error""" If none of them, return an error"""
def decorator(view): def decorator(view):
"""The decorator to use on a specific view """The decorator to use on a specific view"""
"""
def wrapper(request, *args, **kwargs): def wrapper(request, *args, **kwargs):
"""The wrapper used for a specific request """The wrapper used for a specific request"""
"""
all_objects = model.objects.all() all_objects = model.objects.all()
instances_id = [] instances_id = []
for instance in all_objects: for instance in all_objects:
@ -344,8 +342,7 @@ def can_delete_set(model):
request, _("You don't have the right to access this menu.") request, _("You don't have the right to access this menu.")
) )
return redirect( return redirect(
reverse("users:profil", kwargs={ reverse("users:profil", kwargs={"userid": str(request.user.id)})
"userid": str(request.user.id)})
) )
return view(request, instances, *args, **kwargs) return view(request, instances, *args, **kwargs)
@ -373,8 +370,7 @@ def can_view_all(*targets):
def can_view_app(*apps_name): def can_view_app(*apps_name):
"""Decorator to check if an user can view the applications. """Decorator to check if an user can view the applications."""
"""
for app_name in apps_name: for app_name in apps_name:
assert app_name in sys.modules.keys() assert app_name in sys.modules.keys()
return acl_base_decorator( return acl_base_decorator(
@ -388,12 +384,10 @@ def can_edit_history(view):
"""Decorator to check if an user can edit history.""" """Decorator to check if an user can edit history."""
def wrapper(request, *args, **kwargs): def wrapper(request, *args, **kwargs):
"""The wrapper used for a specific request """The wrapper used for a specific request"""
"""
if request.user.has_perm("admin.change_logentry"): if request.user.has_perm("admin.change_logentry"):
return view(request, *args, **kwargs) return view(request, *args, **kwargs)
messages.error(request, _( messages.error(request, _("You don't have the right to edit the history."))
"You don't have the right to edit the history."))
return redirect( return redirect(
reverse("users:profil", kwargs={"userid": str(request.user.id)}) reverse("users:profil", kwargs={"userid": str(request.user.id)})
) )

View file

@ -29,9 +29,9 @@ from django.utils.translation import ugettext as _
class RevMixin(object): class RevMixin(object):
""" A mixin to subclass the save and delete function of a model """A mixin to subclass the save and delete function of a model
to enforce the versioning of the object before those actions to enforce the versioning of the object before those actions
really happen """ really happen"""
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
""" Creates a version of this object and save it to database """ """ Creates a version of this object and save it to database """
@ -49,8 +49,8 @@ class RevMixin(object):
class FormRevMixin(object): class FormRevMixin(object):
""" A mixin to subclass the save function of a form """A mixin to subclass the save function of a form
to enforce the versionning of the object before it is really edited """ to enforce the versionning of the object before it is really edited"""
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
""" Create a version of this object and save it to database """ """ Create a version of this object and save it to database """
@ -61,8 +61,7 @@ class FormRevMixin(object):
) )
elif self.changed_data: elif self.changed_data:
reversion.set_comment( reversion.set_comment(
"Field(s) edited: %s" % ", ".join( "Field(s) edited: %s" % ", ".join(field for field in self.changed_data)
field for field in self.changed_data)
) )
return super(FormRevMixin, self).save(*args, **kwargs) return super(FormRevMixin, self).save(*args, **kwargs)