Browse Source

saving jsons in file folder and displaying files from array

master
jules 5 years ago
parent
commit
4e5f11d37d
  1. 89
      contextualise.py
  2. 45
      contextualiseold.py
  3. BIN
      static/files/001/DwySYC4X4AESeRh.jpg
  4. 1
      static/files/002/2019-01-15_21:42:00_data_file.json
  5. BIN
      static/files/002/YvWL9feSIXyMC2il.mp4
  6. BIN
      static/files/003/Titanic drawing rose meme.-jWcZ2oNAKB4.mp3
  7. 15
      templates/description.html

89
contextualise.py

@ -6,40 +6,95 @@ from wtforms import Form, TextField, TextAreaField, BooleanField, validators, St
from forms import ReusableForm from forms import ReusableForm
from config import Config from config import Config
import json import json
import os
from time import gmtime, strftime
app = Flask(__name__, static_url_path='', static_folder="static", template_folder="templates") app = Flask(__name__, static_url_path='', static_folder="static", template_folder="templates")
app.config.from_object(Config) app.config.from_object(Config)
#### THE CHECKBOX RESULT DOESNT GET RECORDED TO THE JSON FILE
#### FULL LOOP WITH FILE AND RELOADING THE FORM WITH NEXT FILE IN THE ARRAY HAS TO BE MADE
#### OPTIONS TO SWITCH BETWEEN FILES NEED TO BE AVAILABLE
#### MAYBE CONFIRMATION BEFORE JSON IS RECORDED TO FORCE PEOPLE TO DOUBLE THINK
# setting variables for holding paths, folder names and the one file for description
path = "static/files/"
listingfiles= [] #fullpaths
listingdirectories = [] #paths
thefile = None #selected file for description
now = strftime("%Y-%m-%d_%H:%M:%S", gmtime()) #description time
positioninarray = 0 #counter
#listing paths and files, not in order but well...
for path, subdirs, files in os.walk(path):
for name in files:
#excluding json files from listing :-)
if not name.endswith(".json"):
fullpath = os.path.join(path, name)
listingdirectories.append(path)
listingfiles.append(fullpath[7:]) #fullpaths minus static/
print (path)
print (name)
print (os.path.join(path, name))
dict={} #dict for the form entries
@app.route("/") @app.route("/")
def home(): def home():
return render_template('home.html') return render_template('home.html')
@app.route('/about/') @app.route('/about/')
def about(): def about():
return render_template('about.html') return render_template('about.html')
@app.route('/description', methods=['GET', 'POST']) @app.route('/description', methods=['GET', 'POST'])
def description(): def description():
form = ReusableForm(request.form) form = ReusableForm(request.form)
print (form.errors) #pick a file but that will be done an other way later
if request.method == 'POST' and form.validate(): thefile = listingfiles[positioninarray] #select one file from fullpath minus static/
return 'Success!'
return render_template('description.html', form=form) # assigning html tag on the basis of extension, to be finished when all the files have been sent
if thefile.lower().endswith(('.png', '.jpg', '.jpeg')):
thefile = Markup('<img src="'+thefile+'" />')
#if sound
elif thefile.lower().endswith(('.mp3')):
thefile = Markup('''<audio controls>
<source src="'''+thefile+'''" type="audio/mpeg">
Your browser does not support the audio tag.
</audio>''')
#ifvid to be added
elif thefile.lower().endswith(('.mp4')):
thefile = Markup(''' <video width="320" height="240" controls>
<source src="'''+thefile+'''" type="video/mp4">
Your browser does not support the video tag.
</video> ''')
print (form.errors)
if request.method == 'POST' and form.validate():
return 'Success!'
# return render_template('description.html', form=form, file=thefile, listingdirectories=listingdirectories, listingfiles=listingfiles)
return render_template('description.html', form=form, file=thefile)
dict={}
@app.route('/get-data', methods=['GET', 'POST']) @app.route('/get-data', methods=['GET', 'POST'])
def savepost(): def savepost():
if request.method=='POST': if request.method=='POST':
dict={'name':request.form['name'],'email':request.form['email'],'friend':request.form['friend'],'content':request.form['content']} dict={'name':request.form['name'],'email':request.form['email'],'friend':request.form['friend'],'content':request.form['content']}
# open with "a" if the file should concatenate content # open with "a" if the file should concatenate content
with open("data_file.json", "w") as write_file:
json.dump(dict, write_file) #json file naming gone unique by adding time and date of submission
return "The JSON file is ready." with open(listingdirectories[positioninarray]+"/"+now+"_data_file.json", "w") as write_file:
else: json.dump(dict, write_file)
return "Error" return "The JSON file is ready."
#throw description and next item, wee need to iterate through the files
else:
return "Error"
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True) app.run(debug=True)

45
contextualiseold.py

@ -0,0 +1,45 @@
from flask import Flask, url_for, render_template, Markup, redirect, request, flash
from flask import session as login_session
from flask_wtf import FlaskForm
from wtforms.validators import DataRequired
from wtforms import Form, TextField, TextAreaField, BooleanField, validators, StringField, SubmitField
from forms import ReusableForm
from config import Config
import json
app = Flask(__name__, static_url_path='', static_folder="static", template_folder="templates")
app.config.from_object(Config)
@app.route("/")
def home():
return render_template('home.html')
@app.route('/about/')
def about():
return render_template('about.html')
@app.route('/description', methods=['GET', 'POST'])
def description():
form = ReusableForm(request.form)
print (form.errors)
if request.method == 'POST' and form.validate():
return 'Success!'
return render_template('description.html', form=form)
dict={}
@app.route('/get-data', methods=['GET', 'POST'])
def savepost():
if request.method=='POST':
dict={'name':request.form['name'],'email':request.form['email'],'friend':request.form['friend'],'content':request.form['content']}
# open with "a" if the file should concatenate content
with open("data_file.json", "w") as write_file:
json.dump(dict, write_file)
return "The JSON file is ready."
else:
return "Error"
if __name__ == '__main__':
app.run(debug=True)

BIN
static/files/001/DwySYC4X4AESeRh.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

1
static/files/002/2019-01-15_21:42:00_data_file.json

@ -0,0 +1 @@
{"friend": "NY", "name": "mazouni", "email": "pasdechance", "content": "toute la nuit je pense"}

BIN
static/files/002/YvWL9feSIXyMC2il.mp4

Binary file not shown.

BIN
static/files/003/Titanic drawing rose meme.-jWcZ2oNAKB4.mp3

Binary file not shown.

15
templates/description.html

@ -2,6 +2,21 @@
{% block content %} {% block content %}
<div class="description"> <div class="description">
<h2 style = "text-align: center;">Write something very interesting here.</h2> <h2 style = "text-align: center;">Write something very interesting here.</h2>
<!--passing files-->
<p>{{ file }}</p>
<!-- <img src="{{ url_for('static', filename='/files/001/DwySYC4X4AESeRh.jpg') }}">
-->
<!--passing an array-->
<!-- {% for v in listingdirectories %}
<div>{{ v }}</div>
{% endfor %}
{% for f in listingfiles %}
<div>{{ f }}</div>
{% endfor %}
-->
{% for message in form.name.errors %} {% for message in form.name.errors %}
<div>{{ message }}</div> <div>{{ message }}</div>
{% endfor %} {% endfor %}

Loading…
Cancel
Save