I have two Views SwitchWorkspace
and Home
, it takes 2-3 seconds to go from SwitchWorkspace
to Home
view. I am sharing my views code, I need ideas to speed up the process.
Below is my views.py
:
class SwitchWorkspace(LoginRequiredMixin, FormView):
success_url = reverse_lazy("home")
template_name = "accounts/switch-workspace.html"
context = {"title": "Switch workspace"}
def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
if request.user.is_superuser:
return redirect(self.success_url)
USER_WORKSPACES = request.user.get_workspaces_choices()
request.session["workspaces"] = USER_WORKSPACES
form = SwitchWorkspaceForm(request)
self.context = {"form": form}
return render(request, self.template_name, self.context)
def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
if request.user.is_superuser:
return redirect(self.success_url)
form = SwitchWorkspaceForm(request, request.POST)
context = {"form": form}
if not form.is_valid():
messages.error(request, "You must select a field")
return render(request, self.template_name, context)
form_data = form.cleaned_data
choice = form_data["workspaces"]
workspace = Workspace.objects.get(name=choice)
if not workspace.is_active:
raise BadRequest("WORKSPACE INACTIVE: We are sorry but the workspace you are trying to \
switch to, is inactive. Please ask Administrator to make it active \
or try switching to another workspace.")
workspace_membership = WorkspaceMembership.objects.get(members=request.user, workspaces=workspace)
if not workspace_membership.is_active:
raise BadRequest("ACCOUNT INACTIVE: We are sorry but your account for this workspace is inactive. \
Please ask your Workspace Administrator to make your account active in this \
workspace, or try switching to another workspace.")
if workspace_membership.is_workspace_admin:
request.session["is_workspace_admin"] = True
request.user.update_active_workspace(choice, workspace_membership)
return redirect(self.success_url)
class Home(LoginRequiredMixin, View):
template_name = "accounts/home.html"
def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
user = request.user
if user.active_workspace == "w" and not user.is_superuser:
return redirect(reverse_lazy("switch_workspace"))
try:
del request.session['workspaces']
except KeyError:
pass
context = {"title": "Welcome " + request.user.email}
workspace = Workspace.objects.get(name=user.active_workspace)
workspace_membership = WorkspaceMembership.objects.get(members=user, workspaces=workspace)
try:
context["is_workspace_admin"] = workspace_membership.is_workspace_admin
except KeyError:
if request.user.is_superuser:
return render(request, self.template_name, context)
return render(request, self.template_name, context)
Any ideas will be appreciated to speed up the process