Beispiel #1
0
  def test_RecipeSerializer_Update_ValidData(self):
    premade_recipe = self.createRecipe(self.account, self.data)
    recipe_data    = self.retrieveRecipeData()
    # Add another hop
    self.data['recipe_hops'] = list()
    self.data['recipe_hops'].append(dict(
      hop_name="Tettang",
      alpha_acid_content=8.8,
      beta_acid_content=6.4,
      add_time=3,
      add_time_unit="Days",
      dry_hops=True,
    ))

    # Change the malt
    self.data['recipe_malts'] = list()
    self.data['recipe_malts'].append(dict(
        malt_brand="Fruity_Tooty",
        malt_type="Crystal",
        malt_extract=False,
        amount_by_weight=7.0,
    ))

    # Update the notes
    self.data['recipe_notes'] = "Added this crystal to spice it up."

    serializer = RecipeSerializer(instance=premade_recipe, data=self.data)

    self.assertTrue(serializer.is_valid())

    updated_recipe = serializer.save()

    self.checkElement(self.data.pop('recipe_hops'), updated_recipe.recipe_hops.order_by("hop_name"))
    self.checkElement(self.data.pop('recipe_malts'), updated_recipe.recipe_malts.order_by("malt_brand"))
    self.checkElement(self.data, updated_recipe)
Beispiel #2
0
  def test_RecipeSerializer_Create_ValidData(self):
    serialized_data = RecipeSerializer(data=self.data)

    self.assertTrue(serialized_data.is_valid())

    recipe = serialized_data.save(user=self.account)
    self.checkElement(self.data.pop('recipe_hops'), recipe.recipe_hops.order_by("hop_name"))
    self.checkElement(self.data.pop('recipe_malts'), recipe.recipe_malts.order_by("malt_brand"))
    self.checkElement(self.data, recipe)
Beispiel #3
0
  def update(self, request, pk=None):
    """Update a specific recipe."""

    recipe = get_object_or_404(Recipe, pk=pk)

    # Apparently, the framework is JSON decoding things for me...
    serializer = RecipeSerializer(instance=recipe, data=request.data)

    if serializer.is_valid():
      updated_recipe = serializer.save()
      updated_serializer = RecipeSerializer(updated_recipe)
      return Response({
        'message': 'Recipe has been updated.',
        'recipe': updated_serializer.data,
      }, status=status.HTTP_201_CREATED)
    else:
      return Response({
        'status': 'Bad Request',
        'message': 'Recipe could not be updated with the received data.',
        'errors': serializer.errors
      }, status=status.HTTP_400_BAD_REQUEST)
Beispiel #4
0
  def create(self, request):
    """Create a new recipe"""
    recipe_data = request.data
    user = request.user

    incoming_serialized_data = RecipeSerializer(data=recipe_data)

    if incoming_serialized_data.is_valid():
      new_recipe = incoming_serialized_data.save(user=user)
      # Serialize the new recipe to return it as part of the return data.
      # TODO: Evaluate if there is any value to this action
      serialized_recipe = RecipeSerializer(new_recipe)
      return Response({
        'message': 'Recipe has been created.',
        'recipe': serialized_recipe.data,
      }, status=status.HTTP_201_CREATED)
    else:
      return Response({
        'status': 'Bad Request',
        'message': 'Recipe could not be created with the received data.',
        'errors': incoming_serialized_data.errors
      }, status=status.HTTP_400_BAD_REQUEST)