<commit_before>import tempfile
import os

from flask import Flask, render_template, request, abort, Response, redirect, url_for
from werkzeug import secure_filename
from flask_bootstrap import Bootstrap
from elasticsearch import exceptions as es_exceptions
from flask.ext.babel import Babel, gettext
from babel.dates import format_timedelta
from datetime import datetime

from presets import PresetManager
from constants import isoLangs
from util import requestedFormat, send_attachment_file
from archivant import Archivant
from archivant.exceptions import NotFoundException, FileOpNotSupported
from agherant import agherant
from api.blueprint_api import api
from webserver_utils import gevent_run
import users


class LibreantCoreApp(Flask):
    def __init__(self, import_name, conf={}):
        super(LibreantCoreApp, self).__init__(import_name)
        defaults = {
            'PRESET_PATHS': [],  # defaultPreset should be loaded as default?
            'FSDB_PATH': "",
            'SECRET_KEY': '<PASSWORD>, please change me!',
            'ES_HOSTS': None,
            'ES_INDEXNAME': 'libreant',
            'USERS_DATABASE': "",
            'PWD_ROUNDS': None,
            'PWD_SALT_SIZE': None
        }
        defaults.update(conf)
        self.config.update(defaults)

        self.archivant = Archivant(conf={k: self.config[k] for k in ('FSDB_PATH', 'ES_HOSTS', 'ES_INDEXNAME')})
        self.presetManager = PresetManager(self.config['PRESET_PATHS'])

        self.usersDB = users.init_db(self.config['USERS_DATABASE'],
                                     pwd_salt_size=self.config['PWD_SALT_SIZE'],
                                     pwd_rounds=self.config['PWD_ROUNDS'])
        users.populate_with_defaults()


class LibreantViewApp(LibreantCoreApp):
    def __init__(self, import_name, conf={}):
        defaults = {
            'BOOTSTRAP_SERVE_LOCAL': True,
            'AGHERANT_DESCRIPTIONS': [],
        }
        defaults.update(conf)
        super(LibreantViewApp, self).__init__(import_name, defaults)
        if self.config['AGHERANT_DESCRIPTIONS']:
            self.register_blueprint(agherant, url_prefix='/agherant')
        self.register_blueprint(api, url_prefix='/api/v1')
        Bootstrap(self)
        self.babel = Babel(self)
        self.available_translations = [l.language for l in self.babel.list_translations()]


def create_app(conf={}):
    app = LibreantViewApp("webant", conf)

    @app.route('/')
    def index():
        return render_template('index.html')

    @app.route('/search')
    def search():
        query = request.args.get('q', None)
        if query is None:
            abort(400, "No query given")
        res = app.archivant._db.user_search(query)['hits']['hits']
        books = []
        for b in res:
            src = b['_source']
            src['_id'] = b['_id']
            src['_score'] = b['_score']
            books.append(src)
        format = requestedFormat(request,
                                 ['text/html',
                                  'text/xml', 'application/rss+xml', 'opensearch'])
        if (not format) or (format is 'text/html'):
            return render_template('search.html', books=books, query=query)
        elif format in ['opensearch', 'text/xml', 'application/rss+xml']:
            return Response(render_template('opens.xml',
                                            books=books, query=query),
                            mimetype='text/xml')
        else:
            abort(500)

    @app.route('/add', methods=['POST'])
    def upload():
        requiredFields = ['_language']
        optFields = ['_preset']
        body = {}

        # TODO check also for preset consistency?

        for requiredField in requiredFields:
            if requiredField not in request.form:
                renderErrorPage(gettext("Required field '%(mField)s' is missing",
                                mField=requiredField), 400)
            else:
                body[requiredField] = request.form[requiredField]

        for optField in optFields:
            if optField in request.form:
                body[optField] = request.form[optField]

        for key, value in request.form.items():
            if key.startswith('field_') and value:
                body[key[6:]] = value

        attachments = []
        for upName, upFile in request.files.items():
            tmpFileFd, tmpFilePath = tempfile.mkstemp()
            upFile.save(tmpFilePath)
            fileInfo = {}
            fileInfo['file'] = tmpFilePath
            fileInfo['name'] = secure_filename(upFile.filename)<commit_msg>remove aborts from webant views<commit_after>import tempfile
import os

from flask import Flask, render_template, request, Response, redirect, url_for
from werkzeug import secure_filename
from flask_bootstrap import Bootstrap
from elasticsearch import exceptions as es_exceptions
from flask.ext.babel import Babel, gettext
from babel.dates import format_timedelta
from datetime import datetime

from presets import PresetManager
from constants import isoLangs
from util import requestedFormat, send_attachment_file
from archivant import Archivant
from archivant.exceptions import NotFoundException, FileOpNotSupported
from agherant import agherant
from api.blueprint_api import api
from webserver_utils import gevent_run
import users


class LibreantCoreApp(Flask):
    def __init__(self, import_name, conf={}):
        super(LibreantCoreApp, self).__init__(import_name)
        defaults = {
            'PRESET_PATHS': [],  # defaultPreset should be loaded as default?
            'FSDB_PATH': "",
            'SECRET_KEY': '<PASSWORD>, please change me!',
            'ES_HOSTS': None,
            'ES_INDEXNAME': 'libreant',
            'USERS_DATABASE': "",
            'PWD_ROUNDS': None,
            'PWD_SALT_SIZE': None
        }
        defaults.update(conf)
        self.config.update(defaults)

        self.archivant = Archivant(conf={k: self.config[k] for k in ('FSDB_PATH', 'ES_HOSTS', 'ES_INDEXNAME')})
        self.presetManager = PresetManager(self.config['PRESET_PATHS'])

        self.usersDB = users.init_db(self.config['USERS_DATABASE'],
                                     pwd_salt_size=self.config['PWD_SALT_SIZE'],
                                     pwd_rounds=self.config['PWD_ROUNDS'])
        users.populate_with_defaults()


class LibreantViewApp(LibreantCoreApp):
    def __init__(self, import_name, conf={}):
        defaults = {
            'BOOTSTRAP_SERVE_LOCAL': True,
            'AGHERANT_DESCRIPTIONS': [],
        }
        defaults.update(conf)
        super(LibreantViewApp, self).__init__(import_name, defaults)
        if self.config['AGHERANT_DESCRIPTIONS']:
            self.register_blueprint(agherant, url_prefix='/agherant')
        self.register_blueprint(api, url_prefix='/api/v1')
        Bootstrap(self)
        self.babel = Babel(self)
        self.available_translations = [l.language for l in self.babel.list_translations()]


def create_app(conf={}):
    app = LibreantViewApp("webant", conf)

    @app.route('/')
    def index():
        return render_template('index.html')

    @app.route('/search')
    def search():
        query = request.args.get('q', None)
        if query is None:
            return renderErrorPage(message='No query given', httpCode=400)
        res = app.archivant._db.user_search(query)['hits']['hits']
        books = []
        for b in res:
            src = b['_source']
            src['_id'] = b['_id']
            src['_score'] = b['_score']
            books.append(src)
        format = requestedFormat(request,
                                 ['text/html',
                                  'text/xml', 'application/rss+xml', 'opensearch'])
        if (not format) or (format is 'text/html'):
            return render_template('search.html', books=books, query=query)
        elif format in ['opensearch', 'text/xml', 'application/rss+xml']:
            return Response(render_template('opens.xml',
                                            books=books, query=query),
                            mimetype='text/xml')
        else:
            return renderErrorPage(message='Unknown format requested', httpCode=400)

    @app.route('/add', methods=['POST'])
    def upload():
        requiredFields = ['_language']
        optFields = ['_preset']
        body = {}

        # TODO check also for preset consistency?

        for requiredField in requiredFields:
            if requiredField not in request.form:
                renderErrorPage(gettext("Required field '%(mField)s' is missing",
                                mField=requiredField), 400)
            else:
                body[requiredField] = request.form[requiredField]

        for optField in optFields:
            if optField in request.form:
                body[optField] = request.form[optField]

        for key, value in request.form.items():
            if key.startswith('field_') and value:
                body[key[6:]] = value

        attachments = []
        for upName, upFile in request.files.items():
            tmpFileFd, tmpFilePath = tempfile.mkstemp()
            upFile.save(tmpFilePath)
            fileInfo = {}
            fileInfo['file'] = tmpFilePath
            fileInfo['name'] = secure_filename(upFile.filename)
{'fraction_non_alphanumeric': 0.10811970395795345, 'fraction_numerical': 0.0023597554435267616, 'mean_word_length': 2.333571683947086, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7083276', 'n_tokens_mistral': 2692, 'n_tokens_neox': 2612, 'n_words': 593}