Exemple #1
0
def render_explorer_annotation_create():
    with ctx.SessionContext() as app:
        user = check_login(app)
        if user:
            id_ = request.form.get('id', None, int)
            if id_ is not None:
                # -- construct annotation --
                owner_id = user.id
                start = request.form.get('span_start')
                length = request.form.get('span_length')
                features = dict(zip(request.form.getlist('feature_key'), request.form.getlist('feature_value')))
                annotation = Annotation(owner_id=owner_id, document_id=id_, start=start, length=length,
                                        features=features)
                # -- add to database --
                app.database.add(annotation)
                app.database.commit()
                msg = 'Annotation created successfully'
                app.messages.append(Message(Message.Type.SUCCESS, msg))
                document = app.database.query(Document).get(id_)
                return redirect(url_for('.render_explorer_corpus', id=document.corpus.id))
            else:
                msg = 'Invalid value for the document ID. Found \'{}\' expected an integer'.format(
                    request.form.get('id', None)
                )
                app.messages.append(Message(Message.Type.ERROR, msg))
        return redirect(url_for('.render_explorer'))
Exemple #2
0
def render_corpus_edit():
    with ctx.SessionContext() as app:
        user = check_login(app)
        if user:
            id_ = request.args.get('id', None, int)
            if id_ is not None:
                if request.method == 'POST':
                    corpus = app.database.query(Corpus).get(id_)
                    if corpus is not None:
                        corpus.name = request.form['corpus_name']
                        app.database.commit()
                        msg = 'Corpus with ID \'{}\' deleted successfully.'.format(
                            id_)
                        app.messages.append(Message(Message.Type.SUCCESS, msg))
                    else:
                        msg = 'No such corpus with ID \'{}\' to delete.'.format(
                            id_)
                        app.messages.append(Message(Message.Type.ERROR, msg))
                else:
                    corpora = serialize_corpora(app)
                    html = render_template('pages/corpus.html',
                                           messages=app.messages,
                                           corpora=corpora,
                                           mode='edit',
                                           edit_id=id_)
                    app.clear('messages')
                    return html
            else:
                msg = 'Invalid value for parameter ID. Found \'{}\' expected an integer.'.format(
                    id_)
                app.messages.append(Message(Message.Type.ERROR, msg))
    return redirect(url_for('.render_corpus'))
Exemple #3
0
def render_corpus_add():
    with ctx.SessionContext() as app:
        user = check_login(app)
        if user:
            corpus = Corpus(name=request.form['corpus_name'])
            try:
                app.database.add(corpus)
                app.database.commit()
                app.messages.append(
                    Message(Message.Type.SUCCESS,
                            'Corpus created successfully'))
            except IntegrityError as err:
                app.database.rollback()
                if "Duplicate entry '{}' for key 'name'".format(
                        corpus.name) in str(err):
                    msg = 'Corpus name already exists \'{}\''.format(
                        corpus.name)
                    app.messages.append(Message(Message.Type.ERROR, msg))
                else:
                    msg = 'Please contact admin for further assistance'
                    app.messages.append(Message(Message.Type.ERROR, msg))
            except SQLAlchemyError as _:
                msg = 'Please contact admin for further assistance'
                app.messages.append(Message(Message.Type.ERROR, msg))
    return redirect(url_for('.render_corpus'))
Exemple #4
0
def render_corpus_document_add():
    with ctx.SessionContext() as app:
        user = check_login(app)
        if user:
            id_ = request.args.get('id', None, int)
            if id_ is not None:
                if 'file' in request.files:
                    file = request.files['file']
                    if file.filename != '':
                        if file and allowed_file(file.filename):
                            file_data = file.read().decode('utf-8')  # latin-1
                            for line in file_data.split('\n'):
                                text = line.strip()
                                document = Document(corpus_id=id_, text=text)
                                app.database.add(document)
                            app.database.commit()
                            msg = 'Documents added to Corpus with ID = `{}` successfully'.format(
                                id_)
                            app.messages.append(
                                Message(Message.Type.SUCCESS, msg))
                        else:
                            msg = 'File not allowed in the system'
                            app.messages.append(
                                Message(Message.Type.ERROR, msg))
                    else:
                        msg = 'No selected file'
                        app.messages.append(Message(Message.Type.ERROR, msg))
                else:
                    msg = 'No file part'
                    app.messages.append(Message(Message.Type.ERROR, msg))
            else:
                msg = 'Invalid value for parameter ID. ' \
                      'Found \'{}\' expected an integer'.format(request.args.get('id', None))
                app.messages.append(Message(Message.Type.ERROR, msg))
    return redirect(url_for('.render_corpus'))
Exemple #5
0
def render_explorer_corpus():
    with ctx.SessionContext() as app:
        user = check_login(app)
        if user:
            id_ = request.args.get('id', None, int)
            index = request.args.get('page', 1, int)
            if id_ is not None:
                corpora = app.database.query(Corpus).all()
                try:
                    sel_corpus = app.database.query(Corpus).filter_by(id=id_).one()
                except NoResultFound as _:
                    msg = 'Corpus not found in the database. You will be redirected to a default corpus if exists'
                    app.messages.append(Message(Message.Type.ERROR, msg))
                else:
                    qry_documents = app.database.query(Document).filter_by(corpus_id=id_)
                    pagination = Pagination(qry_documents)
                    page_ = pagination.get(index)
                    sel_corpus = {**sel_corpus.serialize(), **serialize_documents(page_.items)}
                    html = render_template(
                        'pages/explorer.html',
                        messages=app.messages, corpora=corpora, selected_corpus=sel_corpus, pagination=pagination,
                        page=page_
                    )
                    app.clear('messages')
                    return html
            else:
                msg = 'Invalid value for parameter ID. ' \
                      'Found \'{}\' expected an integer'.format(request.args.get('id', None))
                app.messages.append(Message(Message.Type.ERROR, msg))
        return redirect(url_for('.render_explorer'))
Exemple #6
0
def render_explorer_annotation_delete():
    with ctx.SessionContext() as app:
        user = check_login(app)
        if user:
            id_ = request.args.get('id', None, int)
            if id_ is not None:
                owner_id = user.id
                annotation = app.database.query(Annotation).get(id_)
                if annotation is not None:
                    corpus_id = annotation.document.corpus.id
                    if owner_id == annotation.owner_id:
                        app.database.delete(annotation)
                        app.database.commit()
                        msg = 'Annotation (id=\'{}\') deleted successfully'.format(id_)
                        app.messages.append(Message(Message.Type.SUCCESS, msg))
                        return redirect(url_for('.render_explorer_corpus', id=corpus_id))
                    else:
                        msg = 'You are not authorized to delete annotation with ID \'{}\''.format(id_)
                        app.messages.append(Message(Message.Type.ERROR, msg))
                else:
                    msg = 'Unable to find an annotation with the provided ID \'{}\''.format(id_)
                    app.messages.append(Message(Message.Type.ERROR, msg))
            else:
                msg = 'Invalid value for the document ID. Found \'{}\' expected an integer'.format(
                    request.args.get('id', None)
                )
                app.messages.append(Message(Message.Type.ERROR, msg))
        return redirect(url_for('.render_explorer'))
Exemple #7
0
def inject_user():
    with ctx.SessionContext() as app:
        try:
            user = app.database.query(User).filter_by(
                username=app.username).one()
        except NoResultFound as _:
            user = None
        return dict(user=user)
Exemple #8
0
def render_signup():
    with ctx.SessionContext() as app:
        if request.method == 'GET':
            return render_template('pages/signup.html')
        else:
            username = request.form['username']
            password = generate_password_hash(request.form['password'])
            app.database.add(User(username=username, password=password))
            app.database.commit()
            return redirect(url_for('.render_index'))
Exemple #9
0
def render_explorer():
    with ctx.SessionContext() as app:
        user = check_login(app)
        if user:
            corpora = app.database.query(Corpus).all()
            html = render_template('pages/explorer.html', messages=app.messages, corpora=corpora, selected_corpus=None)
            app.clear('messages')
        else:
            html = redirect(url_for('index_page.render_index'))
        return html
Exemple #10
0
def render_corpus():
    with ctx.SessionContext() as app:
        user = check_login(app)
        if user:
            corpora = serialize_corpora(app)
            html = render_template('pages/corpus.html',
                                   messages=app.messages,
                                   corpora=corpora,
                                   mode='view')
            app.clear('messages')
        else:
            html = redirect(url_for('index_page.render_index'))
    return html
Exemple #11
0
def render_login():
    with ctx.SessionContext() as app:
        if request.method == 'GET':
            return render_template('pages/login.html')
        else:
            messages = []
            username = request.form['username']
            password = request.form['password']
            try:
                user = app.database.query(User).filter_by(
                    username=username).one()
                if not check_password_hash(user.password, password):
                    raise NoResultFound()
            except NoResultFound as _:
                msg = 'Invalid username or password.'
                messages.append(Message(Message.Type.ERROR, msg))
            else:
                app.username = username
            return redirect(url_for('.render_index'))
Exemple #12
0
def render_corpus_delete():
    id_ = request.args.get('id', None, int)
    with ctx.SessionContext() as app:
        user = check_login(app)
        if user:
            if id_ is not None:
                corpus = app.database.query(Corpus).get(id_)
                if corpus is not None:
                    app.database.delete(corpus)
                    app.database.commit()
                    msg = 'Corpus with ID \'{}\' deleted successfully.'.format(
                        id_)
                    app.messages.append(Message(Message.Type.SUCCESS, msg))
                else:
                    msg = 'No such corpus with ID \'{}\' to delete.'.format(
                        id_)
                    app.messages.append(Message(Message.Type.ERROR, msg))
            else:
                msg = 'Invalid value for parameter ID. Found \'{}\' expected an integer.'.format(
                    id_)
                app.messages.append(Message(Message.Type.ERROR, msg))
    return redirect(url_for('.render_corpus'))
Exemple #13
0
def render_explorer_annotation_update():
    with ctx.SessionContext() as app:
        user = check_login(app)
        if user:
            id_ = request.args.get('id', None, int)
            if id_ is not None:
                owner_id = user.id
                annotation = app.database.query(Annotation).get(id_)
                id_ = request.form.get('id', None, int)
                if owner_id == annotation.owner_id:
                    if id_ == annotation.document.id:
                        start = request.form.get('span_start')
                        length = request.form.get('span_length')
                        feature_keys = request.form.getlist('feature_key')
                        feature_values = request.form.getlist('feature_value')
                        features = dict(zip(feature_keys, feature_values))
                        annotation.start = start
                        annotation.length = length
                        annotation.features = features
                        app.database.commit()
                        corpus_id = annotation.document.corpus.id
                        msg = 'Annotation (id=\'{}\') updated successfully'.format(id_)
                        app.messages.append(Message(Message.Type.SUCCESS, msg))
                        return redirect(url_for('.render_explorer_corpus', id=corpus_id))
                    else:
                        msg = 'Can\'t alter the document of the annotation'
                        app.messages.append(Message(Message.Type.ERROR, msg))
                else:
                    msg = 'You are not authorized to delete annotation with ID \'{}\''.format(id_)
                    app.messages.append(Message(Message.Type.ERROR, msg))
            else:
                msg = 'Invalid value for the document ID. Found \'{}\' expected an integer'.format(
                    request.args.get('id', None)
                )
                app.messages.append(Message(Message.Type.ERROR, msg))
        return redirect(url_for('.render_explorer'))
Exemple #14
0
def render_logout():
    with ctx.SessionContext() as app:
        app.username = None
        return redirect(url_for('.render_index'))
Exemple #15
0
def render_index():
    with ctx.SessionContext() as app:
        html = render_template('pages/index.html', messages=app.messages)
        app.clear('messages')
        return html