mirror of
				https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
				synced 2025-11-04 04:12:45 +00:00 
			
		
		
		
	Add create corpus form to dashboard.
This commit is contained in:
		@@ -1,10 +1,12 @@
 | 
			
		||||
from flask import current_app, flash, redirect, render_template, request, url_for
 | 
			
		||||
from datetime import datetime
 | 
			
		||||
from flask import current_app, flash, redirect, render_template, url_for
 | 
			
		||||
from flask_login import current_user, login_required
 | 
			
		||||
from ..models import User
 | 
			
		||||
from ..tables import AdminUserTable, AdminUserItem
 | 
			
		||||
from . import main
 | 
			
		||||
from .forms import CreateCorpusForm
 | 
			
		||||
from ..decorators import admin_required
 | 
			
		||||
import hashlib
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -15,25 +17,23 @@ def dashboard():
 | 
			
		||||
 | 
			
		||||
    if create_corpus_form.validate_on_submit():
 | 
			
		||||
        app = current_app._get_current_object()
 | 
			
		||||
        files = request.FILES
 | 
			
		||||
        print(files)
 | 
			
		||||
        corpus = {
 | 
			
		||||
            'description': create_corpus_form.description.data,
 | 
			
		||||
            'files': [],
 | 
			
		||||
            'owner': current_user.id,
 | 
			
		||||
            'title': create_corpus_form.title.data
 | 
			
		||||
        }
 | 
			
		||||
        corpus_dir = os.path.join(
 | 
			
		||||
            app.config['OPAQUE_FILES'],
 | 
			
		||||
            'corpora',
 | 
			
		||||
            corpus['title']
 | 
			
		||||
        )
 | 
			
		||||
        id = hashlib.md5(
 | 
			
		||||
            (current_user.username + '_' + datetime.now().isoformat()).encode()
 | 
			
		||||
        ).hexdigest()
 | 
			
		||||
        corpus = {'description': create_corpus_form.description.data,
 | 
			
		||||
                  'id': id,
 | 
			
		||||
                  'creator': current_user.id,
 | 
			
		||||
                  'title': create_corpus_form.title.data
 | 
			
		||||
                  }
 | 
			
		||||
        dir = os.path.join(app.config['OPAQUE_FILES'], 'corpora', id)
 | 
			
		||||
 | 
			
		||||
        try:
 | 
			
		||||
            os.mkdir(corpus_dir)
 | 
			
		||||
        except FileExistsError:
 | 
			
		||||
            flash('FileExistsError')
 | 
			
		||||
            os.makedirs(dir)
 | 
			
		||||
        except OSError:
 | 
			
		||||
            flash('OSError!')
 | 
			
		||||
        else:
 | 
			
		||||
            for file in create_corpus_form.files.data:
 | 
			
		||||
                file.save(os.path.join(dir, file.filename))
 | 
			
		||||
            flash('Corpus created!')
 | 
			
		||||
        return redirect(url_for('main.dashboard'))
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user