2020-10-09 20:10:50 +02:00
from flask import Flask , render_template , Markup , jsonify , session , request , send_file
2019-11-22 18:57:35 +01:00
import json
2019-11-27 19:02:41 +01:00
import os
from json import dumps
2020-09-21 15:50:38 +02:00
import collections
import random
import string
2020-10-09 20:10:50 +02:00
from zipfile import ZipFile
2019-11-22 18:57:35 +01:00
app = Flask ( __name__ , static_url_path = ' ' , static_folder = " static " , template_folder = " templates " )
app . jinja_env . add_extension ( ' jinja2.ext.loopcontrols ' )
# SETTING THE VARIABLES
path = " /static/files/ "
2020-09-21 15:50:38 +02:00
max_selected_items = 30
2019-11-22 18:57:35 +01:00
#move this list to Jinja so it can be placed in layout
#give back clicked value
2019-11-27 19:02:41 +01:00
# librarians = ["Alana", "Gentian", "Laurie", "Matias", "Scott", "Stacy", "Max", "Clara_B", "Michelle", "Clara_J_B", "Karen", "Lumsden_Primary_School", "Aaron", "Leo", "Maria", "Laura", "Honey", "Tender_Center", "wdka", "Naomi", "Cristina", "Julie", "ssw", "cyberspace", "Hannah", "Katarina", "Anastasia", "Kimmy", "Marie","Katherine", "Allison", "Mariana", "Viki", "Alice", "Eric", "hks", "cbk", "Sophie", "Antonio", "Jeremiah", "Angela", "Renee", "Marta", "Katherine", "Anna", "Auryn", "Ashley", "Anne", "Denise", "Eva", "Jonnah", "Kitty", "Masha", "Mia", "Cian", "Senke", "Maud", "Jinnie", "Rubin", "Julia", "Alana", "Gentian", "Laurie", "Matias","Scott", "Stacy"]
2019-11-22 18:57:35 +01:00
2019-11-27 19:02:41 +01:00
librarians = [ ]
2019-12-18 16:14:37 +01:00
files_without_folders = [ ]
2019-11-22 18:57:35 +01:00
2019-12-22 20:36:29 +01:00
with open ( ' static/js/mergedjson.json ' , ' r ' , encoding = ' utf8 ' ) as filesandnames :
2019-11-22 18:57:35 +01:00
filetoname_dict = json . load ( filesandnames )
2019-11-27 19:02:41 +01:00
#get full list librarians from json (printed later through global variable)
for file in filetoname_dict :
for name in file [ " librarian " ] :
librarians . append ( str ( name ) )
librarians = list ( dict . fromkeys ( librarians ) )
2019-11-22 18:57:35 +01:00
#this value we should get from template
2019-11-27 19:02:41 +01:00
librarianselected = " Ruben "
2019-11-22 18:57:35 +01:00
#go from librarian name to file
for file in filetoname_dict :
2019-12-18 16:14:37 +01:00
# if librarianselected in file["librarian"]:
thedebris = file [ " debrisname " ]
#apply regex to the debris
thedebris = os . path . basename ( thedebris )
files_without_folders . append ( str ( thedebris ) )
# print(str(librarianselected)+" contributed with the following debris : "+str(thedebris))
2019-11-22 18:57:35 +01:00
#this value we should get from template
2019-12-18 16:14:37 +01:00
fileselected = filetoname_dict [ 1 ] [ " debrisname " ]
2019-11-22 18:57:35 +01:00
#go from file name to librarian name
for file in filetoname_dict :
2019-12-18 16:14:37 +01:00
if fileselected in file [ " debrisname " ] :
2019-11-22 18:57:35 +01:00
thelibrarian = file [ " librarian " ]
print ( " librarians for " + str ( fileselected ) + " are: " + str ( thelibrarian ) )
2019-11-27 19:02:41 +01:00
print ( " --------------------------------------------------- " )
2019-11-22 18:57:35 +01:00
2020-09-21 15:50:38 +02:00
# preparing a index.json file of files
index_dict = { }
@app.route ( " / " , methods = [ ' POST ' , ' GET ' ] )
2019-11-22 18:57:35 +01:00
def home ( ) :
2020-09-21 15:50:38 +02:00
if request . args :
# We have our query string nicely serialized as a Python dictionary
args = request . args
# We'll create a string to display the parameters & values
# serialized = ", ".join(f"{k}: {v}" for k, v in request.args.items())
# Display the query string to the client in a different format
# return f"(Query) {serialized}", 200
urllibrarian = request . args . getlist ( ' librarian ' ) # new
urlmethod = request . args . getlist ( ' method ' ) # new
for a in urllibrarian :
print ( ' urllibrarian is: ' + a )
for b in urlmethod :
print ( ' urlmethod is: ' + b )
#unsure if Im really using that for now I think not
# selected = "textbysomeone"
# selections = collections.deque(session['selection'], maxlen=max_selected_items) #?
# selections.append(selected)
# session['selection'] = list(selections)
return render_template ( ' layout.html ' , urllibrarian = urllibrarian , urlmethod = urlmethod , filetoname_dict = filetoname_dict , librarians = librarians , files_without_folders = files_without_folders ) # new
2019-11-22 18:57:35 +01:00
@app.route ( ' /about/ ' )
def about ( ) :
2020-09-21 15:50:38 +02:00
selectedstuff = session [ ' selection ' ]
return render_template ( ' about.html ' , selectedstuff = selectedstuff )
2019-12-22 20:36:29 +01:00
@app.route ( ' /exchange ' )
def exchange ( ) :
return render_template ( ' exchange.html ' )
2020-09-21 15:50:38 +02:00
@app.route ( ' /test/ ' , methods = [ ' GET ' , ' POST ' ] )
def test ( ) :
print ( " test " )
clicked = None
2020-10-09 20:10:50 +02:00
bundle = " bundle.zip "
zipObj = ZipFile ( bundle , ' w ' )
2020-09-21 15:50:38 +02:00
if request . method == " POST " :
# clicked=request.form['data']
listofselecteddocs = request . get_json ( )
print ( " hallo " )
print ( listofselecteddocs )
print ( " hola " )
2020-10-09 20:10:50 +02:00
# create a ZipFile object
# Add multiple files to the zip
zipObj . write ( ' static/files/ ' + listofselecteddocs [ 0 ] )
zipObj . write ( ' static/files/ ' + listofselecteddocs [ 1 ] )
zipObj . write ( ' static/default-files/hello.txt ' )
# close the Zip File
# return render_template('test.html')
# return send_file(bundle, mimetype='application/zip', as_attachment=True, attachment_filename=bundle)
# TEST
# print("sending file...")
# result = send_file(bundle, as_attachment=True)
# print("file sent, deleting...")
# os.remove(bundle)
# return result
zipObj . close ( )
return send_file ( bundle , mimetype = ' application/zip ' , attachment_filename = " hello.zip " , as_attachment = True )