Skip to content
Snippets Groups Projects
Commit cf1c7ba5 authored by Teemu Autto's avatar Teemu Autto
Browse files

Merge branch 'removal-btn' into 'main'

Implemented removal request button

See merge request !21
parents 782d88ef ecdbc2c0
No related branches found
No related tags found
1 merge request!21Implemented removal request button
Pipeline #8595 canceled
from email import message
from typing import Dict, List, TypedDict
from flask import (
Flask,
......@@ -15,7 +16,7 @@ from os import environ
from flask_mongoengine import MongoEngine
import mongoengine
from .models import Location
from .models import Location, RemovalRequest
from .logging import logger, init_logging
from . import migrations
......@@ -84,7 +85,12 @@ def fetch_all_locations() -> List[Location]:
# Alternatively, as objects is an iterator, it can be converted directly
# into list.
locations = list(Location.objects)
locations = list((x for x in Location.objects if len(x.removal_requests) == 0))
# --->
# locations = []
# for x in location.objects:
# if len(x.removal_requests == 0:
# locations.append(x)
return locations
......@@ -168,6 +174,25 @@ def get_location_html(location) -> Page:
return page
@app.route("/location/<id>/remove", methods=["POST"])
def page_removal_request(id):
location = fetch_location(id)
try:
removal = RemovalRequest(email=request.form['request-removal-email'],
reason=request.form['request-removal-reason'])
location.removal_requests.append(removal)
location.save()
except mongoengine.errors.ValidationError as exc:
return render_template("message.j2.html",
url=url_for('index', id=id),
message=f"Check the form. Error received was {exc}")
return render_template("message.j2.html",
url=url_for('index'),
message="Removal request received. Admins will soon handle it appropriately.")
@app.route("/_marker", methods=['POST'])
def api_add_marker():
"""
......@@ -213,14 +238,6 @@ def api_add_marker():
return jsonify(_generate_marker(new_spot))
@app.route("/_marker", methods=["DELETE"])
def api_remove_marker():
"""
Callback for removing marker from database.
"""
raise NotImplementedError("Deleting markers is not implemented yet")
@app.route("/build")
def image_build():
"""
......
......@@ -8,7 +8,27 @@ sadly doesn't support type hinting (yet).
"""
from mongoengine import fields, Document
from mongoengine import (
fields,
EmbeddedDocument,
Document,
)
from datetime import datetime
class RemovalRequest(EmbeddedDocument):
"""
Model for storing received removal requests.
"""
email = fields.EmailField(required=True)
"Email address for request creator"
reason = fields.StringField()
"Request field reasoning"
datetime = fields.DateTimeField(default=datetime.utcnow)
"Datetime when request was created"
class Location(Document):
......@@ -18,7 +38,7 @@ class Location(Document):
Location markers are provided by users for suitable dogging spots.
"""
loc = fields.PointField(required=True)
loc = fields.DictField(required=True)
  • Author Owner

    As explained in the video; PointField expects 'X' and 'y', whereas we used leaflet structures 'lat' and 'lon'. To be compatible with leaflet, use dict field (for now).

  • Please register or sign in to reply
"X and Y coordinates for marker location"
description = fields.StringField()
......@@ -27,5 +47,8 @@ class Location(Document):
email = fields.EmailField()
"Email address provided by user"
removal_requests = fields.ListField(fields.EmbeddedDocumentField(RemovalRequest))
"All of the removal requests received for this location."
schema_version = fields.IntField()
......@@ -5,6 +5,11 @@
</style>
<div class="container">
<div class="row">
<div class="col-auto">
<h3>Location details</h3>
</div>
</div>
<div class="row">
<label for="location-email" class="col-sm-3 col-form-label">Email</label>
<div class="col-sm-9" id="location-email">
......@@ -20,6 +25,34 @@
<div class="row">
<div class="col-auto">
<a href="https://twitter.com/intent/tweet?url={{ url_for('index', id=location.id, _external=True) | urlencode }}&text={{ "Check out this super special location!" | urlencode }}" class="btn btn-primary">Tweet location</a>
<a href="#" class="btn btn-warning" data-bs-toggle="modal" data-bs-target="#removal-request-modal">Request removal</a>
<form class="modal" id="removal-request-modal" tabindex="-1" aria-labelledby="removal-request-title" aria-hidden="true" action="{{ url_for('page_removal_request', id=location.id) }}" method="POST">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="removal-request-title">Request location removal</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="request-removal-email" class="form-label">Email address</label>
<input type="text" class="form-control" id="request-removal-email" name="request-removal-email" aria-describedby="request-removal-email-help" required>
<div id="request-removal-email-help" class="form-text">Please fill out your email for removal.</div>
</div>
<div class="mb-3">
<label for="request-removal-reason" class="form-label">Removal request details</label>
<textarea class="form-control" name="request-removal-reason" id="request-removal-reason" rows="3"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Submit removal request</button>
</div>
</div>
</form>
</div>
</div>
</div>
{% block head %}
<meta http-equiv="refresh" content="10; url={{ url }}" />
{% endblock %}
{% block page %}
<p>
{{ message }}
</p>
<p>
<a href="{{url}}">Click here to be redirected.</a>
</p>
{% endblock %}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment