mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2025-07-26 14:01:33 +00:00
normalize forms
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
from flask_wtf import FlaskForm
|
||||
from flask_wtf.file import FileField, FileRequired
|
||||
from werkzeug.utils import secure_filename
|
||||
from wtforms import (
|
||||
FileField,
|
||||
StringField,
|
||||
SubmitField,
|
||||
ValidationError,
|
||||
IntegerField
|
||||
)
|
||||
from wtforms.validators import DataRequired, Length
|
||||
from wtforms.validators import DataRequired, InputRequired, Length
|
||||
|
||||
|
||||
class AddCorpusFileForm(FlaskForm):
|
||||
@@ -15,36 +15,25 @@ class AddCorpusFileForm(FlaskForm):
|
||||
Form to add a .vrt corpus file to the current corpus.
|
||||
'''
|
||||
# Required fields
|
||||
author = StringField('Author', validators=[DataRequired(), Length(1, 255)])
|
||||
file = FileField('File', validators=[DataRequired()])
|
||||
publishing_year = IntegerField('Publishing year',
|
||||
validators=[DataRequired()])
|
||||
title = StringField('Title', validators=[DataRequired(), Length(1, 255)])
|
||||
author = StringField('Author', validators=[InputRequired(), Length(min=1, max=255)])
|
||||
publishing_year = IntegerField('Publishing year', validators=[InputRequired(), Length(min=1, max=255)])
|
||||
title = StringField('Title', validators=[InputRequired(), Length(min=1, max=255)])
|
||||
vrt = FileField('File', validators=[FileRequired()])
|
||||
# Optional fields
|
||||
address = StringField('Adress', validators=[Length(0, 255)])
|
||||
booktitle = StringField('Booktitle', validators=[Length(0, 255)])
|
||||
chapter = StringField('Chapter', validators=[Length(0, 255)])
|
||||
editor = StringField('Editor', validators=[Length(0, 255)])
|
||||
institution = StringField('Institution', validators=[Length(0, 255)])
|
||||
journal = StringField('Journal', validators=[Length(0, 255)])
|
||||
pages = StringField('Pages', validators=[Length(0, 255)])
|
||||
publisher = StringField('Publisher', validators=[Length(0, 255)])
|
||||
school = StringField('School', validators=[Length(0, 255)])
|
||||
address = StringField('Adress', validators=[Length(max=255)])
|
||||
booktitle = StringField('Booktitle', validators=[Length(max=255)])
|
||||
chapter = StringField('Chapter', validators=[Length(max=255)])
|
||||
editor = StringField('Editor', validators=[Length(max=255)])
|
||||
institution = StringField('Institution', validators=[Length(max=255)])
|
||||
journal = StringField('Journal', validators=[Length(max=255)])
|
||||
pages = StringField('Pages', validators=[Length(max=255)])
|
||||
publisher = StringField('Publisher', validators=[Length(max=255)])
|
||||
school = StringField('School', validators=[Length(max=255)])
|
||||
submit = SubmitField()
|
||||
|
||||
def __init__(self, corpus, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.corpus = corpus
|
||||
|
||||
def validate_file(self, field):
|
||||
def validate_vrt(self, field):
|
||||
if not field.data.filename.lower().endswith('.vrt'):
|
||||
raise ValidationError('File does not have an approved extension: '
|
||||
'.vrt')
|
||||
field.data.filename = secure_filename(field.data.filename)
|
||||
for corpus_file in self.corpus.files:
|
||||
if field.data.filename == corpus_file.filename:
|
||||
raise ValidationError('File already registered to corpus.')
|
||||
|
||||
raise ValidationError('VRT files only!')
|
||||
|
||||
class EditCorpusFileForm(FlaskForm):
|
||||
'''
|
||||
@@ -52,8 +41,7 @@ class EditCorpusFileForm(FlaskForm):
|
||||
'''
|
||||
# Required fields
|
||||
author = StringField('Author', validators=[DataRequired(), Length(1, 255)])
|
||||
publishing_year = IntegerField('Publishing year',
|
||||
validators=[DataRequired()])
|
||||
publishing_year = IntegerField('Publishing year', validators=[DataRequired()])
|
||||
title = StringField('Title', validators=[DataRequired(), Length(1, 255)])
|
||||
# Optional fields
|
||||
address = StringField('Adress', validators=[Length(0, 255)])
|
||||
@@ -72,27 +60,23 @@ class AddCorpusForm(FlaskForm):
|
||||
'''
|
||||
Form to add a a new corpus.
|
||||
'''
|
||||
description = StringField('Description',
|
||||
validators=[DataRequired(), Length(1, 255)])
|
||||
submit = SubmitField()
|
||||
description = StringField('Description', validators=[DataRequired(), Length(1, 255)])
|
||||
title = StringField('Title', validators=[DataRequired(), Length(1, 32)])
|
||||
submit = SubmitField()
|
||||
|
||||
|
||||
class ImportCorpusForm(FlaskForm):
|
||||
'''
|
||||
Form to import a corpus.
|
||||
'''
|
||||
description = StringField('Description',
|
||||
validators=[DataRequired(), Length(1, 255)])
|
||||
file = FileField('File', validators=[DataRequired()])
|
||||
submit = SubmitField()
|
||||
description = StringField('Description', validators=[DataRequired(), Length(1, 255)])
|
||||
archive = FileField('File', validators=[DataRequired()])
|
||||
title = StringField('Title', validators=[DataRequired(), Length(1, 32)])
|
||||
submit = SubmitField()
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def validate_file(self, field):
|
||||
if not field.data.filename.lower().endswith('.zip'):
|
||||
raise ValidationError('File does not have an approved extension: '
|
||||
'.zip')
|
||||
field.data.filename = secure_filename(field.data.filename)
|
||||
def validate_archive(self, field):
|
||||
if field.data.mimetype != 'application/zip':
|
||||
raise ValidationError('ZIP files only!')
|
||||
|
@@ -190,7 +190,7 @@ def add_corpus_file(corpus_id):
|
||||
corpus = Corpus.query.get_or_404(corpus_id)
|
||||
if not (corpus.user == current_user or current_user.is_administrator()):
|
||||
abort(403)
|
||||
form = AddCorpusFileForm(corpus, prefix='add-corpus-file-form')
|
||||
form = AddCorpusFileForm(prefix='add-corpus-file-form')
|
||||
if form.is_submitted():
|
||||
if not form.validate():
|
||||
return make_response(form.errors, 400)
|
||||
@@ -269,8 +269,9 @@ def download_corpus_file(corpus_id, corpus_file_id):
|
||||
abort(403)
|
||||
return send_from_directory(
|
||||
as_attachment=True,
|
||||
attachment_filename=corpus_file.filename,
|
||||
directory=os.path.dirname(corpus_file.path),
|
||||
filename=corpus_file.filename
|
||||
filename=os.path.basename(corpus_file.path)
|
||||
)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user