From 11aec35ad27b27c01fe5ef30d6959f6c1726bfe8 Mon Sep 17 00:00:00 2001 From: IssaIan Date: Mon, 10 Dec 2018 15:32:26 +0300 Subject: [PATCH 1/2] [starts #162539042]Add moderator user edit comment --- commandblog.py | 58 ++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/commandblog.py b/commandblog.py index 6daa24f..1450eca 100644 --- a/commandblog.py +++ b/commandblog.py @@ -28,12 +28,12 @@ } ] + def login(): username = input("please input username: ") for user in users: if user['name'] == username: - # return user['password'] password = input("please input password: ") if user['password'] != password: return 'Wrong password' @@ -41,15 +41,15 @@ def login(): if user['role'] == "normal": userinput = input("1. create comment \n 2.Edit comment \n 3. logout ") - + if userinput == str("1"): comment = input("Enter your comment:") - - data = {'comment_id': len(comments) +1, - 'comment': comment, - 'timestamp': datetime.datetime.now() , - 'created_by': username - } + + data = {'comment_id': len(comments) + 1, + 'comment': comment, + 'timestamp': datetime.datetime.now(), + 'created_by': username + } comments.append(data) return comments @@ -57,9 +57,10 @@ def login(): comment_id = int(input('Enter comment id:')) if not comment_id: return "Enter comment id" - comment = next((comment for comment in comments if comment["comment_id"] == comment_id), False) + comment = next( + (comment for comment in comments if comment["comment_id"] == comment_id), False) if comment == False: - return "No comment found" + return "No comment found" edit = input("Enter your comment here:") comment["comment"] = edit return comments @@ -67,18 +68,18 @@ def login(): else: login() - - if user['role'] == "moderator": - userinput = input("1. create comment \n 2. edit comment \n 3. delete comment \n 4. logout \n ") - + elif user['role'] == "moderator": + userinput = input( + "1. create comment \n 2. edit comment \n 3. delete comment \n 4. logout \n ") + if userinput == str("1"): comment = input("Enter your comment:") - - data = {'comment_id': len(comments) +1, - 'comment': comment, - 'timestamp': datetime.datetime.now() , - 'created_by': username - } + + data = {'comment_id': len(comments) + 1, + 'comment': comment, + 'timestamp': datetime.datetime.now(), + 'created_by': username + } comments.append(data) return comments @@ -86,9 +87,10 @@ def login(): comment_id = int(input('Enter comment id:')) if not comment_id: return "Enter comment id: " - comment = next((comment for comment in comments if comment["comment_id"] == comment_id), False) + comment = next( + (comment for comment in comments if comment["comment_id"] == comment_id), False) if comment == False: - return "No comment found" + return "No comment found" edit = input("Enter your comment here:") comment["comment"] = edit return comments @@ -96,19 +98,15 @@ def login(): comment_id = int(input('Enter comment id')) if not comment_id: return 'Enter comment id' - comment = next((comment for comment in comments if comment["comment_id"] == comment_id), False) + comment = next( + (comment for comment in comments if comment["comment_id"] == comment_id), False) if comment == False: - return "No comment found" + return "No comment found" comments.remove(comment) return comments - else: login() - - - - -print(login()) \ No newline at end of file +print(login()) From 0445f3cb89e61ad36b7d2a72e7285fb047bd369c Mon Sep 17 00:00:00 2001 From: IssaIan Date: Mon, 10 Dec 2018 16:25:22 +0300 Subject: [PATCH 2/2] [finishes #162539042] moderator can delete --- commandblog.py | 63 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/commandblog.py b/commandblog.py index 1450eca..1c659cb 100644 --- a/commandblog.py +++ b/commandblog.py @@ -22,7 +22,7 @@ "lastLoginAt": "" }, { - "name": "steve", + "name": "stev", "password": "1234", "role": "normal" } @@ -31,13 +31,24 @@ def login(): username = input("please input username: ") + password = input("please input password: ") - for user in users: - if user['name'] == username: - password = input("please input password: ") - if user['password'] != password: - return 'Wrong password' - user["lastLoginAt"] = datetime.datetime.now() + if not username: + print('please input username !!!') + return "please input username!!!" + if not password: + print('please input password !!!') + return "please input password!!!" + user = next((user for user in users if user["name"] == username), False) + + if user == False: + print('No user with that username exists') + return 'No user with that username exists' + + if user['password'] != password: + print('wrong password') + return "wrong password" + user["lastLoginAt"] = datetime.datetime.now() if user['role'] == "normal": userinput = input("1. create comment \n 2.Edit comment \n 3. logout ") @@ -51,7 +62,8 @@ def login(): 'created_by': username } comments.append(data) - return comments + print(comments) + elif userinput == str("2"): comment_id = int(input('Enter comment id:')) @@ -63,12 +75,12 @@ def login(): return "No comment found" edit = input("Enter your comment here:") comment["comment"] = edit - return comments + print(comments) else: login() - elif user['role'] == "moderator": + if user['role'] == "moderator": userinput = input( "1. create comment \n 2. edit comment \n 3. delete comment \n 4. logout \n ") @@ -81,32 +93,41 @@ def login(): 'created_by': username } comments.append(data) - return comments + print(comments) elif userinput == str("2"): comment_id = int(input('Enter comment id:')) if not comment_id: - return "Enter comment id: " + print("Enter comment id: ") comment = next( (comment for comment in comments if comment["comment_id"] == comment_id), False) if comment == False: - return "No comment found" + print("No comment found") edit = input("Enter your comment here:") comment["comment"] = edit - return comments + print(comments) + elif userinput == str("3"): - comment_id = int(input('Enter comment id')) + comment_id = int(input("Enter the comment id to delete:")) if not comment_id: - return 'Enter comment id' - comment = next( - (comment for comment in comments if comment["comment_id"] == comment_id), False) + print('You must enter the comment id') + comment = next((comment for comment in comments if comment["comment_id"] == comment_id), False) + if comment == False: - return "No comment found" + print('Comment not found!') comments.remove(comment) - return comments + print(comments) else: login() -print(login()) +if __name__ == "__main__": + + choice = 'y' + + while choice is not 'n': + + login() + + choice = input('Continue?')