Beispiel #1
0
def register():
    if request.method == 'POST':
        error = None
        username = request.form['username']
        password = request.form['password']
        name = 'register user' + username
        app.logger.info(name)
        if not username or not password:
            app.logger.debug('Arguments incorrect for %s', name)
            return abort(400)

        from authorization.db.Users import Users
        user = db.get_db().session.query(Users).filter_by(
            username=username).first()
        if user:
            error = 'Username is locked!'

        if error is None:
            user = Users(username, password)
            db.get_db().session.add(user)
            db.get_db().session.commit()

            expire_date = datetime.datetime.now() + datetime.timedelta(hours=1)
            response = app.make_response(redirect('http://127.0.0.1:5000/'))
            response.set_cookie('auth', value=username, expires=expire_date)
            return response
        flash(error)

    return render_template('auth/register.html')
Beispiel #2
0
def clean_history():
    if 'auth' not in request.cookies:
        return redirect('http://127.0.0.1:5001/')
    name = 'clean history DB request'
    app.logger.info(name)
    our_request = Request(RequestType.CLEAN_HISTORY)
    from application.db.History import History
    db.get_db().session.query(History).delete()
    db.get_db().session.commit()
    __write_to_history__(our_request,
                         'history cleaned ')
    return render_template('index.html', user=app.config['user'])
Beispiel #3
0
def get_paper():
    if 'auth' not in request.cookies:
        return redirect('http://127.0.0.1:5001/')
    if request.method == 'POST':
        name = "Get paper request"
        app.logger.info(name)
        title = request.form['title']
        error = None
        if not title:
            app.logger.debug('Parameter incorrect for %s', name)
            error = 'Parameter incorrect for ' + name

        our_request = Request(RequestType.GET_PAPER, title=title)

        from application.db.Articles import Articles
        article = db.get_db().session.query(Articles).filter_by(
            title=our_request.title).first()
        if not article:
            error = 'Such article does not exist'
        if error is None:
            __write_to_history__(our_request, str(article))
            return render_template('forms/get_article.html',
                                   user=app.config['user'],
                                   output=True,
                                   authors=article.authors,
                                   title=article.title,
                                   key_words=article.key_words,
                                   abstract=article.annotation,
                                   ref=article.ref)
        else:
            __write_to_history__(our_request, error)
        flash(error)
    return render_template('forms/get_article.html', user=app.config['user'],
                           output=False)
Beispiel #4
0
def show_history():
    if 'auth' not in request.cookies:
        return redirect('http://127.0.0.1:5001/')
    name = 'Show history DB request'
    app.logger.info(name)
    error = None
    our_request = Request(RequestType.SHOW_HISTORY)
    from application.db.History import History
    if db.get_db().session.query(History).first():
        df = pd.read_sql(db.get_db().session.query(History).statement,
                         app.config['SQLALCHEMY_DATABASE_URI'])
        pd.set_option('display.max_colwidth', -1)
        __write_to_history__(our_request, 'was handled successfully')
    else:
        error = 'was handled unsuccessfully: ' \
                'History is empty'
        __write_to_history__(our_request, error)
    if error is None:
        return render_template('forms/show_history.html',
                               user=app.config['user'], data=df)
Beispiel #5
0
def update_paper():
    if 'auth' not in request.cookies:
        return redirect('http://127.0.0.1:5001/')
    if request.method == 'POST':
        name = 'Update paper DB request'
        app.logger.info(name)
        authors = request.form['authors']
        title = request.form['title']
        key_words = request.form['key_words']
        abstract = request.form['abstract']
        ref = request.form['ref']
        error = None
        if not (authors or title or key_words or abstract or ref):
            app.logger.debug('Arguments incorrect for %s', name)
            error = 'One or more arguments are missing.'
        our_request = Request(RequestType.UPDATE_PAPER, authors,
                              title, key_words,
                              abstract, ref)

        if error is None:
            from application.db.Articles import Articles
            if db.get_db().session.query(Articles).filter_by(
                    title=our_request.title).first():
                db.get_db().session.query(Articles).filter(
                    Articles.title == our_request.title).update(
                    {'annotation': our_request.annotation,
                     'authors': our_request.authors,
                     'key_words': our_request.key_words,
                     'ref': our_request.ref})
                db.get_db().session.commit()
                response = 'was handled successfully'
            else:
                response = 'was handled unsuccessfully:' \
                           ' Such article does not exists'
            __write_to_history__(our_request, response)
            flash(name + ' ' + response)
        else:
            __write_to_history__(our_request, error)
            flash(error)
    return render_template('forms/update_article.html',
                           user=app.config['user'])
Beispiel #6
0
def delete_paper():
    if 'auth' not in request.cookies:
        return redirect('http://127.0.0.1:5001/')
    if request.method == 'POST':
        name = 'Delete paper from DB request'
        app.logger.info(name)
        title = request.form['title']
        error = None
        if not title:
            app.logger.debug('Arguments incorrect for %s', name)
            error = 'Arguments are missing.'
        our_request = Request(RequestType.DELETE_PAPER, title=title)

        if error is None:
            from application.db.Articles import Articles
            if db.get_db().session.query(Articles).filter_by(
                    title=our_request.title).first():
                article = db.get_db().session.query(Articles).filter_by(
                    title=title).first()
                db.get_db().session.delete(article)
                db.get_db().session.commit()
                response = 'was handled successfully'
            else:
                response = 'was handled unsuccessfully: ' \
                           'Such article dose not exists'
            __write_to_history__(our_request, response)
            flash(name + ' ' + response)
        else:
            __write_to_history__(our_request, error)
            flash(error)
    return render_template('forms/delete_article.html',
                           user=app.config['user'])
Beispiel #7
0
def find_similar_paper():
    if 'auth' not in request.cookies:
        return redirect('http://127.0.0.1:5001/')
    if request.method == 'POST':
        name = 'Find similar paper request'
        app.logger.info(name)
        authors = request.form['authors']
        title = request.form['title']
        key_words = request.form['key_words']
        abstract = request.form['abstract']
        ref = request.form['ref']
        try:
            _ = request.form['checkbox']
            checkbox = True
        except KeyError:
            checkbox = False
        error = None
        from application.db.Articles import Articles
        if not title:
            app.logger.debug('Arguments incorrect for %s', name)
            error = 'Title is missing.'
        if not authors or not key_words or not abstract or not ref:
            app.logger.info("Getting elements from bd")
            article = db.get_db().session.query(Articles).filter_by(
                title=title).first()
            if not article:
                app.logger.info('Such article is not in database '
                                'please provide all fields!')
                error = 'Such article is not in database' \
                        ' please provide all fields!'
        elif not db.get_db().session.query(Articles).filter_by(
                title=title).first():
            app.logger.debug('Arguments incorrect for %s', name)
            article = Articles(title, authors, key_words, abstract, ref)
            db.get_db().session.add(article)
            db.get_db().session.commit()
        if error is None:
            our_request = Request(RequestType.FIND_SIMILAR_PAPER,
                                  article.authors, article.title,
                                  article.key_words, article.annotation)
            pd.set_option('display.max_colwidth', -1)
            df = app.config['seeker'].find_article_by_text(
                article.title, article.annotation, article.key_words, checkbox)
            __write_to_history__(our_request, df.title.to_string)
            columns = ['authors', 'title',
                       # 'ref',
                       'annotation', 'key_words']
            delete_columns = [column not in columns for column in df.columns]
            df = df.drop(columns=df.columns[delete_columns])
            return render_template('forms/show_found_articles.html',
                                   user=app.config['user'], data=df)
        else:
            flash(error)
    return render_template('forms/find_similar_paper.html',
                           user=app.config['user'])
Beispiel #8
0
def random_paper():
    if 'auth' not in request.cookies:
        return redirect('http://127.0.0.1:5001/')
    name = "Get random paper request"
    app.logger.info(name)

    our_request = Request(RequestType.GET_RANDOM_PAPER)

    from application.db.Articles import Articles
    article = db.get_db().session.query(Articles).order_by(
        func.random()).first()
    __write_to_history__(our_request, str(article))

    return render_template('forms/random_article.html',
                           user=app.config['user'],
                           authors=article.authors,
                           title=article.title, key_words=article.key_words,
                           abstract=article.annotation, ref=article.ref)
Beispiel #9
0
import time

from application.db.db import get_db

db_loc = get_db()


class History(db_loc.Model):
    id = db_loc.Column(db_loc.Integer, primary_key=True)
    username = db_loc.Column(db_loc.String(30), nullable=False)
    request = db_loc.Column(db_loc.Text, nullable=False)
    response = db_loc.Column(db_loc.Text, nullable=False)
    date = db_loc.Column(db_loc.Date, nullable=False)

    def __init__(self,
                 username,
                 request,
                 response,
                 date=time.strftime('%Y-%m-%d %H:%M:%S')):
        self.date = date
        self.response = response
        self.username = username
        self.request = request

    def __str__(self):
        return f'{type(self).__name__}: Username = {self.username}, ' \
            f'Request = {self.request}, Response = {self.response}'
Beispiel #10
0
def __write_to_history__(request_to_db, response_to_db):
    from application.db.History import History
    history = History(username=app.config['user'], request=str(request_to_db),
                      response=str(response_to_db))
    db.get_db().session.add(history)
    db.get_db().session.commit()