From 9e93d41bc3517388cf644e1069164aa7cbaf2735 Mon Sep 17 00:00:00 2001 From: msinkec Date: Mon, 20 Dec 2021 11:15:01 +0100 Subject: [PATCH] Upload file codes, upload region. --- ...822adf9f_added_upload_file_codes_column.py | 30 +++++++++ portal/model.py | 2 + portal/solar.py | 59 ++++++++++++----- templates/solar-oddaja.html | 63 +++++++++++++------ templates/solar-zgodovina.html | 4 ++ 5 files changed, 122 insertions(+), 36 deletions(-) create mode 100644 migrations/versions/c7bb822adf9f_added_upload_file_codes_column.py 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 @@
- + + +
+
+
+

+
+
+
+
+
+
@@ -90,12 +113,12 @@
@@ -143,10 +166,10 @@
@@ -187,9 +210,9 @@
@@ -295,7 +318,7 @@ //onready selectPredmet.addEventListener("change", function(e) { var predmetCustomBox = document.getElementById("predmet-custom-box").closest('.row'); - if (selectPredmet.value.startsWith("drug")) { + if (selectPredmet.value.startsWith("D")) { predmetCustomBox.style.maxHeight = "150px"; } else { predmetCustomBox.style.maxHeight = "0px"; @@ -304,7 +327,7 @@ selectVrsta.addEventListener("change", function(e) { var vrstaCustomBox = document.getElementById("vrsta-custom-box").closest('.row'); - if (selectVrsta.value == "delo-v-razredu") { + if (selectVrsta.value == "R") { vrstaCustomBox.style.maxHeight = "150px"; } else { vrstaCustomBox.style.maxHeight = "0px"; @@ -351,6 +374,7 @@ e.stopPropagation(); // Check form validity. + var regija = form["regija"].value; var program = form["program"].value; var predmet = form["predmet"].value; var predmetCustom = form["predmet-custom"].value; @@ -360,9 +384,9 @@ var solskoLeto = form["solsko-leto"].value; var jezikovniPopravki = form["jezikovni-popravki"].value; - if (predmet.startsWith("drug") && isEmptyOrSpaces(predmetCustom)) { + if (predmet.startsWith("D") && isEmptyOrSpaces(predmetCustom)) { showError("Polje za predmet ne more biti prazno!"); - } else if (vrsta === "delo-v-razredu" && isEmptyOrSpaces(vrstaCustom)) { + } else if (vrsta === "R" && isEmptyOrSpaces(vrstaCustom)) { showError("Polje za vrsto besedila ne more biti prazno!"); } else if (dataConfirmNotification.style.display == "none") { dataConfirmNotification.style.display = "inherit"; @@ -409,6 +433,7 @@ this.on("sending", function(file, xhr, formData) { + formData.append("regija",form["regija"].value); formData.append("program",form["program"].value); formData.append("predmet",form["predmet"].value); formData.append("predmet-custom",form["predmet-custom"].value); diff --git a/templates/solar-zgodovina.html b/templates/solar-zgodovina.html index 3a2984f..6620d35 100644 --- a/templates/solar-zgodovina.html +++ b/templates/solar-zgodovina.html @@ -130,6 +130,10 @@
{{f_name}} +
+ {% if item.upload_file_codes != None %} +
{{item.upload_file_codes[loop.index - 1]}}
+ {% endif %}
{% endfor %} {% else %}