예제 #1
0
def view_message(message_id):
    """View Message Route [Get]

    Login: Not Required

    This route either retrieves a random Wyspa from the Database,
    or obtains a specific message if one is specified in the request.
    The Wyspa is then passed into the template for rendering. This page
    allows a user to read, edit, listen to, or comment on Wyspas.

    Parameters
    ----------
    message_id : str *optional
        _id parameter of Wyspa to be queried from the database.

    Returns
    -------
    messages.html
    """

    # Query the Database for specified _id
    if message_id:
        message_entry = Wyspa.get_by_id(message_id)
        # Obtain a random Wyspa and flash error if not found
        if not message_entry:
            flash("Oops! We couldn't find the requested Wyspa!")
            return redirect(url_for("messages.view_message"))
    # Otherwise, obtain a random Wyspa
    else:
        message_entry = Wyspa.get_random_wyspa()
    return render_template('messages.html', message_entry=message_entry)
예제 #2
0
    def test_c_edit(self):
        '''Sends a POST request for Wyspa Editting.

        This test logs the user in before performing the test.

        Route:
            "/edit_wyspa/<wyspa_id>"
        Method:
            POST
        Expected Outcome:
            Wyspa is updated in the Database
        Expected Outcome:
            Updated Wyspa is retrieved from database
        Expected Outcome:
            Original and edited Wyspas are different
        Expected Outcome:
            Original and edited Wyspas have the same ID
        '''

        # Set up dictionary for login form
        login_form = {
            "usernameLogin": "******",
            "passwordLogin": "******",
            "timezoneLogin": "******"
        }
        # Log User In
        self.client.post("/login", data=login_form)

        # Set session timezone (required for below method)
        session["timezone"] = "Europe/London"
        # Obtain the Wyspa previously created
        wyspa_to_edit = Wyspa.get_by_user("unittest")[0]

        # Set up dictionary for Wyspa updating
        wyspa_form = {
            "wyspaContent": "Updated Test Wyspa",
            "location": "London",
            "mood": 0,
            "expiryDate": "01-01-2022",
            "expiryTime": "11:00"
        }
        # Submit edit
        self.client.post("/edit_wyspa/" + str(wyspa_to_edit.wyspa_id),
                         data=wyspa_form)

        # Query DB for updated Wyspa
        updated_wyspa = Wyspa.get_by_user("unittest")[0]

        # Compare Wyspas as a whole
        self.assertTrue(updated_wyspa != wyspa_to_edit)
        # Compare ID of Wyspas
        self.assertTrue(updated_wyspa.wyspa_id == wyspa_to_edit.wyspa_id)
예제 #3
0
def create_wyspa():
    """Create Wyspa Route [Get/Post]

    Login: Required

    This route retrieves the form data completed
    by the user, verifies it and formats it accordingly,
    writes the Wyspa to the Database, then re-routes them
    back to the My Voice Route.

    This is a POST route; if accessed via GET, user
    is redirected to My Voice route with no further action.

    Parameters
    ----------
    None

    Returns
    -------
    Redirect (my_voice) [Get/Post]
    """

    if request.method == "POST":

        # Check comment for pure whitespace
        if not Wyspa.whitespace_check(request.form.get("wyspaContent")):
            flash("Oops! Looks like your message was empty!")
            return redirect(url_for("messages.my_voice"))

        # Convert datetime
        formatted_expiry = Wyspa.string_to_datetime(
                    expiry_date=request.form.get("expiryDate"),
                    expiry_time=request.form.get("expiryTime"))

        # If Formatted_Expiry returns False, expiry is in the past
        if not formatted_expiry:
            flash("Expiry must be in the future!")
            return redirect(url_for("messages.my_voice"))

        # Try and convert the address to latlong
        converted_location = Wyspa.location_to_latlong(
            request.form.get("location"))
        # If converted location is None, location was not found
        if not converted_location:
            flash("Unable to locate address!")
            return redirect(url_for("messages.my_voice"))

        # Create a new wyspa, and write to DB
        new_wyspa = Wyspa(author=current_user.username,
                          message=request.form.get("wyspaContent"),
                          mood=int(request.form.get("mood")),
                          location=converted_location,
                          expiry=formatted_expiry)
        new_wyspa.write_wyspa()
        flash("Wyspa successful!")
        return redirect(url_for("messages.my_voice"))

    # Get Route
    return redirect(url_for("messages.my_voice"))
예제 #4
0
def add_comment(message_id):
    """Add Comment Route [Get/Post]

    Login: Required

    This route retrieves the Wyspa currently being viewed,
    ensures the user is logged in, adds the comment provided
    by the user to the Wyspa, then re-routes them back to the
    View Message Route along with the Wyspa ID of the current Wyspa.

    This is a POST route; if accessed via GET, user
    is redirected to View Message route with no further action.

    Parameters
    ----------
    message_id : str
        _id parameter of Wyspa which requires editting.

    Returns
    -------
    Redirect (view_message) [Get/Post]
    """

    # Retrieve Wyspa
    retrieved_wyspa = Wyspa.get_by_id(message_id)

    # If comment posted
    if request.method == "POST":

        # Obtain comment from form
        new_comment = request.form.get("commentReply")

        # Check comment for pure whitespace
        if not Wyspa.whitespace_check(new_comment):
            flash("Oops! Looks like your message was empty!")
        else:
            retrieved_wyspa.add_comment(new_comment=new_comment,
                                        comment_author=current_user.username)

    # Both GET and POST routes
    return redirect(url_for("messages.view_message",
                            message_id=retrieved_wyspa.wyspa_id))
예제 #5
0
    def test_random_wyspa(self):
        '''Tests the random Wyspa generation.

        Method:
            Wyspa.get_random_wyspa()
        Expected Outcome:
            A Wyspa is obtained from the database.
        '''

        random_wyspa = Wyspa.get_random_wyspa()
        self.assertTrue(random_wyspa is not None)
예제 #6
0
def delete_wyspa(message_id):
    """Delete Wyspa Route [Get/Post]

    Login: Required

    This route queries the database for the Wyspa that requires
    deletion, verifies the ownership of the Wyspa, and then deletes
    it from the Database. The user is then redirected to the My Voice route.

    This is a POST route; if accessed via GET, user
    is redirected to My Voice route with no further action.

    Parameters
    ----------
    message_id : str
        _id parameter of Wyspa which requires deletion.

    Returns
    -------
    Redirect (my_voice) [Get/Post]
    """

    if request.method == "POST":
        # Obtain Wyspa details
        retrieved_wyspa = Wyspa.get_by_id(message_id)
        # Verify User
        if not retrieved_wyspa.author == current_user.username:
            flash("You do not have access to this Wyspa!")
            return redirect(url_for("messages.my_voice"))

        # Delete Wyspa
        Wyspa.delete_wyspa(message_id)
        flash("Wyspa deleted successfully!")
        return redirect(url_for("messages.my_voice"))

    flash("Please use the delete icon to delete a Wyspa!")
    # Route for GET
    return redirect(url_for("messages.my_voice"))
예제 #7
0
def remove_comment(message_id, comment_id):
    """Remove Comment Route [Get/Post]

    Login: Required

    This route retrieves the Wyspa currently being viewed,
    ensures the user is logged in, and is either the Wyspa
    owner or the comment owner, then uses the provided
    comment_id to remove the requested comment from the
    comment list. The user is then re-routed back to the
    View Message Route along with the Wyspa ID of the current
    Wyspa.

    This is a POST route; if accessed via GET, user
    is redirected to View Message route with no further action.

    Parameters
    ----------
    message_id : str
        _id parameter of Wyspa which requires editting.

    comment_id : str
        Index position of the comment that needs deleting.

    Returns
    -------
    Redirect (view_message) [Get/Post]
    """

    # Query DB for Wyspa
    retrieved_wyspa = Wyspa.get_by_id(message_id)

    if request.method == "POST":
        # Set the comment index
        comment_index = int(comment_id)-1
        # Ensure user is Wyspa or Comment owner
        for key in retrieved_wyspa.comments[comment_index].keys():
            comment_author = key
        if (current_user.username == retrieved_wyspa.author
                or current_user.username == comment_author):
            retrieved_wyspa.remove_comment(comment_index)
        else:
            flash("You are not authorised to delete this comment!")
    # GET Route
    else:
        flash("Please use the icons provided to delete comments!")

    # GET and POST Routes
    return redirect(url_for("messages.view_message",
                            message_id=retrieved_wyspa.wyspa_id))
예제 #8
0
def add_listen(message_id):
    """Add Listen Route [Get]

    Login: Required

    This route retrieves the Wyspa currently being viewed,
    ensures the user is logged in, checks whether the user's
    username exists in the listens list of the Wyspa, and then
    adds the username to the list and increments the listen_count.
    The user is then re-routed back to the View Message Route
    along with the Wyspa ID of the current Wyspa.

    Login Required decorator has not been used as the
    referrer should not be passed to the Login function for this
    action.

    Parameters
    ----------
    message_id : str
        _id parameter of Wyspa which requires editting.

    Returns
    -------
    Redirect (view_message)

    """

    # Retrieve the wyspa
    retrieved_wyspa = Wyspa.get_by_id(message_id)

    # Check if user is logged in
    if current_user.is_authenticated:

        # Inform the user if they've already listened
        if current_user.username in retrieved_wyspa.get_info()["listens"]:
            flash("You've already listened to this Wyspa!")

        # Store Listen
        else:
            flash("Thank you for listening.")
            retrieved_wyspa.add_listen(current_user.username)

    # Inform the user they need to be logged in
    else:
        flash("You must be logged in to listen to a Wyspa!")

    return redirect(url_for("messages.view_message",
                            message_id=retrieved_wyspa.wyspa_id))
예제 #9
0
    def test_location_to_latlong(self):
        '''Tests the location to latlong conversion.

        This method sets the true lat long co-ordinates
        for London, then calls the conversion method to compare the
        results to ensure they're not identical (i.e,
        the scrambling has worked).

        Method:
            Wyspa.location_to_latlong()
        Expected Outcome:
            True London lat/long does not match scrambled lat/long
        '''

        # Set Lat/Long for London
        london = {"lat": 51.500153, "lng": -0.1262362}
        # Obtain scrambled Lat/Long
        scrambled_london = Wyspa.location_to_latlong("london")
        # Compare True vs Scrambled
        self.assertTrue(london != scrambled_london)
예제 #10
0
    def test_b_create(self):
        '''Sends a POST request for Wyspa Creation.

        This test logs the user in before performing the test.

        Route:
            "/create_wyspa"
        Method:
            POST
        Expected Outcome:
            Wyspa is written to the Database
        Expected Outcome:
            Wyspa is retrieved from database
        '''

        # Set up dictionary for login form
        login_form = {
            "usernameLogin": "******",
            "passwordLogin": "******",
            "timezoneLogin": "******"
        }
        # Log user in
        self.client.post("/login", data=login_form)

        # Set up dictionary for Wyspa form
        wyspa_form = {
            "wyspaContent": "Unit Test Wyspa",
            "location": "London",
            "mood": 1,
            "expiryDate": "01-01-2022",
            "expiryTime": "15:00"
        }
        # Submit Wyspa
        self.client.post("/create_wyspa", data=wyspa_form)

        # Set Timezone in session (required for below method)
        session["timezone"] = "Europe/London"
        # Query DB for all Wyspas belonging to user
        wyspas = Wyspa.get_by_user("unittest")
        # Assert that the array returned is not empty
        self.assertTrue(wyspas != [])
예제 #11
0
def my_voice():
    """My Voice Route [Get]

    Login: Required

    This route obtains all of the logged in users Wyspas,
    and passes them to the My Voice route to be displayed
    and managed. Users can also create Wyspas or delete their
    account from this page.

    Parameters
    ----------
    None

    Returns
    -------
    my_voice.html
    """

    # Query the database for all of the logged in user's wyspas
    users_messages = Wyspa.get_by_user(current_user.username)
    return render_template('my_voice.html', users_messages=users_messages)
예제 #12
0
    def test_d_delete(self):
        '''Sends a POST request for deleting Account and Wyspas.

        This test logs the user in before performing the test.

        Route:
            "/delete_user"
        Method:
            POST
        Expected Outcome:
            Logged in user's account is deleted
        Expected Outcome:
            Logged in user's Wyspas are deleted
        '''

        # Set up dictionary for login form
        login_form = {
            "usernameLogin": "******",
            "passwordLogin": "******",
            "timezoneLogin": "******"
        }
        # Log User In
        self.client.post("/login", data=login_form)

        # Submit Delete Account request
        self.client.post("/delete_user")

        # Query the DB for the testing username
        deleted_user = User.obtain_user("unittest")

        # Set session timezone (required for below method)
        session["timezone"] = "Europe/London"
        # Query the DB for Wyspas by testing username
        deleted_messages = Wyspa.get_by_user("unittest")
        # Check if this user has been deleted
        self.assertTrue(deleted_user is None)
        # Check if this user has no Wyspas
        self.assertTrue(deleted_messages == [])
예제 #13
0
def edit_wyspa(message_id):
    """Edit Wyspa Route [Get/Post]

    Login: Required

    This route contains seperate routes for Get and Post.
    Both routes verify the Wyspa exists in the database,
    and verifies the owner of the Wyspa.

    The Get request passes the existing Wyspa to the edit_wyspa.html
    template to prepopulate the Wyspa form.

    The Post request verifies the data (message, datetime and location),
    processes it, and updates the Wyspa in the database, before
    redirecting the user to the my_voice route.

    Parameters
    ----------
    message_id : str
        _id parameter of Wyspa which requires editting.

    Returns
    -------
    edit_wyspa.html [Get]

    Redirect (my_voice) [Post]
    """

    # Retreive WYSPA
    retrieved_wyspa = Wyspa.get_by_id(message_id)
    if not retrieved_wyspa:
        flash("We couldn't find that Wyspa!")
        return redirect(url_for("messages.my_voice"))

    # Verify the owner of wyspa is trying to edit it
    if retrieved_wyspa.author != current_user.username:
        flash("You can't edit someone elses Wyspa!")
        return redirect(url_for("messages.my_voice"))

    # Post Route
    if request.method == "POST":

        # Check comment for pure whitespace
        if not Wyspa.whitespace_check(request.form.get("wyspaContent")):
            flash("Oops! Looks like your message was empty!")
            expiry_date = Wyspa.datetime_to_string(retrieved_wyspa.expiry)
            return render_template("edit_wyspa.html",
                                   retrieved_wyspa=retrieved_wyspa,
                                   expiry_date=expiry_date)

        # Convert datetime
        formatted_expiry = Wyspa.string_to_datetime(
                expiry_date=request.form.get("expiryDate"),
                expiry_time=request.form.get("expiryTime"))

        # If Formatted_Expiry returns False, expiry is in the past
        if not formatted_expiry:
            flash("Expiry must be in the future!")
            expiry_date = Wyspa.datetime_to_string(retrieved_wyspa.expiry)
            return render_template("edit_wyspa.html",
                                   retrieved_wyspa=retrieved_wyspa,
                                   expiry_date=expiry_date)

        # Try and convert the address to latlong
        converted_location = Wyspa.location_to_latlong(
            request.form.get("location"))
        # If converted location is None, location was not found
        if not converted_location:
            flash("Unable to locate address!")
            expiry_date = Wyspa.datetime_to_string(
                retrieved_wyspa.expiry)
            return render_template("edit_wyspa.html",
                                   retrieved_wyspa=retrieved_wyspa,
                                   expiry_date=expiry_date)

        # Call edit function
        retrieved_wyspa.edit_wyspa(message=request.form.get("wyspaContent"),
                                   mood=int(request.form.get("mood")),
                                   location=converted_location,
                                   expiry=formatted_expiry)

        flash("Wyspa Updated!")
        return redirect(url_for("messages.my_voice"))

    # Get Route
    expiry_date = Wyspa.datetime_to_string(retrieved_wyspa.expiry)

    return render_template('edit_wyspa.html',
                           retrieved_wyspa=retrieved_wyspa,
                           expiry_date=expiry_date)