realise document upload - Part 1

This commit is contained in:
Josako
2024-04-30 22:47:44 +02:00
parent 602a450114
commit 9f350b5ea0
9 changed files with 128 additions and 3 deletions

View File

@@ -0,0 +1,15 @@
from flask_wtf import FlaskForm
from wtforms import (StringField, BooleanField, SubmitField, DateField,
SelectMultipleField, FieldList, FormField)
from wtforms.validators import DataRequired, Length
from flask_wtf.file import FileField, FileAllowed, FileRequired
class AddDocumentForm(FlaskForm):
file = FileField('File', validators=[FileAllowed(['pdf', 'txt']),
FileRequired()])
name = StringField('Name', validators=[Length(max=100)])
language = StringField('Language', validators=[Length(max=2)])
valid_from = DateField('Valid from', id='form-control datepicker')
submit = SubmitField('Submit')

View File

@@ -0,0 +1,55 @@
import os
from datetime import datetime as dt, timezone as tz
from flask import request, redirect, url_for, flash, render_template, Blueprint, session, current_app
from flask_security import hash_password, roles_required, roles_accepted, current_user
from werkzeug.utils import secure_filename
from ..models.document import Document, DocumentLanguage, DocumentVersion
from ..extensions import db
from .document_forms import AddDocumentForm
from ..utils.database import Database
from ..utils.middleware import mw_before_request
document_bp = Blueprint('document_bp', __name__, url_prefix='/document')
@document_bp.before_request
def before_request():
mw_before_request()
@document_bp.route('/add_document', methods=['GET', 'POST'])
@roles_accepted('Super User', 'Tenant Admin')
def add_document():
form = AddDocumentForm()
if request.method == 'POST' and form.validate_on_submit():
file = form.file.data
filename = secure_filename(file.filename)
extension = filename.rsplit('.', 1)[1].lower()
# Create the Document
new_doc = Document()
if form.name.data == '':
new_doc.name = filename.rsplit('.', 1)[0]
else:
new_doc.name = form.name.data
timestamp = dt.now(tz.utc)
if form.valid_from.data or form.valid_from.data != '':
new_doc.valid_from = form.valid_from.data
else:
new_doc.valid_from = timestamp
new_doc.created_at = timestamp
new_doc.updated_at = timestamp
new_doc.created_by = current_user.id
new_doc.updated_by = current_user.id
db.session.add(new_doc)
# TODO: Continue from here on and complete add_document
# Create the DocumentLanguage
new_doc_lang = DocumentLanguage()
language = form.language.data
file.save(os.path.join(current_app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('document_bp.add_document'))

View File

@@ -15,8 +15,6 @@ user_bp = Blueprint('user_bp', __name__, url_prefix='/user')
@user_bp.route('/tenant', methods=['GET', 'POST'])
@roles_required('Super User')
def tenant():
print("SESSION:")
print(session)
if request.method == 'POST':
# Handle the required attributes
name = request.form.get('name')
@@ -58,7 +56,6 @@ def tenant():
# Create schema for new tenant
if error is None:
print(new_tenant.id)
Database(new_tenant.id).create_tenant_schema()
flash(error) if error else flash('Tenant added successfully.')