Example #1
0
def login(request):
    """
    Endpoint for xiaochengxu user login,and then return the token assotiate with user,
    if the user is a new user,create it use the openid and then return the token.
    :method:POST
    :param request:
    {
    "code":"abedefjf"
    }
    :return:
    {
    "token":"abedefg"
    }
    """
    data = request.data
    print(data)
    try:
        js_code = data['code']
        userImg = data.get('userImg', None)
        nickname = data.get('userName', None)
    except KeyError as e:
        return utils.render_exceptions(e.args, 1)
    base_url = settings.WEIXIN_AUTHORIZATION_URL
    appid = settings.WEIXIN_APPID
    secret = settings.WEIXIN_SECRET
    grant_type = 'authorization_code'
    params = urlencode({
        'appid': appid,
        'secret': secret,
        'js_code': js_code,
        'grant_type': grant_type
    })
    req_url = '?'.join([base_url, params])
    print(req_url)
    try:
        response = requests.get(req_url)
        if response.status_code == 200:
            try:
                openid = response.json()['openid']
                user = MyUser.objects.filter(username=openid)
                if user:
                    update_num = MyUser.objects.filter(username=openid).update(
                        avatar_url=userImg, nickname=nickname)
                    token, created = Token.objects.get_or_create(user=user[0])
                    response_data = {"openid": openid, "token": token.key}
                    return utils.render_response(response_data)
                else:
                    user = MyUser.objects.create_user(username=openid,
                                                      password=openid,
                                                      avatar_url=userImg,
                                                      nickname=nickname)
                    token = Token.objects.create(user=user)
                    response_data = {"openid": openid, "token": token.key}
                    return utils.render_response(response_data)
            except KeyError as e:
                return utils.render_exceptions(response, 1)
        else:
            return utils.render_exceptions("http request error", 1)
    except Exception as e:
        return utils.render_exceptions(e.args, 1)
Example #2
0
 def generate_poster(self, request, *args, **kwargs):
     #def file_iterator(file_name, chunk_size=512):
     #   with open(file_name,'rb') as f:
     #        while True:
     #           c = f.read(chunk_size)
     #            if c:
     #                yield c
     #            else:
     #                break
     goal = self.get_object()
     data = request.data
     if 'type' in data:
         poster_type = data['type']
         if poster_type == 'create':
             poster_url = goal.poster
             if poster_url:
                 logging.info("poster_url:{}".format(poster_url))
                 return JsonResponse({"poster_url": poster_url.name})
             else:
                 logging.info("generate poster")
                 poster_path = goal.generate_poster()
                 filename = os.path.split(poster_path)[-1]
                 poster_url = os.path.join('poster', filename)
                 goal.poster = poster_url
                 logging.debug("poster_url:{}".format(poster_url))
                 goal.save()
                 #response = StreamingHttpResponse(file_iterator(poster_path))
                 #response['Content-Type'] = 'application/octet-stream'
                 #response['Content-Disposition'] = 'attachment;filename="{0}"'.format(poster_path)
                 return JsonResponse({"poster_url": poster_url})
         elif poster_type == 'result':
             poster_url = goal.poster_result
             if poster_url:
                 logging.info("poster_url:{}".format(poster_url))
                 return JsonResponse({"poster_url": poster_url.name})
             else:
                 logging.info("generate poster")
                 if goal.goalStatus == 1 or goal.goalStatus == 0:
                     return utils.render_exceptions(
                         "goal does not finished", 1)
                 else:
                     poster = utils.Poster(goal)
                     if goal.goalStatus == 2:
                         poster_path = poster.make_result_poster(1)
                     else:
                         poster_path = poster.make_result_poster(0)
                     filename = os.path.split(poster_path)[-1]
                     poster_url = os.path.join('poster', filename)
                     goal.poster_result = poster_url
                     logging.debug("poster_url:{}".format(poster_url))
                     goal.save()
                     #response = StreamingHttpResponse(file_iterator(poster_path))
                     #response['Content-Type'] = 'application/octet-stream'
                     #response['Content-Disposition'] = 'attachment;filename="{0}"'.format(poster_path)
                     return JsonResponse({"poster_url": poster_url})
         else:
             return utils.render_exceptions("poster type is not provided",
                                            1)
Example #3
0
 def create(self, request, *args, **kwargs):
     data = request.data
     #print(data)
     #print("user:"******"goal_id" not in data:
         return utils.render_exceptions("create transaction failure", 1)
     try:
         record = TransactionRecord.objects.create(**data)
     except Exception as e:
         return utils.render_exceptions(e.args, 1)
     serializer = TransactionRecordSerializer(record)
     return utils.render_response(serializer.data)
Example #4
0
 def update(self, request, *args, **kwargs):
     try:
         data = request.data
         if 'goal_id' not in data:
             return utils.render_exceptions("goal_id is must be provided",
                                            1)
         instance = self.get_object()
         if instance.goal is None:
             return utils.render_exceptions(
                 "illegae operate,has been info administrator", 1)
         goal = Goal.objects.get(pk=data['goal_id'])
         instance.goal = goal
         instance.save()
         return utils.render_response("update success", 1)
     except Exception as e:
         return utils.render_exceptions(e.args, 1)
Example #5
0
def get_form_id(request):
    """
    url:/api/form_ids/
    request param:
    {
        "form_id":
        "prepay_id":
    }
    """
    try:
        logging.debug(request.data)
        print(request.data)
        data = request.data
        user = request.user
        openid = user.username
        print(openid)
        form_ids = cache.get(openid)
        if form_ids is None:
            form_ids = []
        if "form_id" in data:
            form_id = data['form_id']
            form_ids.append((form_id, 1))
        if "prepay_id" in data:
            form_id = data['prepay_id']
            form_ids.append((form_id, 3))
        cache.set(openid, form_ids)
        return utils.render_response({"update formId success!"})
    except Exception as e:
        logging.debug(e)
        return utils.render_exceptions(e.args, 1)
Example #6
0
 def create(self, request, *args, **kwargs):
     try:
         serializer = self.get_serializer(data=request.data)
         serializer.is_valid(raise_exception=True)
         serializer.save()
         return utils.render_response(serializer.data)
     except Exception as e:
         return utils.render_exceptions(e.args, 1)
Example #7
0
 def create(self, request, *args, **kwargs):
     try:
         data = request.data
         logging.info(data)
         if 'trade_no' not in data:
             return utils.render_exceptions("trade_no must be provided", 1)
         trade_no = data.pop('trade_no')
         data['user'] = request.user
         goal = Goal.objects.create(**data)
         transaction = TransactionRecord.objects.get(trade_no=trade_no)
         transaction.goal = goal
         transaction.status = 'done'
         transaction.save()
         serializer = GoalSerializer(goal)
         return utils.render_response(serializer.data)
     except Exception as e:
         return utils.render_exceptions(e.args, 1)
Example #8
0
 def create(self, request, *args, **kwargs):
     #data = dict(request.data)
     # user=Token.objects.get(key=request.auth).user
     try:
         serializer = self.get_serializer(data=request.data)
         serializer.is_valid()
         serializer.save(user=request.user)
         return utils.render_response(serializer.data)
     except Exception as e:
         return utils.render_exceptions(e.args, 1)
Example #9
0
 def create(self, request, *args, **kwargs):
     try:
         data = request.data
         clock = data['clock']
         clock_obj = Clock.objects.get(pk=clock)
         supervisors = clock_obj.goal.supervise.all().filter(
             supervisor__id=request.user.id)
         if not supervisors:
             return utils.render_exceptions("you are not a supervisor", 1)
         serializer = self.get_serializer(data=request.data)
         # try:
         #    clock = Clock.objects.create(**data)
         # except Exception as e:
         #    return utils.render_exceptions(e.args, 1)
         # serializer = self.serializer_class(clock)
         # return utils.render_response(serializer.data)
         serializer.is_valid()
         #self.perform_create(serializer)
         serializer.save(supervisor=request.user)
         return utils.render_response(serializer.data)
     except Exception as e:
         return utils.render_exceptions(e.args, 1)
Example #10
0
    def list(self, request, *args, **kwargs):
        try:
            queryset = self.filter_queryset(self.get_queryset())

            page = self.paginate_queryset(queryset)
            if page is not None:
                serializer = self.get_serializer(page, many=True)
                return self.get_paginated_response(serializer.data)

            serializer = self.get_serializer(queryset, many=True)
            return utils.render_response(serializer.data)
        except Exception as e:
            logging.exception(traceback.format_exc())
            return utils.render_exceptions(e.args, 1)
Example #11
0
 def create(self, request, *args, **kwargs):
     try:
         serializer = self.get_serializer(data=request.data)
         # try:
         #    clock = Clock.objects.create(**data)
         # except Exception as e:
         #    return utils.render_exceptions(e.args, 1)
         # serializer = self.serializer_class(clock)
         # return utils.render_response(serializer.data)
         serializer.is_valid()
         #self.perform_create(serializer)
         serializer.save(user=request.user)
         return utils.render_response(serializer.data)
     except Exception as e:
         return utils.render_exceptions(e.args, 1)
Example #12
0
 def create(self, request, *args, **kwargs):
     try:
         req_data = request.data
         print(req_data)
         try:
             goal_id = req_data.pop('goal_id')
             goal = Goal.objects.get(pk=goal_id)
         except Exception as e:
             return utils.render_exceptions(e.args, 1)
         if goal.goalStatus == 2:
             return utils.render_exceptions("goal has been finished", 1)
         supMoney = float(req_data['supMoney'])
         req_data['supMoney'] = supMoney
         if 'trade_no' not in req_data:
             if supMoney > 0.0:
                 return utils.render_exceptions("trade_no must be provided",
                                                1)
         else:
             trade_no = req_data.pop('trade_no')
         user = request.user
         if goal.user == user:
             return utils.render_exceptions("you can't supervise yourself",
                                            1)
         req_data['supervisor'] = request.user
         req_data['goal'] = goal
         try:
             supervise = Supervise(**req_data)
             supervise.save()
         except Exception as e:
             if "Duplicate entry" in e.args:
                 return utils.render_exceptions("goal has been supervised",
                                                1)
             return utils.render_exceptions(e.args, 1)
         if supMoney > 0.0:
             transaction = TransactionRecord.objects.get(trade_no=trade_no)
             transaction.goal = goal
             transaction.status = 'done'
             transaction.save()
         serializer = self.get_serializer(supervise)
         #serializer.is_valid(raise_exception=True)
         #self.perform_create(serializer)
         #headers = self.get_success_headers(serializer.data)
         return utils.render_response(serializer.data)
     except Exception as e:
         return utils.render_exceptions(e.args, 1)
Example #13
0
def unifiedorder(request):
    """
    :payload:
    {
        "body":订单描述,
        "total_fee":订单金额,
        "goal_id":goal_id
    }
    :return:
    """
    try:
        data = request.data
        print(data)
        logging.debug(data)
        user = request.user
        openid = user.username
        body = data["body"]
        total_fee = data["total_fee"]
        appid = settings.WEIXIN_APPID
        mch_id = settings.WEIXIN_MCH_ID
        key = settings.WEIXIN_KEY
        client = weixin.WeixinPay(
            appid,
            mch_id,
            key,
            notify_url="https://qdting.com/api/unifiedorder/")
        pay_info = client.generate_order_info(openid, body, total_fee)
        nonce_str = weixin.generate_nonce_str()
        print("pay_info:{}".format(pay_info))
        param = deepcopy(client.__dict__)
        param.update(pay_info)
        param.update({'nonce_str': nonce_str})
        key = param.pop('key')
        param = {k: v for k, v in param.items() if v}
        sign = client.generate_sign(param, key)
        param.update({'sign': sign})
        print("param:{}".format(param))
        tree = client.make_request(
            "https://api.mch.weixin.qq.com/pay/unifiedorder", param)
        if tree is None:
            return utils.render_exceptions("https request error", 1)
        else:
            return_code = tree.findtext("return_code")
            if return_code == 'SUCCESS':
                result_code = tree.findtext('result_code')
                if result_code == 'SUCCESS':
                    #with transaction.atomic():
                    if not 'goal_id' in data:
                        transactionrecord = TransactionRecord.objects.create(
                            user=user,
                            amount=total_fee / 100.0,
                            description=u'制定目标',
                            trade_no=pay_info['out_trade_no'],
                            status='ongoing')
                    else:
                        transactionrecord = TransactionRecord.objects.create(
                            user=user,
                            amount=total_fee / 100.0,
                            description=u'监督目标',
                            trade_no=pay_info['out_trade_no'],
                            status='ongoing')
                        #wallet=Wallet.objects.get(user=user)
                        #wallet.banlance-=total_fee/100.0
                        #wallet.save()
                    prepay_id = tree.findtext("prepay_id")
                    timestamp = str(int(time.time()))
                    nonce_str = weixin.generate_nonce_str()
                    sign_type = client.sign_type
                    package = "prepay_id={}".format(prepay_id)
                    param = {
                        "appId": appid,
                        "timeStamp": timestamp,
                        "nonceStr": nonce_str,
                        "package": package,
                        "signType": sign_type
                    }
                    sign = weixin.generate_sign(param, key, sign_type)
                    param.update({"paySign": sign})
                    param.update({'trade_no': pay_info['out_trade_no']})
                    logging.debug("return:{}".format(param))
                    return utils.render_response(param)
                else:
                    return_msg = tree.findtext("return_msg")
                    print(ElementTree.tostring(tree))
                    return utils.render_exceptions(return_msg, 1)
            else:
                return_msg = tree.findtext("return_msg")
                print(ElementTree.tostring(tree))
                return utils.render_exceptions(return_msg, 1)
    except Exception as e:
        logging.exception(e)
        return utils.render_exceptions(e.args, 1)
Example #14
0
def withdrawCash(request):
    """
    :param request:
    json {"desc":desc,"amount":amount}
    :return:
    """
    try:
        data = request.data
        print(data)
        logging.debug(data)
        user = request.user
        openid = user.username
        desc = data["desc"]
        amount = data["amount"]
        wallet = Wallet.objects.get(user=user)
        if amount > wallet.banlance * 100:
            return utils.render_exceptions(
                "amount must be little than banlance", 1)
        appid = settings.WEIXIN_APPID
        mch_id = settings.WEIXIN_MCH_ID
        key = settings.WEIXIN_KEY
        client = weixin.WeixinPay(
            appid,
            mch_id,
            key,
            notify_url="https://qdting.com/api/withdraw_cash/")
        pay_info = client.generate_pay_info(openid, "withdraw", amount)
        nonce_str = weixin.generate_nonce_str()
        print("pay_info:{}".format(pay_info))
        param = deepcopy(client.__dict__)
        param.pop('appid')
        param.pop('mch_id')
        param.pop('trade_type')
        param.pop('sign_type')
        param.pop('notify_url')
        param.update({'mch_appid': appid, 'mchid': mch_id})
        param.update(pay_info)
        param.update({'nonce_str': nonce_str})
        key = param.pop('key')
        param = {k: v for k, v in param.items() if v}
        sign = client.generate_sign(param, key)
        param.update({'sign': sign})
        print("param:{}".format(param))
        tree = client.make_request(
            "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers",
            param,
            user_cert=True)
        if tree is None:
            return utils.render_exceptions("https request error", 1)
        else:
            return_code = tree.findtext("return_code")
            if return_code == 'SUCCESS':
                result_code = tree.findtext('result_code')
                if result_code == 'SUCCESS':
                    with transaction.atomic():
                        transactionrecord = TransactionRecord.objects.create(
                            user=user,
                            operateType='withdrawCash',
                            amount=-amount / 100.0,
                            description=u'提现',
                            trade_no=pay_info['partner_trade_no'],
                            status="done")
                        transactionrecord.save()
                        wallet = Wallet.objects.get(user=user)
                        wallet.banlance -= amount / 100.0
                        wallet.save()
                    return utils.render_response("withdraw success")
                else:
                    return_msg = tree.findtext('return_msg')
                    print(ElementTree.tostring(tree))
                    return utils.render_exceptions(return_msg, 1)
            else:
                return_msg = tree.findtext("return_msg")
                print(ElementTree.tostring(tree))
                return utils.render_exceptions(return_msg, 1)
    except Exception as e:
        return utils.render_exceptions(e.args, 1)
Example #15
0
def refund(request):
    """
    :param request:
    json{"trade_no":trade_no}
    :return:
    """
    try:
        data = request.data
        logging.debug(data)
        if "trade_no" in data:
            trade_no = data.pop('trade_no')
        else:
            return utils.render_exceptions("trade_no is must be provided")
        appid = settings.WEIXIN_APPID
        mch_id = settings.WEIXIN_MCH_ID
        key = settings.WEIXIN_KEY
        query_client = weixin.WeixinPay(
            appid, mch_id, key, notify_url="https://qdting.com/api/refund/")
        orderquery_info = query_client.generate_orderquery_info(
            {"out_trade_no": trade_no})
        nonce_str = weixin.generate_nonce_str()
        query_param = deepcopy(query_client.__dict__)
        query_param.update(orderquery_info)
        query_param.update({'nonce_str': nonce_str})
        key = query_param.pop('key')
        query_param = {k: v for k, v in query_param.items() if v}
        sign = query_client.generate_sign(query_param, key)
        query_param.update({'sign': sign})
        print("param:{}".format(query_param))
        tree = query_client.make_request(
            "https://api.mch.weixin.qq.com/pay/orderquery", query_param)
        if tree is None:
            return utils.render_exceptions("https request error", 1)
        else:
            return_code = tree.findtext("return_code")
            if return_code == 'SUCCESS':
                result_code = tree.findtext("result_code")
                if result_code == 'SUCCESS':
                    trade_state = tree.findtext("trade_state")
                    if trade_state != "SUCCESS":
                        return utils.render_exceptions("非法操作,已通知管理员", 1)
                else:
                    return_msg = tree.findtext("return_msg")
                    ElementTree.tostring(tree)
                    return utils.render_exceptions(return_msg, 1)
            else:
                return_msg = tree.findtext("return_msg")
                ElementTree.tostring(tree)
                return utils.render_exceptions(return_msg, 1)
        transactionrecord = TransactionRecord.objects.get(trade_no=trade_no)
        data['out_trade_no'] = trade_no
        amount = transactionrecord.amount
        data['total_fee'] = amount
        data['refund_fee'] = amount
        user = request.user
        openid = user.username
        appid = settings.WEIXIN_APPID
        mch_id = settings.WEIXIN_MCH_ID
        key = settings.WEIXIN_KEY
        client = weixin.WeixinPay(appid,
                                  mch_id,
                                  key,
                                  notify_url="https://qdting.com/api/refund/")
        refund_info = client.generate_refund_info(data)
        nonce_str = weixin.generate_nonce_str()
        param = deepcopy(client.__dict__)
        param.update(refund_info)
        param.update({'nonce_str': nonce_str})
        key = param.pop('key')
        param = {k: v for k, v in param.items() if v}
        sign = client.generate_sign(param, key)
        param.update({'sign': sign})
        print("param:{}".format(param))
        tree = client.make_request(
            "https://api.mch.weixin.qq.com/pay/refundquery",
            param,
            user_cert=True)
        if tree is None:
            return utils.render_exceptions("https request error", 1)
        else:
            return_code = tree.findtext("return_code")
            if return_code == 'SUCCESS':
                result_code = tree.findtext('result_code')
                if result_code == 'SUCCESS':
                    with transaction.atomic():
                        goal = transactionrecord.goal
                        #supervisor refund
                        if goal.user != request.user:
                            refund_transaction = TransactionRecord.objects.create(
                                user=user,
                                operateType="refund",
                                amount=-amount / 100.0,
                                description=u"创建监督失败",
                                trade_no=refund_info['out_refund_no'],
                                goal=transactionrecord.goal,
                            )
                        #goal_maker refund
                        else:
                            refund_transaction = TransactionRecord.objects.create(
                                user=user,
                                operateType="refund",
                                amount=-amount / 100.0,
                                description="创建目标失败",
                                trade_no=refund_info['out_refund_no'],
                                goal=transactionrecord.goal)
                    return utils.render_response("refund success")
                else:
                    return_msg = tree.findtext("return_msg")
                    #ElementTree.tostring(tree)
                    return utils.render_exceptions(return_msg, 1)
            else:
                return_msg = tree.findtext("return_msg")
                # ElementTree.tostring(tree)
                return utils.render_exceptions(return_msg, 1)
    except Exception as e:
        return utils.render_exceptions(e.args, 1)