def updateWatch(): #only response to post request if request.method == 'POST': token = request.json['userToken'] id = request.json['userId'] action = request.json['action'] 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' #unwatch when action is exist if action == 1: petId = request.json['petId'] return deleteWatch(userId, petId, cnx) elif action == 0: petId = request.json['petId'] return createWatch(userId, petId, cnx) finally: cnx.close() else: abort(404)
def upadateLike(): if request.method == 'POST': token = request.json['token'] id = request.json['id'] moment = request.json['moment'] action = request.json['action'] 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' if action == 1: return addLike(moment, id, cnx) elif action == 0: return deleteLike(moment, id, cnx) finally: cnx.close() else: abort(404)
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 acceptRequest(): if request.method == 'POST': pet = request.json['petId'] id = request.json['userId'] token = request.json['userToken'] cnx = mysql.connector.connect(**config) try: #check user statues 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 if request exist check = checkRequest(pet, id, cnx) if len(check) is 0 or check[0] is None: return '2' #update pet relative add = addRelative(id, pet, cnx) if add == '0': return '3' #remove request return removeRequest(pet, id, 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)
def createPet(): #only response to post if request.method == 'POST': petName = request.form['name'][:10].strip() if len(petName) == 0: return '2' petGender = int(request.form['gender']) if petGender != 0 and petGender != 1: return '2' petType = int(request.form['type']) if petType != 0 and petType != 1 and petType != 2 and petType != 3 and petType != 4: return '2' petNature = int(request.form['nature']) if petNature != 0 and petNature != 1 and petNature != 2 and petNature != 3: return '2' if 'file' not in request.files: return '2' petFile = request.files['file'] id = int(request.form['id']) token = request.form['token'] 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' create = addPet(userId, petName, petGender, petType, petNature, cnx) if create == '0': return '0' return petAvatar(petFile, int(create)) finally: cnx.close() else: abort(404)
def sendComment(): if request.method == 'POST': token = request.json['token'] id = request.json['id'] moment = request.json['moment'] content = request.json['content'] 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' return createComment(id, moment, content, cnx) finally: cnx.close() else: abort(404)
def deleteRequest(): if request.method == 'POST': pet = request.json['petId'] id = request.json['userId'] token = request.json['userToken'] cnx = mysql.connector.connect(**config) try: #check user statues user = findUser(token, cnx) if user == '0': return '0' elif user is None: return '2' else: userId = user[0] if userId != id: return '2' #remove request return removeRequest(pet, id, cnx) finally: cnx.close() else: abort(404)
def profileImage(): #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'] 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' finally: cnx.close() return userAvatar(image, id) else: abort(404)
def profileName(): #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'] 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' return setName(id, name, cnx) finally: cnx.close() else: abort(404)