def add_dislike(self, id, user_id): """Add the "user_id" into "disliked_list" array of the "img_id". """ if utils.validate_id(id) and utils.validate_id(user_id): table = self._img_nosql_table fields = 'disliked_list[$element = %d] AS disliked_result' % ( user_id, ) conditions = 'id = %d' % (id, ) query = f'SELECT {fields} FROM {table} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) if len(nosql_result ) > 0 and nosql_result[0]['disliked_result'] is None: fields = 'disliked_list %d' % (user_id, ) conditions = 'id = %d' % (id, ) query = f'UPDATE {table} ADD {fields} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) if len(nosql_result ) > 0 and nosql_result[0]['NumRowsUpdated'] == 1: self.remove_like(id, user_id) return True else: return False
def get_profile_img_url(self, id=None, email=None, username=None): """Return the image profile URL. """ if id and utils.validate_id(id): conditions = 'id = %d LIMIT 1' % (id,) elif email and utils.validate_email(email): conditions = 'email = "%s" LIMIT 1' % (email,) elif username and utils.validate_username(username): conditions = 'username = "******" LIMIT 1' % (username,) else: return None fields = 'profile_image_url' table = self._nosql_table query = f'SELECT {fields} FROM {table} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) if len(nosql_result) > 0: return nosql_result[0]['profile_image_url'] else: return None
def check_passwd(self, id=None, email=None, username=None, passwd=''): """Checks the user password. """ if id and utils.validate_id(id): conditions = 'id = %d LIMIT 1' % (id,) elif email and utils.validate_email(email): conditions = 'email = "%s" LIMIT 1' % (email,) elif username and utils.validate_username(username): conditions = 'username = "******" LIMIT 1' % (username,) else: return False fields = 'password' table = self._nosql_table query = f'SELECT {fields} FROM {table} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) if len(nosql_result) > 0: passwd_db_hash = nosql_result[0]['password'] try: passwd_check_status = check_password_hash(passwd_db_hash, passwd) except: return False else: if passwd_check_status: return True else: return False else: return False
def update_passwd(self, id=None, email=None, username=None, passwd=''): """Generate a new password hash and update the user. """ if id and utils.validate_id(id): user_pkey_dict = self.get_pkey_values(id=id) elif email and utils.validate_email(email): user_pkey_dict = self.get_pkey_values(email=email) elif username and utils.validate_username(username): user_pkey_dict = self.get_pkey_values(username=username) else: return None new_passwd_hash = generate_password_hash(passwd) fields = 'SET password = "******"' % (new_passwd_hash,) table = self._nosql_table conditions = 'id = %d AND email = "%s" AND username = "******"' % (user_pkey_dict['id'], user_pkey_dict['email'], user_pkey_dict['username'],) query = f'UPDATE {table} {fields} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) if len(nosql_result) > 0: if nosql_result[0]['NumRowsUpdated'] == 1: return True else: return False else: return False
def get_pkey_values(self, id=None, email=None, username=None): """Returns the user's primary key values. Some operations in Oracle NoSQL (like UPDATE) requires all primary key values to be informed. """ if id and utils.validate_id(id): conditions = 'id = %d LIMIT 1' % (id,) elif email and utils.validate_email(email): conditions = 'email = "%s" LIMIT 1' % (email,) elif username and utils.validate_username(username): conditions = 'username = "******" LIMIT 1' % (username,) else: return None fields = 'id, email, username' table = self._nosql_table query = f'SELECT {fields} FROM {table} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) user_dict = {'id': '', 'username': '', 'email': ''} if len(nosql_result) > 0: user_dict['id'] = nosql_result[0]['id'] user_dict['username'] = nosql_result[0]['username'] user_dict['email'] = nosql_result[0]['email'] return user_dict else: return user_dict
def is_private(self, id=None, email=None, username=None): """Check if the user is a private user. """ if id and utils.validate_id(id): conditions = 'id = %d LIMIT 1' % (id,) elif email and utils.validate_email(email): conditions = 'email = "%s" LIMIT 1' % (email,) elif username and utils.validate_username(username): conditions = 'username = "******" LIMIT 1' % (username,) else: return False fields = 'is_private' table = self._nosql_table query = f'SELECT {fields} FROM {table} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) if len(nosql_result) > 0: return nosql_result[0]['is_private'] else: return None
def remove(self, user_id, img_filename): """Remove an image by filename. """ if utils.validate_id(user_id): table = self._img_nosql_table conditions = 'user_id = %d AND image_filename = "%s"' % ( user_id, img_filename, ) query = f'DELETE FROM {table} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) if len(nosql_result ) > 0 and nosql_result[0]['numRowsDeleted'] == 1: self.__del_img_data(img_filename) return True else: return False return None
def __add_list_item(self, list_name, list_owner_id, user_follow_id): """Add the "user_follow_id" into the "list_name" from "list_owner_id". """ fotogal_user = FotogalUser() owner_list_dict = fotogal_user.get_pkey_values(id=list_owner_id) nosql = FotogalNosql() table = self._nosql_table # Check if the ID doesn't exist in follow_list array. fields = 'id' conditions = 'id = %d AND %s[] =any %d' % ( owner_list_dict['id'], list_name, user_follow_id, ) query = f'SELECT {fields} FROM {table} WHERE {conditions}' nosql_result = nosql.exec_query(table, query) if len(nosql_result) > 0: return False else: fields = '%s %d' % ( list_name, user_follow_id, ) conditions = 'id = %d AND username = "******" AND email = "%s"' % ( owner_list_dict['id'], owner_list_dict['username'], owner_list_dict['email'], ) query = f'UPDATE {table} ADD {fields} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) return True
def get_suggestions(self): """Return some users as suggestions to follow. """ MAX_USERS_TO_RETURN = 30 fields = 'id, full_name, username, profile_image_url, is_private' conditions = 'is_professional_account = TRUE LIMIT %d' % ( MAX_USERS_TO_RETURN, ) table = self._nosql_table query = f'SELECT {fields} FROM {table} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) return nosql_result
def get_img_props(self, id): """Return image properties. """ if utils.validate_id(id): fields = 'id, image_filename, image_original_filename, user_id, created_ts, is_profile' table = self._img_nosql_table conditions = 'id = %d LIMIT 1' % (id, ) query = f'SELECT {fields} FROM {table} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) if len(nosql_result) > 0: return nosql_result[0] return None
def get_posted_imgs_total(self, user_id): """Return the total count of images posted by a "user_id". """ if utils.validate_id(user_id): fields = 'count(image_filename) AS total' table = self._img_nosql_table conditions = 'user_id = %d AND is_profile = FALSE' % (user_id, ) query = f'SELECT {fields} FROM {table} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) if len(nosql_result) > 0: return nosql_result[0]['total'] else: return 0
def get_following_total(self, user_id): """Returns the total from "follow_list". """ if utils.validate_id(user_id): fields = 'count(follow_list[]) AS total' table = self._nosql_table conditions = 'id = %d' % (user_id, ) query = f'SELECT {fields} FROM {table} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) if len(nosql_result) > 0: return nosql_result[0]['total'] else: return 0
def get_data(self, request_user_id, img_owner_username, img_filename): """Return the image headers and content if the user requesting the image has following authorization. """ if utils.validate_id(request_user_id) and utils.validate_username( img_owner_username): fotogal_user = FotogalUser() img_owner_user_id = fotogal_user.get_id( username=img_owner_username) fotogal_follow = FotogalFollow() if request_user_id == img_owner_user_id or fotogal_follow.is_following( request_user_id, img_owner_user_id): fields = 'image_filename, image_type' table = self._img_nosql_table conditions = 'user_id = %d AND image_filename = "%s" LIMIT 1' % ( img_owner_user_id, img_filename, ) query = f'SELECT {fields} FROM {table} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) if len(nosql_result) > 0: ( img_headers, img_content, ) = self.__get_img_data(img_filename) return ( img_headers, img_content, ) return ( None, None, )
def is_following(self, user_id, follow_user_id): """Return TRUE if the "user_id" is following the "follow_user_id". """ if utils.validate_id(user_id) and utils.validate_id(follow_user_id): fields = 'follow_list[$element = %d] AS follow_user_id' % ( follow_user_id, ) table = self._nosql_table conditions = 'id = %d' % (user_id, ) query = f'SELECT {fields} FROM {table} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) if len(nosql_result) > 0: if nosql_result[0]['follow_user_id'] == follow_user_id: return True else: return False
def remove_dislike(self, id, user_id): """Remove the "user_id" from "disliked_list" array of the "img_id". """ if utils.validate_id(id) and utils.validate_id(user_id): table = self._img_nosql_table fields = 'disliked_list[$element = %d]' % (user_id, ) conditions = 'id = %d' % (id, ) query = f'UPDATE {table} REMOVE {fields} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) if len(nosql_result ) > 0 and nosql_result[0]['NumRowsUpdated'] == 1: return True else: return False
def remove(self, enc_cookie_value, user_id): """Validate and remove the auth cookie values. """ if utils.validate_id(user_id): if self.validate(enc_cookie_value, user_id): random_token = self.__decrypt(enc_cookie_value) table = self._nosql_table conditions = 'random_token = "%s" AND user_id = %d' % (random_token, user_id,) query = f'DELETE FROM {table} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) if len(nosql_result) > 0: return True return False
def __get_all_user_props(self, id=None, email=None, username=None): """Return all user's properties. """ if id and utils.validate_id(id): conditions = 'id = %d LIMIT 1' % (id,) elif email and utils.validate_email(email): conditions = 'email = "%s" LIMIT 1' % (email,) elif username and utils.validate_username(username): conditions = 'username = "******" LIMIT 1' % (username,) else: return None fields = '*' table = self._nosql_table query = f'SELECT {fields} FROM {table} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) profile_dict = {'id': '', 'email': '', 'full_name': '', 'username': '', 'password': '', 'follow_list': [], 'follow_sent_list': [], 'follow_you_list': [], 'follow_received_list': [], 'created_ts': '', 'is_private': '', 'is_professional_account': '', 'profile_image_url': '', 'user_data': {'birthday_ts': '', 'website': '', 'bio': '', 'gender': '', 'phone_number': ''}} if len(nosql_result) > 0: for k,v in profile_dict.items(): if k == 'user_data': for k,v in nosql_result[0]['user_data'].items(): profile_dict['user_data'][k] = v else: profile_dict[k] = nosql_result[0][k] return profile_dict else: return None
def validate(self, enc_cookie_value, user_id): """Return TRUE if the encrypted cookie value is valid. """ if utils.validate_id(user_id): try: random_token = self.__decrypt(enc_cookie_value) except InvalidToken as e: return False cookie_user_id = int(random_token[:random_token.index(':')]) if utils.validate_id(cookie_user_id): fields = 'random_token, created_ts, expire_ts' table = self._nosql_table conditions = 'random_token = "%s" AND user_id = %d LIMIT 1' % (random_token, user_id,) query = f'SELECT {fields} FROM {table} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) if len(nosql_result) > 0: if user_id == cookie_user_id: now_ts = int(datetime.datetime.now().strftime('%s')) expire_ts = int(nosql_result[0]['expire_ts']) if now_ts >= expire_ts: self.remove(enc_cookie_value, user_id) app.logger.debug('Removing cookie (%s)' % (enc_cookie_value,)) return False else: return True else: return False else: return False return False
def get_profile_img(self, username=None, img_filename=None): """Return the profile image headers and content. """ if username and utils.validate_username(username): profile_img_url = '/profile/%s/image/%s' % ( username, img_filename, ) conditions = 'username = "******" AND profile_image_url = "%s" LIMIT 1' % ( username, profile_img_url, ) else: return None fields = 'profile_image_url' table = self._usr_nosql_table query = f'SELECT {fields} FROM {table} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) if len(nosql_result) > 0: ( img_headers, img_content, ) = self.__get_img_data(img_filename) return ( img_headers, img_content, ) return ( None, None, )
def get_follow_list(self, user_id=None, index_low=None, index_high=None): """Returns a sublist of user IDs from the "follow_list". """ if utils.validate_id(user_id): if index_low and index_high: fields = '[follow_list[%d:%d]] AS follow_list' % ( index_low, index_high, ) else: fields = 'follow_list AS follow_list' table = self._nosql_table conditions = 'id = %d LIMIT 1' % (user_id, ) query = f'SELECT {fields} FROM {table} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) return nosql_result[0]['follow_list']
def get_id(self, email=None, username=None): """Return the user ID. """ if email and utils.validate_email(email): conditions = 'email = "%s" LIMIT 1' % (email,) elif username and utils.validate_username(username): conditions = 'username = "******" LIMIT 1' % (username,) else: return None fields = 'id' table = self._nosql_table query = f'SELECT {fields} FROM {table} WHERE {conditions}' nosql = FotogalNosql() nosql_result = nosql.exec_query(table, query) if len(nosql_result) > 0: return nosql_result[0]['id'] else: return None