Skip to content
Snippets Groups Projects
Commit 5695bf39 authored by jakovits's avatar jakovits
Browse files

Example code for exercise 5.1

parent 74397dbc
No related branches found
No related tags found
No related merge requests found
...@@ -5,17 +5,19 @@ from flask import Flask, render_template, request ...@@ -5,17 +5,19 @@ from flask import Flask, render_template, request
app = Flask(__name__) app = Flask(__name__)
UPLOAD_FOLDER ='./static/images'
def read_messages_from_file(): def read_messages_from_file():
""" Read all messages from a JSON file""" """ Read all messages from a JSON file"""
with open('data.json') as messages_file: with open('data.json') as messages_file:
return json.load(messages_file) return json.load(messages_file)
def append_message_to_file(content): def append_message_to_file(img_path, content):
""" Read the contents of JSON file, add this message to it's contents, then write it back to disk. """ """ Read the contents of JSON file, add this message to it's contents, then write it back to disk. """
data = read_messages_from_file() data = read_messages_from_file()
new_message = { new_message = {
'content': content, 'content': content,
'img_path': img_path,
'timestamp': datetime.now().isoformat(" ", "seconds") 'timestamp': datetime.now().isoformat(" ", "seconds")
} }
...@@ -29,10 +31,14 @@ def append_message_to_file(content): ...@@ -29,10 +31,14 @@ def append_message_to_file(content):
# The Flask route, defining the main behaviour of the webserver: # The Flask route, defining the main behaviour of the webserver:
@app.route("/handle_message", methods=['POST']) @app.route("/handle_message", methods=['POST'])
def handleMessage(): def handleMessage():
img_path = ""
new_message = request.form['msg'] new_message = request.form['msg']
if('file' in request.files and request.files['file']):
image = request.files['file']
img_path = os.path.join(UPLOAD_FOLDER, image.filename)
image.save(img_path)
if new_message: if new_message:
append_message_to_file(new_message) append_message_to_file(img_path, new_message)
return render_template('handle_message.html', message=new_message) return render_template('handle_message.html', message=new_message)
......
...@@ -3,22 +3,23 @@ ...@@ -3,22 +3,23 @@
https://jinja.palletsprojects.com/en/3.0.x/ --> https://jinja.palletsprojects.com/en/3.0.x/ -->
<title>Message board</title> <title>Message board</title>
<body> <body>
<h4>Welcome to Message board </h4> <h4>Welcome to Pelle's Message board </h4>
There are {{ messages|length }} messages on the board. </ br> There are {{ messages|length }} messages on the board. </ br>
<h4> Here are the last 10:</h4> <h4> Here are the last 10:</h4>
<ul> <ul>
{% for m in messages[-9:]|reverse %} {% for m in messages[-9:]|reverse %}
<li> <li>
"{{m.content}}" <small>Posted on {{m.timestamp}} </small> "{{m.content}}" <small>Posted on {{m.timestamp}} </small> <img src="{{ m.img_path }}" width="500">
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>
<h4> Enter a new message</h4> <h4> Enter a new message</h4>
<form action="/handle_message" method="post" > <form action="/handle_message" method="post" enctype=multipart/form-data >
<label >Your message:</label><br> <label >Your message:</label><br>
<input type="text" name="msg"><br> <input type="text" name="msg"><br>
<input type="file" name="file"><br>
<input type="submit" value="Submit"> <input type="submit" value="Submit">
</form> </form>
</body> </body>
......
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