mirror of
				https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
				synced 2025-11-04 04:12:45 +00:00 
			
		
		
		
	Add corpus file upload
This commit is contained in:
		@@ -1,7 +1,21 @@
 | 
				
			|||||||
from flask_wtf import FlaskForm
 | 
					from flask_wtf import FlaskForm
 | 
				
			||||||
from wtforms import (MultipleFileField, StringField, SubmitField,
 | 
					from wtforms import (FileField, IntegerField, StringField, SubmitField,
 | 
				
			||||||
                     ValidationError, IntegerField)
 | 
					                     ValidationError)
 | 
				
			||||||
from wtforms.validators import DataRequired, Length, NumberRange
 | 
					from wtforms.validators import DataRequired, Length
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class AddCorpusFileForm(FlaskForm):
 | 
				
			||||||
 | 
					    author = StringField('Author', validators=[DataRequired(), Length(1, 64)])
 | 
				
			||||||
 | 
					    file = FileField('File', validators=[DataRequired()])
 | 
				
			||||||
 | 
					    publishing_year = IntegerField('Publishing year',
 | 
				
			||||||
 | 
					                                   validators=[DataRequired()])
 | 
				
			||||||
 | 
					    submit = SubmitField()
 | 
				
			||||||
 | 
					    title = StringField('Title', validators=[DataRequired(), Length(1, 64)])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def validate_file(form, field):
 | 
				
			||||||
 | 
					        if not field.data.filename.lower().endswith('.vrt'):
 | 
				
			||||||
 | 
					            raise ValidationError('File does not have an approved extension: '
 | 
				
			||||||
 | 
					                                  '.vrt')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class CreateCorpusForm(FlaskForm):
 | 
					class CreateCorpusForm(FlaskForm):
 | 
				
			||||||
@@ -11,20 +25,6 @@ class CreateCorpusForm(FlaskForm):
 | 
				
			|||||||
    title = StringField('Title', validators=[DataRequired(), Length(1, 32)])
 | 
					    title = StringField('Title', validators=[DataRequired(), Length(1, 32)])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class AddCorpusFileForm(FlaskForm):
 | 
					 | 
				
			||||||
    author = StringField('Author', validators=[DataRequired(), Length(1, 64)])
 | 
					 | 
				
			||||||
    files = MultipleFileField('Files', validators=[DataRequired()])
 | 
					 | 
				
			||||||
    publishing_year = IntegerField('Publishing year', validators=[DataRequired()])
 | 
					 | 
				
			||||||
    submit = SubmitField()
 | 
					 | 
				
			||||||
    title = StringField('Title', validators=[DataRequired(), Length(1, 64)])
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def validate_files(form, field):
 | 
					 | 
				
			||||||
        for file in field.data:
 | 
					 | 
				
			||||||
            if not file.filename.lower().endswith('.vrt'):
 | 
					 | 
				
			||||||
                raise ValidationError('File does not have an approved '
 | 
					 | 
				
			||||||
                                      'extension: .vrt')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class QueryForm(FlaskForm):
 | 
					class QueryForm(FlaskForm):
 | 
				
			||||||
    query = StringField('CQP Query', validators=[DataRequired()])
 | 
					    query = StringField('CQP Query', validators=[DataRequired()])
 | 
				
			||||||
    # hits_per_page = IntegerField('Hits per page',
 | 
					    # hits_per_page = IntegerField('Hits per page',
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,7 +17,7 @@ def index():
 | 
				
			|||||||
    return render_template('main/index.html.j2', title='Opaque')
 | 
					    return render_template('main/index.html.j2', title='Opaque')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@main.route('/corpora/<int:corpus_id>')
 | 
					@main.route('/corpora/<int:corpus_id>', methods=['GET', 'POST'])
 | 
				
			||||||
@login_required
 | 
					@login_required
 | 
				
			||||||
def corpus(corpus_id):
 | 
					def corpus(corpus_id):
 | 
				
			||||||
    corpus = Corpus.query.get_or_404(corpus_id)
 | 
					    corpus = Corpus.query.get_or_404(corpus_id)
 | 
				
			||||||
@@ -26,6 +26,29 @@ def corpus(corpus_id):
 | 
				
			|||||||
        abort(403)
 | 
					        abort(403)
 | 
				
			||||||
    add_corpus_file_form = AddCorpusFileForm()
 | 
					    add_corpus_file_form = AddCorpusFileForm()
 | 
				
			||||||
    if add_corpus_file_form.validate_on_submit():
 | 
					    if add_corpus_file_form.validate_on_submit():
 | 
				
			||||||
 | 
					        filename = secure_filename(add_corpus_file_form.file.data.filename)
 | 
				
			||||||
 | 
					        for corpus_file in corpus.files:
 | 
				
			||||||
 | 
					            if filename == corpus_file.filename:
 | 
				
			||||||
 | 
					                flash('File already registered to this corpus.')
 | 
				
			||||||
 | 
					                return redirect(url_for('main.corpus', corpus_id=corpus_id))
 | 
				
			||||||
 | 
					        # Gather information to create new corpus file database entry
 | 
				
			||||||
 | 
					        author = add_corpus_file_form.author.data
 | 
				
			||||||
 | 
					        dir = os.path.join(str(corpus.user_id), 'jobs', str(corpus.id))
 | 
				
			||||||
 | 
					        file = add_corpus_file_form.file.data
 | 
				
			||||||
 | 
					        publishing_year = add_corpus_file_form.publishing_year.data
 | 
				
			||||||
 | 
					        title = add_corpus_file_form.title.data
 | 
				
			||||||
 | 
					        # Save the file
 | 
				
			||||||
 | 
					        file_dir = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'],
 | 
				
			||||||
 | 
					                                dir)
 | 
				
			||||||
 | 
					        file.save(os.path.join(file_dir, filename))
 | 
				
			||||||
 | 
					        corpus_file = CorpusFile(author=author,
 | 
				
			||||||
 | 
					                                 corpus=corpus,
 | 
				
			||||||
 | 
					                                 dir=dir,
 | 
				
			||||||
 | 
					                                 filename=filename,
 | 
				
			||||||
 | 
					                                 publishing_year=publishing_year,
 | 
				
			||||||
 | 
					                                 title=title)
 | 
				
			||||||
 | 
					        db.session.add(corpus_file)
 | 
				
			||||||
 | 
					        db.session.commit()
 | 
				
			||||||
        flash('Corpus file added!')
 | 
					        flash('Corpus file added!')
 | 
				
			||||||
        return redirect(url_for('main.corpus', corpus_id=corpus_id))
 | 
					        return redirect(url_for('main.corpus', corpus_id=corpus_id))
 | 
				
			||||||
    return render_template('main/corpora/corpus.html.j2',
 | 
					    return render_template('main/corpora/corpus.html.j2',
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -87,7 +87,7 @@
 | 
				
			|||||||
    <form method="POST" enctype="multipart/form-data">
 | 
					    <form method="POST" enctype="multipart/form-data">
 | 
				
			||||||
      {{ add_corpus_file_form.hidden_tag() }}
 | 
					      {{ add_corpus_file_form.hidden_tag() }}
 | 
				
			||||||
      <div class="row">
 | 
					      <div class="row">
 | 
				
			||||||
        <div class="col s12 m8">
 | 
					        <div class="col s12 m4">
 | 
				
			||||||
          <div class="input-field">
 | 
					          <div class="input-field">
 | 
				
			||||||
            <i class="material-icons prefix">person</i>
 | 
					            <i class="material-icons prefix">person</i>
 | 
				
			||||||
            {{ add_corpus_file_form.author(data_length='64') }}
 | 
					            {{ add_corpus_file_form.author(data_length='64') }}
 | 
				
			||||||
@@ -107,16 +107,26 @@
 | 
				
			|||||||
            {% endfor %}
 | 
					            {% endfor %}
 | 
				
			||||||
          </div>
 | 
					          </div>
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
 | 
					        <div class="col s12 m4">
 | 
				
			||||||
 | 
					          <div class="input-field">
 | 
				
			||||||
 | 
					            <i class="material-icons prefix">access_time</i>
 | 
				
			||||||
 | 
					            {{ add_corpus_file_form.publishing_year() }}
 | 
				
			||||||
 | 
					            {{ add_corpus_file_form.publishing_year.label }}
 | 
				
			||||||
 | 
					            {% for error in add_corpus_file_form.publishing_year.errors %}
 | 
				
			||||||
 | 
					              <span class="helper-text red-text">{{ error }}</span>
 | 
				
			||||||
 | 
					            {% endfor %}
 | 
				
			||||||
 | 
					          </div>
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
        <div class="col s12">
 | 
					        <div class="col s12">
 | 
				
			||||||
          <div class="file-field input-field">
 | 
					          <div class="file-field input-field">
 | 
				
			||||||
            <div class="btn">
 | 
					            <div class="btn">
 | 
				
			||||||
              <span>{{ add_corpus_file_form.files.label.text }}</span>
 | 
					              <span>{{ add_corpus_file_form.file.label.text }}</span>
 | 
				
			||||||
              {{ add_corpus_file_form.files(accept='.vrt') }}
 | 
					              {{ add_corpus_file_form.file(accept='.vrt') }}
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
            <div class="file-path-wrapper">
 | 
					            <div class="file-path-wrapper">
 | 
				
			||||||
              <input class="file-path validate" type="text">
 | 
					              <input class="file-path validate" type="text">
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
            {% for error in add_corpus_file_form.files.errors %}
 | 
					            {% for error in add_corpus_file_form.file.errors %}
 | 
				
			||||||
              <span class="helper-text red-text">{{ error }}</span>
 | 
					              <span class="helper-text red-text">{{ error }}</span>
 | 
				
			||||||
            {% endfor %}
 | 
					            {% endfor %}
 | 
				
			||||||
          </div>
 | 
					          </div>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user