Initial commit.

This commit is contained in:
msinkec 2020-10-23 10:07:14 +02:00
commit 8996dbcefe
4 changed files with 426 additions and 0 deletions

142
.gitignore vendored Normal file
View File

@ -0,0 +1,142 @@
uploads/*
!uploads/.gitkeep
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/

149
app.py Normal file
View File

@ -0,0 +1,149 @@
import os
import re
import hashlib
import time
from pathlib import Path
from flask import Flask, render_template, request
from flask_dropzone import Dropzone
enabled_filetypes = ['txt', 'csv', 'pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx']
regex_email = re.compile('^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$')
basedir = Path(__file__).resolve().parent
upload_dir = basedir / 'uploads'
if not upload_dir.exists:
upload_dir.mkdir()
app = Flask(__name__)
app.config.update(
UPLOADED_PATH = upload_dir,
MAX_CONTENT_LENGTH = 1000000000 # 1GB
)
dropzone = Dropzone(app)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def handle_upload():
files = request.files
if len(files) > 20:
return 'Naložite lahko do 20 datotek hkrati.', 400
elif len(files) < 1:
return 'Priložena ni bila nobena datoteka.', 400
err = check_suffixes(files)
if err:
return err, 400
err = check_form(request.form)
if err:
return err, 400
file_hashes = create_file_hashes(files)
store_metadata(request.form, file_hashes)
store_datafiles(files, file_hashes)
return 'Uspešno ste oddali datotek(e). Št. datotek: {}'.format(len(files))
def check_suffixes(files):
for key, f in files.items():
if key.startswith('file'):
suffix = f.filename.split('.')[-1]
if suffix not in enabled_filetypes:
return 'Datoteka "{}" ni pravilnega formata.'.format(f.filename)
return None
def check_form(form):
tip = form.get('tip')
ime = form.get('ime')
podjetje = form.get('podjetje')
email = form.get('email')
telefon = form.get('telefon')
if tip not in ['enojez', 'prevodi']:
return 'Napačen tip besedila.'
if len(ime) > 100:
return 'Predolgo ime.'
if len(podjetje) > 100:
return 'Predolgo ime institucije'
if len(email) > 100:
return 'Predolgi email naslov'
elif not re.search(regex_email, email):
return 'Email napačnega formata.'
if len(telefon) > 100:
return 'Predolga telefonska št.'
return None
def create_file_hashes(files):
res = dict()
for key, f in files.items():
if key.startswith('file'):
h = hashlib.md5(f.filename.encode())
h.update(f.stream.read())
res[key] = h.hexdigest()
f.seek(0)
return res
def store_metadata(form, file_hashes):
base = app.config['UPLOADED_PATH'] / 'meta'
if not base.exists():
base.mkdir()
tip = form.get('tip')
ime = form.get('ime')
podjetje = form.get('podjetje')
email = form.get('email')
telefon = form.get('telefon')
# This hash serves as an identifier for the whole upload.
metahash = hashlib.md5((tip+ime+podjetje+email+telefon).encode())
# Include file hashes to avoid metafile name collisions if they have the same form values,
# but different data files. Sort hashes first so upload order doesn't matter.
sorted_f_hashes = list(file_hashes.values())
sorted_f_hashes.sort()
metahash.update(''.join(sorted_f_hashes).encode())
metahash = metahash.hexdigest()
timestamp = int(time.time())
filename = str(timestamp) + '-' + email + '-' + metahash + '.meta'
path = base / filename
with path.open('w') as f:
f.write('tip=' + tip)
f.write('\nime=' + ime)
f.write('\npodjetje=' + podjetje)
f.write('\nemail=' + email)
f.write('\ndatoteke=' + str(sorted_f_hashes))
def store_datafiles(files, file_hashes):
base = app.config['UPLOADED_PATH'] / 'files'
if not base.exists():
base.mkdir()
for key, f in files.items():
if key.startswith('file'):
path = base / file_hashes[key]
if not path.exists():
path.mkdir()
f.save(path / f.filename)
if __name__ == '__main__':
app.run(debug=True)

135
templates/index.html Normal file
View File

@ -0,0 +1,135 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Portal za oddajanje besedil za DS4 in DS1</title>
{{ dropzone.load_css() }}
<style>
#info-fields {
position: relative;
top: -350px;
}
label, input {
display: block;
}
input, select {
margin-bottom: 10px;
}
label {
margin-bottom: 5px;
}
#izjava {
float:left;
}
</style>
</head>
<body>
<h1>Portal za oddajanje besedil za DS4 in DS1</h1>
<form id="my-dropzone" class="dropzone" style="margin-top: 350px;">
<div id="info-fields">
<label for="tip">Želim prispevati:</label>
<select id="tip" name="tip" form="tip" required="required">
<option value="enojez">Enojezična besedila</option>
<option value="prevodi">Prevodi</option>
</select>
<label for="ime">*Ime:</label>
<input type="text" id="ime" name="ime" required="required"/>
<label for="podjetje">Podjetje / institucija:</label>
<input type="text" id="podjetje" name="podjetje"/>
<label for="email">*Email:</label>
<input type="text" id="email" name="email" required="required"/>
<label for="telefon">Telefon:</label>
<input type="text" id="telefon" name="telefon"/>
<input type="checkbox" id="izjava" name="izjava" value="izjava" required="required">
<label for="izjava">*Izjavljam, da sem lastnik avtorskih pravic in dovoljujem, da se besedila vključijo v korpuse v skladu z ustrezno licenco korpusa.</label>
<button type="submit">Oddaj</button>
</div>
<div class="dropzone-previews"></div>
</form>
{{ dropzone.load_js() }}
<script>
function isEmptyOrSpaces(str){
return str == null || str.match(/^ *$/) !== null;
}
const reEmail = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
Dropzone.options.myDropzone = { // The camelized version of the ID of the form element
url: "/upload",
autoProcessQueue: false,
uploadMultiple: true,
parallelUploads: 20,
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 1000, // MB
acceptedFiles: ".txt, .csv, .pdf, .doc, .docx, .xls, .xlsx, .ppt, .pptx",
maxFiles: 20,
dictDefaultMessage: `Kliknite ali odložite datoteke sem.`,
dictFallbackMessage: "Vaš brskalnik ne podpira izbiranje datotek z odlaganjem (\"drag & drop\").",
dictInvalidFileType: "Datoteka je napačnega formata.",
dictFileTooBig: "Datoteke je prevelika {{filesize}}. Največja dovoljena velikost: {{maxFilesize}}MiB.",
dictResponseError: "Napaka strežnika: {{statusCode}}",
dictMaxFilesExceeded: "Ne morete naložiti več datotek.",
dictCancelUpload: "Prekini prenos",
dictRemoveFile: "Odstrani datoteko",
dictCancelUploadConfirmation: "Ali res želite odstraniti to datoteko?",
dictUploadCanceled: "Prenos prekinjen",
// The setting up of the dropzone
init: function() {
var dz = this;
// First change the button to actually tell Dropzone to process the queue.
this.element.querySelector("button[type=submit]").addEventListener("click", function(e) {
// Make sure that the form isn't actually being sent.
e.preventDefault();
e.stopPropagation();
// Check form validity.
var form = document.forms["my-dropzone"];
var ime = form["ime"].value;
var email = form["email"].value;
var podjetje = form["podjetje"].value;
var telefon = form["telefon"].value;
var izjava = form["izjava"].checked;
if (isEmptyOrSpaces(ime) || isEmptyOrSpaces(email) || !izjava) {
alert("Izpolnite vsa obvezna polja!");
} else if (!reEmail.test(email.toLowerCase())) {
alert("Email napačnega formata!");
} else if (ime.length > 100 || email.length > 100 || podjetje.length > 100 || telefon.length > 100) {
alert("Velikost polj je omejena na 100 znakov.");
} else {
dz.processQueue();
}
});
// Listen to the sendingmultiple event. In this case, it's the sendingmultiple event instead
// of the sending event because uploadMultiple is set to true.
this.on("sendingmultiple", function() {
// Gets triggered when the form is actually being sent.
// Hide the success button or the complete form.
});
this.on("successmultiple", function(files, response) {
// Gets triggered when the files have successfully been sent.
// Redirect user or notify of success.
alert("Odgovor strežnika: " + response);
location.reload();
});
this.on("errormultiple", function(files, response) {
// Gets triggered when there was an error sending the files.
// Maybe show form again, and notify user of error
});
}
}
</script>
</body>
</html>

0
uploads/.gitkeep Normal file
View File