You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

124 lines
4.3 KiB

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
import os
from time import gmtime, strftime
from pprint import pprint
app = Flask(__name__, static_url_path='', static_folder="static", template_folder="templates")
app.jinja_env.add_extension('jinja2.ext.loopcontrols')
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
jsonfiles = [] #json files
thefile = None #selected file for description
now = strftime("%Y-%m-%d_%H:%M:%S", gmtime()) #description time
positioninarray = 2 #counter
listofdicts=[] #to be able to import and use json content
#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") and not name.endswith(".DS_Store"):
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))
if name.endswith(".json"):
fullpath = os.path.join(path, name)
jsonfiles.append(fullpath[7:])
# print(name)
for line in open(fullpath, 'r'):
listofdicts.append(json.loads(line))
# print(listofdicts[0]["name"]) #test
dict = {} #dict for the form entries
@app.route("/")
def home():
return render_template('home.html')
@app.route('/about/')
def about():
return render_template('about.html')
@app.route('/all/')
def all():
thefile = listingfiles[positioninarray]
print(listingfiles)
# print(dict)
counter2=0
return render_template('all.html', file=thefile, listingfiles=listingfiles, jsonfiles=jsonfiles, listofdicts=listofdicts, counter2=counter2)
@app.route('/description', methods=['GET', 'POST'])
def description():
form = ReusableForm(request.form)
#pick a file but that will be done an other way later
thefile = listingfiles[positioninarray] #select one file from fullpath minus static/
# 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)
@app.route('/get-data', methods=['GET', 'POST'])
def savepost():
if request.method=='POST':
dict={'id':listingdirectories[positioninarray][13:],'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
#json file naming gone unique by adding time and date of submission
with open(listingdirectories[positioninarray]+"/"+now+"_data_file.json", "w") as write_file:
json.dump(dict, write_file)
return "The JSON file is ready."
#throw description and next item, wee need to iterate through the files
else:
return "Error"
if __name__ == '__main__':
app.run(debug=True)