def deletePost(msg_received, header): user_id = tokens.getID(header) post_id = msg_received["post_id"] if user_id == "Error expired token" or user_id == "Error invalid token": return json.dumps({'Error': 'login in again'}) else: dbconfig = read_db_config() conn = MySQLConnection(**dbconfig) cursor = conn.cursor() cursor.execute("SELECT * FROM `user` WHERE user_id='" + str(user_id) + "';") account = cursor.fetchall() if len(account) == 1: data = "" for info in account: result = collection.delete_one({"post_id": post_id}) data = result.acknowledged return json.dumps({"deleted": data}) else: return json.dumps({"Error": "user not in DB"})
def update_email(msg_received,header): email = msg_received["email"] code =msg_received["code"] user_id = tokens.getID(header) random_number = random.randint(10000, 99999) dbconfig = read_db_config() conn = MySQLConnection(**dbconfig) cursor = conn.cursor() if user_id == "Error expired token" or user_id == "Error invalid token": conn.close() cursor.close() return json.dumps({'Error': 'login in again'}) else: cursor.execute("SELECT * FROM `verification` WHERE user_id='" + str(user_id) + "' AND verification_code="+str(code)+";") row = cursor.fetchall() if len(row) == 1: #for record in row: cursor.execute("UPDATE `user` SET `email` = '" + email + "' WHERE user_id=" + str(user_id) + ";") conn.commit() cursor.execute("UPDATE `verification` SET `verified` = '1' WHERE user_id=" + str(user_id) + ";") conn.commit() conn.close() cursor.close() return json.dumps({'notification':'email updated'}) else: conn.close() cursor.close() return json.dumps({'Error':'email not updated'})
def update_password(msg_received, header): password = msg_received["password"] user_id = tokens.getID(header) dbconfig = read_db_config() conn = MySQLConnection(**dbconfig) cursor = conn.cursor() if user_id == "Error expired token" or user_id == "Error invalid token": conn.close() cursor.close() return json.dumps({'Error': 'login in again'}) else: cursor.execute("SELECT * FROM `user` WHERE user_id='" + str(user_id) + "';") row = cursor.fetchall() if len(row) == 1: cursor.execute("UPDATE `user` SET `password` = '" + str(password) + "' WHERE user_id=" + str(user_id) + ";") conn.commit() conn.close() cursor.close() return json.dumps({'notification': 'password updated'}) else: conn.close() cursor.close() return json.dumps({'Error': 'password not updated'})
def likePost(msg_received,header): user_id = tokens.getID(header) post_id=msg_received["post_id"] if user_id == "Error expired token" or user_id == "Error invalid token": return json.dumps({'Error': 'login in again'}) else: dbconfig = read_db_config() conn = MySQLConnection(**dbconfig) cursor = conn.cursor() cursor.execute("SELECT * FROM user WHERE user_id='" + str(user_id) + "';") account = cursor.fetchall() if len(account) == 1: data="" for info in account: userName=info[2] #result = collection.find({"post_id": "9r1B9RUOym" }) result=collection.update_one({ 'post_id':post_id },{ '$push': {'post_likes':userName}}) data=result.acknowledged return json.dumps({"liked":data}) else: print("error occurred")
def editPost(msg_received, header): user_id = tokens.getID(header) post_details = msg_received["post_details"] post_id = msg_received["post_id"] if user_id == "Error expired token" or user_id == "Error invalid token": return json.dumps({'Error': 'login in again'}) else: dbconfig = read_db_config() conn = MySQLConnection(**dbconfig) cursor = conn.cursor() cursor.execute("SELECT * FROM user WHERE user_id='" + str(user_id) + "';") account = cursor.fetchall() if len(account) == 1: data = "" for info in account: result = collection.update_many( {'post_id': post_id}, {'$set': { 'post_details': post_details }}) data = result.acknowledged return json.dumps({"edited": data}) else: print("error occurred")
def wall_post(msg_received, header): dbconfig = read_db_config() conn = MySQLConnection(**dbconfig) cursor = conn.cursor() d = collections.OrderedDict() user_id = tokens.getID(header) post_details = msg_received["post_details"] timestamp = msg_received["timestamp"] #post_images = msg_received["post_images"] post_id = runner() # audio = msg_received["audio_files"] # video = msg_received["video_files"] images = [] #for i in post_images: # images.append(i) if user_id == "Error expired token" or user_id == "Error invalid token": return json.dumps({'Error': 'login in again'}) else: user_post = { "post_id": post_id, "post_details": post_details, "posted_by": user_id, "timestamp": timestamp, "post_likes": [] } #"images": images, #, #"audio": audio, #"video": video collection.insert_one(user_post) cursor.execute( "INSERT INTO `posts` (`id`, `user_id`, `post_id`, `date_created`,`uniqueID`) VALUES (NULL, '" + str(user_id) + "', '" + str(post_id) + "', CURRENT_TIMESTAMP,'null');") conn.commit() conn.close() cursor.close() result = collection.find({"post_id": str(post_id)}) data = [] for i in result: d['post_id'] = i['post_id'] d['post_details'] = i['post_details'] d['posted_by'] = i['posted_by'] d['timestamp'] = i['timestamp'] d['post_likes'] = i['post_likes'] data.append(d) return json.dumps(data)
def verify_email(msg_received, header): email = msg_received["email"] user_id = tokens.getID(header) random_number = random.randint(10000, 99999) dbconfig = read_db_config() conn = MySQLConnection(**dbconfig) cursor = conn.cursor() if user_id == "Error expired token" or user_id == "Error invalid token": conn.close() cursor.close() return json.dumps({'Error': 'login in again'}) else: cursor.execute("SELECT * FROM `user` WHERE user_id='" + str(user_id) + "';") row = cursor.fetchall() if len(row) == 1: for record in row: cursor.execute("SELECT * FROM `user` WHERE email='" + str(email) + "';") mails = cursor.fetchall() if len(mails) == 0: send_mail.sendVerification(email, str(record[2]), random_number) cursor.execute( "UPDATE `verification` SET `verification_code` = '" + str(random_number) + "' WHERE user_id=" + str(user_id) + ";") conn.commit() cursor.execute( "UPDATE `verification` SET `verified` = '2' WHERE user_id=" + str(user_id) + ";") conn.commit() conn.close() cursor.close() return json.dumps({'notification': 'code sent'}) else: conn.close() cursor.close() return json.dumps({'Error': 'Email taken'}) else: conn.close() cursor.close() return json.dumps({'Error': 'code not sent'})
def fetchAll(header): user_id = tokens.getID(header) if user_id == "Error expired token" or user_id == "Error invalid token": return json.dumps({'Error': 'login in again'}) else: dbconfig = read_db_config() conn = MySQLConnection(**dbconfig) cursor = conn.cursor() cursor.execute("SELECT * FROM `profile_photo` WHERE user_id='" + str(user_id) + "';") profile_pic = cursor.fetchall() if len(profile_pic) == 1: for info in profile_pic: result = collection.find({"posted_by": user_id}) data = [] for i in result: posts = { 'post_id': i['post_id'], 'post_details': i['post_details'], 'user': { 'userName': i['userName'], 'fullName': i['fullName'], 'posted_by': i['posted_by'], 'profile_photo': info[2] }, 'uniqueID': info[3], 'timestamp': i['timestamp'], 'images': i['images'], 'audio': i['audio'], 'video': i['video'], 'post_likes': i['post_likes'] } data.append(posts) return json.dumps(data)
def update_userName(msg_received,header): userName = msg_received["userName"] user_id = tokens.getID(header) dbconfig = read_db_config() conn = MySQLConnection(**dbconfig) cursor = conn.cursor() if user_id == "Error expired token" or user_id == "Error invalid token": #print(header) conn.close() cursor.close() return json.dumps({'Error': 'login in again'}) else: #print(str(user_id)) cursor.execute("SELECT * FROM `user` WHERE user_id='" + str(user_id) + "';") row = cursor.fetchall() if len(row) == 1: for data in row: old_username=str(data[2]) #likes collection.update_many({"post_likes": old_username}, {'$push': {'post_likes': str(userName) }}) collection.update_many({"post_likes": str(userName)}, {'$pull': {'post_likes':old_username}}) #posts collection.update_many({'userName': old_username}, {'$set': {'userName': str(userName)}}) cursor.execute("UPDATE `user` SET `userName` = '" + str(userName) + "' WHERE user_id=" + str(user_id) + ";") conn.commit() conn.close() cursor.close() return json.dumps({'notification':'user name updated'}) else: conn.close() cursor.close() return json.dumps({'Error':'user name not updated'})
def persist(header): details = [] tags = [] links = {} id_no = " " d = collections.OrderedDict() t = collections.OrderedDict() l = collections.OrderedDict() dbconfig = read_db_config() conn = MySQLConnection(**dbconfig) cursor = conn.cursor() userID = tokens.getID(header) if userID == "Error expired token" or userID == "Error invalid token": return json.dumps({'Error': 'login in again'}) else: try: cursor.execute("SELECT * FROM user where user_id ='" + str(userID) + "';") row = cursor.fetchall() #while row is not None: if len(row) == 1: for record in row: #print(record) d['access'] = "true" #d['user_id'] = record[0] id_no = record[0] d['full_name'] = record[1] #d['second_name'] = record[2] d['username'] = record[2] d['email'] = record[3] #d['password'] = record[4] d['phone_number'] = record[5] d['location'] = record[6] #d['creation_date'] = record[7] #details.append(d) cursor.execute("SELECT * FROM profile_photo where user_id = " + str(id_no)) profile_pic = cursor.fetchall() if len(profile_pic) == 1: for record in profile_pic: d['profile_photo'] = record[2] #details.append(d) cursor.execute("SELECT * FROM cover_photo where user_id = " + str(id_no)) profile_pic = cursor.fetchall() if len(profile_pic) == 1: for record in profile_pic: d['cover_photo'] = record[2] d['uniqueID'] = record[3] cursor.execute("SELECT * FROM verification where user_id = '" + str(id_no) + "';") verified = cursor.fetchall() if len(verified) == 1: for record in verified: d['verified'] = record[4] cursor.execute( "SELECT * FROM `subscription_price` where user_id = '" + str(id_no) + "';") sub_price = cursor.fetchall() if len(sub_price) == 1: for record in sub_price: d['subscription_price'] = record[2] cursor.execute("SELECT * FROM user_data where user_id = '" + str(id_no) + "';") user_data = cursor.fetchall() if len(user_data) == 1: for info in user_data: d['website'] = info[1] d['bio'] = info[2] tags = [info[3], info[4], info[5], info[6], info[7]] links = { 'facebook': info[8], 'twitter': info[9], 'instagram': info[10], 'youtube': info[11], 'snapchat': info[12], 'tiktok': info[13] } #print(json.dumps({'details': details})) conn.close() cursor.close() d['tags'] = tags d['links'] = links details.append(d) return json.dumps(details) else: conn.close() cursor.close() details_error = [] #print("result set is empty") d = collections.OrderedDict() d['access'] = "false" details_error.append(d) return json.dumps(details_error) except (Error, UnboundLocalError, TypeError, errors) as e: print(e) conn.close() cursor.close() d[e] = e.args details.append(d) return json.dumps(details) finally: conn.close() cursor.close()
def cover_photo(msg_received, header): user_id = tokens.getID(header) dbconfig = read_db_config() conn = MySQLConnection(**dbconfig) cursor = conn.cursor() if user_id == "Error expired token" or user_id == "Error invalid token": conn.close() cursor.close() return json.dumps({'Error': 'login in again'}) else: cursor.execute("SELECT * FROM `profile_photo` WHERE user_id='" + str(user_id) + "';") row = cursor.fetchall() if len(row) == 1: for record in row: uniqueID = record[3] S3_BUCKET = "firefansapp" file_type = msg_received["file_type"] fileName = "cover_photos/" + uniqueID my_config = Config(region_name='us-west-1', retries={ 'max_attempts': 10, 'mode': 'standard' }) s3 = boto3.client( 's3', aws_access_key_id=key_id['aws_access_key_id'], aws_secret_access_key=key_id['aws_secret_access_key'], config=my_config) presigned_post = s3.generate_presigned_post(Bucket=S3_BUCKET, Key=fileName, Fields={ "acl": "public-read", "Content-Type": file_type }, Conditions=[{ "acl": "public-read" }, { "Content-Type": file_type }], ExpiresIn=900) cursor.execute("UPDATE `cover_photo` SET `cover_pic` = '" + str('https://%s.s3.amazonaws.com/%s' % (S3_BUCKET, fileName)) + "' WHERE user_id=" + str(user_id) + ";") conn.commit() conn.close() cursor.close() return json.dumps({ 'data': presigned_post, 'image_url': 'https://%s.s3.amazonaws.com/%s' % (S3_BUCKET, fileName) })
def post_image(msg_received, header): user_id = tokens.getID(header) dbconfig = read_db_config() conn = MySQLConnection(**dbconfig) cursor = conn.cursor() if user_id == "Error expired token" or user_id == "Error invalid token": conn.close() cursor.close() return json.dumps({'Error': 'login in again'}) else: cursor.execute("SELECT * FROM `user` WHERE user_id='" + str(user_id) + "';") row = cursor.fetchall() if len(row) == 1: for record in row: uniqueID = str(generate_check.wall_post_gen()) S3_BUCKET = "firefansapp" file_type = msg_received["file_type"] fileName = "post_photos/" + uniqueID + "_" + str(user_id) my_config = Config(region_name='us-west-1', retries={ 'max_attempts': 10, 'mode': 'standard' }) s3 = boto3.client( 's3', aws_access_key_id=key_id['aws_access_key_id'], aws_secret_access_key=key_id['aws_secret_access_key'], config=my_config) presigned_post = s3.generate_presigned_post(Bucket=S3_BUCKET, Key=fileName, Fields={ "acl": "public-read", "Content-Type": file_type }, Conditions=[{ "acl": "public-read" }, { "Content-Type": file_type }], ExpiresIn=900) #cursor.execute("UPDATE `post_images` SET `image_url` = '" + str('https://%s.s3.amazonaws.com/%s' % (S3_BUCKET,fileName)) + "' AND SET `key_name` ='"+uniqueID+"' WHERE user_id=" + str(user_id) + ";") cursor.execute( "INSERT INTO `post_images` (`id`, `user_id`, `image_url`, `date_created`, `key_name`) VALUES (NULL, '" + str(user_id) + "', '" + str('https://%s.s3.amazonaws.com/%s' % (S3_BUCKET, fileName)) + "', CURRENT_TIMESTAMP, '" + str(uniqueID) + "_" + str(user_id) + "');") conn.commit() conn.close() cursor.close() return json.dumps({ 'data': presigned_post, 'post_image_url': 'https://%s.s3.amazonaws.com/%s' % (S3_BUCKET, fileName) })
def add_post(msg_received, header): dbconfig = read_db_config() conn = MySQLConnection(**dbconfig) cursor = conn.cursor() d = collections.OrderedDict() user_id = tokens.getID(header) post_details = msg_received["post_details"] timestamp = msg_received["timestamp"] post_images = msg_received["images"] audio = msg_received["audio"] video = msg_received["video"] post_id = runner() images = [] for i in post_images: images.append(i) if user_id == "Error expired token" or user_id == "Error invalid token": return json.dumps({'Error': 'login in again'}) else: cursor.execute("SELECT * FROM `user` WHERE user_id='" + str(user_id) + "';") name = cursor.fetchall() if len(name) == 1: for info in name: userName = info[2] fullName = info[1] user_post = { "post_id": post_id, "post_details": post_details, "posted_by": user_id, "userName": userName, "fullName": fullName, "timestamp": timestamp, "images": images, "post_likes": [], "audio": audio, "video": video } collection.insert_one(user_post) cursor.execute( "INSERT INTO `posts` (`id`, `user_id`, `post_id`, `date_created`,`uniqueID`) VALUES (NULL, '" + str(user_id) + "', '" + str(post_id) + "', CURRENT_TIMESTAMP,'null');") conn.commit() conn.close() cursor.close() result = collection.find({"post_id": post_id}) data = [] for i in result: d['post_id'] = i['post_id'] d['post_details'] = i['post_details'] d['userName'] = i['userName'] d['timestamp'] = i['timestamp'] d['images'] = i['images'] d['audio'] = i['audio'] d['video'] = i['video'] d['post_likes'] = i['post_likes'] data.append(d) return json.dumps(data)
def edit_profile(msg_received, header): msg = { "subject": "edit profile", "data": { "userName": "******", "fullName": "hassan athmani", "subscription_price": "100", "bio": "i love coding", "location": "Nairobi", "website": "www.me.com", "cover_photo": "www.com", "profile_photo": "www.com", "tags": ["Tag1", "Tag2"], "links": { "facebook": "www.facebook.com", "twitter": "www.twitter.com" } } } data = msg_received["data"] tags = data["tags"] links = data["links"] fullName = data["fullName"] userName = data["userName"] location = data["location"] website = data["website"] subscription_price = data["subscription_price"] bio = data["bio"] facebook = links["facebook"] twitter = links["twitter"] instagram = links["instagram"] youtube = links["youtube"] snapchat = links["snapchat"] tiktok = links["tiktok"] cover_photo = data["cover_photo"] profile_photo = data["profile_photo"] userID = tokens.getID(header) facebook_response = "0" twitter_response = "0" instagram_response = "0" youtube_response = "0" bio_response = "0" subscription_price_response = "0" website_response = "0" location_response = "0" userName_response = "0" fullName_response = "0" details = [] tagsA = [] d = collections.OrderedDict() t = collections.OrderedDict() dbconfig = read_db_config() conn = MySQLConnection(**dbconfig) cursor = conn.cursor() if userID == "Error expired token" or userID == "Error invalid token": return json.dumps({'Error': 'login in again'}) else: cursor.execute("SELECT * FROM `user` WHERE user_id='" + str(userID) + "';") row = cursor.fetchall() if len(row) == 1: for a in row: old_username = str(a[2]) old_fullname = str(a[1]) j = 1 cursor.execute("UPDATE `user_data` SET `facebook` = '" + str(facebook) + "',`twitter` = '" + str(twitter) + "',`instagram` = '" + str(instagram) + "',`youtube` = '" + str(youtube) + "',`snapchat` = '" + str(snapchat) + "',`tiktok` = '" + str(tiktok) + "' WHERE user_id=" + str(userID) + ";") conn.commit() facebook_response = str(cursor.rowcount) for tag in tags: cursor.execute("UPDATE `user_data` SET `tag_" + str(j) + "` = '" + str(tag) + "' WHERE user_id=" + str(userID) + ";") conn.commit() t["tag_" + str(j)] = str(cursor.rowcount) j += 1 if not fullName.isspace(): if fullName: # posts collection.update_many( {'fullName': old_fullname}, {'$set': { 'fullName': str(fullName) }}) cursor.execute("UPDATE `user` SET `fullName` = '" + fullName + "' WHERE user_id=" + str(userID) + ";") conn.commit() fullName_response = str(cursor.rowcount) #print("hello") if not userName.isspace(): if userName: # posts collection.update_many( {'userName': old_username}, {'$set': { 'userName': str(userName) }}) cursor.execute("UPDATE `user` SET `userName` = '" + userName + "' WHERE user_id=" + str(userID) + ";") conn.commit() userName_response = str(cursor.rowcount) if location: cursor.execute("UPDATE `user` SET `location` = '" + location + "' WHERE user_id=" + str(userID) + ";") conn.commit() location_response = str(cursor.rowcount) else: cursor.execute("UPDATE `user` SET `location` = '" + location + "' WHERE user_id=" + str(userID) + ";") conn.commit() if website: cursor.execute("UPDATE `user_data` SET `website` = '" + website + "' WHERE user_id=" + str(userID) + ";") conn.commit() #website_response=str(cursor.rowcount) else: cursor.execute("UPDATE `user_data` SET `website` = '" + website + "' WHERE user_id=" + str(userID) + ";") conn.commit() if not cover_photo.isspace(): if cover_photo: cursor.execute( "UPDATE `cover_photo` SET `cover_pic` = '" + str(cover_photo) + "' WHERE user_id=" + str(userID) + ";") conn.commit() if not profile_photo.isspace(): if profile_photo: cursor.execute( "UPDATE `profile_photo` SET `profile_pic` = '" + str(profile_photo) + "' WHERE user_id=" + str(userID) + ";") conn.commit() if int(subscription_price) > 0: cursor.execute( "UPDATE `subscription_price` SET `subscription_price` = '" + str(subscription_price) + "' WHERE user_id=" + str(userID) + ";") conn.commit() subscription_price_response = str(cursor.rowcount) else: cursor.execute( "UPDATE `subscription_price` SET `subscription_price` = '0' WHERE user_id=" + str(userID) + ";") conn.commit() #if not bio.isspace(): if bio: cursor.execute("UPDATE `user_data` SET `bio` = '" + bio + "' WHERE user_id=" + str(userID) + ";") conn.commit() #bio_response=str(cursor.rowcount) else: cursor.execute("UPDATE `user_data` SET `bio` = '" + bio + "' WHERE user_id=" + str(userID) + ";") conn.commit() #d["fullName"] = fullName_response #d["userName"] = userName_response #d["location"] = location_response #d["website"] = website_response #d["subscription_price"] = subscription_price_response #d["facebook"] = facebook_response #d["twitter"] = twitter_response #d["instagram"] = instagram_response #d["youtube"] = youtube_response #tagsA.append(t) #details.append(d) conn.close() cursor.close() return get_data.getInfo(userID) else: conn.close() cursor.close() return json.dumps({'Error': "invalid token"})