Commit e1f594e2 authored by Floréal Cabanettes's avatar Floréal Cabanettes

Add api, to be continued

parent 1fc9cf47
from django.contrib import admin
# Register your models here.
from django.db import models
# Create your models here.
from rest_framework import serializers
from main.models import Recipe
from main.controllers import CRecipe
class RecipeSerializer(serializers.Serializer):
ingredients = serializers.ListField()
equipment = serializers.ListField()
instructions = serializers.ListField()
proposals = serializers.ListField()
categories = serializers.ListField()
def create(self, validated_data):
return CRecipe.add_new(**validated_data)
def update(self, va):
return True
from django.test import TestCase
# Create your tests here.
from django.conf.urls import url
from rest_framework.urlpatterns import format_suffix_patterns
from api import views
from django.conf.urls import include
urlpatterns = [
url(r'^$', views.RecipeList.as_view()),
url(r'^auth/', include('rest_framework.urls',
urlpatterns = format_suffix_patterns(urlpatterns)
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework import permissions
from rest_framework.parsers import MultiPartParser, FormParser
from api.serializers import RecipeSerializer
from main.controllers import CRecipe
# Create your views here.
class RecipeList(APIView):
List all recipes of the user, or create a new one
permission_classes = (permissions.IsAuthenticated,)
parser_classes = (MultiPartParser, FormParser)
def get(self, request):
return Response(CRecipe.get_author_recipes_data(request.user))
def post(self, request):
serializer = RecipeSerializer(
if serializer.is_valid():
return Response(, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
from pywebcooking.settings import STATIC_URL
from main.models import Recipe, Category
from main.models import Recipe, Category, IngredientInGroup
from django.contrib.auth.models import User
from main.functions.exceptions import RequiredParameterException, BadParameterException
......@@ -111,6 +111,81 @@ class CRecipe:
return r
def get_author_recipes_data(author: User) -> list:
recipes = Recipe.objects.filter(author=author);
recipes_data = []
for recipe in recipes:
return recipes_data
def get_recipe_data_from_id(id_recipe: int) -> dict:
return CRecipe.get_recipe_data(Recipe.objects.get(id=id_recipe))
def get_recipe_data(recipe: Recipe) -> dict:
get data of a recipe
:param recipe: the recipe to get the data
:return: data of the recipe
data = {"author":}
vars_recipe = vars(recipe)
for key in vars_recipe:
if not key.startswith("_") and key != "author_id":
data[key] = vars_recipe[key]
categories = []
for cat in recipe.category.all():
data["category"] = categories
ingredients = []
ingredients_groups = recipe.ingredientgroup_set
for ingredients_group in ingredients_groups.all():
ig = {
"title": ingredients_group.title,
"nb": ingredients_group.nb,
"level": ingredients_group.level,
"ingredients": []
ingredients_in_group = IngredientInGroup.objects.filter(ingredientGroup=ingredients_group)
for iig in ingredients_in_group:
"quantity": iig.quantity,
"unit": iig.unit,
"nb": iig.nb
data["ingredients"] = ingredients
equipments = []
for eq in recipe.equipmentinrecipe_set.all():
equipment = {
"quantity": eq.quantity,
"nb": eq.nb
data["equipments"] = equipments
instructions = []
for instr in recipe.instruction_set.all():
instruction = {
"text_inst": instr.text_inst,
"nb": instr.nb,
"level": instr.level
data["instructions"] = instruction
proposals = []
for prop in recipe.proposal_set.all():
proposal = {
"text_cons": prop.text_cons,
"nb": prop.nb
data["proposals"] = proposals
return data
def add_categories(recipe: Recipe, categories: "list of Categories"):
# Check parameters:
......@@ -39,7 +39,9 @@ INSTALLED_APPS = (
......@@ -17,5 +17,6 @@ from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(,
url(r'^api/', include('api.urls')),
url(r'^', include('website.urls'))
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment