<commit_before># -*- coding: UTF-8 -*-
from __future__ import unicode_literals, print_function, division
import time
import logging
from decimal import Decimal, ROUND_FLOOR
import tornado.web
import db
from collection import DictObject
from utils import convert_timestamp_to_utc_datetime, convert_datetime_to_client_timezone, get_current_timestamp

LOGGER = logging.getLogger(__name__)


class ApplicationsHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('applications.html', applications=list_applications())


class ApplicationHandler(tornado.web.RequestHandler):
    def get(self, application_id):
        self.render('application.html', application=get_application(application_id))


class ApplicationApprovalHandler(tornado.web.RequestHandler):
    def get(self, application_id):
        approve_application(application_id)
        self.write('Thanks! :)')


class ApplicationRejectionHandler(tornado.web.RequestHandler):
    def get(self, application_id):
        reject_application(application_id)
        self.write('Thanks! :(')


def new_application(title, freight, items, comment=None):
    conn = db.get_connection()
    cur = db.get_cursor(conn)
    subtotal = sum(item.total for item in items)
    for item in items:
        item.total = item.price * item.quantity - item.discount
    try:
        cur.execute('''
            INSERT INTO expenditure_application(title, subtotal, freight, comment, created_at)
            VALUES (?, ?, ?, ?, ?)
            ''', (title, subtotal, int(freight), comment, int(time.time())))
        application_id = cur.lastrowid
        values = tuple((application_id, item.title, item.link, item.price, item.quantity, item.discount, item.total) for item in items)
        cur.executemany('''
            INSERT INTO expenditure_application_item(application_id, title, link, price, quantity, discount, total)
            VALUES(?, ?, ?, ?, ?, ?, ?)
            ''', values)
    except Exception as e:
        LOGGER.exception('Got exception when create application: {}'.format(e.message))
        conn.rollback()
    else:
        LOGGER.info('Create application successfully!')
        conn.commit()


def list_applications():
    conn = db.get_connection()
    cur = db.get_cursor(conn)
    applications = cur.execute('SELECT * FROM expenditure_application').fetchall()
    application_items = cur.execute('SELECT * FROM expenditure_application_item').fetchall()
    application_id2items = {}
    for item in application_items:
        application_id2items.setdefault(item.application_id, []).append(item)
    for application in applications:
        application.line_items = application_id2items.get(application.id, [])
    normalize_applications(applications)
    return applications


def get_application(application_id):
    conn = db.get_connection()
    cur = conn.cursor()
    application = cur.execute('SELECT * FROM expenditure_application WHERE id=?', (application_id, )).fetchone()
    if application:
        application.line_items = cur.execute('SELECT * FROM expenditure_application_item WHERE application_id=?', (application_id, )).fetchall()
    normalize_applications([application])
    return application


def normalize_applications(applications):
    for application in applications:<commit_msg>refactor: return 404 if no application exist<commit_after># -*- coding: UTF-8 -*-
from __future__ import unicode_literals, print_function, division
import time
import logging
from decimal import Decimal, ROUND_FLOOR
import tornado.web
import db
from collection import DictObject
from utils import convert_timestamp_to_utc_datetime, convert_datetime_to_client_timezone, get_current_timestamp

LOGGER = logging.getLogger(__name__)


class ApplicationsHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('applications.html', applications=list_applications())


class ApplicationHandler(tornado.web.RequestHandler):
    def get(self, application_id):
        application = get_application(application_id)
        if not application:
            self.send_error(404)
        else:
            self.render('application.html', application=application)


class ApplicationApprovalHandler(tornado.web.RequestHandler):
    def get(self, application_id):
        approve_application(application_id)
        self.write('Thanks! :)')


class ApplicationRejectionHandler(tornado.web.RequestHandler):
    def get(self, application_id):
        reject_application(application_id)
        self.write('Thanks! :(')


def new_application(title, freight, items, comment=None):
    conn = db.get_connection()
    cur = db.get_cursor(conn)
    subtotal = sum(item.total for item in items)
    for item in items:
        item.total = item.price * item.quantity - item.discount
    try:
        cur.execute('''
            INSERT INTO expenditure_application(title, subtotal, freight, comment, created_at)
            VALUES (?, ?, ?, ?, ?)
            ''', (title, subtotal, int(freight), comment, int(time.time())))
        application_id = cur.lastrowid
        values = tuple((application_id, item.title, item.link, item.price, item.quantity, item.discount, item.total) for item in items)
        cur.executemany('''
            INSERT INTO expenditure_application_item(application_id, title, link, price, quantity, discount, total)
            VALUES(?, ?, ?, ?, ?, ?, ?)
            ''', values)
    except Exception as e:
        LOGGER.exception('Got exception when create application: {}'.format(e.message))
        conn.rollback()
    else:
        LOGGER.info('Create application successfully!')
        conn.commit()


def list_applications():
    conn = db.get_connection()
    cur = db.get_cursor(conn)
    applications = cur.execute('SELECT * FROM expenditure_application').fetchall()
    application_items = cur.execute('SELECT * FROM expenditure_application_item').fetchall()
    application_id2items = {}
    for item in application_items:
        application_id2items.setdefault(item.application_id, []).append(item)
    for application in applications:
        application.line_items = application_id2items.get(application.id, [])
    normalize_applications(applications)
    return applications


def get_application(application_id):
    conn = db.get_connection()
    cur = conn.cursor()
    application = cur.execute('SELECT * FROM expenditure_application WHERE id=?', (application_id, )).fetchone()
    if application:
        application.line_items = cur.execute('SELECT * FROM expenditure_application_item WHERE application_id=?', (application_id, )).fetchall()
        normalize_applications([application])
    return application


def normalize_applications(applications):
    for application in applications:
{'fraction_non_alphanumeric': 0.10600654177817424, 'fraction_numerical': 0.0020814748736247396, 'mean_word_length': 3.9463235294117647, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4987929', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1836, 'n_words': 420}