Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code improvements in utils.py #288

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,35 @@ def quota_usage_check_under(
except KeyError:
pass
return HttpResponseRedirect(reverse("dashboard"))


def set_cache(key, value, timeout=300):
cache.set(key, value, timeout=timeout)


def get_cache(key):
return cache.get(key)


def render_quota_error(request, quota_limit):
messages.error(request, f"You have reached the quota limit for this service '{quota_limit.slug}'")
return render(request, "partials/messages_list.html", {"autohide": False})


def render_quota_error_response(quota_limit):
return HttpResponse(status=403, content=f"You have reached the quota limit for this service '{quota_limit.slug}'")


def redirect_to_last_visited(request, fallback_url="dashboard"):
"""
Redirects user to the last visited URL stored in session.
If no previous URL is found, redirects to the fallback URL.
:param request: HttpRequest object
:param fallback_url: URL to redirect to if no previous URL found
:return: HttpResponseRedirect object
"""
try:
last_visited_url = request.session.get("last_visited", fallback_url)
return HttpResponseRedirect(last_visited_url)
except KeyError:
return HttpResponseRedirect(fallback_url)
Loading