def get(self): args = parser.parse_args() token = args['token'] cursor, conn = connect_db() cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) timepoint = float(re.sub(r'.*_', '', token)) userID = int(re.search(r"id(\d+)_", token).group(1)) if time.time() - timepoint > 600: error = "Request timeout." return { "name": None, "gender": None, "phone": None, "date_of_birth": None, "self_description": None, "photo": None, "reason": error }, 400 cursor.execute( "select id,name,gender,phone,date_of_birth,self_description from user_information where id = '{}';" .format(userID)) results = cursor.fetchall() user_info = results[0] cursor.execute( "select position from user_photo where user_id='{}';".format( user_info['id'])) results = cursor.fetchall() user_info.pop('id') user_info['date_of_birth'] = str(user_info['date_of_birth']) user_info['photo'] = results[0]['position'] user_info['reason'] = None close_db(conn) return user_info, 200
def get(self): args = parser.parse_args() item = args['item_id'] error = None if item == None: error = 'Item ID required.' return {'review': [], 'result': error}, 400 cursor, conn = connect_db() cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) cursor.execute( "select * from item_comment where item_id={};".format(item)) reviews = cursor.fetchall() for r in reviews: cursor.execute( "select position from user_photo where user_id={};".format( r['user_id'])) results = cursor.fetchall() if len(results) == 0: r['user_photo'] = None else: r['user_photo'] = results[0]['position'] r['date'] = str(r['date']) r.pop('id') r.pop('item_id') close_db(conn) return {'review': reviews, 'result': error}, 200
def post(self): args = parser.parse_args() token = args['token'] host = args['host_id'] transaction = args['transaction_id'] status = args['status'] error = None userID, timepoint = token_parser(token) if time.time() - timepoint > 600: error = 'Request timeout.' elif host == None: error = 'Host id required.' elif host != userID: error = 'Request rejected.' elif transaction == None: error = 'Transaction id required.' elif status == None or not status in ('accept', 'decline'): error = 'Invalid status required.' if error != None: return {'reason': error}, 400 cursor, conn = connect_db() cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) if status == 'accept': status_code = 'A' cursor.execute( "select start_date,end_date,item_id from transaction where id={};" .format(transaction)) results = cursor.fetchall() start = results[0]['start_date'] end = results[0]['end_date'] date = start item = results[0]['item_id'] while date <= end: cursor.execute( "insert into unavailable_date (item_id,date) values({},'{}')" .format(item, date)) conn.commit() date += datetime.timedelta(1) elif status == 'decline': status_code = 'U' cursor.execute( "update transaction set status='{}' where id={};".format( status_code, transaction)) conn.commit() cursor.execute( "select user_id from transaction where id={};".format(transaction)) user = cursor.fetchall()[0]['user_id'] cursor.execute( "update user_information set unread_trip = unread_trip+1 where id = {};" .format(user)) conn.commit() close_db(conn) return {'reason': error}, 200
def post(self): args = parser.parse_args() email = args['email'] password = args['password'] name = args['name'] gender = args['gender'] birthday = args['date_of_birth'] description = args['self_description'] photo = args['photo'] phone = args['phone'] error = None cursor, conn = connect_db() cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) if email == None: error = 'Email is required.' elif password == None: error = 'Password is required.' elif name == None: error = 'Username is required.' elif not gender in ('F', 'M'): error = 'Gender should be F or M' if error != None: return {'reason': error}, 400 cursor.execute( "select * from user_information where email='{}';".format(email)) results = cursor.fetchall() if len(results) != 0: error = 'User already exists.\nCreate user with another email.' return {'reason': error}, 403 current_date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) cursor.execute("insert into user_information \ (email,password,name,gender,date_of_birth,self_description,create_date,unread_message,unread_trip,verified,phone)\ values('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}');".format( email, password, name, gender, birthday, description, current_date, 0, 0, False, phone)) conn.commit() if not photo: photo = "https://a0.muscache.com/im/pictures/20180027/bb9ccce5_original.jpg?aki_policy=large" cursor.execute( "select id from user_information where email='{}';".format(email)) results = cursor.fetchall() user_ID = results[0]['id'] cursor.execute( "insert into user_photo (user_id,position) values('{}','{}');". format(user_ID, photo)) conn.commit() close_db(conn) return {'reason': error}, 200
def post(self): args = parser.parse_args() token = args['token'] user = args['user_id'] transaction = args['transaction_id'] date = args['date'] comment = args['comment'] accuracy = args['accuracy'] communication = args['communication'] cleanliness = args['cleanliness'] location = args['location'] checkin = args['check_in'] value = args['value'] error = None userID, timepoint = token_parser(token) if time.time() - timepoint > 600: error = 'Request timeout.' elif user == None: error = 'User ID required.' elif user != userID: error = 'Request rejected.' elif transaction == None: error = 'Transaction required.' if error != None: return {'result': error}, 400 cursor, conn = connect_db() cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) cursor.execute( "select item_id from transaction where id={} and status='{}';". format(transaction, 'S')) results = cursor.fetchall() if len(results) == 0: error = "Transaction is not existing or not completed." return {'result': error}, 400 cursor.execute( "update transaction set rating_or_not=true where id={} and status='{}';" .format(transaction, 'S')) conn.commit() item = results[0]['item_id'] cursor.execute( "insert into item_comment (user_id,item_id,date,comment,accuracy,communication,cleanliness,location,check_in,value) values({},{},'{}','{}','{}','{}','{}','{}','{}','{}');" .format(user, item, date, comment, accuracy, communication, cleanliness, location, checkin, value)) conn.commit() close_db(conn) return {'reason': error}, 200
def post(self,request): args = parser.parse_args() token = args['token'] sender = args['sender_id'] receiver = args['receiver_id'] content = args['message_content'] date = args['date'] error = None timepoint = float(re.sub(r'.*_','',token)) userID = int(re.search(r"id(\d+)_",token).group(1)) if time.time()-timepoint > 600 : error = "Request timeout." return {'reason': error},400 if userID != sender: error = "Request rejected." return {'reason': error},400 cursor, conn = connect_db() cursor.execute("insert into message (sender,receiver,content,date,read_or_not) values ('{}','{}','{}','{}','{}');".format(sender,receiver,content,date,False)) conn.commit() close_db(conn) return {'reason': error},200
def put(self): args = parser.parse_args() token = args['token'] name = args['name'] gender = args['gender'] phone = args['phone'] birthday = args['date_of_birth'] description = args['self_description'] photo = args['photo'] error = None timepoint = float(re.sub(r'.*_', '', token)) userID = int(re.search(r"id(\d+)_", token).group(1)) if time.time() - timepoint > 600: error = "Request timeout." return {'reason': error}, 400 if name == None: error = 'Username is required.' return {'reason': error}, 404 elif not gender in ('F', 'M'): error = 'Incorrect gender type.' return {'reason': error}, 404 cursor, conn = connect_db() cursor.execute( "update user_information set name='{}', gender='{}', phone='{}',\ date_of_birth='{}',self_description='{}' where id='{}';".format( name, gender, phone, birthday, description, userID)) conn.commit() cursor.execute( "update user_photo set position='{}' where user_id='{}';".format( photo, userID)) conn.commit() close_db(conn) return {'reason': error}, 200
def get(self): args = parser.parse_args() token = args['token'] user = args['user_id'] error = None timepoint = float(re.sub(r'.*_','',token)) user_check = int(re.search(r"id(\d+)_",token).group(1)) if time.time()-timepoint > 600 : error = 'Request timeout.' elif user==None: error = 'User required.' elif user != user_check: error = 'Request rejected.' if error != None: return {'transaction':[], 'reason': error},400 cursor, conn = connect_db() cursor = conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor) cursor.execute("select id,item_id,user_id,host_id,status,start_date,end_date,rating_or_not from transaction where user_id = {} or host_id={};".format(user,user)) trips = cursor.fetchall() current_date = datetime.date.today() for t in trips: if t['user_id'] == user: cursor.execute("select name from user_information where id={};".format(t['host_id'])) user_name = cursor.fetchall()[0]['name'] cursor.execute("select position from user_photo where user_id={};".format(t['host_id'])) results = cursor.fetchall() if len(results)==0: user_photo = None else: user_photo = results[0]['position'] t['trip_or_not'] = True t['user_id']=t['host_id'] else: cursor.execute("select name from user_information where id={};".format(t['user_id'])) user_name = cursor.fetchall()[0]['name'] cursor.execute("select position from user_photo where user_id={};".format(t['user_id'])) results = cursor.fetchall() if len(results)==0: user_photo = None else: user_photo = results[0]['position'] t['trip_or_not'] = False t['user_name'] = user_name t['user_photo'] = user_photo cursor.execute("select name from item where id={};".format(t['item_id'])) item_name = cursor.fetchall()[0]['name'] t['house_name'] = item_name t['transaction_id'] = t['id'] t['house_id'] = t['item_id'] t['review_or_not'] = t['rating_or_not'] if t['status'] == 'A': if current_date > t['end_date']: cursor.execute("update transaction set status='{}' where id={};".format('S',t['id'])) conn.commit() t['status'] = 'completed' else: t['status'] = 'accepted' elif t['status'] == 'U': t['status'] = 'declined' elif t['status'] == 'P': t['status'] = 'pending' elif t['status'] == 'S': t['status'] = 'completed' t['start_date'] = str(t['start_date']) t['end_date'] = str(t['end_date']) t.pop('id') t.pop('item_id') t.pop('host_id') t.pop('rating_or_not') cursor.execute("update user_information set unread_trip=0 where id ={};".format(user)) conn.commit() close_db(conn) return {'transaction': trips, 'reason': error},200
def post(self): args = parser.parse_args() email = args['email'] password = args['password'] error = None token = None photo = None unread_message = None unread_trip = None verified = None user_id = None cursor, conn = connect_db() cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) cursor.execute( "select password,verified,id,unread_message,unread_trip from user_information where email = '{}';" .format(email)) results = cursor.fetchall() if len(results) != 0: user = results[0] if password == user['password']: token = "id" + str(user['id']) + "_" + str(int(time.time())) verified = user['verified'] user_id = user['id'] unread_message = user['unread_message'] unread_trip = user['unread_trip'] cursor.execute( "select position from user_photo where user_id = '{}';". format(user['id'])) results = cursor.fetchall() if len(results) != 0: photo = results[0]['position'] else: photo = None else: error = 'incorrect password' else: error = 'no such user' close_db(conn) if error == None: return { "reason": error, "user_id": user_id, "photo": photo, 'unread_message': unread_message, 'unread_trip': unread_trip, "token": token, "verified": verified }, 200 else: return { "reason": error, "user_id": user_id, "photo": photo, 'unread_message': unread_message, 'unread_trip': unread_trip, "token": token, "verified": verified }, 400
room_arrangement, common_spaces, bath_number, max_people, amenities) values ('3', '{}', 'good house, do not miss', '{}', 'Australia', '{}', '{}', '{}', '{}', '1', '{}', '0', '0', '0', '0', '0', '0', '0', '{{{{1}}}}', '{{}}', '{}', '{}', '{{1,2,3,4,5}}')""".format( item['title'], datetime.datetime.now(), city, suburbs, address, postcode, item['price'], int(item['bathroom_num']), item['guest_num']) print(a) cursor.execute( """insert into Item(hoster_id, name, description, create_date, country, city, suburb, address, post_code, type, price, accuracy, communication, cleanliness, location, check_in, value, rating_number, room_arrangement, common_spaces, bath_number, max_people, amenities) values ('3', '{}', 'good house, do not miss', '{}', 'Australia', '{}', '{}', '{}', '{}', '1', '{}', '0', '0', '0', '0', '0', '0', '0', '{{{{1}}}}', '{{}}', '{}', '{}', '{{1,2,3,4,5}}')""".format( item['title'], datetime.datetime.now(), city, suburbs, address, postcode, item['price'], int(item['bathroom_num']), item['guest_num'])) conn.commit() for photo in item['album']: cursor.execute( """insert into Item_photo(Item_id, position) values ('{}', '{}')""" .format(i, photo)) i += 1 conn.commit() # cursor.execute("""select * from Item(Item_id, posision) values (%s, %s)""", i, photo) # rows = cursor.fetchall() # print(rows) close_db(conn)
def post(self): args = parser.parse_args() token = args['token'] user = args['user_id'] transaction = args['transaction_id'] status = args['status'] error = None userID, timepoint = token_parser(token) if time.time() - timepoint > 600: error = 'Request timeout.' elif user == None: error = 'User id required.' elif user != userID: error = 'Request rejected.' elif transaction == None: error = 'Transaction id required.' elif status == None or status != 'cancel': error = 'Invalid status required.' if error != None: return {'reason': error}, 400 cursor, conn = connect_db() cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) cursor.execute( "select status from transaction where id={};".format(transaction)) origin_status = cursor.fetchall()[0]['status'] if origin_status == 'S': error = 'Transaction already completed.' return {'reason': error}, 400 elif origin_status == 'U': error = 'Transaction already declined.' return {'reason': error}, 400 elif origin_status == 'A': cursor.execute( "select start_date,end_date,item_id from transaction where id={};" .format(transaction)) results = cursor.fetchall() start = results[0]['start_date'] end = results[0]['end_date'] date = start item = results[0]['item_id'] while date <= end: cursor.execute( "delete from unavailable_date where item_id={} and date='{}';" .format(item, date)) conn.commit() date += datetime.timedelta(1) cursor.execute( "update transaction set status='{}' where id={};".format( 'U', transaction)) conn.commit() cursor.execute( "select host_id from transaction where id={};".format(transaction)) host = cursor.fetchall()[0]['host_id'] cursor.execute( "update user_information set unread_trip = unread_trip+1 where id = {};" .format(host)) conn.commit() close_db(conn) return {'reason': error}, 200
def get(self,request): if request == 'detail': args = parser.parse_args() token = args['token'] user1_ID = args['user_id'] user2_ID = args['receiver_id'] error = None userID = int(re.search(r"id(\d+)_",token).group(1)) timepoint = float(re.sub(r'.*_','',token)) if time.time()-timepoint > 600 or user1_ID != userID: return {'message':None, 'sender':None, 'receiver':None},400 cursor, conn = connect_db() cursor = conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor) cursor.execute("select id,name from user_information where id='{}';".format(user1_ID)) results = cursor.fetchall() if len(results) == 0: return {'message':None, 'sender':None, 'receiver':None},400 user1 = results[0] cursor.execute("select position from user_photo where user_id='{}';".format(user1_ID)) results = cursor.fetchall() if len(results) == 0: user1['photo'] = None else: user1['photo'] = results[0]['position'] cursor.execute("select id,name from user_information where id='{}';".format(user2_ID)) results = cursor.fetchall() if len(results) == 0: return {'message':None, 'sender':None, 'receiver':None},400 user2 = results[0] cursor.execute("select position from user_photo where user_id='{}';".format(user2_ID)) results = cursor.fetchall() if len(results) == 0: user2['photo'] = None else: user2['photo'] = results[0]['position'] cursor.execute("select content,sender,date from message where sender='{}' and receiver='{}' or sender='{}' and receiver='{}';".format(user1_ID,user2_ID,user2_ID,user1_ID)) results = cursor.fetchall() message = sorted(results,key=lambda message:message["date"],reverse=True) for m in message: m['date'] = str(m['date']) cursor.execute("select count(*) as nums from message where sender='{}' and receiver='{}' and read_or_not = '{}';".format(user2_ID,user1_ID,False)) results = cursor.fetchall() count = results[0]['nums'] cursor.execute("update message set read_or_not=True where sender = '{}' and receiver = '{}' and read_or_not = '{}';".format(user2_ID,user1_ID,False)) conn.commit() cursor.execute("update user_information set unread_message = unread_message-'{}' where id = '{}';".format(count,user1_ID)) conn.commit() close_db(conn) return {'message':message, 'user':user1, 'sender':user2},200 elif request == 'preview': args = parser.parse_args() token = args['token'] userID_check = args['user_id'] error = None userID = int(re.search(r"id(\d+)_",token).group(1)) timepoint = float(re.sub(r'.*_','',token)) if time.time()-timepoint > 600 or userID_check != userID: return {'message':None},400 cursor, conn = connect_db() cursor.execute("select * from message where sender = '{}' or receiver = '{}';".format(userID,userID)) results = cursor.fetchall() close_db(conn) messages_dict = {} for record in results: if record[1] == userID: chat_with = record[2] else: chat_with = record[1] if not chat_with in messages_dict.keys(): messages_dict[chat_with] = record elif str(record[4]) > str(messages_dict[chat_with][4]): messages_dict[chat_with] = record messages_list=[] message={} for m in messages_dict.values(): message['last_message'] = m[3] message['user_id'] = userID message['receiver_id'] = m[2] message['sender_id'] = m[1] message['read_or_not'] = m[6] messages_list.append(message) message ={} return {'message' :messages_list},200