mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-12-24 23:51:03 +00:00
Added ImportView
This commit is contained in:
parent
40ab16f632
commit
72420615b0
3 changed files with 52 additions and 3 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -3,4 +3,5 @@ __pycache__
|
||||||
.vscode
|
.vscode
|
||||||
advLab.db
|
advLab.db
|
||||||
server_setup.md
|
server_setup.md
|
||||||
db_backups
|
db_backups
|
||||||
|
import_files
|
|
@ -1,18 +1,23 @@
|
||||||
from flask import flash, request, url_for
|
from flask import flash, request, url_for, redirect
|
||||||
from flask_admin.contrib.sqla.fields import QuerySelectField, QuerySelectMultipleField
|
from flask_admin.contrib.sqla.fields import QuerySelectField, QuerySelectMultipleField
|
||||||
from flask_admin.contrib.sqla.filters import BaseSQLAFilter
|
from flask_admin.contrib.sqla.filters import BaseSQLAFilter
|
||||||
from flask_admin.helpers import get_form_data
|
from flask_admin.helpers import get_form_data
|
||||||
from flask_admin.menu import MenuLink
|
from flask_admin.menu import MenuLink
|
||||||
from flask_admin.model.template import EndpointLinkRowAction
|
from flask_admin.model.template import EndpointLinkRowAction
|
||||||
|
from flask_admin import expose
|
||||||
from flask_security import admin_change_password, current_user, hash_password
|
from flask_security import admin_change_password, current_user, hash_password
|
||||||
from sqlalchemy import func, or_, and_
|
from sqlalchemy import func, or_, and_
|
||||||
from wtforms import BooleanField, Form, RadioField, SelectField, TextField
|
from wtforms import BooleanField, Form, RadioField, SelectField, TextField
|
||||||
from wtforms.fields.html5 import DateField, IntegerField
|
from wtforms.fields.html5 import DateField, IntegerField
|
||||||
from wtforms.validators import URL, DataRequired, Email, NumberRange, Optional
|
from wtforms.validators import URL, DataRequired, Email, NumberRange, Optional
|
||||||
|
from flask_wtf import FlaskForm
|
||||||
|
from flask_wtf.file import FileField, FileRequired, FileAllowed
|
||||||
|
from werkzeug.utils import secure_filename
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from advlabdb import adminSpace, app, assistantSpace, db, user_datastore
|
from advlabdb import adminSpace, app, assistantSpace, db, user_datastore
|
||||||
from advlabdb.configUtils import getConfig
|
from advlabdb.configUtils import getConfig
|
||||||
from advlabdb.customClasses import SecureAdminModelView
|
from advlabdb.customClasses import SecureAdminModelView, SecureAdminBaseView
|
||||||
from advlabdb.exceptions import DataBaseException, ModelViewException
|
from advlabdb.exceptions import DataBaseException, ModelViewException
|
||||||
from advlabdb.models import (
|
from advlabdb.models import (
|
||||||
Appointment,
|
Appointment,
|
||||||
|
@ -36,6 +41,7 @@ from advlabdb.utils import (
|
||||||
setUserActiveSemester,
|
setUserActiveSemester,
|
||||||
userActiveSemester,
|
userActiveSemester,
|
||||||
)
|
)
|
||||||
|
from advlabdb.database_import import importFromFile
|
||||||
|
|
||||||
|
|
||||||
def semesterExperimentQueryFactory():
|
def semesterExperimentQueryFactory():
|
||||||
|
@ -775,6 +781,38 @@ class ProgramView(SecureAdminModelView):
|
||||||
column_details_list = column_list + form_excluded_columns
|
column_details_list = column_list + form_excluded_columns
|
||||||
|
|
||||||
|
|
||||||
|
class ImportView(SecureAdminBaseView):
|
||||||
|
class FileForm(FlaskForm):
|
||||||
|
file = FileField(
|
||||||
|
label="Import file",
|
||||||
|
validators=[FileRequired(), FileAllowed(["txt"], "Only txt files are allowed!")],
|
||||||
|
description="The import file has to be a text file (with .txt at the end) encoded in UTF-8. It has to strictly follow the required format.",
|
||||||
|
)
|
||||||
|
|
||||||
|
@expose("/", methods=["GET", "POST"])
|
||||||
|
def index(self):
|
||||||
|
form = ImportView.FileForm()
|
||||||
|
|
||||||
|
if form.validate_on_submit():
|
||||||
|
f = form.file.data
|
||||||
|
filename = secure_filename(f.filename)
|
||||||
|
|
||||||
|
directory = "import_files"
|
||||||
|
Path(directory).mkdir(exist_ok=True)
|
||||||
|
|
||||||
|
filePath = directory + f"/{filename}"
|
||||||
|
f.save(filePath)
|
||||||
|
|
||||||
|
try:
|
||||||
|
importFromFile(filePath)
|
||||||
|
except Exception as exc:
|
||||||
|
flash(str(exc), "error")
|
||||||
|
|
||||||
|
return redirect(url_for("index"))
|
||||||
|
|
||||||
|
return self.render("import.html", form=form)
|
||||||
|
|
||||||
|
|
||||||
adminSpace.add_view(StudentView(Student, db.session))
|
adminSpace.add_view(StudentView(Student, db.session))
|
||||||
adminSpace.add_view(PartStudentView(PartStudent, db.session))
|
adminSpace.add_view(PartStudentView(PartStudent, db.session))
|
||||||
adminSpace.add_view(GroupView(Group, db.session))
|
adminSpace.add_view(GroupView(Group, db.session))
|
||||||
|
@ -789,5 +827,6 @@ adminSpace.add_view(AssistantView(Assistant, db.session))
|
||||||
adminSpace.add_view(UserView(User, db.session))
|
adminSpace.add_view(UserView(User, db.session))
|
||||||
adminSpace.add_view(RoleView(Role, db.session))
|
adminSpace.add_view(RoleView(Role, db.session))
|
||||||
adminSpace.add_view(ProgramView(Program, db.session))
|
adminSpace.add_view(ProgramView(Program, db.session))
|
||||||
|
adminSpace.add_view(ImportView(name="Import"))
|
||||||
|
|
||||||
initActiveSemesterMenuLinks(adminSpace)
|
initActiveSemesterMenuLinks(adminSpace)
|
||||||
|
|
9
advlabdb/templates/import.html
Normal file
9
advlabdb/templates/import.html
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
{% extends "admin/master.html" %}
|
||||||
|
|
||||||
|
{% block body %}
|
||||||
|
<form method="POST" enctype="multipart/form-data">
|
||||||
|
{{ form.csrf_token }}
|
||||||
|
{{ form.file }}
|
||||||
|
<input type="submit" value="Upload and import">
|
||||||
|
</form>
|
||||||
|
{% endblock body %}
|
Loading…
Reference in a new issue