Skip to content

Instantly share code, notes, and snippets.

@jonathanmach
Created February 11, 2020 14:37
Show Gist options
  • Save jonathanmach/f9cd7f4459b8a624c621276aa2aa065c to your computer and use it in GitHub Desktop.
Save jonathanmach/f9cd7f4459b8a624c621276aa2aa065c to your computer and use it in GitHub Desktop.
from django.shortcuts import render
from .models import Habit, Category
from .serializers import HabitSerializer, NewHabitSerializer, SectionSerializer
from rest_framework import generics
from rest_framework.response import Response
from rest_framework.decorators import api_view
from rest_framework import status
from django.utils import dateparse
class HabitViewSet(generics.ListAPIView):
"""
API endpoint that returns all Habit objects from the database
"""
queryset = Habit.objects.all()
serializer_class = HabitSerializer
def get_all_data(request):
habits = Habit.objects.all()
habit_list = HabitSerializer(habits, many=True).data
return habit_list
@api_view(['POST', 'PATCH', 'DELETE', 'GET'])
def habit_handler(request, habit_id=None):
response = None
if request.method == 'GET':
response = get_all_data(request)
elif request.method == 'POST':
response = insert_habit(request)
elif request.method == 'PATCH':
response = update_habit_log(request, habit_id)
elif request.method == 'DELETE':
response = delete_habit_log(request, habit_id)
return Response(response, status=status.HTTP_200_OK)
@api_view(['POST', 'PATCH', 'DELETE', 'GET'])
def section_handler(request, section_id=None):
response = None
if request.method == 'GET':
response = get_all_data(request)
elif request.method == 'POST':
response = insert_section(request)
# elif request.method == 'PATCH':
# response = update_habit_log(request, habit_id)
# elif request.method == 'DELETE':
# response = delete_habit_log(request, habit_id)
return Response(response, status=status.HTTP_200_OK)
def insert_section(request):
section = Category(title=request.data["title"])
section.save()
return SectionSerializer(section).data
def insert_habit(request):
habit = NewHabitSerializer(data=request.data)
habit.is_valid()
print(habit.validated_data)
habit.save()
return habit.data
@api_view(['PATCH', ])
def update_habit_sorting(request, section_id):
new_sorting = request.data
section = Category.objects.get(pk=section_id)
section.habit_order = new_sorting
section.save()
# @todo update response
return Response("Ok", status=status.HTTP_200_OK)
@api_view(['POST', 'PATCH', 'DELETE'])
def post_habit_log(request, habit_id):
response = None
if request.method == 'POST':
response = insert_log(request, habit_id)
elif request.method == 'PATCH':
response = update_habit_log(request, habit_id)
elif request.method == 'DELETE':
response = delete_habit_log(request, habit_id)
return Response(response, status=status.HTTP_200_OK)
def insert_log(request, habit_id):
# @todo: Create log serializer
date = request.data['date']
log_id = request.data['id']
value = request.data['value']
# Create log object
log = {"date": date, "id": log_id, "value": value}
habit = Habit.objects.get(id=habit_id)
if habit.logs:
# If habit.logs contain existing logs: append
habit.logs.append(log)
else:
habit.logs = [log]
habit.save()
return log
def update_habit_log(request, habit_id):
# @todo: Create log serializer
log_id = request.data['id']
value = request.data['value']
habit = Habit.objects.get(pk=habit_id)
# habit.logs returns an array
index, log = [(idx, log) for idx, log in enumerate(
habit.logs) if log['id'] == log_id][0]
log['value'] = value
habit.logs[index] = log
habit.save()
return log
def delete_habit_log(request, habit_id):
log_id = request.data['id']
habit = Habit.objects.get(pk=habit_id)
# habit.logs returns an array
index, log = [(idx, log) for idx, log in enumerate(
habit.logs) if log['id'] == log_id][0]
del habit.logs[index]
habit.save()
return 'Ok'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment