예제 #1
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})
예제 #2
0
class DeliveryInformationList(APIView):
    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})

    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)
        print "新收货信息id:", new_delivery_information.id
        return JsonResponse({"id": new_delivery_information.id})
예제 #3
0
class SellerList(APIView):
    def get(self, request):
        # 获取卖家列表
        sellers = [seller.to_string() for seller in Seller.objects.all()]
        return JsonResponse({"seller_list": sellers})

    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_seller = Seller(
            username=username,
            password=password,
            nickname=nickname,
            account_type=account_type
        )
        try:
            new_seller.save()
        except Exception, e:
            print e
            return JsonErrorResponse("Fail:" + e.message)
        print "新注册卖家id:", new_seller.id
        # 登陆
        token = get_token(username, password, "bussiness")
        return JsonResponse({
            "id": new_seller.id,
            "token": token
        })
예제 #4
0
class StoreList(APIView):
    def get(self, request):
        # 获取商店列表
        stores = [store.to_string() for store in Store.objects.all()]
        return JsonResponse({"store_list": stores})

    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)
        print "新注册id:", new_store.id
        return JsonResponse({"id": new_store.id})
예제 #5
0
class AdminList(APIView):
    def get(self, request):
        # 获取管理员列表
        admins = [admin.to_string() for admin in Admin.objects.all()]
        return JsonResponse({"admin_list": admins})

    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)
        print "新注册管理员id:", new_admin.id
        # 登陆
        token = get_token(username, password, "admin")
        return JsonResponse({"id": new_admin.id, "token": token})
예제 #6
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)
예제 #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()})
예제 #8
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)
예제 #9
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})
예제 #10
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})
예제 #11
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)
예제 #12
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({})
예제 #13
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)
예제 #14
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)
예제 #15
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})
예제 #16
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)
예제 #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)
예제 #18
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)
예제 #19
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)
예제 #20
0
class FoodList(APIView):
    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})

    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)
        print "新注册id:", new_food.id
        return JsonResponse({"id": new_food.id})
예제 #21
0
class ComplaintList(APIView):
    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})

    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)
        print "新投诉id:", new_complaint.id
        return JsonResponse({"id": new_complaint.id})
예제 #22
0
 def get(self, request):
     # 获取商店列表
     stores = [store.to_string() for store in Store.objects.all()]
     return JsonResponse({"store_list": stores})
예제 #23
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()})
예제 #24
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()})
예제 #25
0
 def get(self, request):
     # 获取卖家列表
     sellers = [seller.to_string() for seller in Seller.objects.all()]
     return JsonResponse({"seller_list": sellers})
예제 #26
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()})
예제 #27
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()})
예제 #28
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()})
예제 #29
0
 def get(self, request):
     # 获取管理员列表
     admins = [admin.to_string() for admin in Admin.objects.all()]
     return JsonResponse({"admin_list": admins})
예제 #30
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()})