mirror of
				https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
				synced 2025-11-03 20:02:47 +00:00 
			
		
		
		
	Implement flask-breadcrumbs everywhere
This commit is contained in:
		@@ -2,4 +2,6 @@ from flask import Blueprint
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
bp = Blueprint('contributions', __name__)
 | 
			
		||||
from . import json_routes, routes
 | 
			
		||||
from . import routes
 | 
			
		||||
from . import spacy_nlp_pipeline_models
 | 
			
		||||
from . import tesseract_ocr_pipeline_models
 | 
			
		||||
 
 | 
			
		||||
@@ -1,14 +1,11 @@
 | 
			
		||||
from flask_wtf import FlaskForm
 | 
			
		||||
from flask_wtf.file import FileField, FileRequired
 | 
			
		||||
from wtforms import (
 | 
			
		||||
    StringField,
 | 
			
		||||
    SubmitField,
 | 
			
		||||
    SelectMultipleField,
 | 
			
		||||
    IntegerField,
 | 
			
		||||
    ValidationError
 | 
			
		||||
    IntegerField
 | 
			
		||||
)
 | 
			
		||||
from wtforms.validators import InputRequired, Length
 | 
			
		||||
from app.services import SERVICES
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ContributionBaseForm(FlaskForm):
 | 
			
		||||
@@ -48,79 +45,3 @@ class ContributionBaseForm(FlaskForm):
 | 
			
		||||
 | 
			
		||||
class EditContributionBaseForm(ContributionBaseForm):
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
##############################################################################
 | 
			
		||||
# /spacy-nlp-pipeline-models                                                 #
 | 
			
		||||
##############################################################################
 | 
			
		||||
class CreateSpaCyNLPPipelineModelForm(ContributionBaseForm):
 | 
			
		||||
    spacy_model_file = FileField(
 | 
			
		||||
        'File',
 | 
			
		||||
        validators=[FileRequired()]
 | 
			
		||||
    )
 | 
			
		||||
    pipeline_name = StringField(
 | 
			
		||||
        'Pipeline name',
 | 
			
		||||
        validators=[InputRequired(), Length(max=64)]
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    def validate_spacy_model_file(self, field):
 | 
			
		||||
        if not field.data.filename.lower().endswith('.tar.gz'):
 | 
			
		||||
            raise ValidationError('.tar.gz files only!')
 | 
			
		||||
 | 
			
		||||
    def __init__(self, *args, **kwargs):
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
        service_manifest = SERVICES['spacy-nlp-pipeline']
 | 
			
		||||
        self.compatible_service_versions.choices = [('', 'Choose your option')]
 | 
			
		||||
        self.compatible_service_versions.choices += [
 | 
			
		||||
            (x, x) for x in service_manifest['versions'].keys()
 | 
			
		||||
        ]
 | 
			
		||||
        self.compatible_service_versions.default = ''
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class EditSpaCyNLPPipelineModelForm(EditContributionBaseForm):
 | 
			
		||||
    pipeline_name = StringField(
 | 
			
		||||
        'Pipeline name',
 | 
			
		||||
        validators=[InputRequired(), Length(max=64)]
 | 
			
		||||
    )
 | 
			
		||||
    def __init__(self, *args, **kwargs):
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
        service_manifest = SERVICES['spacy-nlp-pipeline']
 | 
			
		||||
        self.compatible_service_versions.choices = [('', 'Choose your option')]
 | 
			
		||||
        self.compatible_service_versions.choices += [
 | 
			
		||||
            (x, x) for x in service_manifest['versions'].keys()
 | 
			
		||||
        ]
 | 
			
		||||
        self.compatible_service_versions.default = ''
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
##############################################################################
 | 
			
		||||
# /tesseract-ocr-pipeline-models                                             #
 | 
			
		||||
##############################################################################
 | 
			
		||||
class CreateTesseractOCRPipelineModelForm(ContributionBaseForm):
 | 
			
		||||
    tesseract_model_file = FileField(
 | 
			
		||||
        'File',
 | 
			
		||||
        validators=[FileRequired()]
 | 
			
		||||
    )
 | 
			
		||||
    
 | 
			
		||||
    def validate_tesseract_model_file(self, field):
 | 
			
		||||
        if not field.data.filename.lower().endswith('.traineddata'):
 | 
			
		||||
            raise ValidationError('traineddata files only!')
 | 
			
		||||
 | 
			
		||||
    def __init__(self, *args, **kwargs):
 | 
			
		||||
        service_manifest = SERVICES['tesseract-ocr-pipeline']
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
        self.compatible_service_versions.choices = [('', 'Choose your option')]
 | 
			
		||||
        self.compatible_service_versions.choices += [
 | 
			
		||||
            (x, x) for x in service_manifest['versions'].keys()
 | 
			
		||||
        ]
 | 
			
		||||
        self.compatible_service_versions.default = ''
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class EditTesseractOCRPipelineModelForm(EditContributionBaseForm):
 | 
			
		||||
    def __init__(self, *args, **kwargs):
 | 
			
		||||
        service_manifest = SERVICES['tesseract-ocr-pipeline']
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
        self.compatible_service_versions.choices = [('', 'Choose your option')]
 | 
			
		||||
        self.compatible_service_versions.choices += [
 | 
			
		||||
            (x, x) for x in service_manifest['versions'].keys()
 | 
			
		||||
        ]
 | 
			
		||||
        self.compatible_service_versions.default = ''
 | 
			
		||||
 
 | 
			
		||||
@@ -1,107 +0,0 @@
 | 
			
		||||
from flask import abort, current_app, request
 | 
			
		||||
from flask_login import login_required, current_user
 | 
			
		||||
from threading import Thread
 | 
			
		||||
from app import db
 | 
			
		||||
from app.decorators import content_negotiation, permission_required
 | 
			
		||||
from app.models import SpaCyNLPPipelineModel, TesseractOCRPipelineModel
 | 
			
		||||
from . import bp
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
##############################################################################
 | 
			
		||||
# /spacy-nlp-pipeline-models                                                 #
 | 
			
		||||
##############################################################################
 | 
			
		||||
@bp.route('/spacy-nlp-pipeline-models<hashid:spacy_nlp_pipeline_model_id>', methods=['DELETE'])
 | 
			
		||||
@login_required
 | 
			
		||||
@content_negotiation(produces='application/json')
 | 
			
		||||
def delete_spacy_model(spacy_nlp_pipeline_model_id):
 | 
			
		||||
    def _delete_spacy_model(app, spacy_nlp_pipeline_model_id):
 | 
			
		||||
        with app.app_context():
 | 
			
		||||
            snpm = SpaCyNLPPipelineModel.query.get(spacy_nlp_pipeline_model_id)
 | 
			
		||||
            snpm.delete()
 | 
			
		||||
            db.session.commit()
 | 
			
		||||
    
 | 
			
		||||
    snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
 | 
			
		||||
    if not (snpm.user == current_user or current_user.is_administrator()):
 | 
			
		||||
        abort(403)
 | 
			
		||||
    thread = Thread(
 | 
			
		||||
        target=_delete_spacy_model,
 | 
			
		||||
        args=(current_app._get_current_object(), snpm.id)
 | 
			
		||||
    )
 | 
			
		||||
    thread.start()
 | 
			
		||||
    resonse_data = {
 | 
			
		||||
        'message': \
 | 
			
		||||
            f'SpaCy NLP Pipeline Model "{snpm.title}" marked for deletion'
 | 
			
		||||
    }
 | 
			
		||||
    return resonse_data, 202
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/spacy-nlp-pipeline-models<hashid:spacy_nlp_pipeline_model_id>/is_public', methods=['PUT'])
 | 
			
		||||
@login_required
 | 
			
		||||
@permission_required('CONTRIBUTE')
 | 
			
		||||
@content_negotiation(consumes='application/json', produces='application/json')
 | 
			
		||||
def update_spacy_nlp_pipeline_model_is_public(spacy_nlp_pipeline_model_id):
 | 
			
		||||
    is_public = request.json
 | 
			
		||||
    if not isinstance(is_public, bool):
 | 
			
		||||
        abort(400)
 | 
			
		||||
    snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
 | 
			
		||||
    if not (snpm.user == current_user or current_user.is_administrator()):
 | 
			
		||||
        abort(403)
 | 
			
		||||
    snpm.is_public = is_public
 | 
			
		||||
    db.session.commit()
 | 
			
		||||
    response_data = {
 | 
			
		||||
        'message': (
 | 
			
		||||
            f'SpaCy NLP Pipeline Model "{snpm.title}"'
 | 
			
		||||
            f' is now {"public" if is_public else "private"}'
 | 
			
		||||
        )
 | 
			
		||||
    }
 | 
			
		||||
    return response_data, 200
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
##############################################################################
 | 
			
		||||
# /tesseract-ocr-pipeline-models                                             #
 | 
			
		||||
##############################################################################
 | 
			
		||||
@bp.route('/tesseract-ocr-pipeline-models/<hashid:tesseract_ocr_pipeline_model_id>', methods=['DELETE'])
 | 
			
		||||
@login_required
 | 
			
		||||
@content_negotiation(produces='application/json')
 | 
			
		||||
def delete_tesseract_model(tesseract_ocr_pipeline_model_id):
 | 
			
		||||
    def _delete_tesseract_ocr_pipeline_model(app, tesseract_ocr_pipeline_model_id):
 | 
			
		||||
        with app.app_context():
 | 
			
		||||
            topm = TesseractOCRPipelineModel.query.get(tesseract_ocr_pipeline_model_id)
 | 
			
		||||
            topm.delete()
 | 
			
		||||
            db.session.commit()
 | 
			
		||||
 | 
			
		||||
    topm = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
 | 
			
		||||
    if not (topm.user == current_user or current_user.is_administrator()):
 | 
			
		||||
        abort(403)
 | 
			
		||||
    thread = Thread(
 | 
			
		||||
        target=_delete_tesseract_ocr_pipeline_model,
 | 
			
		||||
        args=(current_app._get_current_object(), topm.id)
 | 
			
		||||
    )
 | 
			
		||||
    thread.start()
 | 
			
		||||
    response_data = {
 | 
			
		||||
        'message': \
 | 
			
		||||
            f'Tesseract OCR Pipeline Model "{topm.title}" marked for deletion'
 | 
			
		||||
    }
 | 
			
		||||
    return response_data, 202
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/tesseract-ocr-pipeline-models/<hashid:tesseract_ocr_pipeline_model_id>/is_public', methods=['PUT'])
 | 
			
		||||
@login_required
 | 
			
		||||
@permission_required('CONTRIBUTE')
 | 
			
		||||
@content_negotiation(consumes='application/json', produces='application/json')
 | 
			
		||||
def update_tesseract_ocr_pipeline_model_is_public(tesseract_ocr_pipeline_model_id):
 | 
			
		||||
    is_public = request.json
 | 
			
		||||
    if not isinstance(is_public, bool):
 | 
			
		||||
        abort(400)
 | 
			
		||||
    topm = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
 | 
			
		||||
    if not (topm.user == current_user or current_user.is_administrator()):
 | 
			
		||||
        abort(403)
 | 
			
		||||
    topm.is_public = is_public
 | 
			
		||||
    db.session.commit()
 | 
			
		||||
    response_data = {
 | 
			
		||||
        'message': (
 | 
			
		||||
            f'Tesseract OCR Pipeline Model "{topm.title}"'
 | 
			
		||||
            f' is now {"public" if is_public else "private"}'
 | 
			
		||||
        )
 | 
			
		||||
    }
 | 
			
		||||
    return response_data, 200
 | 
			
		||||
@@ -1,189 +1,14 @@
 | 
			
		||||
from flask import abort, flash, redirect, render_template, request, url_for
 | 
			
		||||
from flask import render_template
 | 
			
		||||
from flask_breadcrumbs import register_breadcrumb
 | 
			
		||||
from flask_login import current_user, login_required
 | 
			
		||||
from app import db
 | 
			
		||||
from app.models import SpaCyNLPPipelineModel, TesseractOCRPipelineModel
 | 
			
		||||
from flask_login import login_required
 | 
			
		||||
from . import bp
 | 
			
		||||
from .forms import (
 | 
			
		||||
    CreateSpaCyNLPPipelineModelForm,
 | 
			
		||||
    EditSpaCyNLPPipelineModelForm,
 | 
			
		||||
    CreateTesseractOCRPipelineModelForm,
 | 
			
		||||
    EditTesseractOCRPipelineModelForm
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('')
 | 
			
		||||
@register_breadcrumb(bp, '.', 'Contributions')
 | 
			
		||||
@register_breadcrumb(bp, '.', '<i class="material-icons left">new_label</i>Contributions')
 | 
			
		||||
@login_required
 | 
			
		||||
def contributions():
 | 
			
		||||
    return render_template(
 | 
			
		||||
        'contributions/contributions.html.j2',
 | 
			
		||||
        title='Contributions'
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
##############################################################################
 | 
			
		||||
# /spacy-nlp-pipeline-models                                                 #
 | 
			
		||||
##############################################################################
 | 
			
		||||
@bp.route('/spacy-nlp-pipeline-models')
 | 
			
		||||
@register_breadcrumb(bp, '.spacy_nlp_pipeline_models', 'SpaCy NLP Pipeline Models')
 | 
			
		||||
@login_required
 | 
			
		||||
def spacy_nlp_pipeline_models():
 | 
			
		||||
    return render_template(
 | 
			
		||||
        'contributions/spacy_nlp_pipeline_models.html.j2',
 | 
			
		||||
        title='SpaCy NLP Pipeline Models'
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/spacy-nlp-pipeline-models/create', methods=['GET', 'POST'])
 | 
			
		||||
@register_breadcrumb(bp, '.spacy_nlp_pipeline_models.create', 'Create')
 | 
			
		||||
@login_required
 | 
			
		||||
def create_spacy_nlp_pipeline_model():
 | 
			
		||||
    form_prefix = 'create-spacy-nlp-pipeline-model-form'
 | 
			
		||||
    form = CreateSpaCyNLPPipelineModelForm(prefix=form_prefix)
 | 
			
		||||
    if form.is_submitted():
 | 
			
		||||
        if not form.validate():
 | 
			
		||||
            return {'errors': form.errors}, 400
 | 
			
		||||
        try:
 | 
			
		||||
            snpm = SpaCyNLPPipelineModel.create(
 | 
			
		||||
                form.spacy_model_file.data,
 | 
			
		||||
                compatible_service_versions=form.compatible_service_versions.data,
 | 
			
		||||
                description=form.description.data,
 | 
			
		||||
                pipeline_name=form.pipeline_name.data,
 | 
			
		||||
                publisher=form.publisher.data,
 | 
			
		||||
                publisher_url=form.publisher_url.data,
 | 
			
		||||
                publishing_url=form.publishing_url.data,
 | 
			
		||||
                publishing_year=form.publishing_year.data,
 | 
			
		||||
                is_public=False,
 | 
			
		||||
                title=form.title.data,
 | 
			
		||||
                version=form.version.data,
 | 
			
		||||
                user=current_user
 | 
			
		||||
            )
 | 
			
		||||
        except OSError:
 | 
			
		||||
            abort(500)
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        flash(f'SpaCy NLP Pipeline model "{snpm.title}" created')
 | 
			
		||||
        return {}, 201, {'Location': url_for('.spacy_nlp_pipeline_models')}
 | 
			
		||||
    return render_template(
 | 
			
		||||
        'contributions/create_spacy_nlp_pipeline_model.html.j2',
 | 
			
		||||
        form=form,
 | 
			
		||||
        title='Create SpaCy NLP Pipeline Model'
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def spacy_nlp_pipeline_model_dlc(*args, **kwargs):
 | 
			
		||||
    snpm_id = request.view_args['spacy_nlp_pipeline_model_id']
 | 
			
		||||
    snpm = SpaCyNLPPipelineModel.query.get(snpm_id)
 | 
			
		||||
    return [
 | 
			
		||||
        {
 | 
			
		||||
            'text': f'{snpm.title} {snpm.version}',
 | 
			
		||||
            'url': url_for('.spacy_nlp_pipeline_model', spacy_nlp_pipeline_model_id=snpm_id)
 | 
			
		||||
        }
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/spacy-nlp-pipeline-models/<hashid:spacy_nlp_pipeline_model_id>', methods=['GET', 'POST'])
 | 
			
		||||
@register_breadcrumb(bp, '.spacy_nlp_pipeline_models.entity', '', dynamic_list_constructor=spacy_nlp_pipeline_model_dlc)
 | 
			
		||||
@login_required
 | 
			
		||||
def spacy_nlp_pipeline_model(spacy_nlp_pipeline_model_id):
 | 
			
		||||
    snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
 | 
			
		||||
    form_prefix = 'edit-spacy-nlp-pipeline-model-form'
 | 
			
		||||
    form = EditSpaCyNLPPipelineModelForm(
 | 
			
		||||
        data=snpm.to_json_serializeable(),
 | 
			
		||||
        prefix=form_prefix
 | 
			
		||||
    )
 | 
			
		||||
    if form.validate_on_submit():
 | 
			
		||||
        form.populate_obj(snpm)
 | 
			
		||||
        if db.session.is_modified(snpm):
 | 
			
		||||
            flash(f'SpaCy NLP Pipeline model "{snpm.title}" updated')
 | 
			
		||||
            db.session.commit()
 | 
			
		||||
        return redirect(url_for('.spacy_nlp_pipeline_models'))
 | 
			
		||||
    return render_template(
 | 
			
		||||
        'contributions/spacy_nlp_pipeline_model.html.j2',
 | 
			
		||||
        form=form,
 | 
			
		||||
        spacy_nlp_pipeline_model=snpm,
 | 
			
		||||
        title=f'{snpm.title} {snpm.version}'
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
##############################################################################
 | 
			
		||||
# /tesseract-ocr-pipeline-models                                             #
 | 
			
		||||
##############################################################################
 | 
			
		||||
@bp.route('/tesseract-ocr-pipeline-models')
 | 
			
		||||
@register_breadcrumb(bp, '.tesseract_ocr_pipeline_models', 'Tesseract OCR Pipeline Models')
 | 
			
		||||
@login_required
 | 
			
		||||
def tesseract_ocr_pipeline_models():
 | 
			
		||||
    return render_template(
 | 
			
		||||
        'contributions/tesseract_ocr_pipeline_models.html.j2',
 | 
			
		||||
        title='Tesseract OCR Pipeline Models'
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/tesseract-ocr-pipeline-models/create', methods=['GET', 'POST'])
 | 
			
		||||
@register_breadcrumb(bp, '.tesseract_ocr_pipeline_models.create', 'Create')
 | 
			
		||||
@login_required
 | 
			
		||||
def create_tesseract_ocr_pipeline_model():
 | 
			
		||||
    form_prefix = 'create-tesseract-ocr-pipeline-model-form'
 | 
			
		||||
    form = CreateTesseractOCRPipelineModelForm(prefix=form_prefix)
 | 
			
		||||
    if form.is_submitted():
 | 
			
		||||
        if not form.validate():
 | 
			
		||||
            return {'errors': form.errors}, 400
 | 
			
		||||
        try:
 | 
			
		||||
            topm = TesseractOCRPipelineModel.create(
 | 
			
		||||
                form.tesseract_model_file.data,
 | 
			
		||||
                compatible_service_versions=form.compatible_service_versions.data,
 | 
			
		||||
                description=form.description.data,
 | 
			
		||||
                publisher=form.publisher.data,
 | 
			
		||||
                publisher_url=form.publisher_url.data,
 | 
			
		||||
                publishing_url=form.publishing_url.data,
 | 
			
		||||
                publishing_year=form.publishing_year.data,
 | 
			
		||||
                is_public=False,
 | 
			
		||||
                title=form.title.data,
 | 
			
		||||
                version=form.version.data,
 | 
			
		||||
                user=current_user
 | 
			
		||||
            )
 | 
			
		||||
        except OSError:
 | 
			
		||||
            abort(500)
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        flash(f'Tesseract OCR Pipeline model "{topm.title}" created')
 | 
			
		||||
        return {}, 201, {'Location': url_for('.tesseract_ocr_pipeline_models')}
 | 
			
		||||
    return render_template(
 | 
			
		||||
        'contributions/create_tesseract_ocr_pipeline_model.html.j2',
 | 
			
		||||
        form=form,
 | 
			
		||||
        title='Create Tesseract OCR Pipeline Model'
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def tesseract_ocr_pipeline_model_dlc(*args, **kwargs):
 | 
			
		||||
    topm_id = request.view_args['tesseract_ocr_pipeline_model_id']
 | 
			
		||||
    topm = TesseractOCRPipelineModel.query.get(topm_id)
 | 
			
		||||
    return [
 | 
			
		||||
        {
 | 
			
		||||
            'text': f'{topm.title} {topm.version}',
 | 
			
		||||
            'url': url_for('.tesseract_ocr_pipeline_model', tesseract_ocr_pipeline_model_id=topm_id)
 | 
			
		||||
        }
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/tesseract-ocr-pipeline-models/<hashid:tesseract_ocr_pipeline_model_id>', methods=['GET', 'POST'])
 | 
			
		||||
@register_breadcrumb(bp, '.tesseract_ocr_pipeline_models.entity', '', dynamic_list_constructor=tesseract_ocr_pipeline_model_dlc)
 | 
			
		||||
@login_required
 | 
			
		||||
def tesseract_ocr_pipeline_model(tesseract_ocr_pipeline_model_id):
 | 
			
		||||
    topm = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
 | 
			
		||||
    form_prefix = 'edit-tesseract-ocr-pipeline-model-form'
 | 
			
		||||
    form = EditTesseractOCRPipelineModelForm(
 | 
			
		||||
        data=topm.to_json_serializeable(),
 | 
			
		||||
        prefix=form_prefix
 | 
			
		||||
    )
 | 
			
		||||
    if form.validate_on_submit():
 | 
			
		||||
        form.populate_obj(topm)
 | 
			
		||||
        if db.session.is_modified(topm):
 | 
			
		||||
            flash(f'Tesseract OCR Pipeline model "{topm.title}" updated')
 | 
			
		||||
            db.session.commit()
 | 
			
		||||
        return redirect(url_for('.tesseract_ocr_pipeline_models'))
 | 
			
		||||
    return render_template(
 | 
			
		||||
        'contributions/tesseract_ocr_pipeline_model.html.j2',
 | 
			
		||||
        form=form,
 | 
			
		||||
        tesseract_ocr_pipeline_model=topm,
 | 
			
		||||
        title=f'{topm.title} {topm.version}'
 | 
			
		||||
    )
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								app/contributions/spacy_nlp_pipeline_models/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								app/contributions/spacy_nlp_pipeline_models/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
from .. import bp
 | 
			
		||||
from . import json_routes, routes
 | 
			
		||||
							
								
								
									
										44
									
								
								app/contributions/spacy_nlp_pipeline_models/forms.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								app/contributions/spacy_nlp_pipeline_models/forms.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
			
		||||
from flask_wtf.file import FileField, FileRequired
 | 
			
		||||
from wtforms import StringField, ValidationError
 | 
			
		||||
from wtforms.validators import InputRequired, Length
 | 
			
		||||
from app.services import SERVICES
 | 
			
		||||
from ..forms import ContributionBaseForm, EditContributionBaseForm
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class CreateSpaCyNLPPipelineModelForm(ContributionBaseForm):
 | 
			
		||||
    spacy_model_file = FileField(
 | 
			
		||||
        'File',
 | 
			
		||||
        validators=[FileRequired()]
 | 
			
		||||
    )
 | 
			
		||||
    pipeline_name = StringField(
 | 
			
		||||
        'Pipeline name',
 | 
			
		||||
        validators=[InputRequired(), Length(max=64)]
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    def validate_spacy_model_file(self, field):
 | 
			
		||||
        if not field.data.filename.lower().endswith('.tar.gz'):
 | 
			
		||||
            raise ValidationError('.tar.gz files only!')
 | 
			
		||||
 | 
			
		||||
    def __init__(self, *args, **kwargs):
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
        service_manifest = SERVICES['spacy-nlp-pipeline']
 | 
			
		||||
        self.compatible_service_versions.choices = [('', 'Choose your option')]
 | 
			
		||||
        self.compatible_service_versions.choices += [
 | 
			
		||||
            (x, x) for x in service_manifest['versions'].keys()
 | 
			
		||||
        ]
 | 
			
		||||
        self.compatible_service_versions.default = ''
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class EditSpaCyNLPPipelineModelForm(EditContributionBaseForm):
 | 
			
		||||
    pipeline_name = StringField(
 | 
			
		||||
        'Pipeline name',
 | 
			
		||||
        validators=[InputRequired(), Length(max=64)]
 | 
			
		||||
    )
 | 
			
		||||
    def __init__(self, *args, **kwargs):
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
        service_manifest = SERVICES['spacy-nlp-pipeline']
 | 
			
		||||
        self.compatible_service_versions.choices = [('', 'Choose your option')]
 | 
			
		||||
        self.compatible_service_versions.choices += [
 | 
			
		||||
            (x, x) for x in service_manifest['versions'].keys()
 | 
			
		||||
        ]
 | 
			
		||||
        self.compatible_service_versions.default = ''
 | 
			
		||||
							
								
								
									
										54
									
								
								app/contributions/spacy_nlp_pipeline_models/json_routes.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								app/contributions/spacy_nlp_pipeline_models/json_routes.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,54 @@
 | 
			
		||||
from flask import abort, current_app, request
 | 
			
		||||
from flask_login import login_required, current_user
 | 
			
		||||
from threading import Thread
 | 
			
		||||
from app import db
 | 
			
		||||
from app.decorators import content_negotiation, permission_required
 | 
			
		||||
from app.models import SpaCyNLPPipelineModel
 | 
			
		||||
from .. import bp
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/spacy-nlp-pipeline-models<hashid:spacy_nlp_pipeline_model_id>', methods=['DELETE'])
 | 
			
		||||
@login_required
 | 
			
		||||
@content_negotiation(produces='application/json')
 | 
			
		||||
def delete_spacy_model(spacy_nlp_pipeline_model_id):
 | 
			
		||||
    def _delete_spacy_model(app, spacy_nlp_pipeline_model_id):
 | 
			
		||||
        with app.app_context():
 | 
			
		||||
            snpm = SpaCyNLPPipelineModel.query.get(spacy_nlp_pipeline_model_id)
 | 
			
		||||
            snpm.delete()
 | 
			
		||||
            db.session.commit()
 | 
			
		||||
    
 | 
			
		||||
    snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
 | 
			
		||||
    if not (snpm.user == current_user or current_user.is_administrator()):
 | 
			
		||||
        abort(403)
 | 
			
		||||
    thread = Thread(
 | 
			
		||||
        target=_delete_spacy_model,
 | 
			
		||||
        args=(current_app._get_current_object(), snpm.id)
 | 
			
		||||
    )
 | 
			
		||||
    thread.start()
 | 
			
		||||
    resonse_data = {
 | 
			
		||||
        'message': \
 | 
			
		||||
            f'SpaCy NLP Pipeline Model "{snpm.title}" marked for deletion'
 | 
			
		||||
    }
 | 
			
		||||
    return resonse_data, 202
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/spacy-nlp-pipeline-models<hashid:spacy_nlp_pipeline_model_id>/is_public', methods=['PUT'])
 | 
			
		||||
@login_required
 | 
			
		||||
@permission_required('CONTRIBUTE')
 | 
			
		||||
@content_negotiation(consumes='application/json', produces='application/json')
 | 
			
		||||
def update_spacy_nlp_pipeline_model_is_public(spacy_nlp_pipeline_model_id):
 | 
			
		||||
    is_public = request.json
 | 
			
		||||
    if not isinstance(is_public, bool):
 | 
			
		||||
        abort(400)
 | 
			
		||||
    snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
 | 
			
		||||
    if not (snpm.user == current_user or current_user.is_administrator()):
 | 
			
		||||
        abort(403)
 | 
			
		||||
    snpm.is_public = is_public
 | 
			
		||||
    db.session.commit()
 | 
			
		||||
    response_data = {
 | 
			
		||||
        'message': (
 | 
			
		||||
            f'SpaCy NLP Pipeline Model "{snpm.title}"'
 | 
			
		||||
            f' is now {"public" if is_public else "private"}'
 | 
			
		||||
        )
 | 
			
		||||
    }
 | 
			
		||||
    return response_data, 200
 | 
			
		||||
							
								
								
									
										83
									
								
								app/contributions/spacy_nlp_pipeline_models/routes.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								app/contributions/spacy_nlp_pipeline_models/routes.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,83 @@
 | 
			
		||||
from flask import abort, flash, redirect, render_template, url_for
 | 
			
		||||
from flask_breadcrumbs import register_breadcrumb
 | 
			
		||||
from flask_login import current_user, login_required
 | 
			
		||||
from app import db
 | 
			
		||||
from app.models import SpaCyNLPPipelineModel
 | 
			
		||||
from . import bp
 | 
			
		||||
from .forms import (
 | 
			
		||||
    CreateSpaCyNLPPipelineModelForm,
 | 
			
		||||
    EditSpaCyNLPPipelineModelForm
 | 
			
		||||
)
 | 
			
		||||
from .utils import (
 | 
			
		||||
    spacy_nlp_pipeline_model_dlc as spacy_nlp_pipeline_model_dlc
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/spacy-nlp-pipeline-models')
 | 
			
		||||
@register_breadcrumb(bp, '.spacy_nlp_pipeline_models', 'SpaCy NLP Pipeline Models')
 | 
			
		||||
@login_required
 | 
			
		||||
def spacy_nlp_pipeline_models():
 | 
			
		||||
    return render_template(
 | 
			
		||||
        'contributions/spacy_nlp_pipeline_models/spacy_nlp_pipeline_models.html.j2',
 | 
			
		||||
        title='SpaCy NLP Pipeline Models'
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/spacy-nlp-pipeline-models/create', methods=['GET', 'POST'])
 | 
			
		||||
@register_breadcrumb(bp, '.spacy_nlp_pipeline_models.create', 'Create')
 | 
			
		||||
@login_required
 | 
			
		||||
def create_spacy_nlp_pipeline_model():
 | 
			
		||||
    form_prefix = 'create-spacy-nlp-pipeline-model-form'
 | 
			
		||||
    form = CreateSpaCyNLPPipelineModelForm(prefix=form_prefix)
 | 
			
		||||
    if form.is_submitted():
 | 
			
		||||
        if not form.validate():
 | 
			
		||||
            return {'errors': form.errors}, 400
 | 
			
		||||
        try:
 | 
			
		||||
            snpm = SpaCyNLPPipelineModel.create(
 | 
			
		||||
                form.spacy_model_file.data,
 | 
			
		||||
                compatible_service_versions=form.compatible_service_versions.data,
 | 
			
		||||
                description=form.description.data,
 | 
			
		||||
                pipeline_name=form.pipeline_name.data,
 | 
			
		||||
                publisher=form.publisher.data,
 | 
			
		||||
                publisher_url=form.publisher_url.data,
 | 
			
		||||
                publishing_url=form.publishing_url.data,
 | 
			
		||||
                publishing_year=form.publishing_year.data,
 | 
			
		||||
                is_public=False,
 | 
			
		||||
                title=form.title.data,
 | 
			
		||||
                version=form.version.data,
 | 
			
		||||
                user=current_user
 | 
			
		||||
            )
 | 
			
		||||
        except OSError:
 | 
			
		||||
            abort(500)
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        flash(f'SpaCy NLP Pipeline model "{snpm.title}" created')
 | 
			
		||||
        return {}, 201, {'Location': url_for('.spacy_nlp_pipeline_models')}
 | 
			
		||||
    return render_template(
 | 
			
		||||
        'contributions/spacy_nlp_pipeline_models/create_spacy_nlp_pipeline_model.html.j2',
 | 
			
		||||
        form=form,
 | 
			
		||||
        title='Create SpaCy NLP Pipeline Model'
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/spacy-nlp-pipeline-models/<hashid:spacy_nlp_pipeline_model_id>', methods=['GET', 'POST'])
 | 
			
		||||
@register_breadcrumb(bp, '.spacy_nlp_pipeline_models.entity', '', dynamic_list_constructor=spacy_nlp_pipeline_model_dlc)
 | 
			
		||||
@login_required
 | 
			
		||||
def spacy_nlp_pipeline_model(spacy_nlp_pipeline_model_id):
 | 
			
		||||
    snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
 | 
			
		||||
    form_prefix = 'edit-spacy-nlp-pipeline-model-form'
 | 
			
		||||
    form = EditSpaCyNLPPipelineModelForm(
 | 
			
		||||
        data=snpm.to_json_serializeable(),
 | 
			
		||||
        prefix=form_prefix
 | 
			
		||||
    )
 | 
			
		||||
    if form.validate_on_submit():
 | 
			
		||||
        form.populate_obj(snpm)
 | 
			
		||||
        if db.session.is_modified(snpm):
 | 
			
		||||
            flash(f'SpaCy NLP Pipeline model "{snpm.title}" updated')
 | 
			
		||||
            db.session.commit()
 | 
			
		||||
        return redirect(url_for('.spacy_nlp_pipeline_models'))
 | 
			
		||||
    return render_template(
 | 
			
		||||
        'contributions/spacy_nlp_pipeline_models/spacy_nlp_pipeline_model.html.j2',
 | 
			
		||||
        form=form,
 | 
			
		||||
        spacy_nlp_pipeline_model=snpm,
 | 
			
		||||
        title=f'{snpm.title} {snpm.version}'
 | 
			
		||||
    )
 | 
			
		||||
							
								
								
									
										13
									
								
								app/contributions/spacy_nlp_pipeline_models/utils.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								app/contributions/spacy_nlp_pipeline_models/utils.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
from flask import request, url_for
 | 
			
		||||
from app.models import SpaCyNLPPipelineModel
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def spacy_nlp_pipeline_model_dlc():
 | 
			
		||||
    snpm_id = request.view_args['spacy_nlp_pipeline_model_id']
 | 
			
		||||
    snpm = SpaCyNLPPipelineModel.query.get(snpm_id)
 | 
			
		||||
    return [
 | 
			
		||||
        {
 | 
			
		||||
            'text': f'{snpm.title} {snpm.version}',
 | 
			
		||||
            'url': url_for('.spacy_nlp_pipeline_model', spacy_nlp_pipeline_model_id=snpm_id)
 | 
			
		||||
        }
 | 
			
		||||
    ]
 | 
			
		||||
@@ -0,0 +1,2 @@
 | 
			
		||||
from .. import bp
 | 
			
		||||
from . import json_routes, routes
 | 
			
		||||
							
								
								
									
										35
									
								
								app/contributions/tesseract_ocr_pipeline_models/forms.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								app/contributions/tesseract_ocr_pipeline_models/forms.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
from flask_wtf.file import FileField, FileRequired
 | 
			
		||||
from wtforms import ValidationError
 | 
			
		||||
from app.services import SERVICES
 | 
			
		||||
from ..forms import ContributionBaseForm, EditContributionBaseForm
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class CreateTesseractOCRPipelineModelForm(ContributionBaseForm):
 | 
			
		||||
    tesseract_model_file = FileField(
 | 
			
		||||
        'File',
 | 
			
		||||
        validators=[FileRequired()]
 | 
			
		||||
    )
 | 
			
		||||
    
 | 
			
		||||
    def validate_tesseract_model_file(self, field):
 | 
			
		||||
        if not field.data.filename.lower().endswith('.traineddata'):
 | 
			
		||||
            raise ValidationError('traineddata files only!')
 | 
			
		||||
 | 
			
		||||
    def __init__(self, *args, **kwargs):
 | 
			
		||||
        service_manifest = SERVICES['tesseract-ocr-pipeline']
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
        self.compatible_service_versions.choices = [('', 'Choose your option')]
 | 
			
		||||
        self.compatible_service_versions.choices += [
 | 
			
		||||
            (x, x) for x in service_manifest['versions'].keys()
 | 
			
		||||
        ]
 | 
			
		||||
        self.compatible_service_versions.default = ''
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class EditTesseractOCRPipelineModelForm(EditContributionBaseForm):
 | 
			
		||||
    def __init__(self, *args, **kwargs):
 | 
			
		||||
        service_manifest = SERVICES['tesseract-ocr-pipeline']
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
        self.compatible_service_versions.choices = [('', 'Choose your option')]
 | 
			
		||||
        self.compatible_service_versions.choices += [
 | 
			
		||||
            (x, x) for x in service_manifest['versions'].keys()
 | 
			
		||||
        ]
 | 
			
		||||
        self.compatible_service_versions.default = ''
 | 
			
		||||
@@ -0,0 +1,54 @@
 | 
			
		||||
from flask import abort, current_app, request
 | 
			
		||||
from flask_login import login_required, current_user
 | 
			
		||||
from threading import Thread
 | 
			
		||||
from app import db
 | 
			
		||||
from app.decorators import content_negotiation, permission_required
 | 
			
		||||
from app.models import TesseractOCRPipelineModel
 | 
			
		||||
from . import bp
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/tesseract-ocr-pipeline-models/<hashid:tesseract_ocr_pipeline_model_id>', methods=['DELETE'])
 | 
			
		||||
@login_required
 | 
			
		||||
@content_negotiation(produces='application/json')
 | 
			
		||||
def delete_tesseract_model(tesseract_ocr_pipeline_model_id):
 | 
			
		||||
    def _delete_tesseract_ocr_pipeline_model(app, tesseract_ocr_pipeline_model_id):
 | 
			
		||||
        with app.app_context():
 | 
			
		||||
            topm = TesseractOCRPipelineModel.query.get(tesseract_ocr_pipeline_model_id)
 | 
			
		||||
            topm.delete()
 | 
			
		||||
            db.session.commit()
 | 
			
		||||
 | 
			
		||||
    topm = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
 | 
			
		||||
    if not (topm.user == current_user or current_user.is_administrator()):
 | 
			
		||||
        abort(403)
 | 
			
		||||
    thread = Thread(
 | 
			
		||||
        target=_delete_tesseract_ocr_pipeline_model,
 | 
			
		||||
        args=(current_app._get_current_object(), topm.id)
 | 
			
		||||
    )
 | 
			
		||||
    thread.start()
 | 
			
		||||
    response_data = {
 | 
			
		||||
        'message': \
 | 
			
		||||
            f'Tesseract OCR Pipeline Model "{topm.title}" marked for deletion'
 | 
			
		||||
    }
 | 
			
		||||
    return response_data, 202
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/tesseract-ocr-pipeline-models/<hashid:tesseract_ocr_pipeline_model_id>/is_public', methods=['PUT'])
 | 
			
		||||
@login_required
 | 
			
		||||
@permission_required('CONTRIBUTE')
 | 
			
		||||
@content_negotiation(consumes='application/json', produces='application/json')
 | 
			
		||||
def update_tesseract_ocr_pipeline_model_is_public(tesseract_ocr_pipeline_model_id):
 | 
			
		||||
    is_public = request.json
 | 
			
		||||
    if not isinstance(is_public, bool):
 | 
			
		||||
        abort(400)
 | 
			
		||||
    topm = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
 | 
			
		||||
    if not (topm.user == current_user or current_user.is_administrator()):
 | 
			
		||||
        abort(403)
 | 
			
		||||
    topm.is_public = is_public
 | 
			
		||||
    db.session.commit()
 | 
			
		||||
    response_data = {
 | 
			
		||||
        'message': (
 | 
			
		||||
            f'Tesseract OCR Pipeline Model "{topm.title}"'
 | 
			
		||||
            f' is now {"public" if is_public else "private"}'
 | 
			
		||||
        )
 | 
			
		||||
    }
 | 
			
		||||
    return response_data, 200
 | 
			
		||||
							
								
								
									
										82
									
								
								app/contributions/tesseract_ocr_pipeline_models/routes.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								app/contributions/tesseract_ocr_pipeline_models/routes.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,82 @@
 | 
			
		||||
from flask import abort, flash, redirect, render_template, request, url_for
 | 
			
		||||
from flask_breadcrumbs import register_breadcrumb
 | 
			
		||||
from flask_login import current_user, login_required
 | 
			
		||||
from app import db
 | 
			
		||||
from app.models import TesseractOCRPipelineModel
 | 
			
		||||
from . import bp
 | 
			
		||||
from .forms import (
 | 
			
		||||
    CreateTesseractOCRPipelineModelForm,
 | 
			
		||||
    EditTesseractOCRPipelineModelForm
 | 
			
		||||
)
 | 
			
		||||
from .utils import (
 | 
			
		||||
    tesseract_ocr_pipeline_model_dlc as tesseract_ocr_pipeline_model_dlc
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/tesseract-ocr-pipeline-models')
 | 
			
		||||
@register_breadcrumb(bp, '.tesseract_ocr_pipeline_models', 'Tesseract OCR Pipeline Models')
 | 
			
		||||
@login_required
 | 
			
		||||
def tesseract_ocr_pipeline_models():
 | 
			
		||||
    return render_template(
 | 
			
		||||
        'contributions/tesseract_ocr_pipeline_models/tesseract_ocr_pipeline_models.html.j2',
 | 
			
		||||
        title='Tesseract OCR Pipeline Models'
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/tesseract-ocr-pipeline-models/create', methods=['GET', 'POST'])
 | 
			
		||||
@register_breadcrumb(bp, '.tesseract_ocr_pipeline_models.create', 'Create')
 | 
			
		||||
@login_required
 | 
			
		||||
def create_tesseract_ocr_pipeline_model():
 | 
			
		||||
    form_prefix = 'create-tesseract-ocr-pipeline-model-form'
 | 
			
		||||
    form = CreateTesseractOCRPipelineModelForm(prefix=form_prefix)
 | 
			
		||||
    if form.is_submitted():
 | 
			
		||||
        if not form.validate():
 | 
			
		||||
            return {'errors': form.errors}, 400
 | 
			
		||||
        try:
 | 
			
		||||
            topm = TesseractOCRPipelineModel.create(
 | 
			
		||||
                form.tesseract_model_file.data,
 | 
			
		||||
                compatible_service_versions=form.compatible_service_versions.data,
 | 
			
		||||
                description=form.description.data,
 | 
			
		||||
                publisher=form.publisher.data,
 | 
			
		||||
                publisher_url=form.publisher_url.data,
 | 
			
		||||
                publishing_url=form.publishing_url.data,
 | 
			
		||||
                publishing_year=form.publishing_year.data,
 | 
			
		||||
                is_public=False,
 | 
			
		||||
                title=form.title.data,
 | 
			
		||||
                version=form.version.data,
 | 
			
		||||
                user=current_user
 | 
			
		||||
            )
 | 
			
		||||
        except OSError:
 | 
			
		||||
            abort(500)
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        flash(f'Tesseract OCR Pipeline model "{topm.title}" created')
 | 
			
		||||
        return {}, 201, {'Location': url_for('.tesseract_ocr_pipeline_models')}
 | 
			
		||||
    return render_template(
 | 
			
		||||
        'contributions/tesseract_ocr_pipeline_models/create_tesseract_ocr_pipeline_model.html.j2',
 | 
			
		||||
        form=form,
 | 
			
		||||
        title='Create Tesseract OCR Pipeline Model'
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/tesseract-ocr-pipeline-models/<hashid:tesseract_ocr_pipeline_model_id>', methods=['GET', 'POST'])
 | 
			
		||||
@register_breadcrumb(bp, '.tesseract_ocr_pipeline_models.entity', '', dynamic_list_constructor=tesseract_ocr_pipeline_model_dlc)
 | 
			
		||||
@login_required
 | 
			
		||||
def tesseract_ocr_pipeline_model(tesseract_ocr_pipeline_model_id):
 | 
			
		||||
    topm = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
 | 
			
		||||
    form_prefix = 'edit-tesseract-ocr-pipeline-model-form'
 | 
			
		||||
    form = EditTesseractOCRPipelineModelForm(
 | 
			
		||||
        data=topm.to_json_serializeable(),
 | 
			
		||||
        prefix=form_prefix
 | 
			
		||||
    )
 | 
			
		||||
    if form.validate_on_submit():
 | 
			
		||||
        form.populate_obj(topm)
 | 
			
		||||
        if db.session.is_modified(topm):
 | 
			
		||||
            flash(f'Tesseract OCR Pipeline model "{topm.title}" updated')
 | 
			
		||||
            db.session.commit()
 | 
			
		||||
        return redirect(url_for('.tesseract_ocr_pipeline_models'))
 | 
			
		||||
    return render_template(
 | 
			
		||||
        'contributions/tesseract_ocr_pipeline_models/tesseract_ocr_pipeline_model.html.j2',
 | 
			
		||||
        form=form,
 | 
			
		||||
        tesseract_ocr_pipeline_model=topm,
 | 
			
		||||
        title=f'{topm.title} {topm.version}'
 | 
			
		||||
    )
 | 
			
		||||
							
								
								
									
										13
									
								
								app/contributions/tesseract_ocr_pipeline_models/utils.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								app/contributions/tesseract_ocr_pipeline_models/utils.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
from flask import request, url_for
 | 
			
		||||
from app.models import TesseractOCRPipelineModel
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def tesseract_ocr_pipeline_model_dlc():
 | 
			
		||||
    topm_id = request.view_args['tesseract_ocr_pipeline_model_id']
 | 
			
		||||
    topm = TesseractOCRPipelineModel.query.get(topm_id)
 | 
			
		||||
    return [
 | 
			
		||||
        {
 | 
			
		||||
            'text': f'{topm.title} {topm.version}',
 | 
			
		||||
            'url': url_for('.tesseract_ocr_pipeline_model', tesseract_ocr_pipeline_model_id=topm_id)
 | 
			
		||||
        }
 | 
			
		||||
    ]
 | 
			
		||||
		Reference in New Issue
	
	Block a user