def register(args): try: user = User(**request.get_json()) user.verifyUser() db.session.add(user) db.session.commit() return jsonify({'Status': 'Success', 'StatusCode': 0, 'Msg': '注册成功!', 'User': user.toJson()}), 200 except ValueError, error: logger.info('ValueError: errorMsg = {}'.format(error.message)) return jsonify({'Status': 'Failed', 'StatusCode': -1, 'Msg': error.message}), 400
def createComment(args): content = args.get('CommentInfo').get('Content', "") voteFlag = args.get('CommentInfo').get('VoteFlag') score = args.get("CommentInfo").get('Score') imageUrl = args.get("CommentInfo").get('ImageUrl') user = User.getUserByIdOrPhoneOrMail(id=args.get('UserId', ''), phone=args.get('Phone', '')) share = Share.query.get(shareId) if not share: return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': "该分享{}不存在!".format(shareId) }), 400 commentDict = { 'UserId': user.id, 'ShareId': share.id, 'Content': content, 'VoteFlag': voteFlag, 'Score': Score, 'ImageUrl': imageUrl } comment = Comment(**commentDict) db.session.add(comment) db.session.commit() return jsonify({ 'Status': 'Success', 'StatusCode': 0, 'Msg': "操作成功!", 'Comemnt': comment.toJson() }), 200
def getMyQuestion(args): user = User.getUserByIdOrPhoneOrMail(phone=args.get('Phone')) if not user: return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': "User phone={} does't exist".format(args.get('Phone')) }), 400 if user.passWordQA: questionId = int(user.passWordQA.split(";")[0]) question = [ x.get('question') for x in getPassWordQuestion() if x.get('id') == questionId ][0] return jsonify({ 'Status': 'Success', 'StatusCode': 0, 'Msg': '操作成功!', 'MyQuestion': { 'id': questionId, 'question': question } }), 200 else: return jsonify({ 'Status': 'Failded', 'StatusCode': -1, 'Msg': '你没有设置密保问题!' }), 400
def addUserPermission(args): userList = request.get_json().get("UserList") userId = userList.get('UserId', None) phone = userList.get('Phone', None) user = User.getUserByIdOrPhoneOrMail(userId, phone) if not user: if phone: return jsonify({'Status': 'Failed', 'StatusCode':-1, 'Msg': "User phone={} does't exist".format(phone)}), 400 if userId: return jsonify({'Status': 'Failed', 'StatusCode':-1, 'Msg': "User id={} does't exist".format(userId)}), 400 macAddress = userList.get('Mac', '') machine = Machine.getMachineByMac(macAddress) if not machine: return jsonify({'Status': 'Failed', 'StatusCode':-1, 'Msg': "MacAddress {} does't exist".format(macAddress)}), 400 permisson = userList.get('Permission') startTime = userList.get('StartTime', None) endTime = userList.get('EndTime', None) money = userList.get('Money', 0.0) quanXian = QuanXian(user.id, machine.id, permission=permisson, startTime=startTime, endTime=endTime, money=money) db.session.merge(quanXian) db.session.commit() pushContent = {'Action': 'addUserPermission', 'Permission': permisson, 'Mac': macAddress, 'StartTime': startTime, 'EndTime': endTime, 'Money': money} pushMessageToSingle([user.pushToken], TransmissionTemplateDemo( json.dumps(pushContent) )) return jsonify({'Status' : 'Success', 'StatusCode':0, 'Msg' : '操作成功!'}), 200
def updatePassword(args): phone = args.get('Phone', '') user = User.getUserByIdOrPhoneOrMail(phone=phone) questionId = args.get('QuestionId') questionAnswer = args.get('QuestionAnswer') if (user.passWordQA != "") and (user.passWordQA == str(questionId) + ";" + questionAnswer): try: user.updatePassWord(args.get('NewPassWord')) db.session.merge(user) db.session.commit() return jsonify({ 'Status': 'Success', 'StatusCode': 0, 'Msg': "操作成功!", 'User': user.toJson() }), 200 except ValueError, error: logger.info('ValueError: errorMsg = {}'.format(error.message)) return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': error.message }), 400
def addMachines(args): userId = args.get("UserId", '') phone = args.get('Phone', '') user = User.getUserByIdOrPhoneOrMail(id=userId, phone=phone) if not user: return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': "UserId {} does't exist".format(userId) }), 400 machine = request.get_json().get("Machines") result = Machine.getMachineByMac(machine.get('Mac', '')) if not result: machine = Machine(**machine) db.session.add(machine) db.session.flush() db.session.add(QuanXian(user.id, machine.id, permission=QuanXian.Admin)) db.session.commit() return jsonify({ 'Status': 'Success', 'StatusCode': 0, 'Msg': '操作成功!', 'Machine': machine.toJson() }), 200 else: return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': '操作失败,改机器已被添加!' }), 400
def applyPermission(args): userId = args.get('UserId', '') phone = args.get('Phone', '') user = User.getUserByIdOrPhoneOrMail(id=userId, phone=phone) applyDetail = args.get('ApplyDetail') macAddress = applyDetail.get('Mac', '') startTime = applyDetail.get('StartTime', '') if applyDetail.get('StartTime', '') else None endTime = applyDetail.get('EndTime', '') if applyDetail.get('EndTime', '') else None money = applyDetail.get('Money', 0.0) machine = Machine.query.filter_by(macAddress=macAddress).first() if not machine: return jsonify({'Status': 'Failed', 'StatusCode':-1, 'Msg': "MacAddress {} does't exist".format(macAddress)}), 400 needPermission = applyDetail.get('Permission') reason = applyDetail.get('Reason') shenQing = ShenQing(user.id, machine.id, reason=reason, needPermission=needPermission, startTime=startTime, endTime=endTime, money=money) db.session.add(shenQing) db.session.commit() managerIds = [element.userId for element in QuanXian.query.filter_by(machineId=machine.id) if element.permission in [QuanXian.SuperAdmin, QuanXian.Admin]] tokenList = filter(lambda x : len(x) > 0, [User.query.get(id).pushToken for id in managerIds]) logger.info("managerIds = {}\ntokens ={}".format(managerIds, tokenList)) pushContent = request.get_json() pushContent.pop('Token', None) pushContent['UserName'] = user.userName pushContent['Phone'] = user.phone pushContent['Action'] = 'applyPermission' pushContent['ShenQingId'] = shenQing.id pushMessageToSingle(tokenList, TransmissionTemplateDemo( json.dumps(pushContent) )) return jsonify({'Status': 'Success', 'StatusCode': 0, 'Msg': '申请成功!', 'ApplyDetail': shenQing.toJson()}), 200
def updateMachineInfo(args): userId = args.get("UserId", '') user = User.getUserByIdOrPhoneOrMail(id=userId) if not user: return jsonify({'Status': 'Failed', 'StatusCode': -1, 'Msg': "UserId {} does't exist".format(userId)}), 400 macAddress = args.get('MachineInfo').get('Mac') machine = Machine.getMachineByMac(macAddress) if not machine: return jsonify({'Status': 'Failed', 'StatusCode': -1, 'Msg': "MacAddress {} does't exist".format(macAddress)}), 400 res = [] for element in QuanXian.query.filter_by(userId=userId, machineId=machine.id): if element.permission in [QuanXian.Producer, QuanXian.SuperAdmin, QuanXian.Admin]: res.append(element) if res == []: return jsonify({'Status': 'Failed', 'StatusCode':-1, 'Msg': u"无权更新该机器的信息"}), 400 for fieldName in ['MachineName', 'MachineMoney', 'AdminPass', 'UserPass', 'MachineType']: if args.get('MachineInfo').has_key(fieldName): setattr(machine, fieldName[0].lower() + fieldName[1:], args.get('MachineInfo').get(fieldName)) db.session.merge(machine) db.session.commit() return jsonify({'Status': 'Success', 'StatusCode': 0, 'Msg': '操作成功!', 'Machine': machine.toJson()}), 200
def setPassWordAnswer(args): questionId = args.get('QuestionId') questionAnswer = args.get('QuestionAnswer') user = User.getUserByIdOrPhoneOrMail(id=args.get('UserId')) user.passWordQA = str(questionId) + ";" + questionAnswer db.session.merge(user) db.session.commit() return jsonify({'Status': 'Success', 'StatusCode': 0, 'Msg': '操作成功!'}), 200
def getMyQuestion(args): user = User.getUserByIdOrPhoneOrMail(phone=args.get('Phone')) if not user: return jsonify({'Status': 'Failed', 'StatusCode':-1, 'Msg': "User phone={} does't exist".format(args.get('Phone'))}), 400 if user.passWordQA: questionId = int(user.passWordQA.split(";")[0]) question = [x.get('question') for x in getPassWordQuestion() if x.get('id') == questionId][0] return jsonify({'Status': 'Success', 'StatusCode': 0, 'Msg': '操作成功!', 'MyQuestion': {'id': questionId, 'question': question}}), 200 else: return jsonify({'Status': 'Failded', 'StatusCode': -1, 'Msg': '你没有设置密保问题!'}), 400
def applyPermission(args): userId = args.get('UserId', '') phone = args.get('Phone', '') user = User.getUserByIdOrPhoneOrMail(id=userId, phone=phone) applyDetail = args.get('ApplyDetail') macAddress = applyDetail.get('Mac', '') startTime = applyDetail.get('StartTime', '') if applyDetail.get( 'StartTime', '') else None endTime = applyDetail.get('EndTime', '') if applyDetail.get('EndTime', '') else None money = applyDetail.get('Money', 0.0) machine = Machine.query.filter_by(macAddress=macAddress).first() if not machine: return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': "MacAddress {} does't exist".format(macAddress) }), 400 needPermission = applyDetail.get('Permission') reason = applyDetail.get('Reason') shenQing = ShenQing(user.id, machine.id, reason=reason, needPermission=needPermission, startTime=startTime, endTime=endTime, money=money) db.session.add(shenQing) db.session.commit() managerIds = [ element.userId for element in QuanXian.query.filter_by(machineId=machine.id) if element.permission in [QuanXian.SuperAdmin, QuanXian.Admin] ] tokenList = filter(lambda x: len(x) > 0, [User.query.get(id).pushToken for id in managerIds]) logger.info("managerIds = {}\ntokens ={}".format(managerIds, tokenList)) pushContent = request.get_json() pushContent.pop('Token', None) pushContent['UserName'] = user.userName pushContent['Phone'] = user.phone pushContent['Action'] = 'applyPermission' pushContent['ShenQingId'] = shenQing.id pushMessageToSingle(tokenList, TransmissionTemplateDemo(json.dumps(pushContent))) return jsonify({ 'Status': 'Success', 'StatusCode': 0, 'Msg': '申请成功!', 'ApplyDetail': shenQing.toJson() }), 200
def addUserPermission(args): userList = request.get_json().get("UserList") userId = userList.get('UserId', None) phone = userList.get('Phone', None) user = User.getUserByIdOrPhoneOrMail(userId, phone) if not user: if phone: return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': "User phone={} does't exist".format(phone) }), 400 if userId: return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': "User id={} does't exist".format(userId) }), 400 macAddress = userList.get('Mac', '') machine = Machine.getMachineByMac(macAddress) if not machine: return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': "MacAddress {} does't exist".format(macAddress) }), 400 permisson = userList.get('Permission') startTime = userList.get('StartTime', None) endTime = userList.get('EndTime', None) money = userList.get('Money', 0.0) quanXian = QuanXian(user.id, machine.id, permission=permisson, startTime=startTime, endTime=endTime, money=money) db.session.merge(quanXian) db.session.commit() pushContent = { 'Action': 'addUserPermission', 'Permission': permisson, 'Mac': macAddress, 'StartTime': startTime, 'EndTime': endTime, 'Money': money } pushMessageToSingle([user.pushToken], TransmissionTemplateDemo(json.dumps(pushContent))) return jsonify({'Status': 'Success', 'StatusCode': 0, 'Msg': '操作成功!'}), 200
def getUserDetailInfo(args): userList = request.get_json().get('UserList') userId = userList.get('UserId', '') phone = userList.get('Phone', '') user = User.getUserByIdOrPhoneOrMail(id=userId, phone=phone) userJson = user.toJson() userJson.pop('passWord', None) if user: return jsonify({'Status': 'Success', 'StatusCode': 0, 'Msg': '操作成功!', 'UserInfo': userJson}), 200 else: return jsonify({'Status': 'Success', 'StatusCode': -1, 'Msg': '操作失败,用户id={}不存在!'.format(userId)}), 400
def updateMachineInfo(args): userId = args.get("UserId", '') user = User.getUserByIdOrPhoneOrMail(id=userId) if not user: return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': "UserId {} does't exist".format(userId) }), 400 macAddress = args.get('MachineInfo').get('Mac') machine = Machine.getMachineByMac(macAddress) if not machine: return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': "MacAddress {} does't exist".format(macAddress) }), 400 res = [] for element in QuanXian.query.filter_by(userId=userId, machineId=machine.id): if element.permission in [ QuanXian.Producer, QuanXian.SuperAdmin, QuanXian.Admin ]: res.append(element) if res == []: return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': u"无权更新该机器的信息" }), 400 for fieldName in [ 'MachineName', 'MachineMoney', 'AdminPass', 'UserPass', 'MachineType' ]: if args.get('MachineInfo').has_key(fieldName): setattr(machine, fieldName[0].lower() + fieldName[1:], args.get('MachineInfo').get(fieldName)) db.session.merge(machine) db.session.commit() return jsonify({ 'Status': 'Success', 'StatusCode': 0, 'Msg': '操作成功!', 'Machine': machine.toJson() }), 200
def addMachines(args): userId = args.get("UserId", '') phone = args.get('Phone', '') user = User.getUserByIdOrPhoneOrMail(id=userId, phone=phone) if not user: return jsonify({'Status': 'Failed', 'StatusCode':-1, 'Msg': "UserId {} does't exist".format(userId)}), 400 machine = request.get_json().get("Machines") result = Machine.getMachineByMac(machine.get('Mac', '')) if not result: machine = Machine(**machine) db.session.add(machine) db.session.flush() db.session.add(QuanXian(user.id, machine.id, permission=QuanXian.Admin)) db.session.commit() return jsonify({'Status' : 'Success', 'StatusCode':0, 'Msg' : '操作成功!', 'Machine': machine.toJson()}), 200 else: return jsonify({'Status' : 'Failed', 'StatusCode':-1, 'Msg' : '操作失败,改机器已被添加!'}), 400
def updatePassword(args): phone = args.get('Phone', '') user = User.getUserByIdOrPhoneOrMail(phone=phone) if not user: return jsonify({'Status': 'Failed', 'StatusCode':-1, 'Msg': "User phone={} does't exist".format(phone)}), 400 questionId = args.get('QuestionId') questionAnswer = args.get('QuestionAnswer') if (user.passWordQA != "") and (user.passWordQA == str(questionId) + ";" + questionAnswer): try: user.updatePassWord(args.get('NewPassWord')) db.session.merge(user) db.session.commit() return jsonify({'Status': 'Success', 'StatusCode': 0, 'Msg': "操作成功!", 'User': user.toJson()}), 200 except ValueError, error: logger.info('ValueError: errorMsg = {}'.format(error.message)) return jsonify({'Status': 'Failed', 'StatusCode': -1, 'Msg': error.message}), 400
def getUserDetailInfo(args): userList = request.get_json().get('UserList') userId = userList.get('UserId', '') phone = userList.get('Phone', '') user = User.getUserByIdOrPhoneOrMail(id=userId, phone=phone) userJson = user.toJson() userJson.pop('passWord', None) if user: return jsonify({ 'Status': 'Success', 'StatusCode': 0, 'Msg': '操作成功!', 'UserInfo': userJson }), 200 else: return jsonify({ 'Status': 'Success', 'StatusCode': -1, 'Msg': '操作失败,用户id={}不存在!'.format(userId) }), 400
def createShare(args): user = User.getUserByIdOrPhoneOrMail(id=args.get('UserId', ''), phone=args.get('Phone', '')) if Share.query.filter_by(userId=user.id, title=args.get('Title'), address=args.get('Address')).count() > 0: return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': '你已经发布过相同的分享了!' }), 400 try: (longitude, latitude) = addressGeoCoding(args.get('Address')) except Exception as error: return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': str(error) }), 400 hotPoint = HotPoint.checkExist(args.get('HotPointTag')) shareDict = dict(args) shareDict.update({ 'UserId': user.id, 'HotPointIds': hotPoint.id, 'Longitude': longitude, 'ImageUrls': ','.join(args.get('ImageUrls')), 'Latitude': latitude }) shareDict['Address'] = args.get('Address') logger.info("createShare, shareObj = \n" + pprint.pformat(shareDict)) shareObj = Share(**shareDict) db.session.add(shareObj) db.session.commit() return jsonify({ 'Status': 'Success', 'StatusCode': 0, 'Msg': "操作成功!", 'Share': shareObj.toJson() }), 200
def updateUserPermission(args): userPermissonList = request.get_json().get("UserPermissionList") userId = userPermissonList.get('UserId', None) phone = userPermissonList.get('Phone', None) user = User.getUserByIdOrPhoneOrMail(userId, phone) if not user: if phone: return jsonify({'Status': 'Failed', 'StatusCode':-1, 'Msg': "User phone={} does't exist".format(phone)}), 400 if userId: return jsonify({'Status': 'Failed', 'StatusCode':-1, 'Msg': "User id={} does't exist".format(userId)}), 400 macAddress = userPermissonList.get('Mac', '') machine = Machine.getMachineByMac(macAddress) if not machine: return jsonify({'Status': 'Failed', 'StatusCode':-1, 'Msg': "MacAddress {} does't exist".format(macAddress)}), 400 permission = userPermissonList.get('Permission') startTime = userPermissonList.get('StartTime', None) endTime = userPermissonList.get('EndTime', None) money = userPermissonList.get('Money', -1) quanXian = QuanXian.query.filter_by(userId=user.id, machineId=machine.id).order_by('id desc').first() quanXian.permission = permission if not quanXian: return jsonify({'Status': 'Failed', 'StatusCode':-1, 'Msg': "User {} don't use machine {}".format(user.id, macAddress)}), 400 if money != -1: quanXian.money = money if startTime != None: quanXian.startTime = startTime if endTime != None: quanXian.endTime = endTime db.session.merge(quanXian) db.session.commit() pushContent = {'Action': 'updateUserPermission', 'Permission': permission, 'Mac': macAddress, 'Money':money, 'StartTime':startTime, 'EndTime':endTime} pushMessageToSingle([user.pushToken], TransmissionTemplateDemo( json.dumps(pushContent) )) return jsonify({'Status' : 'Success', 'StatusCode':0, 'Msg' : '操作成功!'}), 200
def createOrder(args): user = User.getUserByIdOrPhoneOrMail(id=args.get('UserId', ''), phone=args.get('Phone', '')) order = Order(user.id, args.get('ShareId'), args.get('Money')) share = Share.query.filter_by(id=args.get('ShareId')).first() if not share: return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': "ShareId {} does't exist".format(share.id) }), 400 db.session.add(order) db.session.commit() print order.toJson() url = create_partner_trade_by_buyer(order.orderId, share.title, share.address, str(args.get('Money'))) orderJson = order.toJson() orderJson['alipayUrl'] = url return jsonify({ 'Status': 'Success', 'StatusCode': 0, 'Order': orderJson, 'Msg': '操作成功!' }), 200
def updateUserPermission(args): userPermissonList = request.get_json().get("UserPermissionList") userId = userPermissonList.get('UserId', None) phone = userPermissonList.get('Phone', None) user = User.getUserByIdOrPhoneOrMail(userId, phone) if not user: if phone: return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': "User phone={} does't exist".format(phone) }), 400 if userId: return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': "User id={} does't exist".format(userId) }), 400 macAddress = userPermissonList.get('Mac', '') machine = Machine.getMachineByMac(macAddress) if not machine: return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': "MacAddress {} does't exist".format(macAddress) }), 400 permission = userPermissonList.get('Permission') startTime = userPermissonList.get('StartTime', None) endTime = userPermissonList.get('EndTime', None) money = userPermissonList.get('Money', -1) quanXian = QuanXian.query.filter_by( userId=user.id, machineId=machine.id).order_by('id desc').first() quanXian.permission = permission if not quanXian: return jsonify({ 'Status': 'Failed', 'StatusCode': -1, 'Msg': "User {} don't use machine {}".format(user.id, macAddress) }), 400 if money != -1: quanXian.money = money if startTime != None: quanXian.startTime = startTime if endTime != None: quanXian.endTime = endTime db.session.merge(quanXian) db.session.commit() pushContent = { 'Action': 'updateUserPermission', 'Permission': permission, 'Mac': macAddress, 'Money': money, 'StartTime': startTime, 'EndTime': endTime } pushMessageToSingle([user.pushToken], TransmissionTemplateDemo(json.dumps(pushContent))) return jsonify({'Status': 'Success', 'StatusCode': 0, 'Msg': '操作成功!'}), 200