def endRelation(): #only response to post if request.method == 'POST': id = request.json['id'] token = request.json['token'] pet = request.json['pet'] cnx = mysql.connector.connect(**config) try: user = findUser(token, cnx) if user == '0': return '0' elif user is None: return '2' else: userId = user[0] if userId != id: return '2' #check pet belonging belong = getBelong(pet, cnx) if belong == '0': return '0' #must be relative to end if belong[1] != id: return '2' return deleteRelative(pet, cnx) finally: cnx.close() else: abort(404)
def petImage(): #only response to post if request.method == 'POST': if 'file' not in request.files: return '2' image = request.files['file'] id = int(request.form['id']) token = request.form['token'] pet = int(request.form['pet']) cnx = mysql.connector.connect(**config) try: user = findUser(token, cnx) if user == '0': return '0' elif user is None: return '2' else: userId = user[0] if userId != id: return '2' #check pet belonging belong = getBelong(pet, cnx) if belong == '0': return '0' if belong[0] != id and belong[1] != id: return '2' finally: cnx.close() return petAvatar(image, pet) else: abort(404)
def petName(): #only response to post if request.method == 'POST': name = request.json['name'][:10].strip() if len(name) == 0: return '2' id = request.json['id'] token = request.json['token'] pet = request.json['pet'] cnx = mysql.connector.connect(**config) try: user = findUser(token, cnx) if user == '0': return '0' elif user is None: return '2' else: userId = user[0] if userId != id: return '2' #check pet belonging belong = getBelong(pet, cnx) if belong == '0': return '0' if belong[0] != id and belong[1] != id: return '2' return newName(pet, name, cnx) finally: cnx.close() else: abort(404)
def petRequest(): if request.method == 'POST': senderId = request.json['senderId'] token = request.json['token'] petId = request.json['petId'] receiverId = request.json['receiverId'] cnx = mysql.connector.connect(**config) try: user = findUser(token, cnx) if user == '0': return '0' elif user is None: return '2' else: userId = user[0] if userId != senderId: return '2' #check pet belonging belong = getBelong(petId, cnx) if belong == '0': return '0' if belong[0] != senderId: return '2' return sendRequest(senderId, receiverId, petId, cnx) finally: cnx.close() else: abort(404)
def transferOwnership(): #only response to post if request.method == 'POST': id = request.json['id'] token = request.json['token'] pet = request.json['pet'] cnx = mysql.connector.connect(**config) try: user = findUser(token, cnx) if user == '0': return '0' elif user is None: return '2' else: userId = user[0] if userId != id: return '2' #check pet belonging belong = getBelong(pet, cnx) if belong == '0': return '0' #must be owner to transfer if belong[0] != id or belong[1] is None: return '2' return transferPet(pet, id, belong[1], cnx) finally: cnx.close() else: abort(404)
def createMoment(): #only response to post if request.method == 'POST': momentMessage = request.form['message'][:120].strip() if len(momentMessage) == 0: return '2' if 'file' not in request.files: return '2' momentFile = request.files['file'] id = int(request.form['id']) token = request.form['token'] pet = int(request.form['pet']) cnx = mysql.connector.connect(**config) try: #check user id user = findUser(token, cnx) if user == '0': return '0' elif user is None: return '2' else: userId = user[0] if userId != id: return '2' #check pet belonging belong = getBelong(pet, cnx) if belong == '0': return '0' if belong[0] != id and belong[1] != id: return '2' upload = momentImage(momentFile, pet) if upload == '0' or upload == '2': return upload add = addMoment(upload, momentMessage, pet, cnx) finally: cnx.close() if add == '0': return '0' else: return add else: abort(404)