Ejemplo n.º 1
0
 def do_create_user(self, arg):
     """
     Command:       create_user {UserID} {Name} {Birthday}(optional)
     Description:   Create a new username
     """
     parameters = arg.split()
     if len(parameters) < 2 or len(parameters) > 3:
         print_error_param_num()
         return
     condition = "UserID=\"{}\"".format(
         parameters[0]
     )
     result = self.check_record_exist("Users", condition)
     if len(result) == 0:
         if len(parameters) == 2:
             values = "\"{}\",\"{}\",NULL".format(
                 parameters[0], 
                 parameters[1]
             )
         elif len(parameters) == 3:
             values = "\"{}\",\"{}\",\"{}\"".format(
                 parameters[0], 
                 parameters[1],
                 parameters[2]
             )
         self.insert_record("Users", "", values)
         print_record_create_success("Username")
     elif len(result) == 1:
         print_record_already_exist("Username")
     else:
         print_error_duplicate_record_found()
Ejemplo n.º 2
0
 def do_exit(self, arg):
     """
     Command:       exit
     Description:   Exit the command line interface
     """
     if len(arg) != 0:
         print_error_param_num()
         return
     return True
Ejemplo n.º 3
0
 def do_read_user(self, arg):
     """
     Command:       read_user {UserID} {Type}(all/unread)
     Description:   Read all unread posts by a followed user (cannot read yourself)
     """
     if self.login_status == False:
         print_error_not_login()
         return
     parameters = arg.split()
     if len(parameters) != 2:
         print_error_param_num()
         return
     if parameters[0] == self.user_id:
         print_error_read_oneself()
         return
     if not (parameters[1] == READ_TYPE_ALL or parameters[1] == READ_TYPE_UNREAD):
         print_error_invalid_read_type()
         return
     condition = "UserID=\"{}\"".format(
         parameters[0]
     )
     result = self.check_record_exist("Users", condition)
     if len(result) == 0:
         print_error_record_not_found("User")
     elif len(result) == 1:
         condition = "UserID=\"{}\" and FollowUserID=\"{}\"".format(
             self.user_id,
             parameters[0]
         )
         result = self.check_record_exist("UserFollowsUser", condition)
         if len(result) == 0:
             print_error_not_following(parameters[0])
         elif len(result) == 1:
             last_read_post_id = result[0][2]
             if last_read_post_id is None:
                 last_read_post_id = -1
             result = self.query_post_by_user(parameters[0], last_read_post_id, parameters[1])
             if len(result) == 0:
                 print_already_read_all_user(parameters[0])
             else:
                 print_post(result)
                 condition = "UserID=\"{}\" and FollowUserID=\"{}\"".format(
                     self.user_id,
                     parameters[0]
                 )
                 self.update_record("UserFollowsUser", "LastReadPost", result[0][0], condition)
         else:
             print_error_duplicate_record_found()
     else:
         print_error_duplicate_record_found()
Ejemplo n.º 4
0
 def do_show_group(self, arg):
     """
     Command:       show_group
     Description:   Show all groups
     """
     if len(arg) != 0:
         print_error_param_num()
         return
     cursor = self.connection.cursor()
     query = "select * from UserGroups"
     cursor.execute(query)
     print("\nHeader:\n('GroupID', 'Name', 'CreatedBy')\nData:")
     print_cursor(cursor)
     cursor.close()
Ejemplo n.º 5
0
 def do_show_topic(self, arg):
     """
     Command:       show_topic
     Description:   Show all topics
     """
     if len(arg) != 0:
         print_error_param_num()
         return
     cursor = self.connection.cursor()
     query = "select * from Topics"
     cursor.execute(query)
     print("\nHeader:\n('TopicID')\nData:")
     print_cursor(cursor)
     cursor.close()
Ejemplo n.º 6
0
 def do_logout(self, arg):
     """
     Command:       logout
     Description:   Logout from current user
     """
     if len(arg) != 0:
         print_error_param_num()
         return
     if self.login_status == False:
         print_error_not_login()
         return
     self.user_id = ""
     self.login_status = False
     print_logout_success()
Ejemplo n.º 7
0
 def do_show_user(self, arg):
     """
     Command:       show_user
     Description:   List out all usernames
     """
     if len(arg) != 0:
         print_error_param_num()
         return
     cursor = self.connection.cursor()
     query = "select * from Users"
     cursor.execute(query)
     print("\nHeader:\n('UserID', 'Name', 'Birthday')\nData:")
     print_cursor(cursor)
     cursor.close()
Ejemplo n.º 8
0
 def do_create_group(self, arg):
     """
     Command:       create_group {Name}
     Description:   Create a group
     """
     if self.login_status == False:
         print_error_not_login()
         return
     parameters = arg.split()
     if len(parameters) != 1:
         print_error_param_num()
         return
     values = "\"{}\",\"{}\"".format(
         parameters[0],
         self.user_id
     )
     group_id = self.insert_record("UserGroups", "(Name,CreatedBy)", values)
     print_record_create_success_with_id("Group", group_id)
Ejemplo n.º 9
0
 def do_show_follow_topic(self, arg):
     """
     Command:       show_follow_topic
     Description:   Show all topics you are following
     """
     if len(arg) != 0:
         print_error_param_num()
         return
     if self.login_status == False:
         print_error_not_login()
         return
     cursor = self.connection.cursor()
     query = "select * from UserFollowsTopic where UserID = \"{}\"".format(
         self.user_id
     )
     cursor.execute(query)
     print("\nlist of topic id you are currently following:\n")
     print_cursor(cursor, 1)
     cursor.close()
Ejemplo n.º 10
0
 def do_follow_user(self, arg):
     """
     Command:       follow_user {UserID}
     Description:   Follow a user (cannot follow yourself)
     """
     if self.login_status == False:
         print_error_not_login()
         return
     parameters = arg.split()
     if len(parameters) != 1:
         print_error_param_num()
         return
     if parameters[0] == self.user_id:
         print_error_follow_oneself()
         return
     condition = "UserID=\"{}\"".format(
         parameters[0]
     )
     result = self.check_record_exist("Users", condition)
     if len(result) == 0:
         print_error_record_not_found("User")
     elif len(result) == 1:
         user_name = result[0][1]
         condition = "UserID=\"{}\" and FollowUserID=\"{}\"".format(
             self.user_id,
             parameters[0]
         )
         result = self.check_record_exist("UserFollowsUser", condition)
         if len(result) == 0:
             values = "\"{}\",\"{}\",NULL".format(
                 self.user_id,
                 parameters[0]
             )
             self.insert_record("UserFollowsUser", "", values)
             print_follow_success("user", user_name)
         elif len(result) == 1:
             print_error_already_followed(user_name)
         else:
             print_error_duplicate_record_found()
     else:
         print_error_duplicate_record_found()
Ejemplo n.º 11
0
 def do_login(self, arg):
     """
     Command:       login {UserID}
     Description:   Login with a username
     """
     parameters = arg.split()
     if len(parameters) != 1:
         print_error_param_num()
         return
     condition = "UserID=\"{}\"".format(
         parameters[0]
     )
     result = self.check_record_exist("Users", condition)
     if len(result) == 0:
         print_error_record_not_found("Username")
     elif len(result) == 1:
         self.login_status = True
         self.user_id = result[0][0]
         print_login_success(result[0][1])
     else:
         print_error_duplicate_record_found()
Ejemplo n.º 12
0
    def do_init_post(self, arg):
        """
        Command:       init_post {Title} {TopicID},{TopicID} {Content}
        Description:   Initial a post. Note {TopicID} need to be at least one
        """
        if self.login_status == False:
            print_error_not_login()
            return
        parameters = arg.split(' ',2)
        if len(parameters) < 3:
            print_error_param_num()
            return
        topics = set(parameters[1].split(','))
        topic_exists = True
        for _topic in topics:
            condition = "TopicID=\"{}\"".format(
                _topic
            )
            result = self.check_record_exist("Topics", condition)
            if len(result) == 0:
                topic_exists = False
                break
        if not topic_exists:
            print_error_record_not_found("One of the topics is")
        else:
            values = "\"{}\",\"{}\",\"{}\",\"{}\"".format(
                parameters[0],
                POST_TYPE_INITIAL,
                parameters[2],
                self.user_id
            )
            post_id = self.insert_record("Posts", "(Name,Type,Content,CreatedBy)", values)

            for _topic in topics:
                values = "\"{}\",\"{}\"".format(
                    post_id,
                    _topic
                )
                self.insert_record("PostUnderTopic", "", values)
            print_record_create_success_with_id("Post", post_id)
Ejemplo n.º 13
0
 def do_unfollow_user(self, arg):
     """
     Command:       unfollow_user {UserID}
     Description:   Unfollow a user
     """
     if self.login_status == False:
         print_error_not_login()
         return
     parameters = arg.split()
     if len(parameters) != 1:
         print_error_param_num()
         return
     condition = "UserID=\"{}\"".format(
         parameters[0]
     )
     result = self.check_record_exist("Users", condition)
     if len(result) == 0:
         print_error_record_not_found("User")
     elif len(result) == 1:
         user_name = result[0][1]
         condition = "UserID=\"{}\" and FollowUserID=\"{}\"".format(
             self.user_id,
             parameters[0]
         )
         result = self.check_record_exist("UserFollowsUser", condition)
         if len(result) == 0:
             print_error_not_following(parameters[0])
         elif len(result) == 1:
             condition = "UserID=\"{}\" and FollowUserID=\"{}\"".format(
                 self.user_id,
                 parameters[0]
             )
             self.remove_record("UserFollowsUser", condition)
             print_unfollow_success("user", user_name)
         else:
             print_error_duplicate_record_found()
     else:
         print_error_duplicate_record_found()
Ejemplo n.º 14
0
 def do_leave_group(self, arg):
     """
     Command:       leave_group {GroupID}
     Description:   Leave a group
     """
     if self.login_status == False:
         print_error_not_login()
         return
     parameters = arg.split()
     if len(parameters) != 1:
         print_error_param_num()
         return
     condition = "GroupID=\"{}\"".format(
         parameters[0]
     )
     result = self.check_record_exist("UserGroups", condition)
     if len(result) == 0:
         print_error_record_not_found("Group")
     elif len(result) == 1:
         group_name = result[0][1]
         condition = "UserID=\"{}\" and GroupID=\"{}\"".format(
             self.user_id,
             parameters[0]
         )
         result = self.check_record_exist("UserJoinGroup", condition)
         if len(result) == 0:
             print_error_not_in_group()
         elif len(result) == 1:
             condition = "UserID=\"{}\" and GroupID=\"{}\"".format(
                 self.user_id,
                 parameters[0]
             )
             self.remove_record("UserJoinGroup", condition)
             print_leave_group_success(group_name)
         else:
             print_error_duplicate_record_found()
     else:
         print_error_duplicate_record_found()
Ejemplo n.º 15
0
 def do_create_topic(self, arg):
     """
     Command:       create_topic {TopicID}
     Description:   Create a topic. Note that: {TopicID} will eliminate comma.
     """
     parameters = arg.split()
     if len(parameters) != 1:
         print_error_param_num()
         return
     condition = "TopicID=\"{}\"".format(
         parameters[0]
     )
     result = self.check_record_exist("Topics", condition)
     if len(result) == 0:
         values = "\"{}\"".format(
             parameters[0].replace(',','')
         )
         self.insert_record("Topics", "", values)
         print_record_create_success("Topic")
     elif len(result) == 1:
         print_record_already_exist("Topic")
     else:
         print_error_duplicate_record_found()
Ejemplo n.º 16
0
 def do_follow_topic(self, arg):
     """
     Command:       follow_topic {TopicID}
     Description:   Follow a topic
     """
     if self.login_status == False:
         print_error_not_login()
         return
     parameters = arg.split()
     if len(parameters) != 1:
         print_error_param_num()
         return
     condition = "TopicID=\"{}\"".format(
         parameters[0]
     )
     result = self.check_record_exist("Topics", condition)
     if len(result) == 0:
         print_error_record_not_found("Topic")
     elif len(result) == 1:
         condition = "UserID=\"{}\" and FollowTopicID=\"{}\"".format(
             self.user_id,
             parameters[0]
         )
         result = self.check_record_exist("UserFollowsTopic", condition)
         if len(result) == 0:
             values = "\"{}\",\"{}\",NULL".format(
                 self.user_id,
                 parameters[0]
             )
             self.insert_record("UserFollowsTopic", "", values)
             print_follow_success("topic", parameters[0])
         elif len(result) == 1:
             print_error_already_followed(parameters[0])
         else:
             print_error_duplicate_record_found()
     else:
         print_error_duplicate_record_found()