Commit 27b6c728 authored by Jakob Lund Johannessen's avatar Jakob Lund Johannessen
Browse files

fix: refactor user views

parent f8c97135
Pipeline #171232 failed with stages
in 1 minute and 5 seconds
from django.urls import path, include
from users import views
from users.views.user import UserList, UserDetail, UserSearch, UserVisibility
from users.views.athlete import AthleteFileList, AthleteFileDetail
from users.views.offer import OfferList, OfferDetail
users_url_patterns = [
path("", views.UserList.as_view(), name="user-list"),
path("", UserList.as_view(), name="user-list"),
path(
"<int:pk>/",
views.UserDetail.as_view(),
UserDetail.as_view(),
name="user-detail"),
path(
"<str:username>/",
views.UserDetail.as_view(),
UserDetail.as_view(),
name="user-detail"),
]
offers_url_patterns = [
path(
"",
views.OfferList.as_view(),
OfferList.as_view(),
name="offer-list"),
path(
"<int:pk>/",
views.OfferDetail.as_view(),
OfferDetail.as_view(),
name="offer-detail"),
]
athlete_files_url_patterns = [
path(
"",
views.AthleteFileList.as_view(),
AthleteFileList.as_view(),
name="athlete-file-list"
),
path(
"<int:pk>/",
views.AthleteFileDetail.as_view(),
AthleteFileDetail.as_view(),
name="athletefile-detail",
),
]
......@@ -41,9 +43,9 @@ urlpatterns = [
path("users/", include(users_url_patterns)),
path("offers/", include(offers_url_patterns)),
path("athelete-files/", include(athlete_files_url_patterns)),
path("usersearch/", views.UserSearch.as_view(), name="user-search"),
path("usersearch/", UserSearch.as_view(), name="user-search"),
path(
"uservisibility/<int:pk>/",
views.UserVisibility.as_view(),
UserVisibility.as_view(),
name="user-visibility"),
]
import django
from rest_framework import mixins, generics
from workouts.mixins import CreateListModelMixin
from rest_framework import permissions
from users.serializers.UserCustomSerializers import (
AthleteFileSerializer,
)
from users.models import AthleteFile
from django.db.models import Q
from rest_framework.parsers import MultiPartParser, FormParser
from users.permissions import IsAthlete, IsCoach
from workouts.permissions import IsOwner
# Create your views here.
class AthleteFileList(
mixins.ListModelMixin,
mixins.CreateModelMixin,
CreateListModelMixin,
generics.GenericAPIView,
):
queryset = AthleteFile.objects.all()
serializer_class = AthleteFileSerializer
permission_classes = [permissions.IsAuthenticated & (IsAthlete | IsCoach)]
parser_classes = [MultiPartParser, FormParser]
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
def get_queryset(self):
qs = AthleteFile.objects.none()
if self.request.user:
qs = AthleteFile.objects.filter(
Q(athlete=self.request.user) | Q(owner=self.request.user)
).distinct()
return qs
class AthleteFileDetail(
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView,
):
queryset = AthleteFile.objects.all()
serializer_class = AthleteFileSerializer
permission_classes = [permissions.IsAuthenticated & (IsAthlete | IsOwner)]
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
import django
from rest_framework import mixins, generics
from users.serializers.UserCustomSerializers import (
OfferSerializer,
)
from rest_framework.permissions import (
IsAuthenticatedOrReadOnly,
)
from users.models import Offer
from django.db.models import Q
# Create your views here.
class OfferList(
mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView
):
permission_classes = [IsAuthenticatedOrReadOnly]
serializer_class = OfferSerializer
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
def get_queryset(self):
qs = Offer.objects.none()
result = Offer.objects.none()
if self.request.user:
qs = Offer.objects.filter(
Q(owner=self.request.user) | Q(recipient=self.request.user)
).distinct()
qp = self.request.query_params
u = self.request.user
# filtering by status (if provided)
s = qp.get("status", None)
if s is not None and self.request is not None:
qs = qs.filter(status=s)
if qp.get("status", None) is None:
qs = Offer.objects.filter(Q(owner=u)).distinct()
# filtering by category (sent or received)
c = qp.get("category", None)
if c is not None and qp is not None:
if c == "sent":
qs = qs.filter(owner=u)
elif c == "received":
qs = qs.filter(recipient=u)
return qs
else:
return result
class OfferDetail(
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView,
):
permission_classes = [IsAuthenticatedOrReadOnly]
queryset = Offer.objects.all()
serializer_class = OfferSerializer
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
def patch(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
import django
# from SO
import json
from django.core.serializers.json import DjangoJSONEncoder
####
from rest_framework import mixins, generics
from workouts.mixins import CreateListModelMixin
from rest_framework import permissions
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from users.serializers.UserSerializer import UserSerializer
from users.serializers.UserCustomSerializers import (
OfferSerializer,
AthleteFileSerializer,
UserPutAthleteSerializer,
UserPutVisibilitySerializer,
UserGetSerializer,
)
from rest_framework.permissions import (
AllowAny,
IsAdminUser,
IsAuthenticated,
IsAuthenticatedOrReadOnly,
)
from users.models import Offer, AthleteFile, User
from users.models import User
from django.contrib.auth import get_user_model
from django.db.models import Q
from django.shortcuts import get_object_or_404
from rest_framework.parsers import MultiPartParser, FormParser
from users.permissions import IsCurrentUser, IsAthlete, IsCoach
from workouts.permissions import IsOwner, IsReadOnly
from users.permissions import IsCurrentUser
from workouts.permissions import IsReadOnly
# Create your views here.
......@@ -94,122 +80,6 @@ class UserDetail(
return self.partial_update(request, *args, **kwargs)
class OfferList(
mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView
):
permission_classes = [IsAuthenticatedOrReadOnly]
serializer_class = OfferSerializer
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
def get_queryset(self):
qs = Offer.objects.none()
result = Offer.objects.none()
if self.request.user:
qs = Offer.objects.filter(
Q(owner=self.request.user) | Q(recipient=self.request.user)
).distinct()
qp = self.request.query_params
u = self.request.user
# filtering by status (if provided)
s = qp.get("status", None)
if s is not None and self.request is not None:
qs = qs.filter(status=s)
if qp.get("status", None) is None:
qs = Offer.objects.filter(Q(owner=u)).distinct()
# filtering by category (sent or received)
c = qp.get("category", None)
if c is not None and qp is not None:
if c == "sent":
qs = qs.filter(owner=u)
elif c == "received":
qs = qs.filter(recipient=u)
return qs
else:
return result
class OfferDetail(
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView,
):
permission_classes = [IsAuthenticatedOrReadOnly]
queryset = Offer.objects.all()
serializer_class = OfferSerializer
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
def patch(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
class AthleteFileList(
mixins.ListModelMixin,
mixins.CreateModelMixin,
CreateListModelMixin,
generics.GenericAPIView,
):
queryset = AthleteFile.objects.all()
serializer_class = AthleteFileSerializer
permission_classes = [permissions.IsAuthenticated & (IsAthlete | IsCoach)]
parser_classes = [MultiPartParser, FormParser]
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
def get_queryset(self):
qs = AthleteFile.objects.none()
if self.request.user:
qs = AthleteFile.objects.filter(
Q(athlete=self.request.user) | Q(owner=self.request.user)
).distinct()
return qs
class AthleteFileDetail(
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView,
):
queryset = AthleteFile.objects.all()
serializer_class = AthleteFileSerializer
permission_classes = [permissions.IsAuthenticated & (IsAthlete | IsOwner)]
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
class UserSearch(APIView):
def post(self, request, *args, **kwargs):
substring = json.loads(request.body.decode("utf-8"))["username"]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment