Skip to content

Commit

Permalink
Improve query of views.py functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nnsnodnb committed Dec 29, 2023
1 parent 1d5d285 commit d1d5b52
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions passkeys/views.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseForbidden
from django.shortcuts import get_object_or_404, render

from .models import UserPasskey


@login_required
def index(request, enroll=False): # noqa
keys = UserPasskey.objects.filter(user=request.user) # pragma: no cover
return render(request, 'PassKeys.html', {"keys": keys, "enroll": enroll}) # pragma: no cover
return render(request, "PassKeys.html", {"keys": keys, "enroll": enroll}) # pragma: no cover


@login_required
def delKey(request):
key=UserPasskey.objects.get(id=request.GET["id"])
if key.user.pk == request.user.pk:
key.delete()
return HttpResponse("Deleted Successfully")
return HttpResponse("Error: You own this token so you can't delete it", status=403)
key = get_object_or_404(UserPasskey, id=request.GET["id"], user=request.user)
key.delete()
return HttpResponse("Deleted Successfully")


@login_required
def toggleKey(request):
id=request.GET["id"]
q=UserPasskey.objects.filter(user=request.user, id=id)
if q.count()==1:
key=q[0]
key.enabled=not key.enabled
key.save()
key = UserPasskey.objects.filter(id=request.GET["id"], user=request.user).first()
if key is not None:
key.enabled = not key.enabled
key.save(update_fields=["enabled"])
return HttpResponse("OK")
return HttpResponse("Error: You own this token so you can't toggle it", status=403)
return HttpResponseForbidden("Error: You own this token so you can't toggle it")

0 comments on commit d1d5b52

Please sign in to comment.