Files
decomp.me/backend/coreapp/views/user.py
T
Ethan Roseman eb7534be0d Black (#380)
* Initial format

* action

* git subrepo pull --force backend/asm_differ

subrepo:
  subdir:   "backend/asm_differ"
  merged:   "ee239a2b"
upstream:
  origin:   "https://github.com/simonlindholm/asm-differ"
  branch:   "main"
  commit:   "ee239a2b"
git-subrepo:
  version:  "0.4.3"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "2f68596"

* git subrepo pull --force backend/mips_to_c

subrepo:
  subdir:   "backend/mips_to_c"
  merged:   "6704d61f"
upstream:
  origin:   "https://github.com/matt-kempster/mips_to_c"
  branch:   "master"
  commit:   "6704d61f"
git-subrepo:
  version:  "0.4.3"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "2f68596"

* vscode

* fix

* .
2022-02-20 23:21:38 +09:00

78 lines
2.0 KiB
Python

from django.contrib.auth import logout
from django.shortcuts import get_object_or_404
from rest_framework import generics
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.views import APIView
from .scratch import ScratchPagination
from ..middleware import Request
from ..models.profile import Profile
from ..models.scratch import Scratch
from ..models.github import GitHubUser
from ..serializers import serialize_profile, TerseScratchSerializer
class CurrentUser(APIView):
"""
View to access the current user profile.
"""
def get(self, request: Request):
user = serialize_profile(request, request.profile)
return Response(user)
def post(self, request: Request):
"""
Login if the 'code' parameter is provided. Log out otherwise.
"""
if "code" in request.data:
GitHubUser.login(request, request.data["code"])
assert not request.profile.is_anonymous()
return self.get(request)
else:
logout(request)
profile = Profile()
profile.save()
request.profile = profile
request.session["profile_id"] = request.profile.id
return self.get(request)
class CurrentUserScratchList(generics.ListAPIView):
"""
Gets the current user's scratches
"""
pagination_class = ScratchPagination
serializer_class = TerseScratchSerializer
def get_queryset(self):
return Scratch.objects.filter(owner=self.request.profile)
class UserScratchList(generics.ListAPIView):
"""
Gets a user's scratches
"""
pagination_class = ScratchPagination
serializer_class = TerseScratchSerializer
def get_queryset(self):
return Scratch.objects.filter(owner__user__username=self.kwargs["username"])
@api_view(["GET"])
def user(request, username):
"""
Gets a user's basic data
"""
return Response(
serialize_profile(request, get_object_or_404(Profile, user__username=username))
)