# Path: damvitool/db.py
# def generate_orm_classes(db_conn_str):
# 
# Path: damvitool/users.py
# class Login:
#     pass
# 
# class Logout:
#     pass
# 
# Path: damvitool/utils.py
# def to_json_type(value):
#     if isinstance(value, (Decimal, datetime, date)):
#         return str(value)
#     return value


from morepath import Response
from sqlalchemy import func, or_, and_, not_, sql
from sqlalchemy.orm import class_mapper, aliased
from damvitool.db import Base, orm_classes, Session
from json import loads
from damvitool.users import Login, Logout
from damvitool.utils import to_json_type
    def query_summaries(self, request):
        request = request['unigrid']
        entity = request['entities']

        # array of requested fields
        fields = []
        # array of all query entities for simple field search
        entities = []
        # parse UniGridRequest. Get array of fields and array of entities
        for e in entity:
            self._process_entity_attrs(e, fields, entities)

        offset = request.get('offset', 0)
        query = Session().query(*[f['ref'] for f in fields if f['selected']])
        for e in entities:
            if 'join' in e:
                query = query.outerjoin((e['alias'], e['join']))
        # filtration
        where = self._process_where(request.get('where', {}), fields)
        if where is not None:
            query = query.filter(where)

        sum_fields = [(getattr(sql.func, s)(f['ref']), s, f['alias']) for f in fields if f['summaries'] for s in
                      f['summaries']]
        data = []
        if sum_fields:
            data = query.from_self(*[f[0] for f in sum_fields]).one()
        count = 1
        return {
            'cols': [str(f[1]) + '_' + str(f[2]) for f in sum_fields],

# Path: damvitool/db.py
# def generate_orm_classes(db_conn_str):
# 
# Path: damvitool/users.py
# class Login:
#     pass
# 
# class Logout:
#     pass
# 
# Path: damvitool/utils.py
# def to_json_type(value):
#     if isinstance(value, (Decimal, datetime, date)):
#         return str(value)
#     return value


# Path: damvitool/model.py
from morepath import Response
from sqlalchemy import func, or_, and_, not_, sql
from sqlalchemy.orm import class_mapper, aliased
from damvitool.db import Base, orm_classes, Session
from json import loads
from damvitool.users import Login, Logout
from damvitool.utils import to_json_type

    def query_summaries(self, request):
        request = request['unigrid']
        entity = request['entities']

        # array of requested fields
        fields = []
        # array of all query entities for simple field search
        entities = []
        # parse UniGridRequest. Get array of fields and array of entities
        for e in entity:
            self._process_entity_attrs(e, fields, entities)

        offset = request.get('offset', 0)
        query = Session().query(*[f['ref'] for f in fields if f['selected']])
        for e in entities:
            if 'join' in e:
                query = query.outerjoin((e['alias'], e['join']))
        # filtration
        where = self._process_where(request.get('where', {}), fields)
        if where is not None:
            query = query.filter(where)

        sum_fields = [(getattr(sql.func, s)(f['ref']), s, f['alias']) for f in fields if f['summaries'] for s in
                      f['summaries']]
        data = []
        if sum_fields:
            data = query.from_self(*[f[0] for f in sum_fields]).one()
        count = 1
        return {
            'cols': [str(f[1]) + '_' + str(f[2]) for f in sum_fields],

            'data': [to_json_type(data[i]) for i in range(len(data))],