Example #1
0
def update_menu(request, pk):
    """
    修改指定菜单
    ---
        serializer: menu.serializers.MenuSerializer
        omit_serializer: false

        responseMessages:
            - code: 400
              message: Bad Request
            - code: 404
              message: Not Found
            - code: 401
              message: Not authenticated
    """
    try:
        menu = Menu.objects.get(pk=pk)
    except Menu.DoesNotExist:
        return Response('menu type not found', status=status.HTTP_404_NOT_FOUND)

    serializer = MenuSerializer(menu, data=request.DATA, partial=True)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #2
0
    def post(self, request, format=None):
        data = request.data.copy()
        data['user_id'] = request.user.id
        serializer = MenuSerializer(data=data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        #print('SE', serializer.errors)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #3
0
def menu_list(request, format=None):
    if request.method == 'GET':
        menu = Menu.objects.all()
        serializer = MenuSerializer(menu, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = MenuSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #4
0
class MenuSerializerTests(TestCase):
    def setUp(self):
        self.user = User.objects.create_user(username='******',
                                             email='*****@*****.**',
                                             password='******')
        self.menu = Menu.objects.create(name='Test Menu 1',
                                        description='Test menu description 1',
                                        author=self.user)
        self.dish_meat = Dish.objects.create(
            name='Test Meat Dish 1',
            description='Test meat description 1',
            price='10.50',
            prep_time=60,
            is_vegetarian=False,
            author=self.user)
        self.menu.dish.add(self.dish_meat)

        self.serializer_input = Menu.objects.all()
        self.serializer = MenuSerializer(data=self.serializer_input, many=True)
        self.serializer.is_valid()

    def test_contain_expected_fields(self):
        data = self.serializer.data[0]
        self.assertCountEqual(
            data.keys(),
            ['id', 'name', 'description', 'created_at', 'updated_at', 'dish'])

    def test_contain_expected_values(self):
        data = self.serializer.data[0]
        self.assertEqual(data['id'], self.menu.id)
        self.assertEqual(data['name'], self.menu.name)
        self.assertEqual(data['description'], self.menu.description)
        self.assertEqual(data['created_at'],
                         datetime.strftime(self.menu.created_at, "%Y-%m-%d"))
        self.assertEqual(data['updated_at'],
                         datetime.strftime(self.menu.updated_at, "%Y-%m-%d"))
        self.assertTrue(self.menu.dish.first().id in data['dish'])
Example #5
0
def menu_detail(request, pk, format=None):
    try:
        menu = Menu.objects.get(pk=pk)
    except Menu.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = MenuSerializer(menu)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = MenuSerializer(menu, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        menu.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Example #6
0
def create_menu(request):
    """
    创建菜单
    ---
        serializer: menu.serializers.MenuSerializer
        omit_serializer: false

        responseMessages:
            - code: 201
              message: Created
            - code: 400
              message: Bad Request
            - code: 401
              message: Not authenticated
    """
    data = request.DATA.copy()
    serializer = MenuSerializer(data=data)
    if serializer.is_valid():
        menu = serializer.save()
        menu.restaurant = request.current_restaurant
        menu.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #7
0
def create_menu(request):
    """
    创建菜单
    ---
        serializer: menu.serializers.MenuSerializer
        omit_serializer: false

        responseMessages:
            - code: 201
              message: Created
            - code: 400
              message: Bad Request
            - code: 401
              message: Not authenticated
    """
    data = request.DATA.copy()
    serializer = MenuSerializer(data=data)
    if serializer.is_valid():
        menu = serializer.save()
        menu.restaurant = request.current_restaurant
        menu.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)