def _handle_post(self, request, *args, **kwargs):
        try:
            vendor_location = VendorLocation.objects.get(pk=kwargs.get('vendor_location_id'),
                                                         vendor__pk=kwargs.get('vendor_id'))
        except VendorLocation.DoesNotExist:
            self.raise_not_found()

        post_data = request.DATA
        post_data['vendor_location'] = vendor_location.pk
        serializer = MealSerializer(data=post_data)

        if post_data.get('available_starting', None) is not None:
            post_data['available_starting'] = datetime.datetime.strptime(post_data['available_starting'],
                                                                         '%m-%d-%Y').isoformat()

        if post_data.get('available_ending', None) is not None:
            post_data['available_ending'] = datetime.datetime.strptime(post_data['available_ending'],
                                                                       '%m-%d-%Y').isoformat()

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

            return serializer.data

        return self.raise_bad_request(serializer.errors)
Example #2
0
    def test_add_existing_collaboration_to_meal_fails(self):
        '''
        No new pending collaboration object should be created if the specified user is already a collaborator
        on the meal.

        Test steps:
        - create a meal
        - add an existing collaborator to the meal
        - send a patch request attempting to add the same collaborator to the meal
        - check that there is no new pending collaboration for that collaborator/meal combination
        '''
        client = APIClient()
        meal_data = MealSerializer.get_meal_data(self.new_meal_data)
        collaborators_data = MealSerializer.get_collaborators(
            self.new_meal_data)
        meal = Meal(**meal_data)
        meal.save()
        meal.collaborators.set(collaborators_data)
        meal_id = meal.pk
        meal_owner = meal.owner.username
        user = User.objects.get(username=meal_owner)
        client.force_authenticate(user=user)
        url = "/meals/%d/" % meal_id
        client.patch(url, {"collaborators": [2]}, format="json")
        collaborator = User.objects.get(id=2)
        new_collaboration_exists = collaborator.new_shared_meals.filter(
            meal=meal).exists()

        self.assertFalse(new_collaboration_exists)
    def test_filter_existing_collaborations_returns_true_if_no_collaboration_exists(self):
        new_meal = Meal(**self.new_meal_data)
        new_meal.save()
        collaborator = User.objects.get(id=self.pending_collaborators_data[0])
        include_in_list = MealSerializer.filter_existing_collaborations(collaborator, new_meal)

        self.assertTrue(include_in_list)
Example #4
0
class UserSerializer(serializers.ModelSerializer):
    meals = MealSerializer(many=True)
    recipes = RecipeSerializer(many=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'first_name', 'last_name', 'meals',
                  'recipes')
    def _handle_put(self, request, *args, **kwargs):
        meal = self.get_object(kwargs.get('pk'), kwargs.get('vendor_id'), kwargs.get('vendor_location_id'))

        post_data = request.DATA
        post_data['vendor_location'] = meal.vendor_location.pk

        if post_data.get('price', None) is None:
            post_data['price'] = meal.price

        serializer = MealSerializer(meal, data=post_data)

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

            return serializer.data

        return self.raise_bad_request(serializer.errors)
    def test_get_collaborators_returns_empty_list(self):
        test_data = {
            "otherdata": "this is more data"
        }

        result = MealSerializer.get_collaborators(test_data)

        self.assertEqual(result, [])
    def test_get_collaborators_returns_collaborators(self):
        test_data = {
            "collaborators": [1, 2, 3],
            "otherdata": "this is more data"
        }

        result = MealSerializer.get_collaborators(test_data)

        self.assertEqual(result, [1, 2, 3])
    def test_get_meal_data_returns_meal_without_collaborators(self):
        test_data = {
            "collaborators": [1, 2, 3],
            "otherdata": "this is more data"
        }

        result = MealSerializer.get_meal_data(test_data)

        self.assertEqual(result, {"otherdata": "this is more data"})
        self.assertNotIn("collaborators", result)
Example #9
0
class MealChoiceSerializer(serializers.ModelSerializer):
    day_plan = DayPlanSerializer()
    meal = MealSerializer()
    meal_choice = serializers.ChoiceField(required=False, choices=MealChoice.MEAL_CHOICES)

    class Meta:
        model = DayPlan
        fields = ('id', 'date', 'user', 'meals')
        depth = 1

    def create(self, validated_data):
        return super(MealChoiceSerializer, self).create(validated_data)
    def test_filter_existing_collaborations_returns_false_if_collaboration_exists(self):
        new_meal = Meal(**self.new_meal_data)
        new_meal.save()
        owner = User.objects.get(id=4)
        for collaborator in self.pending_collaborators_data:
            user = User.objects.get(id=collaborator)
            PendingCollaboration(meal=new_meal, collaborator=user, owner=owner).save()

        new_meal.collaborators.set(self.existing_collaborators_data)
        collaborator = User.objects.get(id=self.existing_collaborators_data[0])

        self.assertFalse(MealSerializer.filter_existing_collaborations(collaborator, new_meal))
    def test_create_pending_collaborations(self):
        user = User()
        user.save()
        collaborators = (pipe
                         | list
                         | (filter, lambda x: user is x)
                         | list
                         | (lambda col: col[0:2]))(User.objects.all())

        meal = Meal(name="test",
                    taste=1,
                    owner=user,
                    difficulty=5)
        meal.save()

        pending_collaborations = MealSerializer.create_pending_collaborations(collaborators, meal)
        for collaboration in pending_collaborations:
            with self.subTest(collaboration=collaboration):
                self.assertEqual(collaboration, meal)
Example #12
0
def show(request, id):

    if request.method == 'GET':
        meal = Meal.objects.get(id=id)
        serializer = MealSerializer(meal, many=True)
        return JsonResponse(serializer.data, safe=False)
Example #13
0
def index(request):

    if request.method == 'GET':
        meal_list = Meal.objects.all()
        serializer = MealSerializer(meal_list, many=True)
        return JsonResponse(serializer.data, safe=False)
Example #14
0
class AccountCartSerializer(serializers.ModelSerializer):
    meals = MealSerializer(many=True, read_only=True)

    class Meta:
        model = Account
        fields = ['id', 'email', 'meals']
class CategoryMealsSerializer(serializers.ModelSerializer):
    meals = MealSerializer(many=True, read_only=True)

    class Meta:
        model = Category
        fields = ['id', 'title', 'meals']
 def test_eater_is_not_validated(self):
     """
     Eater field is not checked to be Account instance.
     """
     serializer = MealSerializer(data=self.sample_meal)
     self.assertTrue(serializer.is_valid())