Ejemplo n.º 1
0
 def post(self, request):
     # 创建商店
     owner = request.u
     name = request.json.get("name")
     address = request.json.get("address")
     announcement = request.json.get("announcement")
     description = request.json.get("description")
     phone = request.json.get("phone")
     image_ids = request.json.get("image_ids")
     if not all([owner, name, address, announcement, description]):
         return JsonErrorResponse("owner, name, address, announcement, description, phone are needed", 400)
     new_store = Store(
         name=name,
         address=address,
         announcement=announcement,
         description=description,
         phone=phone,
         image_ids=image_ids,
         owner=owner
     )
     try:
         new_store.save()
     except Exception, e:
         print e
         return JsonErrorResponse("Fail:" + e.message)
Ejemplo n.º 2
0
 def get(self, request):
     # 获取食品列表
     store_id = request.param.get("store_id", None)
     if not store_id:
         return JsonErrorResponse("You must provide a store_id", 400)
     store = Store.objects.filter(id=store_id).first()
     if not store:
         return JsonErrorResponse("Store not existed", 404)
     foods = [food.to_string() for food in store.foods.all()]
     return JsonResponse({"food_list": foods})
Ejemplo n.º 3
0
    def post(self, request):
        # 添加订单
        try:
            customer = request.u
            note = request.json.get("note")
            delivery_information_id = request.json.get(
                "delivery_information_id")
            store_id = request.json.get("store_id")
            food_list = request.json.get("food_list")
            store = Store.objects.get(id=store_id)
            total_price = 0

            if not all([food_list, delivery_information_id, store]):
                return JsonErrorResponse(
                    "food_list, delivery_information_id, store_id are needed",
                    400)

            # 检查food_list
            assert isinstance(
                food_list,
                list) and len(food_list) > 0, "food_list format wrong"
            # 检查库存+计算价格
            for order_food in food_list:
                food = store.foods.get(id=order_food['food_id'])
                food_count = int(order_food['count'])
                total_price += food.price * food_count
                assert food.stock > food_count, "food stock is not enough"
            # 检查收货信息
            delivery_information = customer.delivery_informations.get(
                id=delivery_information_id)
            # 检查账户类型
            assert request.account_type == "customer", "only customer can make order"

            # 创建order
            new_order = Order(note=note,
                              total_price=total_price,
                              customer=customer,
                              store=store,
                              delivery_information=delivery_information)
            new_order.save()

            # 减少库存,创建order_food
            for order_food in food_list:
                food = store.foods.get(id=order_food['food_id'])
                food_count = int(order_food['count'])
                new_stock = food.stock - food_count
                store.foods.filter(id=order_food['food_id']).update(
                    stock=new_stock)
                OrderFood(count=food_count, food=food, order=new_order).save()

        except Exception, e:
            print e
            return JsonErrorResponse("Fail:" + e.message)
Ejemplo n.º 4
0
def file_uploader(request):
    try:
        if not request.u:
            JsonErrorResponse("AUTH FAIL", status.HTTP_401_UNAUTHORIZED)
        data = request.FILES.get("image")
        file_content = ContentFile(data.read())
        img = ImageStore(name=data.name, img=data)
        img.save()
        return JsonResponse({"id": img.id}, status.HTTP_200_OK)
    except:
        pass
    return JsonErrorResponse("AUTH FAIL", status.HTTP_401_UNAUTHORIZED)
Ejemplo n.º 5
0
 def get(self, request):
     # 获取订单列表
     owner = request.u
     account_type = request.account_type
     if not owner:
         return JsonErrorResponse("can't find user", 404)
     if account_type != "customer" and account_type != "bussiness":
         return JsonErrorResponse("wrong account type", 403)
     if account_type == "customer":
         orders = [order.to_string() for order in owner.orders.all()]
     else:
         orders = [order.to_string() for order in owner.store.orders.all()]
     return JsonResponse({"order_list": orders})
Ejemplo n.º 6
0
def login(request, account_type):
    try:
        if request.method == 'POST':
            data = request.json
            username = data.get('username')
            password = data.get('password')
            if account_type not in USER_MODEL_MAP:
                return JsonErrorResponse("account_type must be admin, customer or bussiness")
            token = get_token(username, password, account_type)
            if token:
                return JsonResponse({"token": token}, status.HTTP_200_OK)
    except:
        pass
    return JsonErrorResponse("AUTH FAIL", status.HTTP_401_UNAUTHORIZED)
Ejemplo n.º 7
0
 def get(self, request, delivery_information_id):
     try:
         delivery_information = DeliveryInformation.objects.get(
             id=delivery_information_id)
     except DeliveryInformation.DoesNotExist:
         return JsonErrorResponse("DeliveryInformation does not exist", 404)
     return JsonResponse(
         {"delivery_information": delivery_information.to_detail_string()})
Ejemplo n.º 8
0
 def put(self, request, seller_id):
     # 更新个人信息
     update_item = ['nickname', 'password']
     update_dict = get_update_dict_by_list(update_item, request.json)
     modify_num = Seller.objects.filter(id=seller_id).update(**update_dict)
     if modify_num == 1:
         return JsonResponse({})
     return JsonErrorResponse("Update failed", 400)
Ejemplo n.º 9
0
 def post(self, request):
     # 注册
     username = request.json.get("username")
     password = request.json.get("password")
     nickname = request.json.get("nickname")
     account_type = request.json.get("account_type")
     if not all([username, password, nickname, account_type]):
         return JsonErrorResponse(
             "username, password, nickname, account_type are needed", 400)
     new_admin = Admin(username=username,
                       password=password,
                       nickname=nickname,
                       account_type=account_type)
     try:
         new_admin.save()
     except Exception, e:
         print e
         return JsonErrorResponse("Fail" + e.message)
Ejemplo n.º 10
0
 def delete(self, request, delivery_information_id):
     # 删除食品
     try:
         result = DeliveryInformation.objects.get(
             id=delivery_information_id).delete()
         assert result[0] == 1
         return JsonResponse({})
     except Exception, e:
         return JsonErrorResponse("Delete failed:" + e.message, 400)
Ejemplo n.º 11
0
 def post(self, request):
     # 添加投诉
     try:
         owner = request.u
         content = request.json.get("content")
         store_id = request.json.get("store_id")
         store = Store.objects.get(id=store_id)
         if not all([content, store]):
             return JsonErrorResponse("content, store_id are needed", 400)
         new_complaint = Complaint(
             content=content,
             store=store,
             customer=owner,
         )
         new_complaint.save()
     except Exception, e:
         print e
         return JsonErrorResponse("Fail:" + e.message)
Ejemplo n.º 12
0
 def delete(self, request, food_id):
     # 删除食品
     try:
         # owner = request.u
         # store = owner.store
         # modify_num = store.foods.filter(id=food_id).update(**update_dict)
         result = Food.objects.get(id=food_id).delete()
         assert result[0] == 1
         return JsonResponse({})
     except Exception, e:
         return JsonErrorResponse("Delete failed:" + e.message, 400)
Ejemplo n.º 13
0
 def post(self, request):
     # 添加收货信息
     owner = request.u
     address = request.json.get("address")
     phone = request.json.get("phone")
     receiver = request.json.get("receiver")
     if not all([address, phone, receiver]):
         return JsonErrorResponse("address, phone, receiver are needed",
                                  400)
     new_delivery_information = DeliveryInformation(
         address=address,
         phone=phone,
         receiver=receiver,
         customer=owner,
     )
     try:
         new_delivery_information.save()
     except Exception, e:
         print e
         return JsonErrorResponse("Fail:" + e.message)
Ejemplo n.º 14
0
 def get(self, request):
     # 获取收货信息列表
     customer = request.u
     if not customer:
         return JsonErrorResponse("can't find customer", 404)
     delivery_informations = [
         delivery_information.to_string()
         for delivery_information in customer.delivery_informations.all()
     ]
     return JsonResponse(
         {"delivery_information_list": delivery_informations})
Ejemplo n.º 15
0
 def put(self, request, delivery_information_id):
     # 更新收货信息
     try:
         owner = request.u
         update_item = ['address', 'phone', 'receiver']
         update_dict = get_update_dict_by_list(update_item, request.json)
         modify_num = owner.delivery_informations.filter(
             id=delivery_information_id).update(**update_dict)
         assert modify_num == 1
         return JsonResponse({})
     except Exception, e:
         return JsonErrorResponse("Update failed:" + e.message, 400)
Ejemplo n.º 16
0
    def put(self, request, order_id):
        # 更新订单
        try:
            action = request.json.get("action")
            order = Order.objects.get(id=order_id)
            status = order.status
            if request.account_type == "customer":
                action_to_func = {"finish": order.finish}
            else:
                action_to_func = {
                    "accept": order.accept,
                    "transport": order.transport,
                    "close": order.close,
                }
            if action not in action_to_func:
                return JsonErrorResponse("fail to action on order", 400)
            action_to_func[action]()
            # 评论商品和订单 只允许一次
            if action == "finish" and status == "3":
                order_foods = order.order_foods.all()
                order_foods_food = [i.food for i in order_foods]
                food_review_list = request.json.get("food_review_list", [])

                # 商品评论
                data = {}
                data['customer'] = request.u
                data['order'] = order
                for food_review in food_review_list:
                    try:
                        food = Food.objects.get(id=food_review.get("food_id"))
                        assert food in order_foods_food
                        data['food'] = food
                        data['content'] = food_review.get("content", "")
                        data['star'] = food_review.get("star", "5")
                        new_food_review = FoodReview(**data)
                        new_food_review.save()
                    except Exception, e:
                        print e.message
                # 订单评论
                order_review = request.json.get("order_review")
                data['store'] = order.store
                if order_review:
                    if data.get('food'):
                        data.pop('food')
                    data['delivery_time'] = order_review.get(
                        "delivery_time", 120)
                    data['content'] = order_review.get("content", "")
                    data['star'] = order_review.get("star", "5")
                    new_order_review = OrderReview(**data)
                    new_order_review.save()

                order_review = request.json.get("order_review")
            return JsonResponse({})
Ejemplo n.º 17
0
 def put(self, request, store_id):
     # 更新信息
     update_item = ['name', 'address', 'announcement', 'description', 'phone', 'image_ids']
     try:
         update_dict = get_update_dict_by_list(update_item, request.json)
         # owner = request.u
         # store = owner.store
         # assert store.id == store_id
         modify_num = Store.objects.filter(id=store_id).update(**update_dict)
         assert modify_num == 1
         return JsonResponse({})
     except Exception, e:
         return JsonErrorResponse("Update failed:" + e.message, 400)
Ejemplo n.º 18
0
 def put(self, request, food_id):
     # 更新食品
     try:
         update_item = ['name', 'price', 'stock', 'description', 'image_ids']
         update_dict = get_update_dict_by_list(update_item, request.json)
         # owner = request.u
         # store = owner.store
         # modify_num = store.foods.filter(id=food_id).update(**update_dict)
         modify_num = Food.objects.filter(id=food_id).update(**update_dict)
         assert modify_num == 1
         return JsonResponse({})
     except Exception, e:
         return JsonErrorResponse("Update failed:" + e.message, 400)
Ejemplo n.º 19
0
 def put(self, request, complaint_id):
     # 更新投诉
     try:
         update_item = ['status']
         status = request.json.get("status")
         assert status in map(lambda i: i[0],
                              Complaint.StatusList), "not valid"
         update_dict = get_update_dict_by_list(update_item, request.json)
         modify_num = Complaint.objects.filter(id=complaint_id).update(
             **update_dict)
         assert modify_num == 1
         return JsonResponse({})
     except Exception, e:
         return JsonErrorResponse("Update failed:" + e.message, 400)
Ejemplo n.º 20
0
 def post(self, request):
     # 创建食品
     owner = request.u
     store = owner.store
     name = request.json.get("name")
     description = request.json.get("description")
     price = float(request.json.get("price"))
     stock = int(request.json.get("stock"))
     image_ids = request.json.get("image_ids")
     if not all([store, name, description, price, stock]):
         return JsonErrorResponse("store, name, description, price, stock are needed", 400)
     new_food = Food(
         name=name,
         description=description,
         price=price,
         stock=stock,
         image_ids=image_ids,
         store=store
     )
     try:
         new_food.save()
     except Exception, e:
         print e
         return JsonErrorResponse("Fail:" + e.message)
Ejemplo n.º 21
0
 def get(self, request):
     # 获取投诉列表
     account_type = request.account_type
     if account_type == "admin":
         complaints = [
             complaint.to_string() for complaint in Complaint.objects.all()
         ]
         return JsonResponse({"complaint_list": complaints})
     customer = request.u
     if not customer:
         return JsonErrorResponse("can't find customer", 404)
     complaints = [
         complaint.to_string() for complaint in customer.complaints.all()
     ]
     return JsonResponse({"complaint_list": complaints})
Ejemplo n.º 22
0
 def process_request(self, request):
     request.u = None
     request.account_type = None
     token = request.json.get('token', None) or request.META.get(
         "HTTP_AUTHORIZATION_TOKEN", None)
     if not token:
         return
     # check token
     try:
         assert token and len(token.split('$')) == 5
         info = token.split('$')
         account_type = info[1]
         user_id = info[2]
         assert redisClient.get("token:%s:%s" %
                                (account_type, user_id)) == token
         assert account_type in USER_MODEL_MAP
         user = USER_MODEL_MAP[account_type].objects.get(id=user_id)
         request.u = user
         request.account_type = account_type
     except AssertionError:
         return JsonErrorResponse("Token Auth Fail", 403)
Ejemplo n.º 23
0
 def get(self, request, order_id):
     try:
         order = Order.objects.get(id=order_id)
     except Order.DoesNotExist:
         return JsonErrorResponse("Order does not exist", 404)
     return JsonResponse({"order": order.to_detail_string()})
Ejemplo n.º 24
0
 def get(self, request, store_id):
     try:
         store = Store.objects.get(id=store_id)
     except Store.DoesNotExist:
         return JsonErrorResponse("Store does not exist", 404)
     return JsonResponse({"store": store.to_detail_string()})
Ejemplo n.º 25
0
 def get(self, request, customer_id):
     try:
         customer = Customer.objects.get(id=customer_id)
     except Customer.DoesNotExist:
         return JsonErrorResponse("Customer does not exist", 404)
     return JsonResponse({"customer": customer.to_detail_string()})
Ejemplo n.º 26
0
 def get(self, request, seller_id):
     try:
         seller = Seller.objects.get(id=seller_id)
     except Seller.DoesNotExist:
         return JsonErrorResponse("Seller does not exist", 404)
     return JsonResponse({"seller": seller.to_detail_string()})
Ejemplo n.º 27
0
 def get(self, request, admin_id):
     try:
         admin = Admin.objects.get(id=admin_id)
     except Admin.DoesNotExist:
         return JsonErrorResponse("Admin does not exist", 404)
     return JsonResponse({"admin": admin.to_detail_string()})
Ejemplo n.º 28
0
 def get(self, request, complaint_id):
     try:
         complaint = Complaint.objects.get(id=complaint_id)
     except Complaint.DoesNotExist:
         return JsonErrorResponse("Complaint does not exist", 404)
     return JsonResponse({"complaint": complaint.to_detail_string()})
Ejemplo n.º 29
0
 def get(self, request, food_id):
     try:
         food = Food.objects.get(id=food_id)
     except Food.DoesNotExist:
         return JsonErrorResponse("Food does not exist", 404)
     return JsonResponse({"food": food.to_detail_string()})