Skip to content

Commit

Permalink
reorganizing some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
emoltz-carnegie committed Jul 23, 2024
1 parent 590dc1e commit 23343bd
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions api/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import Optional

import requests
from django.contrib.auth.models import User
from rest_framework import status
Expand All @@ -20,28 +19,6 @@
from api.serializers import FoodSerializer, MealSerializer, CreateUserSerializer


class InvalidMealType(APIException):
status_code = 400
default_detail = "Invalid meal type"
default_code = 'invalid_meal_type'


class ErrorMessage(APIException):
status_code = 400
default_detail = "Error processing request."
default_code = 'error'


class UserExists(APIView):
def get(self, request, *args, **kwargs):
print("Checking if user exists...")
user_id: str = self.kwargs.get('user_id')
if not user_id:
return Response({'message': 'Please provide a user_id'}, status=status.HTTP_400_BAD_REQUEST)
if User.objects.filter(username=user_id).exists():
return Response({'exists': True}, status=status.HTTP_200_OK)
return Response({'exists': False}, status=status.HTTP_200_OK)

class SaveFood(APIView):
permission_classes = [IsAuthenticated]

Expand Down Expand Up @@ -95,7 +72,6 @@ def post(self, request):
name = request.data.get("name")
image = request.data.get("image", None)


temperature = 0.1

json_format = """
Expand Down Expand Up @@ -129,8 +105,6 @@ def post(self, request):

response = json.loads(response)



# serialize into database
# add extra properties
if image:
Expand Down Expand Up @@ -166,7 +140,6 @@ def get(request):
food_serializer = FoodSerializer(all_foods, many=True)
return Response(food_serializer.data)


@staticmethod
def post(request):
user = request.user
Expand Down Expand Up @@ -304,6 +277,20 @@ def get(self, request, *args, **kwargs):
return Response({'message': 'User does not exist.'}, status=status.HTTP_404_NOT_FOUND)



# AUTH
class UserExists(APIView):
def get(self, request, *args, **kwargs):
print("Checking if user exists...")
user_id: str = self.kwargs.get('user_id')
if not user_id:
return Response({'message': 'Please provide a user_id'}, status=status.HTTP_400_BAD_REQUEST)
if User.objects.filter(username=user_id).exists():
return Response({'exists': True}, status=status.HTTP_200_OK)
return Response({'exists': False}, status=status.HTTP_200_OK)



class Apple_CreateAccount(APIView):
@staticmethod
def post(request):
Expand Down Expand Up @@ -337,6 +324,7 @@ def post(request):
return Response({'error': 'Apple User already exists.'}, status=status.HTTP_400_BAD_REQUEST)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class VerifyAppleToken(APIView):
def post(self, request, *args, **kwargs):
user_id = request.data.get('user_id')
Expand Down Expand Up @@ -375,4 +363,16 @@ def post(self, request, *args, **kwargs):
if not hasattr(user, 'auth_token'):
Token.objects.create(user=user)

return Response({'token': user.auth_token.key}, status=status.HTTP_200_OK)
return Response({'token': user.auth_token.key}, status=status.HTTP_200_OK)


class InvalidMealType(APIException):
status_code = 400
default_detail = "Invalid meal type"
default_code = 'invalid_meal_type'


class ErrorMessage(APIException):
status_code = 400
default_detail = "Error processing request."
default_code = 'error'

0 comments on commit 23343bd

Please sign in to comment.