-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication_state.py
79 lines (52 loc) · 2.03 KB
/
application_state.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
pathfinder application_state.py
:copyright: (c) 2018 by Pavel Konovalov
Created on 16.07.2018
"""
from helper_database import cursor_to_json
def get_list(db, order, limit, page):
sort_parameters = ['name-desc', 'name-asc']
table_name = 'application_state'
if order not in sort_parameters:
return {'error': 'Sort parameter must in {}'.format(', '.join(sort_parameters))}
if limit is not None and page is not None:
if limit <= 0:
return {'status': 'Error', 'message': 'Limit is not acceptable'}
if page <= 0:
return {'status': 'Error', 'message': 'Page is not acceptable'}
r = {}
cursor = db.cursor()
sql_count = 'select count(*) as count from {}'.format(table_name)
cursor.execute(sql_count)
count = cursor.fetchone()[0]
sql_limit = ''
if limit is not None and page is not None:
sql_limit = ' limit {} offset {}'.format(limit, (page - 1) * limit)
sql_base = 'select {}.id, {}.name, {}.color ' \
'from {} '.format(table_name, table_name, table_name, table_name)
sql_order = ''
if order == 'name-desc':
sql_order = 'order by {}.name desc'.format(table_name)
elif order == 'name-asc':
sql_order = 'order by {}.name asc'.format(table_name)
sql = '{} {} {}'.format(sql_base, sql_order, sql_limit)
cursor.execute(sql)
items = cursor_to_json(cursor)
r['count'] = count
r['items'] = items
return r
def get_item(db, id_item):
if id_item is None or id_item <= 0:
return {'error': 'Id is not acceptable'}
table_name = 'application_state'
cursor = db.cursor()
sql_where = 'where {}.id = {}'.format(table_name, id_item)
sql_base = 'select {}.id, {}.name, {}.color ' \
'from {} '.format(table_name, table_name, table_name, table_name)
sql = '{} {}'.format(sql_base, sql_where)
cursor.execute(sql)
item = cursor_to_json(cursor)
if item is None or len(item) == 0:
return {}
return item