def get(self, url): """ get from the ulord-platform :param url: request's url :type url: str :return: return result.You can query the errcode. """ self.calculate_sign() self.ulord_head = ulordconfig.get('ulord_head') try: r = requests.get(url=url, headers=self.ulord_head) except Exception as e: self.log.error( "Failed request from the ulord-platform: {0}, URL is {1}". format(e, url)) return return_result(60400) self.log.info(url) self.log.debug(self.ulord_head) self.log.info(r.status_code) if (r.status_code == requests.codes.ok): self.log.debug(r.json()) return r.json() else: self.log.debug(r) return return_result(60400)
def blog_update(): """ update blog :return: claim id,ulord-platform DB id """ current_user = auth_login_required() # check token if type(current_user) is dict: return jsonify(current_user) id = request.json.get('id') if id: try: id = str(id) except: return return_result(60100) pay_password = request.json.get('password') if not id and not pay_password: return jsonify(return_result(60100)) title = request.json.get('title') body = request.json.get('body') amount = request.json.get('amount') tags = request.json.get('tag') description = request.json.get('description') return jsonify( junior.resource_update(id=id, pay_password=pay_password, encrypted=True, title=title, body=body, price=amount, tags=tags, des=description))
def get_pubkey(): """ Get:generate publikey to fronted-end.Post:Check the message if crypted. :return: get-publickey/post-decrypted message """ log.info("start get password") if request.method == 'GET': return jsonify(return_result(0, result={"pubkey":junior.rsahelper.pubkeybytes})) elif request.method == 'POST': message = request.json.get("password") return jsonify(return_result(0, result={'password': junior.get_purearg(message)}))
def auth_login_required(): """ check token :return: current user """ head_token = request.headers.get('token') if not head_token: return {'errcode': 60103, 'reason': "需要token"} login_user = User.query.filter_by(token=head_token).first() if not login_user: return return_result(60104) if int(login_user.timestamp) < time.time(): return return_result(60104) return login_user
def blog_delete(): """ delete blog :return: errcode.You can query from the errcode dict. """ current_user = auth_login_required() # check token if type(current_user) is dict: return jsonify(current_user) id = request.json.get('id') password = request.json.get('password') if not id and not password: return return_result(60100) password = junior.decrypt(password) if not current_user.verify_password(password): return jsonify(return_result(60003)) return jsonify(junior.delete(id, current_user.pay_password))
def blog_publish(): """ publish blog :return: claim id,ulord-platform DB ID """ current_user = auth_login_required() # check token if type(current_user) is dict: return jsonify(current_user) title = request.json.get('title') body = request.json.get('body') amount = request.json.get('amount') tags = request.json.get('tag') description = request.json.get('description') body_hash = junior.udfs_upload([body]) if body_hash and body_hash.get(body): return jsonify( junior.resource_publish( title=title, udfshash=body_hash.get(body), amount=amount, tags=tags, des=description, usercondition={'usertoken': current_user.token})) else: return jsonify(return_result(errcode=60400))
def get_expensebillings(): """ get user expense billing information """ current_user = auth_login_required() # check token if type(current_user) is dict: return jsonify(current_user) try: page = request.json.get('page') except: page = 1 try: num = request.json.get('num') except: num = 10 try: category = request.json.get('category') except: category = 2 sdate = request.json.get('sdate') edate = request.json.get('edate') if not page: page = 1 if not num: num = 10 if not category: category = 2 if not sdate or not edate: return jsonify(return_result(60100)) return jsonify(junior.queryoutgobillings(current_user.wallet, sdate, edate ,page, num, category=category))
def get_billings(): """ query user's billing """ current_user = auth_login_required() # check token if type(current_user) is dict: return jsonify(current_user) sdate = request.json.get('sdate') edate = request.json.get('edate') if not sdate or not edate: return jsonify(return_result(60100)) return jsonify(junior.querybillings(current_user.wallet, sdate, edate))
def post(self, url, data): """ post to the ulord-platform :param url: request's url :type url: str :param data: post data :type data: dict :return: return result.you can query the errcode """ self.log.info("url is {}".format(url)) self.log.info("data is {}".format(data)) # deal with unicode and utf-8 from version import __py_version__ if 2 <= __py_version__ < 3: from utils import _byteify data = _byteify(data=data) elif 3 <= __py_version__ < 4: pass else: print("error python version") exit(-1) # calculate U-Sign self.calculate_sign(data) # self.ulord_head = ulordconfig.get('ulord_head') try: r = requests.post(url=url, json=data, headers=self.ulord_head) except Exception as e: self.log.error( "Failed request from the ulord-platform: {0}, URL is {1}". format(e, url)) return return_result(60400) self.log.info(r.status_code) if r.status_code == requests.codes.ok: self.log.debug(r.json()) return r.json() else: self.log.debug(r) return return_result(60400)
def regist(): """ user regist :return: user token """ username = request.json.get('username') password = request.json.get('password') cellphone = request.json.get('cellphone') email = request.json.get('email') if username is None or password is None: # missing arguments return jsonify(return_result(60100)) args = junior.decrypt([username, password, cellphone, email]) if args: result = junior.user_regist(username=args[0], password=args[1], cellphone=args[2], email=args[3]) return jsonify(result) else: return jsonify(return_result(60100))
def blog_list_by_ID(): """ list blogs by ID :return: blog list """ current_user = auth_login_required() # check token if type(current_user) is dict: return jsonify(current_user) ids = request.json.get('ids') if not ids and not isinstance(ids, list): return jsonify(return_result()) return jsonify(junior.query_resourc_by_ID(ids))
def paytouser(self, username): """ activity send some ulords to the user :param username: user wallet name :type username: str :return: errcode.You can query from the errcode dict. """ if webconfig.get('activity'): data = { 'is_developer': True, 'recv_user': username, 'amount': webconfig.get('amount') } return self.post(self.ulord_paytouser, data) else: return return_result(60300)
def regist(): """ user regist :return: user token """ username = request.json.get('username') password = request.json.get('password') cellphone = request.json.get('cellphone') email = request.json.get('email') if username is None or password is None: # missing arguments return jsonify(return_result(60100)) username = junior.decrypt(username) password = junior.decrypt(password) cellphone = junior.decrypt(cellphone) email = junior.decrypt(email) result = junior.user_regist(username, password,cellphone,email) return jsonify(result)