125 lines
4.3 KiB
Python
125 lines
4.3 KiB
Python
import logging
|
|
import traceback
|
|
import re
|
|
from datetime import datetime
|
|
|
|
import portal.base
|
|
from portal.base import UploadHandler, ContractCreator, REGEX_EMAIL
|
|
from portal.model import db, UploadPredavanja
|
|
|
|
|
|
MAXLEN_FORM = 150
|
|
|
|
class UploadHandlerPredavanja(UploadHandler):
|
|
|
|
ENABLED_FILETYPES = None # None means all filetypes
|
|
|
|
def __init__(self, **kwargs):
|
|
super().__init__(**kwargs)
|
|
self.contract_creator = ContractCreator(base_path=self.get_uploads_subdir('contracts'),
|
|
template_path='contract/predavanja.html')
|
|
|
|
def generate_upload_contract_pdf(self, upload_metadata):
|
|
form_data = upload_metadata['form_data']
|
|
|
|
files_table_str = []
|
|
for file_name in upload_metadata['file_names']:
|
|
files_table_str.append('<tr><td style="text-align: center;">')
|
|
files_table_str.append(file_name)
|
|
files_table_str.append('</td></tr>')
|
|
files_table_str = ''.join(files_table_str)
|
|
|
|
data = {
|
|
'ime_priimek': form_data['ime'],
|
|
'files_table_str': files_table_str
|
|
}
|
|
|
|
self.contract_creator.create_pdf(upload_metadata['contract_file'], data)
|
|
|
|
@staticmethod
|
|
def store_metadata(upload_metadata):
|
|
timestamp = datetime.fromtimestamp(upload_metadata['timestamp'])
|
|
form_data = upload_metadata['form_data']
|
|
file_hashes = upload_metadata['file_hashes_dict']
|
|
sorted_f_hashes = list(file_hashes.values())
|
|
sorted_f_hashes.sort()
|
|
|
|
try:
|
|
model_obj = UploadPredavanja(
|
|
upload_hash=upload_metadata['upload_id'],
|
|
timestamp=timestamp,
|
|
name=form_data['ime'],
|
|
address=form_data['naslov-predavanja'],
|
|
subject=form_data['predmet'],
|
|
faculty=form_data['fakulteta'],
|
|
email=form_data['email'],
|
|
phone=form_data.get('phone'),
|
|
keywords=form_data['kljucne-besede'],
|
|
agree_publish_future=form_data['javna-objava-prihodnost'],
|
|
agree_machine_translation=True if 'strojno-prevajanje' in form_data else False,
|
|
agree_news_cjvt=True if 'obvestila' in form_data else False,
|
|
file_contract=upload_metadata['contract_file'],
|
|
upload_file_hashes=sorted_f_hashes,
|
|
)
|
|
db.session.add(model_obj)
|
|
db.session.commit()
|
|
except Exception:
|
|
traceback.print_exc()
|
|
|
|
def handle_upload(self, request):
|
|
err = self.check_upload_request(request)
|
|
if err:
|
|
return err, 400
|
|
|
|
err = self.check_form(request.form)
|
|
if err:
|
|
return err, 400
|
|
|
|
# Parse request.
|
|
upload_metadata = self.extract_upload_metadata('predavanja', request)
|
|
|
|
logging.info('Upload for "predavanja" with id "{}" supplied form data: {}'.format(
|
|
upload_metadata['upload_id'], str(upload_metadata['form_data'])))
|
|
|
|
# Store uploaded files to disk.
|
|
self.store_datafiles(request.files, upload_metadata)
|
|
|
|
# Store metadata to database.
|
|
self.store_metadata(upload_metadata)
|
|
|
|
# Send confirmation mail
|
|
self.send_confirm_mail(upload_metadata)
|
|
|
|
return 'Uspešno ste oddali datotek(e). Št. datotek: {}'.format(len(request.files))
|
|
|
|
|
|
@staticmethod
|
|
def check_form(form):
|
|
name = form.get('ime')
|
|
address = form.get('naslov-predavanja')
|
|
subject = form.get('predmet')
|
|
faculty = form.get('fakulteta')
|
|
email = form.get('email')
|
|
phone = form.get('telefon')
|
|
keywords = form.get('kljucne-besede')
|
|
|
|
if not name \
|
|
or not address \
|
|
or not subject \
|
|
or not faculty \
|
|
or not email \
|
|
or not keywords:
|
|
return 'Izpolnite vsa obvezna polja.'
|
|
|
|
for keyword in keywords.split():
|
|
if not keyword.isalpha():
|
|
return 'Ključna beseda "{}" ni pravilnega formata.'.format(keyword)
|
|
|
|
if not re.search(REGEX_EMAIL, email):
|
|
return 'Email napačnega formata.'
|
|
|
|
for key, val in form.items():
|
|
if len(val) > MAXLEN_FORM:
|
|
return 'Polje "{}" presega dolžino {} znakov.'.format(key, MAXLEN_FORM)
|
|
|