RootFactory   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 12
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 2 1
1
import datetime
2
import functools
3
from decimal import Decimal
4
from decimal import ROUND_HALF_UP
5
import decimal
6
from sqlalchemy import (
7
    LargeBinary,
8
    Column,
9
    Index,
10
    Integer,
11
    String,
12
    Numeric,
13
    Text,
14
    Enum,
15
    ForeignKey,
16
    Boolean,
17
    LargeBinary,
18
    desc,
19
    asc
20
    )
21
22
from sqlalchemy.ext.declarative import declarative_base
23
24
from sqlalchemy.orm import (
25
    scoped_session,
26
    sessionmaker,
27
    validates,
28
    relationship,
29
    object_session,
30
    backref
31
    )
32
33
from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method
34
from sqlalchemy.orm import synonym
35
from sqlalchemy.sql.expression import or_, and_, func, exists
36
from sqlalchemy.sql.functions import coalesce
37
from zope.sqlalchemy import ZopeTransactionExtension
38
39
from .history_meta import Versioned, versioned_session
40
41
from pyramid.security import Allow, Everyone
42
43
DBSession = versioned_session(scoped_session(sessionmaker(extension=ZopeTransactionExtension())))
44
Base = declarative_base()
45
46
class RootFactory(object):
47
    __name__ = None
48
    __parent__ = None
49
    __acl__ = [
50
        (Allow, 'user', 'user'),
51
        (Allow, 'serviceaccount', 'service'),
52
        (Allow, 'manager', 'manage'),
53
        (Allow, 'admin', 'admin'),
54
    ]
55
56
    def __init__(self, request):
57
        pass
58
59
# Decorator that adds optional limit parameter to any all() query
60
def limitable_all(fn_being_decorated):
61
    @functools.wraps(fn_being_decorated)
62
    def wrapped_fn(*args, limit=None, offset=None, count=False, **kwargs):
63
        q = fn_being_decorated(*args, **kwargs)
64
        if offset:
65
            q = q.offset(offset)
66
        if limit:
67
            if count:
68
                return q.limit(limit).all(), q.count()
69
            else:
70
                return q.limit(limit).all()
71
        else:
72
            if count:
73
                return q.all(), q.count()
74
            else:
75
                return q.all()
76
    return wrapped_fn
77
78
# Helper that checks for common get parameters for limitable queries
79
def limitable_request(request, fn, prefix=None, limit=None, count=False):
80
    if prefix:
81
        limit_str = prefix + '_limit'
82
        offset_str = prefix + '_offset'
83
    else:
84
        limit_str = 'limit'
85
        offset_str = 'offset'
86
    try:
87
        LIMIT  = int(request.GET[limit_str ]) if limit_str  in request.GET else limit
88
    except ValueError:
89
        LIMIT  = limit
90
    try:
91
        OFFSET = int(request.GET[offset_str]) if offset_str in request.GET else None
92
    except ValueError:
93
        OFFSET = None
94
95
    if count:
96
        r, r_tot = fn(limit=LIMIT, offset=OFFSET, count=count)
97
        if LIMIT is None or r_tot <= LIMIT:
98
            r_tot = 0
99
        return r, r_tot
100
    else:
101
        return fn(limit=LIMIT, offset=OFFSET, count=count)
102
103