mirror of
				https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
				synced 2025-11-04 12:22:47 +00:00 
			
		
		
		
	Merge branch 'development' of gitlab.ub.uni-bielefeld.de:sfb1288inf/opaque into development
This commit is contained in:
		@@ -1,7 +1,21 @@
 | 
			
		||||
from flask_wtf import FlaskForm
 | 
			
		||||
from wtforms import (MultipleFileField, StringField, SubmitField,
 | 
			
		||||
from wtforms import (FileField, StringField, SubmitField,
 | 
			
		||||
                     ValidationError, IntegerField, SelectField)
 | 
			
		||||
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):
 | 
			
		||||
@@ -11,20 +25,6 @@ class CreateCorpusForm(FlaskForm):
 | 
			
		||||
    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):
 | 
			
		||||
    query = StringField('CQP Query', validators=[DataRequired(), (Length(1, 1024))])
 | 
			
		||||
    hits_per_page = SelectField('Hits per page',
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,7 @@ def index():
 | 
			
		||||
    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
 | 
			
		||||
def corpus(corpus_id):
 | 
			
		||||
    corpus = Corpus.query.get_or_404(corpus_id)
 | 
			
		||||
@@ -26,6 +26,29 @@ def corpus(corpus_id):
 | 
			
		||||
        abort(403)
 | 
			
		||||
    add_corpus_file_form = AddCorpusFileForm()
 | 
			
		||||
    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), 'corpora', 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!')
 | 
			
		||||
        return redirect(url_for('main.corpus', corpus_id=corpus_id))
 | 
			
		||||
    return render_template('main/corpora/corpus.html.j2',
 | 
			
		||||
 
 | 
			
		||||
@@ -69,7 +69,7 @@
 | 
			
		||||
            <td>{{ file.publishing_year }}</td>
 | 
			
		||||
            <td class="right-align">
 | 
			
		||||
              <a class="waves-effect waves-light btn-small"><i class="material-icons">edit</i></a>
 | 
			
		||||
              <a class="waves-effect waves-light btn-small"><i class="material-icons">file_download</i></a>
 | 
			
		||||
              <a class="waves-effect waves-light btn-small" href="{{ url_for('main.corpus_download', corpus_file_id=file.id, corpus_id=corpus.id) }}"><i class="material-icons">file_download</i></a>
 | 
			
		||||
              <a class="waves-effect waves-light btn-small red"><i class="material-icons">delete</i></a>
 | 
			
		||||
            </td>
 | 
			
		||||
          </tr>
 | 
			
		||||
@@ -87,7 +87,7 @@
 | 
			
		||||
    <form method="POST" enctype="multipart/form-data">
 | 
			
		||||
      {{ add_corpus_file_form.hidden_tag() }}
 | 
			
		||||
      <div class="row">
 | 
			
		||||
        <div class="col s12 m8">
 | 
			
		||||
        <div class="col s12 m4">
 | 
			
		||||
          <div class="input-field">
 | 
			
		||||
            <i class="material-icons prefix">person</i>
 | 
			
		||||
            {{ add_corpus_file_form.author(data_length='64') }}
 | 
			
		||||
@@ -107,16 +107,26 @@
 | 
			
		||||
            {% endfor %}
 | 
			
		||||
          </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="file-field input-field">
 | 
			
		||||
            <div class="btn">
 | 
			
		||||
              <span>{{ add_corpus_file_form.files.label.text }}</span>
 | 
			
		||||
              {{ add_corpus_file_form.files(accept='.vrt') }}
 | 
			
		||||
              <span>{{ add_corpus_file_form.file.label.text }}</span>
 | 
			
		||||
              {{ add_corpus_file_form.file(accept='.vrt') }}
 | 
			
		||||
            </div>
 | 
			
		||||
            <div class="file-path-wrapper">
 | 
			
		||||
              <input class="file-path validate" type="text">
 | 
			
		||||
            </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>
 | 
			
		||||
            {% endfor %}
 | 
			
		||||
          </div>
 | 
			
		||||
 
 | 
			
		||||
@@ -63,5 +63,7 @@ def background_delete_corpus(app, corpus_id):
 | 
			
		||||
        logger.warning('Called by delete_thread.')
 | 
			
		||||
        logger.warning('Corpus id is: {}.'.format(corpus_id))
 | 
			
		||||
        corpus = Corpus.query.filter_by(id=corpus_id).first()
 | 
			
		||||
        logger.warning('Corpus object is: {}'.format(corpus))
 | 
			
		||||
        for corpus_file in corpus.files:
 | 
			
		||||
            db.session.delete(corpus_file)
 | 
			
		||||
            logger.warning('Corpus object is: {}'.format(corpus))
 | 
			
		||||
        corpus.delete_corpus()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user