Exemple #1
0
    def image(self, request, pk):
        obj = self.get_object()

        if obj.get_space() != request.space:
            raise PermissionDenied(
                detail=
                'You do not have the required permission to perform this action',
                code=403)

        serializer = self.serializer_class(obj,
                                           data=request.data,
                                           partial=True)

        if self.request.space.demo:
            raise PermissionDenied(detail='Not available in demo', code=None)

        if serializer.is_valid():
            serializer.save()

            if serializer.validated_data == {}:
                obj.image = None
            else:
                img, filetype = handle_image(request, obj.image)
                obj.image = File(img,
                                 name=f'{uuid.uuid4()}_{obj.pk}{filetype}')
            obj.save()

            return Response(serializer.data)
        return Response(serializer.errors, 400)
Exemple #2
0
 def import_recipe_image(self, recipe, image_file, filetype='.jpeg'):
     """
     Adds an image to a recipe naming it correctly
     :param recipe: Recipe object
     :param image_file: ByteIO stream containing the image
     :param filetype: type of file to write bytes to, default to .jpeg if unknown
     """
     recipe.image = File(handle_image(self.request,
                                      File(image_file, name='image'),
                                      filetype=filetype)[0],
                         name=f'{uuid.uuid4()}_{recipe.pk}{filetype}')
     recipe.save()
Exemple #3
0
                    ingredient.no_amount = True
                    pass
            elif isinstance(ing['amount'], float) \
                    or isinstance(ing['amount'], int):
                ingredient.amount = ing['amount']
            ingredient.note = ing['note'].strip() if 'note' in ing else ''

            ingredient.save()
            step.ingredients.add(ingredient)

        if 'image' in data and data['image'] != '' and data[
                'image'] is not None:
            try:
                response = requests.get(data['image'])

                img, filetype = handle_image(
                    request, File(BytesIO(response.content), name='image'))
                recipe.image = File(
                    img, name=f'{uuid.uuid4()}_{recipe.pk}{filetype}')
                recipe.save()
            except UnidentifiedImageError as e:
                print(e)
                pass
            except MissingSchema as e:
                print(e)
                pass
            except Exception as e:
                print(e)
                pass

        return HttpResponse(reverse('view_recipe', args=[recipe.pk]))
Exemple #4
0
                    pass
            elif isinstance(ing['amount'], float) \
                    or isinstance(ing['amount'], int):
                ingredient.amount = ing['amount']
            ingredient.note = ing['note'].strip() if 'note' in ing else ''

            ingredient.save()
            step.ingredients.add(ingredient)
            print(ingredient)

        if 'image' in data and data['image'] != '' and data[
                'image'] is not None:
            try:
                response = requests.get(data['image'])

                img, filetype = handle_image(request,
                                             BytesIO(response.content))
                recipe.image = File(
                    img, name=f'{uuid.uuid4()}_{recipe.pk}{filetype}')
                recipe.save()
            except UnidentifiedImageError as e:
                print(e)
                pass
            except MissingSchema as e:
                print(e)
                pass
            except Exception as e:
                print(e)
                pass

        return HttpResponse(reverse('view_recipe', args=[recipe.pk]))