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('') files_table_str.append(file_name) files_table_str.append('') 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() # Normalize keywords list keywords_list = [] for keyword in form_data['kljucne-besede'].split(','): keyword = keyword.strip() keywords_list.append(keyword) keywords = ','.join(keywords_list) 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=keywords, 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') agree_publish_future = form.get('javna-objava-prihodnost') if not agree_publish_future: return 'Manjkajoča izbrana vrednost pri polju za javno objavo.' 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(','): # keyword = keyword.strip() # if keyword.isspace() or not keyword.replace(' ', '').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 key == 'kljucne-besde': if len(val) > 500: return 'Polje "{}" presega dolžino {} znakov.'.format(key, 500) else: if len(val) > MAXLEN_FORM: return 'Polje "{}" presega dolžino {} znakov.'.format(key, MAXLEN_FORM)