Skip to content

Commit

Permalink
Make action url in guidance context lazy
Browse files Browse the repository at this point in the history
Otherwise rendering many guidances on request site will create many
autologin URLs via AccessToken objects.
  • Loading branch information
stefanw committed Mar 31, 2022
1 parent be6312a commit f94499b
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions froide/guide/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ def wrapper(self):
return wrapper


class LazyStr:
def __init__(self, func):
self.func = func

def __str__(self):
return str(self.func())


class Guidance(models.Model):
message = models.ForeignKey(FoiMessage, on_delete=models.CASCADE)
action = models.ForeignKey(Action, null=True, blank=True, on_delete=models.CASCADE)
Expand Down Expand Up @@ -123,10 +131,14 @@ def get_context(self):
"user": user,
"name": user.get_full_name(),
"message": self.message,
"action_url": "{}-guidance".format(self.message.get_autologin_url()),
"action_url": LazyStr(
lambda: "{}-guidance".format(self.message.get_autologin_url())
),
}
if self.action and self.action.letter_template_id:
ctx["action_url"] = user.get_autologin_url(self.get_letter_url())
ctx["action_url"] = LazyStr(
lambda: user.get_autologin_url(self.get_letter_url())
)
return ctx

@render_with_context
Expand Down

0 comments on commit f94499b

Please sign in to comment.