From 11c4f4ef212d31a8a99033d7642f243aa65ff480 Mon Sep 17 00:00:00 2001 From: Hugo Levy-Falk Date: Mon, 28 Dec 2020 20:50:00 +0100 Subject: [PATCH] Fix club edit and add some documentation on that error. --- re2o/acl.py | 10 ++++++++-- users/models.py | 10 +++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/re2o/acl.py b/re2o/acl.py index 600b839f..e73ffdfa 100644 --- a/re2o/acl.py +++ b/re2o/acl.py @@ -64,11 +64,17 @@ def acl_base_decorator(method_name, *targets, on_instance=True): """Base decorator for acl. It checks if the `request.user` has the permission by calling model.method_name. If the flag on_instance is True, tries to get an instance of the model by calling - `model.get_instance(*args, **kwargs)` and runs `instance.mehod_name` + `model.get_instance(obj_id, *args, **kwargs)` and runs `instance.mehod_name` rather than model.method_name. It is not intended to be used as is. It is a base for others ACL - decorators. + decorators. Beware, if you redefine the `get_instance` method for your + model, give it a signature such as + `def get_instance(cls, object_id, *_args, **_kwargs)`, because you will + likely have an url with a named parameter "userid" if *e.g.* your model + is an user. Otherwise, if the parameter name in `get_instance` was also + `userid`, then `get_instance` would end up having two identical parameter + passed on, and this would result in a `TypeError` exception. Args: method_name: The name of the method which is to to be used for ACL. diff --git a/users/models.py b/users/models.py index 67560472..14215fdf 100755 --- a/users/models.py +++ b/users/models.py @@ -2027,10 +2027,10 @@ class Adherent(User): self.gpg_fingerprint = gpg_fingerprint @classmethod - def get_instance(cls, adherentid, *_args, **_kwargs): + def get_instance(cls, object_id, *_args, **_kwargs): """Try to find an instance of `Adherent` with the given id. - :param adherentid: The id of the adherent we are looking for. + :param object_id: The id of the adherent we are looking for. :return: An adherent. """ @@ -2154,13 +2154,13 @@ class Club(User): ) @classmethod - def get_instance(cls, clubid, *_args, **_kwargs): + def get_instance(cls, object_id, *_args, **_kwargs): """Try to find an instance of `Club` with the given id. - :param clubid: The id of the adherent we are looking for. + :param object_id: The id of the adherent we are looking for. :return: A club. """ - return cls.objects.get(pk=clubid) + return cls.objects.get(pk=object_id) @receiver(post_save, sender=Adherent)