示例#1
0
def login(request):
    if request.method == "POST":
        form = FormParser(request)
        phone = form.get("phone")
        password = form.get("password")

        if phone is None or password is None:
            return JsonResponse({
                "reason": "Need phone or password",
            },
                                status=400)

        try:
            result = Vendor.objects.get(phone=phone, password=password)
        except Vendor.DoesNotExist:
            return JsonResponse({
                "reason": "Username or password wrong!",
            },
                                status=400)

        uuid = sessions.create_session(_id=result.id)
        resp = JsonResponse({
            "authorization": uuid,
            "user_id": result.id
        },
                            status=200)
        resp.set_cookie("sessionID", uuid)
        return resp

    else:
        return JsonResponse({}, status=405)
示例#2
0
    def post(self, request):
        form = FormParser(request)
        pw = form.get("password")
        request_id = form.get("request_id")
        if not 8 < len(pw) < 64:
            return JsonResponse({"reason": "password too long or too short"},
                                status=400)

        try:
            verify_message = VerifyMessage.objects.get(request_id=request_id)
        except VerifyMessage.DoesNotExist:
            return JsonResponse({"reason": "request_id wrong"}, status=404)

        if verify_message.verified is not True:
            return JsonResponse({"reason": "request not verified"}, status=403)

        query_set = Vendor.objects.filter(phone=verify_message.phone_number)
        if len(query_set) != 0:
            return JsonResponse({"reason": "phone already exist"}, status=400)

        new_user = Vendor(phone=verify_message.phone_number,
                          password=pw,
                          verified=False)

        new_user.save()

        return JsonResponse({}, status=200)
示例#3
0
    def post(self, request):
        form = FormParser(request)

        person_name = form.get("person_name")
        person_ident = form.get("person_id")
        if not (person_ident or person_name):
            return JsonResponse({
                "reason": "form item required",
                "code": "FORM_ITEM_LACK"
            }, status=400)

        form_1 = vendorAuthFileStore.get_upload_form()
        form_2 = vendorAuthFileStore.get_upload_form()

        auth_info = VendorAuthInfo(
            checked=False,
            vendor_responsible_person_name=person_name,
            vendor_responsible_person_ID=person_ident,
            vendor_responsible_person_ID_imageID=form_1["file_id"],
            vendor_license_imageID=form_2["file_id"])
        auth_info.save()

        return JsonResponse({
            "person_id_image_form": form_1,
            "vendor_licence_image_form": form_2
        }, status=200)
示例#4
0
    def _func(self, request):
        u_auth = "authorization"
        v_auth = "authorization_v"
        is_user = True
        au = None
        form = FormParser(request)

        au = au or request.GET.get(u_auth) or form.get(
            u_auth) or request.META.get("authorization")
        if not au:
            au = request.GET.get(v_auth) or form.get(v_auth)
            is_user = False

        user_id = sessions.get(au)

        if user_id is not None:
            try:
                if is_user:
                    user = models.User.objects.get(id=user_id)
                    setattr(request, "is_from_user", True)
                else:
                    user = models.Vendor.objects.get(id=user_id)
                    setattr(request, "is_from_user", False)
                setattr(request, "user", user)

                return func(self, request)
            except models.User.DoesNotExist or models.Vendor.DoesNotExist:
                pass

        return JsonResponse({"reason": "authorize failed"}, status=401)
示例#5
0
    def get(self, request):
        form = FormParser(request)
        user_id = form.get("user_id")
        if user_id:
            if str(user_id) != str(request.user.id):
                return JsonResponse(
                    {
                        "reason": "your id is not equals user_id",
                        "code": "ACCESS_DENIED_CROSS"
                    },
                    status=405)
            addresses = UserAddress.objects.filter(
                user_id=request.user.id).all()

            addresses_resp = []
            for i in addresses:
                addresses_resp.append({
                    "school": i.school,
                    "name": i.receiver,
                    "phone": i.receiver_phone,
                    "location": i.location
                })

            return JsonResponse({"user_addresses": addresses_resp}, status=200)

        address_id = form.get('address_id')
        if address_id:
            pass
示例#6
0
    def put(self, request):
        form = FormParser(request)
        changed = []
        not_allowed = []
        userinfo = models.UserInfo.objects.get(user_id=request.user.id)

        def try_to_int(x):
            try:
                return int(x)
            except ValueError:
                return 0

        change = {
            "nickname": [None, lambda x: len(x) < 64],
            "sex": [None, lambda x: x in ("male", "female")],
            "age": [None, try_to_int],
            "college": [None, lambda x: len(x) < 64]
        }
        for key in change.keys():
            val = form.get(key)
            if val:
                if change[key][1](val):
                    change[key][0] = val
                else:
                    not_allowed.append(key)
        if not_allowed:
            return JsonResponse({"not_allowed": not_allowed}, 400)
        for key, val in change.items():
            if val[0] is not None:
                userinfo.__setattr__(key, val[0])
                changed.append(key)

        userinfo.save()
        return JsonResponse({"changed": changed}, status=200)
示例#7
0
    def get(self, request):
        form = FormParser(request)
        vendor_id = form.get("vendor_id")
        if not vendor_id:
            return JsonResponse({
                "reason": "vendor_id is required",
                "code": "FORM_ITEM_LACK"
            }, status=400)
        try:
            auth_info = VendorAuthInfo.objects.get(vendor_id=int(vendor_id))

        except VendorAuthInfo.DoesNotExist:
            return JsonResponse({
                "reason": "vendor's auth_info not found",
                "code": "AUTH_INFO_NOT_FOUND"
            }, status=404)

        image_id = auth_info.vendor_license_imageID

        try:
            image = FileStorage.objects.get(id=image_id)
            url, _ = get_url(image.id, image.oss_key)

            return JsonResponse({
                "name": auth_info.vendor_responsible_person_name,
                "id_number": auth_info.vendor_responsible_person_ID,
                "licence_url": auth_info.url2
            })

        except ImageStorage.DoesNotExist:
            return JsonResponse({
                "reason": "server error",
                "code": "SERVER_ERROR"
            }, status=500)
示例#8
0
 def post(self, request):
     form = FormParser(request)
     require_dict = {
         "name": form.get("name"),
         "course_id": form.get("course_id"),
         "prime": form.get("prime"),
         "info": form.get("info"),
     }
     for k, v in require_dict.items():
         if v is None:
             return JsonResponse(
                 {"reason": "form item {} is required".format(k)},
                 status=403)
     new_course = Course(**require_dict)
     new_course.save()
     return JsonResponse({"course_id": new_course.id}, status=200)
示例#9
0
    def get(self, request):
        form = FormParser(request)

        order_id = form.get("order_id")
        if not order_id:
            return JsonResponse({
                "reason": "form_item order_id required",
                "code": "FORM_ITEM_LACK"
            }, status=400)

        try:
            order = Order.objects.get(id=int(order_id))
        except Order.DoesNotExist:
            return JsonResponse({
                "reason": "order not found",
                "code": "ORDER_NOT_FOUND"
            }, status=404)
        except ValueError:
            return JsonResponse({
                "reason": "order_id must be int",
                "code": "INVALID_FORM_ITEM"
            }, status=401)

        return JsonResponse({
            "order_id": order.id,
            "vendor_id": order.vendor_id,
            "prime_at_least": order.prime_at_least,
            "create_time": order.create_time,
            "pay_time": order.pay_time,
            "vendor_insure_time": order.vendor_ensure_time,
            "ensure_time": order.ensure_time,
            "order_item_id_list": json.loads(order.order_items),
            "address_id": order.address_id
        })
示例#10
0
    def get(self, request):
        form = FormParser(request)

        school = form.get("most_popular_from")
        page = form.get("page")  #  一页十个
        if school and page:
            try:
                vendors = VendorBaseInfo.objects.filter(
                    school=school).order_by("sales").reverse()
                index = int(page) * 10
                vendors = vendors[index:index + 10]
            except IndexError:
                return JsonResponse(
                    {
                        "reason": "this page has no vendor",
                        "code": "QUERY_INDEX_OUT"
                    },
                    status=404)
            except ValueError:
                return JsonResponse(
                    {
                        "reason": "form_item page invalid",
                        "code": "FORM_ITEM_INVALID"
                    },
                    status=401)

            return JsonResponse(
                {
                    "vendors": [{
                        "name": i.name,
                        "school": i.school,
                        "condition": i.condition,
                        "opening_time_day": i.opening_time_day,
                        "logo_url": i.logo_url,
                        "bulletin": i.bulletin,
                        "sales": i.sales,
                        "category": i.category
                    } for i in vendors],
                    "order_by":
                    "sales"
                },
                status=200)
示例#11
0
    def put(self, request):
        form = FormParser(request)
        vendor_info = VendorBaseInfo.objects.get(vendor_id=request.user.id)

        form_dict = {
            "name": form.get("name"),
            "address": form.get("address"),
            "logo_url": form.get("logo_url"),
            "bulletin": form.get("bulletin"),
            "category": form.get("category"),
            "opening_time_day": form.get("opening_time_day"),
            "phone": form.get("phone")
        }

        changed = {}
        for k, v in form_dict.items():
            if v is not None:
                changed[k] = v

        for key, val in changed.items():
            if val[0] is not None:
                vendor_info.__setattr__(key, val[0])

        vendor_info.save()
        return JsonResponse({"changed": changed}, status=200)
示例#12
0
    def get(self, request):
        form = FormParser(request)
        course_id = form.get("course_id")
        if course_id is not None:
            try:
                course = Course.objects.get(id=course_id)
                i = course
                return JsonResponse(
                    {
                        "course_id": i.id,
                        "name": i.name,
                        "prime": i.prime,
                        "info": i.info,
                        "img_url": i.url,
                        "big_img_url": i.url,
                        "rate": i.rate,
                        "sales": i.sales
                    },
                    status=200)
            except Course.DoesNotExist:
                return JsonResponse({"reason": "course_id not found"},
                                    status=404)

        vendor_id = form.get("vendor_id")
        if vendor_id is not None:
            courses = Course.objects.filter(vendor_id=vendor_id).all()
            results = [{
                "course_id": i.id,
                "name": i.name,
                "prime": i.prime,
                "info": i.info,
                "img_url": i.url,
                "big_img_url": i.url,
                "rate": i.rate,
                "sales": i.sales
            } for i in courses]

            return JsonResponse(results, status=200)

        return JsonResponse({"reason": "no limitation"}, status=400)
示例#13
0
    def post(self, request):
        form = FormParser(request)
        name = form.get("name")
        phone = form.get("phone")
        location = form.get("location")
        school = form.get("school")

        if name and phone and location and school:
            if request.is_from_user:
                address = UserAddress(user_id=request.user.id,
                                      receiver=name,
                                      receiver_phone=phone,
                                      location=location,
                                      school=school)
                address.save()
                return JsonResponse({}, status=200)

        else:
            return JsonResponse(
                {
                    "reason": "form_item required",
                    "code": "FORM_ITEM_LACK"
                },
                status=400)
示例#14
0
    def post(self, request):
        form = FormParser(request)
        try:
            VendorBaseInfo.objects.get(vendor_id=request.user.id)
            return JsonResponse({"reason": "The user's info already exist"},
                                status=400)
        except VendorBaseInfo:
            dic = {
                "name": form.get("name"),
                "phone": form.get("phone"),
                "address": form.get("address"),
                "category": form.get("category"),
                "create_time": timezone.now()
            }

            for k, v in dic.items():
                if v is None:
                    return JsonResponse({"reason": "{} is required".format(k)},
                                        status=400)

            vendor_info = VendorBaseInfo(**dic)
            vendor_info.save()

            return JsonResponse({}, status=200)
示例#15
0
 def put(self, request):
     form = FormParser(request)
     callback = form.get("callback")
     if callback is not None:
         if callback == "OK":
             try:
                 auth_info = VendorAuthInfo.objects.get(user_id=request.vendor.id)
             except VendorAuthInfo.DoesNotExist:
                 return JsonResponse({
                     "reason": "the authorize info not upload",
                     "code": "AUTH_INFO_NOT_FOUND"
                 }, status=404)
             FileStore.set_verified(auth_info.vendor_responsible_person_ID_imageID)
             FileStore.set_verified(auth_info.vendor_license_imageID)
             return JsonResponse({}, status=200)
         else:
             return JsonResponse({}, status=203)
示例#16
0
    def _func(self, request):
        v_auth = "authorization_v"
        au = None
        form = FormParser(request)

        au = au or request.GET.get(v_auth) or form.get(v_auth)

        user_id = sessions.get(au)

        if user_id is not None:
            try:
                vendor = models.Vendor.objects.get(id=user_id)
                setattr(request, "vendor", vendor)
                return func(self, request)
            except models.User.DoesNotExist or models.Vendor.DoesNotExist:
                pass

        return JsonResponse({"reason": "authorize failed"}, status=401)
示例#17
0
    def post(self, request):
        form = FormParser(request)

        order_items = form.get("order_items")
        prime = 0
        if isinstance(order_items, list):
            for i in order_items:
                try:
                    course = Course.objects.get(id=int(i))
                except ValueError:
                    return JsonResponse({
                        "reason": "course id illegal",
                        "code": "INVALID_FORM"
                    }, status=400)
                except Order.DoesNotExist:
                    return JsonResponse({
                        "reason": "course not found",
                        "code": "COURSE_NOT_FOUND"
                    }, status=404)

                prime += course.prime

            pass
示例#18
0
    def put(self, request):
        form = FormParser(request)
        items = {
            "id": form.get("course_id"),
            "vendor_id": request.vendor.id,
            "prime": form.get("prime"),
            "category": form.get("category"),
            "info": form.get("info"),
            "img_url": form.get("img_url"),
            "big_img_url": form.get("big_img_url")
        }
        try:
            course = Course.objects.get(id=items["id"])
        except Course.DoesNotExist:
            return JsonResponse({"reason": "course not found"}, status=404)

        _ = {
            course.__setattr__(k, v)
            for k, v in items.items() if v is not None
        }

        return JsonResponse({}, status=200)