mirror of
				https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
				synced 2025-11-03 20:02:47 +00:00 
			
		
		
		
	restructure corpus delete method
This commit is contained in:
		@@ -57,6 +57,18 @@ def corpus(corpus_id):
 | 
			
		||||
                           title='Corpus')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@main.route('/corpora/<int:corpus_id>/delete')
 | 
			
		||||
@login_required
 | 
			
		||||
def delete_corpus(corpus_id):
 | 
			
		||||
    delete_thread = threading.Thread(
 | 
			
		||||
        target=background_delete_corpus,
 | 
			
		||||
        args=(current_app._get_current_object(), corpus_id)
 | 
			
		||||
    )
 | 
			
		||||
    delete_thread.start()
 | 
			
		||||
    flash('Corpus has been deleted!')
 | 
			
		||||
    return redirect(url_for('main.dashboard'))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@main.route('/corpora/<int:corpus_id>/download')
 | 
			
		||||
@login_required
 | 
			
		||||
def corpus_download(corpus_id):
 | 
			
		||||
@@ -100,27 +112,7 @@ def dashboard():
 | 
			
		||||
                        title=create_corpus_form.title.data)
 | 
			
		||||
        db.session.add(corpus)
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        dir = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'],
 | 
			
		||||
                           str(corpus.user_id),
 | 
			
		||||
                           'corpora',
 | 
			
		||||
                           str(corpus.id))
 | 
			
		||||
        try:
 | 
			
		||||
            os.makedirs(dir)
 | 
			
		||||
        except OSError:
 | 
			
		||||
            flash('OSError!')
 | 
			
		||||
        else:
 | 
			
		||||
            for file in create_corpus_form.files.data:
 | 
			
		||||
                filename = secure_filename(file.filename)
 | 
			
		||||
                file.save(os.path.join(dir, filename))
 | 
			
		||||
                file_dir = os.path.join(str(corpus.user_id),
 | 
			
		||||
                                        'corpora',
 | 
			
		||||
                                        str(corpus.id))
 | 
			
		||||
                corpus_file = CorpusFile(filename=filename,
 | 
			
		||||
                                         corpus_id=corpus.id,
 | 
			
		||||
                                         dir=file_dir)
 | 
			
		||||
                db.session.add(corpus_file)
 | 
			
		||||
            db.session.commit()
 | 
			
		||||
            flash('Corpus created!')
 | 
			
		||||
        flash('Corpus created!')
 | 
			
		||||
        return redirect(url_for('main.dashboard'))
 | 
			
		||||
    return render_template('main/dashboard.html.j2',
 | 
			
		||||
                           create_corpus_form=create_corpus_form,
 | 
			
		||||
@@ -169,15 +161,3 @@ def delete_job(job_id):
 | 
			
		||||
    delete_thread.start()
 | 
			
		||||
    flash('Job has been deleted!')
 | 
			
		||||
    return redirect(url_for('main.dashboard'))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@main.route('/corpora/<int:corpus_id>/delete')
 | 
			
		||||
@login_required
 | 
			
		||||
def delete_corpus(corpus_id):
 | 
			
		||||
    delete_thread = threading.Thread(
 | 
			
		||||
        target=background_delete_corpus,
 | 
			
		||||
        args=(current_app._get_current_object(), corpus_id)
 | 
			
		||||
    )
 | 
			
		||||
    delete_thread.start()
 | 
			
		||||
    flash('Corpus has been deleted!')
 | 
			
		||||
    return redirect(url_for('main.dashboard'))
 | 
			
		||||
 
 | 
			
		||||
@@ -410,6 +410,19 @@ class CorpusFile(db.Model):
 | 
			
		||||
    title = db.Column(db.String(64))
 | 
			
		||||
    corpus_id = db.Column(db.Integer, db.ForeignKey('corpora.id'))
 | 
			
		||||
 | 
			
		||||
    def delete(self):
 | 
			
		||||
        logger = logging.getLogger(__name__)
 | 
			
		||||
        path = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'],
 | 
			
		||||
                            self.dir,
 | 
			
		||||
                            self.filename)
 | 
			
		||||
        try:
 | 
			
		||||
            os.remove(path)
 | 
			
		||||
        except:
 | 
			
		||||
            logger.warning('[ERROR] CorpusFile.delete')
 | 
			
		||||
            return
 | 
			
		||||
        db.session.delete(self)
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Corpus(db.Model):
 | 
			
		||||
    """
 | 
			
		||||
@@ -444,17 +457,19 @@ class Corpus(db.Model):
 | 
			
		||||
                'title': self.title,
 | 
			
		||||
                'user_id': self.user_id}
 | 
			
		||||
 | 
			
		||||
    def delete_corpus(self):
 | 
			
		||||
    def delete(self):
 | 
			
		||||
        logger = logging.getLogger(__name__)
 | 
			
		||||
        delete_path = os.path.join('/mnt/opaque/', str(self.user_id), 'corpora',
 | 
			
		||||
                                   str(self.id))
 | 
			
		||||
        logger.warning('Delete path is: {}'.format(delete_path))
 | 
			
		||||
        while os.path.exists(delete_path):
 | 
			
		||||
            try:
 | 
			
		||||
                shutil.rmtree(delete_path, ignore_errors=True)
 | 
			
		||||
                logger.warning('Path does still exist.')
 | 
			
		||||
            except OSError:
 | 
			
		||||
                pass
 | 
			
		||||
        for corpus_file in self.files:
 | 
			
		||||
            corpus_file.delete()
 | 
			
		||||
        path = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'],
 | 
			
		||||
                            self.user_id,
 | 
			
		||||
                            'corpora',
 | 
			
		||||
                            self.id)
 | 
			
		||||
        try:
 | 
			
		||||
            shutil.rmtree(path)
 | 
			
		||||
        except:
 | 
			
		||||
            logger.warning('[ERROR] Corpus.delete')
 | 
			
		||||
            return
 | 
			
		||||
        db.session.delete(self)
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -63,7 +63,4 @@ 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()
 | 
			
		||||
        for corpus_file in corpus.files:
 | 
			
		||||
            db.session.delete(corpus_file)
 | 
			
		||||
            logger.warning('Corpus object is: {}'.format(corpus))
 | 
			
		||||
        corpus.delete_corpus()
 | 
			
		||||
        corpus.delete()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user