2021-05-17 12:33:53 +00:00
|
|
|
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):
|
|
|
|
|
2021-05-25 12:51:40 +00:00
|
|
|
ENABLED_FILETYPES = None # None means all filetypes
|
|
|
|
|
2021-05-17 12:33:53 +00:00
|
|
|
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()
|
2021-06-11 10:39:29 +00:00
|
|
|
|
|
|
|
# Normalize keywords list
|
|
|
|
keywords_list = []
|
|
|
|
for keyword in form_data['kljucne-besede'].split(','):
|
|
|
|
keyword = keyword.strip()
|
|
|
|
keywords_list.append(keyword)
|
|
|
|
keywords = ','.join(keywords_list)
|
2021-05-17 12:33:53 +00:00
|
|
|
|
|
|
|
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'),
|
2021-06-11 10:39:29 +00:00
|
|
|
keywords=keywords,
|
2021-05-24 08:15:54 +00:00
|
|
|
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,
|
2021-05-17 12:33:53 +00:00
|
|
|
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)
|
|
|
|
|
2021-05-25 11:59:38 +00:00
|
|
|
# Send confirmation mail
|
2021-05-17 12:33:53 +00:00
|
|
|
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')
|
2021-06-11 10:39:29 +00:00
|
|
|
agree_publish_future = form.get('javna-objava-prihodnost')
|
|
|
|
|
|
|
|
if not agree_publish_future:
|
|
|
|
return 'Manjkajoča izbrana vrednost pri polju za javno objavo.'
|
2021-05-17 12:33:53 +00:00
|
|
|
|
|
|
|
if not name \
|
|
|
|
or not address \
|
|
|
|
or not subject \
|
|
|
|
or not faculty \
|
|
|
|
or not email \
|
|
|
|
or not keywords:
|
|
|
|
return 'Izpolnite vsa obvezna polja.'
|
|
|
|
|
2021-06-11 10:39:29 +00:00
|
|
|
for keyword in keywords.split(','):
|
|
|
|
keyword = keyword.strip()
|
|
|
|
if keyword.isspace() or not keyword.isalpha():
|
2021-05-17 12:33:53 +00:00
|
|
|
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)
|
|
|
|
|