diff --git a/database.py b/database.py index a120ee9caf62a0db9788fdbfee092bf2d8742d0e..298894d9ecee62ba3794fbe03767b930ca2d7a06 100644 --- a/database.py +++ b/database.py @@ -85,6 +85,31 @@ class Workspace: self.opening_hours = opening_hours self.checkin_instructions = checkin_instructions + def validate(self): + errors = [] + if len(self.name.strip()) <= 0: + errors.append("Name must not be empty") + if len(self.address.strip()) <= 0: + errors.append("Address must not be empty") + if len(self.main_photo.strip()) <= 0: + errors.append("Main photo must not be empty") + if len(self.description.strip()) <= 0: + errors.append("Description must not be empty") + if len(self.website.strip()) <= 0: + errors.append("Website must not be empty") + if len(self.email.strip()) <= 0: + errors.append("Email must not be empty") + if len(self.phone_number.strip()) <= 0: + errors.append("Phone number must not be empty") + if len(self.opening_hours.strip()) <= 0: + errors.append("Opening hours must not be empty") + + for x in self.additional_photos: + if len(x.strip()) <= 0: + errors.append("Each edditional photos must not be empty") + + return errors + def from_query(conn, tuple): (id, name, address, main_photo, description, website, email, phone_number, opening_hours, checkin_instructions) = tuple additional_photos = [] diff --git a/main.py b/main.py index d94babee53763417be976fbdc728d2a49aeb38da..03362b52eb47bbf559c48682ecc3126ab02c34b8 100644 --- a/main.py +++ b/main.py @@ -19,5 +19,46 @@ def map(): dumped = json.dumps(data) return render_template("map.html", json = re.sub(r"(?i)\</script\>", "<\/script>", dumped)) # escape </script> +@app.route("/admin/add-workspace", methods = ["GET", "POST"]) +def add_workspace(): + if request.method == "GET": + return render_template("add-workspace.html") + + if request.method == "POST": + additional_photos = request.form["additional_photos"] + additional_photos = [x.strip() for x in additional_photos.split("\n")] if len(additional_photos) > 0 else [] + workspace = database.Workspace( + request.form["name"], + request.form["address"], + request.form["main_photo"], + additional_photos, + request.form["description"], + request.form["website"], + request.form["email"], + request.form["phone_number"], + request.form["opening_hours"], + request.form["checkin_instructions"] + ) + + errors = workspace.validate() + try: + latitude = float(request.form["latitude"]) + except ValueError: + errors.append("Latitude must be a number") + try: + longitude = float(request.form["longitude"]) + except ValueError: + errors.append("Longitude must be a number") + + if len(errors) > 0: + messages = ["Errors were found:"] + messages.extend(errors) + else: + database.set_address_latlong(workspace.address, (latitude, longitude)) + database.add_workspace(workspace) + messages = ["Workspace added successfully"] + + return render_template("add-workspace-result.html", messages = messages) + if __name__ == "__main__": app.run(debug = True) diff --git a/templates/add-workspace-result.html b/templates/add-workspace-result.html new file mode 100644 index 0000000000000000000000000000000000000000..8695077fbfe71aff340eb153dffb015776bc3966 --- /dev/null +++ b/templates/add-workspace-result.html @@ -0,0 +1,7 @@ +{% extends "main.html" %} +{% block title %}Add Workspace{% endblock %} +{% block mainBlock %} +{% for message in messages %} +<div>{{ message }}</div> +{% endfor %} +{% endblock %} \ No newline at end of file diff --git a/templates/add-workspace.html b/templates/add-workspace.html new file mode 100644 index 0000000000000000000000000000000000000000..cd6c836594e07944b8c237f3b3fb850214f4daa8 --- /dev/null +++ b/templates/add-workspace.html @@ -0,0 +1,57 @@ +{% extends "main.html" %} +{% block title %}Add Workspace{% endblock %} +{% block mainBlock %} +<form action="/admin/add-workspace" method="POST"> + <div> + <label for="name">Name:</label> + <input id="name" type="text" name="name"> + </div> + <div> + <label for="name">Address:</label> + <input id="address" type="text" name="address"> + </div> + <div> + <label for="main-photo">Main Photo:</label> + <input id="main-photo" type="text" name="main_photo"> + </div> + <div> + <label for="additional-photos">Additional Photos:</label> + <div><textarea id="additional-photos" name="additional_photos"></textarea></div> + </div> + <div> + <label for="description">Description:</label> + <div><textarea id="description" name="description"></textarea></div> + </div> + <div> + <label for="website">Website:</label> + <input id="website" type="text" name="website"> + </div> + <div> + <label for="email">Email:</label> + <input id="email" type="text" name="email"> + </div> + <div> + <label for="phone-number">Phone Number:</label> + <input id="phone-number" type="text" name="phone_number"> + </div> + <div> + <label for="opening-hours">Opening Hours:</label> + <input id="opening-hours" type="text" name="opening_hours"> + </div> + <div> + <label for="checkin-instructions">Checkin Instructions:</label> + <div><textarea id="checkin-instructions" name="checkin_instructions"></textarea></div> + </div> + <div> + <label for="latitude">Latitude:</label> + <input id="latitude" type="text" name="latitude"> + </div> + <div> + <label for="longitude">Longitude:</label> + <input id="longitude" type="text" name="longitude"> + </div> + <div> + <input type="submit"> + </div> +</form> +{% endblock %} \ No newline at end of file diff --git a/templates/main.html b/templates/main.html index 5d3fd44a08ee3cf4d9fd678bc25f439eaf3b4159..4557b91d74188f5b7b04fecb70fa3f43c18c51cc 100644 --- a/templates/main.html +++ b/templates/main.html @@ -9,14 +9,14 @@ "sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> - <link rel="stylesheet" href="static/style.css"> + <link rel="stylesheet" href="/static/style.css"> <title>{% block title %}{% endblock %} - Tramshed Workspaces</title> </head> <div class="parallax"> <body class="body"> - <img src="static/Tramshed_Logo.jpg" alt="Logo" height="50" width="100"> + <img src="/static/Tramshed_Logo.jpg" alt="Logo" height="50" width="100"> <!-- navigation --> <nav> <ul>