Example #1
0
def logout():
    """Logout from the system. It means the current user return to be None
        """
    try:
        current_user.get_current_user().set_logout()
        current_user.set_current_user(None)
    except:
        return jsonify({"status": "error"})
    return jsonify({"status": "successfully logged out"})
Example #2
0
def get_all_messages():
    """Get all messages from the user currently logged in
        """
    try:
        curr = current_user.get_current_user()
    except:
        return jsonify({"status": "error"})

    return jsonify(curr.get_messages().output_json_all_messages())
Example #3
0
def delete_message():
    """Delete the last message of the logged in user, no matter if its read or not.
        """
    try:
        curr = current_user.get_current_user()
        curr.get_messages().delete_message()
    except:
        return jsonify({"status": "delete message error"})
    return jsonify({"status": "message deleted successfully"})
Example #4
0
def read_message():
    """Read the last message of the logged in user, no matter if its read or not.
        """
    try:
        curr = current_user.get_current_user()
        message = curr.get_messages().get_one_message()
    except:
        return jsonify({"status": "register error"})

    return jsonify(message.json_output())
Example #5
0
def write_message():
    try:
        curr = current_user.get_current_user()
        data = request.get_json()
        sender = curr.get_username()
        receiver = data.get("receiver")
        message = data.get("message")
        subject = data.get("subject")
        creation_date = datetime.now().strftime("%d/%m/%Y, %H:%M:%S")
        if curr.get_username() == sender:
            receiver_user = users.get_receiver(receiver)
            if receiver_user:
                curr.write_message(sender, receiver, message, subject,
                                   creation_date, receiver_user)
            else:
                return jsonify({"status": "the receiver not registered yet"})
        else:
            return jsonify({"status": "you are not the sender in the message"})

        return jsonify({"status": "successfully written message"})
    except:
        return jsonify({"status": "error"})
Example #6
0
def login():
    """Login to the system with username and password. If the user doest exists in the system or
    the password or username incorrect the system will alert and the user can try again.
    The system initialize a current user so from now it will be the user logged it.
        """
    try:
        data = request.get_json()
        username = data.get("username")
        password = data.get("password")
        new_current_user = users.get_user(username, password)
        if new_current_user:
            current_user.set_current_user(new_current_user)
            curr = current_user.get_current_user()
            if not curr.get_logged_in():
                curr.set_login()
                return jsonify({"status": "successfully logged in"})
            else:
                return jsonify({"status": "user already logged in"})
        else:
            return jsonify({"status": "username or password not correct"})
    except:
        return jsonify({"status": "error"})