Browse Source

added new template that displays all files

master
Cristina Cochior 6 years ago
parent
commit
f0179dacbb
  1. BIN
      .DS_Store
  2. 37
      contextualise.py
  3. 1
      data_file.json
  4. 1
      forms.py
  5. 38
      json_actions.py
  6. BIN
      static/.DS_Store
  7. BIN
      static/files/.DS_Store
  8. BIN
      static/files/001/.DS_Store
  9. 1
      static/files/001/2019-01-22_08:49:43_data_file.json
  10. BIN
      static/files/002/.DS_Store
  11. 1
      static/files/002/2019-01-15_21:42:00_data_file.json
  12. 1
      static/files/002/2019-01-20_17:47:03_data_file.json
  13. 1
      static/files/002/2019-01-22_09:09:09_data_file.json
  14. 1
      static/files/003/2019-01-22_09:09:52_data_file.json
  15. 64
      templates/all.html
  16. 1
      templates/layout.html

BIN
.DS_Store

Binary file not shown.

37
contextualise.py

@ -8,6 +8,8 @@ from config import Config
import json import json
import os import os
from time import gmtime, strftime from time import gmtime, strftime
from pprint import pprint
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)
@ -23,24 +25,35 @@ app.config.from_object(Config)
path = "static/files/" path = "static/files/"
listingfiles= [] #fullpaths listingfiles= [] #fullpaths
listingdirectories = [] #paths listingdirectories = [] #paths
jsonfiles = [] #json files
thefile = None #selected file for description thefile = None #selected file for description
now = strftime("%Y-%m-%d_%H:%M:%S", gmtime()) #description time now = strftime("%Y-%m-%d_%H:%M:%S", gmtime()) #description time
positioninarray = 0 #counter positioninarray = 2 #counter
listofdicts=[] #to be able to import and use json content
#listing paths and files, not in order but well... #listing paths and files, not in order but well...
for path, subdirs, files in os.walk(path): for path, subdirs, files in os.walk(path):
for name in files: for name in files:
#excluding json files from listing :-) #excluding json files from listing :-)
if not name.endswith(".json"): if not name.endswith(".json") and not name.endswith(".DS_Store"):
fullpath = os.path.join(path, name) fullpath = os.path.join(path, name)
listingdirectories.append(path) listingdirectories.append(path[7:])
listingfiles.append(fullpath[7:]) #fullpaths minus static/ listingfiles.append(fullpath[7:]) #fullpaths minus static/
print (path) # print (path)
print (name) # print (name)
print (os.path.join(path, 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 dict = {} #dict for the form entries
@app.route("/") @app.route("/")
def home(): def home():
@ -50,6 +63,15 @@ def home():
def about(): def about():
return render_template('about.html') return render_template('about.html')
@app.route('/all/')
def all():
thefile = listingfiles[positioninarray]
print(listingfiles)
#here i need to json.load each file
print(dict)
return render_template('all.html', file=thefile, listingfiles=listingfiles, jsonfiles=jsonfiles, listofdicts=listofdicts)
@app.route('/description', methods=['GET', 'POST']) @app.route('/description', methods=['GET', 'POST'])
def description(): def description():
form = ReusableForm(request.form) form = ReusableForm(request.form)
@ -87,6 +109,7 @@ def savepost():
# open with "a" if the file should concatenate content # open with "a" if the file should concatenate content
#json file naming gone unique by adding time and date of submission #json file naming gone unique by adding time and date of submission
with open(listingdirectories[positioninarray]+"/"+now+"_data_file.json", "w") as write_file: with open(listingdirectories[positioninarray]+"/"+now+"_data_file.json", "w") as write_file:
json.dump(dict, write_file) json.dump(dict, write_file)
return "The JSON file is ready." return "The JSON file is ready."

1
data_file.json

@ -0,0 +1 @@
{"name": "as", "email": "asd", "friend": "Y", "content": "asd"}

1
forms.py

@ -9,3 +9,4 @@ class ReusableForm(Form):
friend = RadioField('Are you a friend of De Player?', choices = [('Y','Yes'),('NY','Not Yet')]) friend = RadioField('Are you a friend of De Player?', choices = [('Y','Yes'),('NY','Not Yet')])
content = StringField('Description:',[validators.Required('Please enter a description.')]) content = StringField('Description:',[validators.Required('Please enter a description.')])
submit = SubmitField('Send') submit = SubmitField('Send')

38
json_actions.py

@ -0,0 +1,38 @@
import json
# # to iterate through existing json file and find the correct json file
# def find_json(id):
# get path/to/file
# return file
# #
# def save_json(id, name, email, friend, content):
# file
# data = {"id": "path/to/file", "name":,"email":,"friend":,"content":}
# with open('file.json', 'w') as f:
# json.dump(data, f)
# def jaction(original, id, name, email, friend, content):
# f = find_json_file(id)
# data = make_dict(f)
# updated = update_dict(data, name, email, friend, content)
# save_json_file(f, updated)
# # to find the file with the correct id
# def find_json_file():
# f = open('file.json', 'w')
# iterate files to find id
# return f
# # saving the json file
# def save_json_file(name, email, friend, content):
# dict= request.args.get(
# write(file, json.dump(data))

BIN
static/.DS_Store

Binary file not shown.

BIN
static/files/.DS_Store

Binary file not shown.

BIN
static/files/001/.DS_Store

Binary file not shown.

1
static/files/001/2019-01-22_08:49:43_data_file.json

@ -0,0 +1 @@
{"name": "Canonimouse", "email": "yes@mail.com", "friend": "Y", "content": "The contestants are embracing."}

BIN
static/files/002/.DS_Store

Binary file not shown.

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

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

1
static/files/002/2019-01-20_17:47:03_data_file.json

@ -0,0 +1 @@
{"name": "Cristina Cochior", "email": "clcochior@gmail.com", "friend": "Y", "content": "Sure"}

1
static/files/002/2019-01-22_09:09:09_data_file.json

@ -0,0 +1 @@
{"name": "Anonymousse", "email": "bettermail@mail.com", "friend": "Y", "content": "A beach in an Arab-speaking place."}

1
static/files/003/2019-01-22_09:09:52_data_file.json

@ -0,0 +1 @@
{"name": "Mysterious", "email": "sound@mail.com", "friend": "Y", "content": "This is a sound file, but I am in a public setting and cannot listen back to it."}

64
templates/all.html

@ -0,0 +1,64 @@
{% extends "layout.html" %}
{% block content %}
<div class="all">
<h1>These are all the files</h1>
<!-- {{ listingfiles }} <br>
{{ jsonfiles }} <br>
{{ listingdirectories }} -->
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
{% for filename in listingfiles %}
{% if filename.lower().endswith(('.png', '.jpg', '.jpeg')) %}
<img class="img-responsive" src=" ../{{ filename }}">
{% for jsonfile in jsonfiles %}
{% if filename[0:9] == jsonfile[0:9] %}
<div class="descrip">
{{ jsonfile }}
<p>{{listofdicts[0]["name"]}}<br>
{{listofdicts[0]["email"]}}<br>
{{listofdicts[0]["friend"]}}<br>
{{listofdicts[0]["content"]}}</p>
</div>
{% endif %}
{% endfor %}
{% elif filename.lower().endswith(('.mp4')) %}
<video width="320" height="240" controls><source src="../{{filename}}" type="video/mp4">
Your browser does not support the video tag.
</video>
{% for jsonfile in jsonfiles %}
{% if filename[0:9] == jsonfile[0:9] %}
<div class="descrip">
{{ jsonfile }}
<p>{{listofdicts[1]["name"]}}<br>
{{listofdicts[1]["email"]}}<br>
{{listofdicts[1]["friend"]}}<br>
{{listofdicts[1]["content"]}}</p>
</div>
{% endif %}
{% endfor %}
{% elif filename.lower().endswith(('.mp3')) %}
<audio controls><source src="../{{filename}}" type="audio/mpeg">
Your browser does not support the audio tag.
</audio>
{% for jsonfile in jsonfiles %}
{% if filename[0:9] == jsonfile[0:9] %}
<div class="descrip">
{{ jsonfile }}
<p>{{listofdicts[2]["name"]}}<br>
{{listofdicts[2]["email"]}}<br>
{{listofdicts[2]["friend"]}}<br>
{{listofdicts[2]["content"]}}</p>
</div>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
</div>
</div>
{% endblock %}

1
templates/layout.html

@ -14,6 +14,7 @@
<li><a href="{{ url_for('home') }}">Home</a></li> <li><a href="{{ url_for('home') }}">Home</a></li>
<li><a href="{{ url_for('about') }}">About</a></li> <li><a href="{{ url_for('about') }}">About</a></li>
<li><a href="{{ url_for('description') }}">Form</a></li> <li><a href="{{ url_for('description') }}">Form</a></li>
<li><a href="{{ url_for('all') }}">All files</a></li>
</ul> </ul>
</nav></strong> </nav></strong>
</div> </div>

Loading…
Cancel
Save