diff --git a/migrations/versions/c7bb822adf9f_added_upload_file_codes_column.py b/migrations/versions/c7bb822adf9f_added_upload_file_codes_column.py new file mode 100644 index 0000000..22a9de9 --- /dev/null +++ b/migrations/versions/c7bb822adf9f_added_upload_file_codes_column.py @@ -0,0 +1,30 @@ +"""Added upload file codes column. + +Revision ID: c7bb822adf9f +Revises: 0d9dd68fd94b +Create Date: 2021-12-20 09:00:57.208921 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'c7bb822adf9f' +down_revision = '0d9dd68fd94b' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('upload_solar', sa.Column('region', sa.String(), nullable=True)) + op.add_column('upload_solar', sa.Column('upload_file_codes', sa.ARRAY(sa.String()), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('upload_solar', 'upload_file_codes') + op.drop_column('upload_solar', 'region') + # ### end Alembic commands ### diff --git a/portal/model.py b/portal/model.py index fd5addf..fb51677 100644 --- a/portal/model.py +++ b/portal/model.py @@ -19,6 +19,7 @@ class UploadSolar(db.Model): institution = db.Column(db.Integer, sqlalchemy.ForeignKey('institution.id'), nullable=True) upload_hash = db.Column(db.String, nullable=False) timestamp = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) + region = db.Column(db.String, nullable=True) program = db.Column(db.String, nullable=True) subject = db.Column(db.String, nullable=True) subject_custom = db.Column(db.String, nullable=True) @@ -29,6 +30,7 @@ class UploadSolar(db.Model): grammar_corrections = db.Column(db.String, nullable=True) upload_file_hashes = db.Column(sqlalchemy.types.ARRAY(db.String), nullable=True) upload_file_names = db.Column(sqlalchemy.types.ARRAY(db.String), nullable=True) + upload_file_codes = db.Column(sqlalchemy.types.ARRAY(db.String), nullable=True) class ContractsSolar(db.Model): diff --git a/portal/solar.py b/portal/solar.py index 4be063a..5151e50 100644 --- a/portal/solar.py +++ b/portal/solar.py @@ -6,15 +6,10 @@ import traceback import ssl from datetime import datetime from sqlalchemy import desc -from collections import Counter from pathlib import Path -import imaplib from smtplib import SMTP_SSL -import email -from email import encoders -from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.application import MIMEApplication @@ -30,9 +25,9 @@ from . model import * VALID_PROGRAMS = {'OS', 'SSG', 'MGP', 'ZG', 'NPI', 'SPI', 'SSI', 'PTI'} -VALID_SUBJECTS = {'slo', 'drug-jez', 'drug-druz', 'drug-narav', 'drug-strok', 'drug-izb'} -VALID_TEXT_TYPES = {'esej-spis', 'prakticno', 'solski-test', 'delo-v-razredu'} -VALID_GRAMMAR_CORRECTIONS = {'popr-ne', 'brez-popr', 'popr-da'} +VALID_SUBJECTS = {'SLO', 'DJP', 'DDP', 'DNP', 'DSP', 'DIP'} +VALID_TEXT_TYPES = {'E', 'PB', 'T', 'R'} +VALID_GRAMMAR_CORRECTIONS = {'DD', 'N', 'DN'} VALID_REGIONS = {'CE', 'GO', 'KK', 'KP', 'KR', 'LJ', 'MB', 'MS', 'NM', 'PO', 'SG'} #REGEX_EMAIL = re.compile('^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$') @@ -157,21 +152,45 @@ class UploadHandlerSolar(): institution_id = get_user_institution(user_id).id + region = form_data['regija'] + program = form_data['program'] + subject = form_data['predmet'], + subject_custom = form_data['predmet-custom'], + grade = form_data['letnik'], + text_type = form_data['vrsta'], + text_type_custom = form_data['vrsta-custom'], + school_year = form_data['solsko-leto'], + grammar_corrections = form_data['jezikovni-popravki'], + upload_file_codes = [] + for i in range(len(sorted_file_items)): + file_code = '{}_{}_{}_{}_{}_{}_{}_{}'.format( + region[0], + program[0], + subject[0], + grade[0], + text_type[0], + school_year[0], + grammar_corrections[0], + i) + upload_file_codes.append(file_code) + model_obj = UploadSolar( upload_user = user_id, institution = institution_id, upload_hash=upload_metadata['upload_id'], timestamp=timestamp, - program=form_data['program'], - subject=form_data['predmet'], - subject_custom=form_data['predmet-custom'], - grade=form_data['letnik'], - text_type=form_data['vrsta'], - text_type_custom=form_data['vrsta-custom'], - school_year=form_data['solsko-leto'], - grammar_corrections=form_data['jezikovni-popravki'], + region=region, + program=program, + subject=subject, + subject_custom=subject_custom, + grade=grade, + text_type=text_type, + text_type_custom=text_type_custom, + school_year=school_year, + grammar_corrections=grammar_corrections, upload_file_hashes=[x[1] for x in sorted_file_items], upload_file_names=[x[0] for x in sorted_file_items], + upload_file_codes=upload_file_codes, ) UploadHandlerSolar.store_model(model_obj) @@ -281,6 +300,8 @@ class UploadHandlerSolar(): @staticmethod def check_form(form): + logging.info(form) + region = form['regija'] program = form['program'] predmet = form['predmet'] letnik = int(form['letnik']) @@ -288,6 +309,8 @@ class UploadHandlerSolar(): solsko_leto = form['solsko-leto'] jezikovni_popravki = form['jezikovni-popravki'] + if region not in VALID_REGIONS: + return 'Invalid region "{}"'.format(region) if program not in VALID_PROGRAMS: return 'Invalid program "{}"'.format(program) if predmet not in VALID_SUBJECTS: @@ -444,8 +467,9 @@ def get_all_active_users(): res.append(user) return res -def update_upload_item(item_id, program, subject, subject_custom, grade, text_type, text_type_custom, school_year, grammar_corrections): +def update_upload_item(item_id, region, program, subject, subject_custom, grade, text_type, text_type_custom, school_year, grammar_corrections): rowcount = db.session.query(UploadSolar).filter_by(id=item_id).update({ + 'region': region, 'program': program, 'subject': subject, 'subject_custom': subject_custom, @@ -819,3 +843,4 @@ def send_user_activation_mail(user_id, config): except Exception: traceback.print_exc() + diff --git a/templates/solar-oddaja.html b/templates/solar-oddaja.html index dc46696..e65f6a7 100644 --- a/templates/solar-oddaja.html +++ b/templates/solar-oddaja.html @@ -68,7 +68,30 @@