def like_kweek(request, authorized_username): """ To like a kweek. *Parameters:* - *request*: Request which contains the id of th kweek to be liked. - *authorized_username(string)*: The user currently logged in. *Returns:* -*Tuple*: { | *bool*: To indicate whether kweek credentials creation was successful or not., | *message (str)*: To specify the reason of failure if detected. | *code*: The code to be returned in the request. | } """ kweek_id = request['id'] if kweek_id is not None: if len(kweek_id) == 0 or (kweek_id.isspace()): return False, 'No kweek id found.', 400 if not kweek_id.isdigit(): return False, 'Invalid kweek ID.', 400 check = validate_id(kweek_id) if len(check) == 0: return False, 'Kweek does not exist.', 404 else: return False, 'No kweek id found.', 400 add_like(kweek_id, authorized_username) notified_user = retrieve_user(kweek_id, 1)[0]['username'] create_notifications(authorized_username, notified_user, 'LIKE', kweek_id) return True, 'success.', 200
def create_rekweek(request, authorized_username): """ Insert a rekweek in the data base. *Parameters:* - *request*: the request which contains the id of th kweek to be rekweeked. *Returns:* -*Tuple*: { | *bool*: To indicate whether kweek credentials creation was successful or not., | *message (str)*: To specify the reason of failure if detected. | *code*: The code to be returned in the request. | } """ rekweek_id = request["id"] if rekweek_id is not None: if len(rekweek_id) == 0 or (rekweek_id.isspace()): return False, 'No kweek id found.', 400 if not rekweek_id.isdigit(): return False, 'Invalid kweek ID.', 400 check = validate_id(rekweek_id) if len(check) == 0: return False, 'Kweek does not exist.', 404 else: return False, 'No kweek id found.', 400 add_rekweek(rekweek_id, authorized_username) notified_user = retrieve_user(rekweek_id, 1)[0]['username'] create_notifications(authorized_username, notified_user, 'REKWEEK', rekweek_id) return True, 'success.', 200
def create_kweek(request, authorized_username): """ Set the data needed to be assigned to the kweek object to later be inserted in the data base *Parameters:* - *request*: the body of The kweek creation request sent from the user. - *authorized_username*: The name of the authorized user who sent the request. *Returns:* -*Tuple*: { | *bool*: To indicate whether kweek credentials creation was successful or not., | *message (str)*: To specify the reason of failure if detected. | *code*: The code to be returned in the request. | } """ data = {} reply_to = request["reply_to"] if reply_to is not None: if not reply_to.isdigit(): return False, 'Invalid ID to be replied to.', 400 check = validate_id(reply_to) if len(check) == 0: return False, 'The kweek to be replied to does not exist.', 404 text = request['text'] if len(text) == 0 or (text.isspace()): return False, 'No text body found.', 400 # check if str and have a length of minimum one char and is not fully white space hashtags, mentions = extract_mentions_hashtags( text) # two lists of objects partial_user = get_user(authorized_username) partial_user = partial_user[0] status_dic = { 'following': None, 'follows_you': None, 'blocked': None, 'muted': None } partial_user.update(status_dic) user = User(partial_user) # user obj data['id'] = 0 data['created_at'] = datetime.utcnow() data['username'] = authorized_username data['hashtags'] = hashtags # list of dics data['mentions'] = mentions # list of dics data['media_url'] = create_url(request['media_id']) data['number_of_likes'] = 0 data['number_of_rekweeks'] = 0 data['number_of_replies'] = 0 data['rekweek_info'] = None data['reply_info'] = None data['liked_by_user'] = False data['rekweeked_by_user'] = False data['user'] = user data.update(request) kweek = Kweek(data) check, message, code = insert_kweek(kweek) return check, message, code
def validate_request(kid): """ Validate the request parametar. *Parameters:* - *kid*: The kweek id to be Validate. *Returns:* -*Tuple*: { | *check (bool)*: To indicate whether kweek is valid or not ., | *message (str)*: To specify the resaon of error . | *code*: The code to be returned in the request. | } """ try: int(kid) check = validate_id(kid) if len(check) == 0: message = 'Kweek does not exist.' return False, message, 404 else: return True, 'success.', 200 except ValueError: return False, 'Invalid kweek ID.', 400
def validate_request(kid): """ Validate the request parametar. *Parameters:* - *kid*: The kweek id to be Validate. *Returns:* -*Tuple*: { | *check (bool)*: To indicate whether kweek is valid or not ., | *message (str)*: To specify the resaon of error . | } """ try: int(kid) check = validate_id(kid) if len(check) == 0: message = 'Kweek is not available' return False, message else: return True, 'success' except ValueError: return False, 'Invalid data type'