Commit c0ca65f4 authored by Christopher Collin Løkken's avatar Christopher Collin Løkken 🛀
Browse files

Merge branch 'urlrefactor' into 'master'

Refactored url config

See merge request !4
parents 73e26036 2474c53e
Pipeline #171208 passed with stages
in 2 minutes and 32 seconds
backend/secfit/.vscode/
backend/secfit/*/migrations/__pycache__/
backend/secfit/*/__pycache__/
backend/secfit/**/__pycache__/**
backend/secfit/db.sqlite3
venv/
backend/secfit/.coverage
.vscode
\ No newline at end of file
from calendar import c
from django.urls import path, include
from comments.models import Comment, Like
from comments.views import CommentList, CommentDetail, LikeList, LikeDetail
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
path("api/comments/", CommentList.as_view(), name="comment-list"),
likes_url_patterns = [
path("", LikeList.as_view(), name="like-list"),
path(
"<int:pk>/",
LikeDetail.as_view(),
name="like-detail"),
]
comments_url_patterns = [
path("", CommentList.as_view(), name="comment-list"),
path(
"api/comments/<int:pk>/",
"<int:pk>/",
CommentDetail.as_view(),
name="comment-detail"),
path("api/likes/", LikeList.as_view(), name="like-list"),
path("api/likes/<int:pk>/", LikeDetail.as_view(), name="like-detail"),
]
urlpatterns = [
path("comments/", include(comments_url_patterns)),
path("likes/", include(likes_url_patterns)),
]
from django.urls import path, include
from meals import views
from rest_framework.urlpatterns import format_suffix_patterns
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
)
# This is messy and should be refactored
meal_url_patterns = [
path(
"",
views.MealList.as_view(),
name="meal-list"),
path(
"<int:pk>/",
views.MealDetail.as_view(),
name="meal-detail",
),
]
meal_files_url_patterns = [
path(
"",
views.MealFileList.as_view(),
name="meal-file-list",
),
path(
"<int:pk>/",
views.MealFileDetail.as_view(),
name="mealfile-detail",
),
]
urlpatterns = format_suffix_patterns(
[
path("", views.api_root),
path("api/meals/", views.MealList.as_view(), name="meal-list"),
path(
"api/meals/<int:pk>/",
views.MealDetail.as_view(),
name="meal-detail",
),
path(
"api/meal-files/",
views.MealFileList.as_view(),
name="meal-file-list",
),
path(
"api/meal-files/<int:pk>/",
views.MealFileDetail.as_view(),
name="mealfile-detail",
),
path("", include("users.urls")),
path("meals/", include(meal_url_patterns)),
path("meal-files/", include(meal_files_url_patterns)),
]
)
......@@ -31,19 +31,6 @@ import pickle
from django.core.signing import Signer
@api_view(["GET"])
def api_root(request, format=None):
return Response(
{
"users": reverse("user-list", request=request, format=format),
"meals": reverse("meal-list", request=request, format=format),
"meal-files": reverse(
"meal-file-list", request=request, format=format
),
}
)
class MealList(
mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView
):
......
......@@ -17,12 +17,42 @@ from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
)
import workouts.views
from secfit import views
urlpatterns = [
path("admin/", admin.site.urls),
api_url_patterns = [
path("", include("workouts.urls")),
path("", include("users.urls")),
path("", include("comments.urls")),
path("", include("meals.urls")),
]
token_url_patterns = [
path(
"",
TokenObtainPairView.as_view(),
name="token_obtain_pair"),
path(
"refresh/",
TokenRefreshView.as_view(),
name="token_refresh"),
]
urlpatterns = [
path("", views.api_root),
path("admin/", admin.site.urls),
path("api/", include(api_url_patterns)),
path("auth/", include("rest_framework.urls")),
path("token/", include(token_url_patterns)),
path(
"remember_me/",
workouts.views.RememberMe.as_view(),
name="remember_me"),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.reverse import reverse
@api_view(["GET"])
def api_root(request, format=None):
return Response(
{
"users": reverse("user-list", request=request, format=format),
"meals": reverse("meal-list", request=request, format=format),
"meal-files": reverse(
"meal-file-list", request=request, format=format
),
"workouts": reverse("workout-list", request=request, format=format),
"exercises": reverse("exercise-list", request=request, format=format),
"exercise-instances": reverse(
"exercise-instance-list", request=request, format=format
),
"workout-files": reverse(
"workout-file-list", request=request, format=format
),
"comments": reverse("comment-list", request=request, format=format),
"likes": reverse("like-list", request=request, format=format),
}
)
from django.urls import path, include
from users import views
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
path("api/usersearch/", views.UserSearch.as_view(), name="user-search"),
path(
"api/uservisibility/<int:pk>/",
views.UserVisibility.as_view(),
name="user-visibility"),
path("api/users/", views.UserList.as_view(), name="user-list"),
users_url_patterns = [
path("", views.UserList.as_view(), name="user-list"),
path(
"api/users/<int:pk>/",
"<int:pk>/",
views.UserDetail.as_view(),
name="user-detail"),
path(
"api/users/<str:username>/",
"<str:username>/",
views.UserDetail.as_view(),
name="user-detail"),
path("api/offers/", views.OfferList.as_view(), name="offer-list"),
]
offers_url_patterns = [
path(
"api/offers/<int:pk>/",
"",
views.OfferList.as_view(),
name="offer-list"),
path(
"<int:pk>/",
views.OfferDetail.as_view(),
name="offer-detail"),
]
athlete_files_url_patterns = [
path(
"api/athlete-files/", views.AthleteFileList.as_view(), name="athlete-file-list"
"",
views.AthleteFileList.as_view(),
name="athlete-file-list"
),
path(
"api/athlete-files/<int:pk>/",
"<int:pk>/",
views.AthleteFileDetail.as_view(),
name="athletefile-detail",
),
]
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(
"uservisibility/<int:pk>/",
views.UserVisibility.as_view(),
name="user-visibility"),
]
from django.urls import path, include
from workouts import views
from rest_framework.urlpatterns import format_suffix_patterns
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
)
# This is a bit messy and will need to change
workouts_url_patterns = [
path(
"",
views.WorkoutList.as_view(),
name="workout-list"),
path(
"<int:pk>/",
views.WorkoutDetail.as_view(),
name="workout-detail",
),
]
exercises_url_patterns = [
path(
"",
views.ExerciseList.as_view(),
name="exercise-list"),
path(
"<int:pk>/",
views.ExerciseDetail.as_view(),
name="exercise-detail",
),
]
exercise_instances_url_patterns = [
path(
"",
views.ExerciseInstanceList.as_view(),
name="exercise-instance-list",
),
path(
"int:pk>/",
views.ExerciseInstanceDetail.as_view(),
name="exerciseinstance-detail",
),
]
workout_files_url_patterns = [
path(
"",
views.WorkoutFileList.as_view(),
name="workout-file-list",
),
path(
"<int:pk>/",
views.WorkoutFileDetail.as_view(),
name="workoutfile-detail",
),
]
urlpatterns = format_suffix_patterns(
[
path("", views.api_root),
path(
"api/workouts/",
views.WorkoutList.as_view(),
name="workout-list"),
path(
"api/workouts/<int:pk>/",
views.WorkoutDetail.as_view(),
name="workout-detail",
),
path(
"api/exercises/",
views.ExerciseList.as_view(),
name="exercise-list"),
path(
"api/exercises/<int:pk>/",
views.ExerciseDetail.as_view(),
name="exercise-detail",
),
path(
"api/exercise-instances/",
views.ExerciseInstanceList.as_view(),
name="exercise-instance-list",
),
path(
"api/exercise-instances/<int:pk>/",
views.ExerciseInstanceDetail.as_view(),
name="exerciseinstance-detail",
),
path(
"api/workout-files/",
views.WorkoutFileList.as_view(),
name="workout-file-list",
),
path(
"api/workout-files/<int:pk>/",
views.WorkoutFileDetail.as_view(),
name="workoutfile-detail",
),
path("", include("users.urls")),
path("", include("comments.urls")),
path("api/auth/", include("rest_framework.urls")),
path(
"api/token/",
TokenObtainPairView.as_view(),
name="token_obtain_pair"),
path(
"api/token/refresh/",
TokenRefreshView.as_view(),
name="token_refresh"),
path(
"api/remember_me/",
views.RememberMe.as_view(),
name="remember_me"),
path("workouts/", include(workouts_url_patterns)),
path("exercises/", include(exercises_url_patterns)),
path("exercise-instances/", include(exercise_instances_url_patterns)),
path("workout-files/", include(workout_files_url_patterns)),
]
)
"""Contains views for the workouts application. These are mostly class-based views.
"""
import base64
import pickle
from collections import namedtuple
from rest_framework import generics, mixins
from rest_framework import permissions
from rest_framework.parsers import (
JSONParser,
)
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.reverse import reverse
from django.db.models import Q
from rest_framework import filters
from rest_framework_simplejwt.tokens import RefreshToken
from django.db.models import Q
from django.core.exceptions import PermissionDenied
from django.core.signing import Signer
from workouts.parsers import MultipartJsonParser
from workouts.permissions import (
IsOwner,
......@@ -26,33 +30,6 @@ from workouts.models import Workout, Exercise, ExerciseInstance, WorkoutFile
from workouts.serializers import WorkoutSerializer, ExerciseSerializer
from workouts.serializers import RememberMeSerializer
from workouts.serializers import ExerciseInstanceSerializer, WorkoutFileSerializer
from django.core.exceptions import PermissionDenied
from rest_framework_simplejwt.tokens import RefreshToken
from rest_framework.response import Response
import json
from collections import namedtuple
import base64
import pickle
from django.core.signing import Signer
@api_view(["GET"])
def api_root(request, format=None):
return Response(
{
"users": reverse("user-list", request=request, format=format),
"workouts": reverse("workout-list", request=request, format=format),
"exercises": reverse("exercise-list", request=request, format=format),
"exercise-instances": reverse(
"exercise-instance-list", request=request, format=format
),
"workout-files": reverse(
"workout-file-list", request=request, format=format
),
"comments": reverse("comment-list", request=request, format=format),
"likes": reverse("like-list", request=request, format=format),
}
)
# Allow users to save a persistent session in their browser
......@@ -66,16 +43,16 @@ class RememberMe(
serializer_class = RememberMeSerializer
def get(self, request):
if request.user.is_authenticated == False:
if not request.user.is_authenticated:
raise PermissionDenied
else:
return Response({"remember_me": self.rememberme()})
def post(self, request):
cookieObject = namedtuple("Cookies", request.COOKIES.keys())(
cookie_object = namedtuple("Cookies", request.COOKIES.keys())(
*request.COOKIES.values()
)
user = self.get_user(cookieObject)
user = self.get_user(cookie_object)
refresh = RefreshToken.for_user(user)
return Response(
{
......@@ -84,8 +61,8 @@ class RememberMe(
}
)
def get_user(self, cookieObject):
decode = base64.b64decode(cookieObject.remember_me)
def get_user(self, cookie_object):
decode = base64.b64decode(cookie_object.remember_me)
user, sign = pickle.loads(decode)
# Validate signature
......@@ -103,7 +80,9 @@ class RememberMe(
class WorkoutList(
mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView
mixins.ListModelMixin,
mixins.CreateModelMixin,
generics.GenericAPIView
):
"""Class defining the web response for the creation of a Workout, or displaying a list
of Workouts
......@@ -176,7 +155,9 @@ class WorkoutDetail(
class ExerciseList(
mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView
mixins.ListModelMixin,
mixins.CreateModelMixin,
generics.GenericAPIView
):
"""Class defining the web response for the creation of an Exercise, or
a list of Exercises.
......
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