예제 #1
0
import json
import os
from json import JSONDecodeError

from services.db import DB
from services.mcc import get_flight_data_for_demetreus
from services.misc import api_fail, get_logger
from tornado.web import RequestHandler, HTTPError, asynchronous

error_logger = get_logger(__name__, 'logs/api_errors.log')
logger = get_logger(__name__, 'logs/api_posts.log')


class DefaultHandler(RequestHandler):
    def set_default_headers(self):
        self.set_header("Access-Control-Allow-Origin", "*")
        self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
        self.set_header(
            "Access-Control-Allow-Headers",
            "x-requested-with,access-control-allow-origin,authorization,content-type"
        )

    def options(self):
        self.set_status(204)
        self.finish()

    def get(self, *args, **kwargs):
        return self.post(*args, **kwargs)


class ApiHandler(DefaultHandler):
예제 #2
0
from collections import defaultdict

from services.misc import api_fail, api_ok, inject_db, get_logger
from services.model_crud import get_model_upkeep_price

logger = get_logger(__name__)


@inject_db
def add_pump(self, data):
    """ params: {company: str,
                section: nodes/models/crises/markets/mines,
                entity_id: int/str,
                comment: str,
                is_income: 1/0,
                resources {code: value}
    } """
    avail_vendors = self.db.fetchColumn('select code from companies')
    if not data.get('company') in avail_vendors:
        return api_fail("Не существует компания с кодом '{}'".format(
            data.get('company', '')))
    avail_sections = self.db.fetchColumn('select code from pump_sections')
    if not data.get('section') in avail_sections:
        return api_fail("Неизвестная секция {}. Возможные секции: {}".format(
            data.get('section'), ', '.join(avail_sections)))
    pump_id = self.db.insert('pumps', data)
    insert_parameters = [{
        "pump_id": pump_id,
        "resource_code": code,
        "value": def_value
    } for code, def_value in data.get('resources').items()]