Exemplo n.º 1
0
    def create_user(self, user, email, password):
        """
        Create a new user. The function will raise an exception if the user already exists or if the password, username or e-mail are invalid.
        :param user: username
        :param email: e-mail of the user
        :param password: password of the user
        """
        if not check_username(user):
            raise ValueError(f"Invalid username")

        if not check_email(email):
            raise ValueError(f"Invalid e-mail")

        if not check_password(password):
            raise ValueError(f"Invalid password")

        if self.get_user_by_username(user):
            raise ValueError(f"Username already registered")

        if self.get_user_by_email(email):
            raise ValueError(f"E-mail already registered")

        self.users_table.put_item(
            Item={
                "User": user,
                "EMail": email,
                "PasswordHash": create_password_hash(password),
                "Registered": get_current_timestamp(),
                "LastActive": get_current_timestamp(),
                "IsPublic": False,
                "VotedMessage": None,
                "VotedRedirect": None,
            })
Exemplo n.º 2
0
    def update_user_last_active(self, user):
        """
        Update a user's last active time with the current time. The function will raise an exception if the user doesn't exist.
        :param user: existing username
        """
        if not self.get_user_by_username(user):
            raise ValueError(f"Cannot find user {user}")

        self.users_table.update_item(
            Key={"User": user},
            ExpressionAttributeNames={
                "#LastActive": "LastActive",
            },
            ExpressionAttributeValues={":LastActive": get_current_timestamp()},
            UpdateExpression="SET #LastActive = :LastActive",
        )
Exemplo n.º 3
0
 def create_topic(self, user, topic, project_name):
     """
     Create a new topic
     :param user: user for which a new topic should be created
     :param topic: name of the topic
     :param project_name: project name for which the topic should be created
     :return:
     """
     self.votes_table.put_item(
         Item={
             "User": user,
             "TopicKey": get_topic_key(project_name, topic),
             "ProjectName": project_name,
             "Topic": topic,
             "LastVote": get_current_timestamp(),
             "VoteCount": 1,
         })
Exemplo n.º 4
0
    def add_vote_history(self, user, project, topic, ip_address):
        """
        Add a new vote history entry
        :param user: username
        :param project: topic key
        :param topic: topic key
        :param ip_address: IP address of the user that voted
        """
        topic_key = get_topic_key(project, topic)

        if not self.check_ip_voted(user, topic_key, ip_address):
            self.votes_history_table.put_item(
                Item={
                    "User": user,
                    "TopicKey": topic_key,
                    "VoteTimestamp": get_current_timestamp(),
                    "IPHash": hash_string(user + topic_key + ip_address),
                    "IPHashProject": hash_string(user + project + ip_address),
                })
Exemplo n.º 5
0
 def set_vote_count(self, user, topic_key, vote_count):
     """
     Set the vote count for a topic key
     :param user: user to which the topic key belongs
     :param topic_key: topic key for which the vote count should be set
     :param vote_count: new vote count
     """
     self.votes_table.update_item(
         Key={
             "User": user,
             "TopicKey": topic_key
         },
         ExpressionAttributeNames={
             "#VoteCount": "VoteCount",
             "#LastVote": "LastVote",
         },
         ExpressionAttributeValues={
             ":VoteCount": vote_count,
             ":LastVote": get_current_timestamp(),
         },
         UpdateExpression=
         "SET #VoteCount = :VoteCount, #LastVote = :LastVote",
     )
Exemplo n.º 6
0
 def test_get_current_timestamp(self, _):
     self.assertEqual("9999", get_current_timestamp())