diff --git a/app/main/forms.py b/app/main/forms.py index e69de29b..82b3a7c7 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -0,0 +1,21 @@ +from flask_wtf import FlaskForm +from wtforms import MultipleFileField, SelectField, StringField, SubmitField +from wtforms.validators import DataRequired, Length + + +class CreateCorpusForm(FlaskForm): + description = StringField( + 'Description', + validators=[DataRequired(), Length(1, 64)] + ) + files = MultipleFileField('Files') + title = StringField( + 'Title', + validators=[DataRequired(), Length(1, 32)] + ) + submit = SubmitField('Create corpus') + ''' + def validate_files(form, field): + for file in form.files.getlist[field.name]: + if file.mimetype + ''' diff --git a/app/main/views.py b/app/main/views.py index 93be680b..ccd1b755 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -1,14 +1,47 @@ -from flask import render_template +from flask import current_app, flash, redirect, render_template, request, url_for +from flask_login import current_user, login_required from ..models import User from ..tables import AdminUserTable, AdminUserItem from . import main +from .forms import CreateCorpusForm from ..decorators import admin_required -from flask_login import login_required +import os -@main.route('/') -def index(): - return render_template('main/index.html.j2', title='Portal') +@main.route('/', methods=['GET', 'POST']) +@login_required +def dashboard(): + create_corpus_form = CreateCorpusForm() + + if create_corpus_form.validate_on_submit(): + app = current_app._get_current_object() + files = request.FILES + print(files) + corpus = { + 'description': create_corpus_form.description.data, + 'files': [], + 'owner': current_user.id, + 'title': create_corpus_form.title.data + } + corpus_dir = os.path.join( + app.config['OPAQUE_FILES'], + 'corpora', + corpus['title'] + ) + + try: + os.mkdir(corpus_dir) + except FileExistsError: + flash('FileExistsError') + else: + flash('Corpus created!') + return redirect(url_for('main.dashboard')) + + return render_template( + 'main/dashboard.html.j2', + title='Dashboard', + create_corpus_form=create_corpus_form + ) @main.route('/about') diff --git a/app/services/views.py b/app/services/views.py index a6cf9ba4..75d494dd 100644 --- a/app/services/views.py +++ b/app/services/views.py @@ -1,5 +1,5 @@ from datetime import datetime -from flask import current_app, flash, redirect, render_template, url_for, request +from flask import current_app, flash, redirect, render_template, url_for from . import services from flask_login import current_user, login_required from .forms import OCRJobForm @@ -18,7 +18,7 @@ def ocr(): id = hashlib.md5( (current_user.username + '_' + datetime.now().isoformat()).encode() ).hexdigest() - dir = os.path.join(app.config['OPAQUE_UPLOAD_DIRECTORY'], id) + dir = os.path.join(app.config['OPAQUE_FILES'], 'jobs', id) try: os.mkdir(dir) diff --git a/app/templates/base.html.j2 b/app/templates/base.html.j2 index 784d26ed..497dc6b9 100644 --- a/app/templates/base.html.j2 +++ b/app/templates/base.html.j2 @@ -29,7 +29,7 @@
- - -{% endblock %} diff --git a/config.py b/config.py index 1de0f6b4..1d27a603 100644 --- a/config.py +++ b/config.py @@ -12,9 +12,9 @@ class Config: MAIL_USERNAME = os.environ.get('MAIL_USERNAME') MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD') OPAQUE_ADMIN = os.environ.get('OPAQUE_ADMIN') + OPAQUE_FILES = os.environ.get('OPAQUE_FILES') OPAQUE_MAIL_SUBJECT_PREFIX = '[Opaque]' OPAQUE_MAIL_SENDER = 'Opaque Development