-
Notifications
You must be signed in to change notification settings - Fork 2
/
views.py
118 lines (97 loc) · 3.87 KB
/
views.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import traceback
import random
from flask.ext.restful import abort, Resource
from flask.ext.restful import reqparse
from db import db
from models import Contract, Agreement
from serializers import serialize_agreement, serialize_contract
class ContractList(Resource):
@staticmethod
def get():
return {'results': map(serialize_contract, Contract.query.all())}
@staticmethod
def post():
try:
parser = reqparse.RequestParser()
parser.add_argument('title')
parser.add_argument('body')
parser.add_argument('usage_limit', type=int)
data = parser.parse_args(strict=True)
contract = Contract(**data)
db.session.add(contract)
db.session.commit()
except Exception as e:
abort(400, message=str(e))
return serialize_contract(contract), 201
class ContractDetail(Resource):
@staticmethod
def get(contract_id):
contract = Contract.query.get_or_404(contract_id)
return serialize_contract(contract)
@staticmethod
def put(contract_id):
try:
parser = reqparse.RequestParser()
parser.add_argument('title')
parser.add_argument('body')
parser.add_argument('usage_limit', type=int)
data = parser.parse_args(strict=True)
contract = Contract.query.get_or_404(contract_id)
contract.title = data.get('title', None) or contract.title
contract.body = data.get('body', None) or contract.body
contract.usage_limit = data.get('usage_limit', None) or contract.usage_limit
db.session.commit()
except Exception as e:
abort(400, message=str(e))
return serialize_contract(contract), 201
class AgreementList(Resource):
@staticmethod
def get():
return {'results': map(serialize_agreement, Agreement.query.all())}
@staticmethod
def post():
try:
parser = reqparse.RequestParser()
parser.add_argument('name')
parser.add_argument('email')
parser.add_argument('contract_id', type=int)
parser.add_argument('signature')
data = parser.parse_args(strict=True)
agreement = Agreement(**data)
db.session.add(agreement)
db.session.commit()
except Exception as e:
abort(400, message=str(e))
return serialize_agreement(agreement), 201
class AgreementDetail(Resource):
@staticmethod
def get(agreement_id):
agreement = Agreement.query.get_or_404(agreement_id)
return serialize_agreement(agreement)
@staticmethod
def put(agreement_id):
try:
parser = reqparse.RequestParser()
parser.add_argument('name')
parser.add_argument('email')
parser.add_argument('contract_id', type=int)
data = parser.parse_args(strict=True)
agreement = Agreement.query.get_or_404(agreement_id)
agreement.name = data.get('name', None) or agreement.name
agreement.email = data.get('email', None) or agreement.email
db.session.commit()
except Exception as e:
abort(400, message=str(e))
return serialize_agreement(agreement), 201
class Random(Resource):
def get(self):
contracts = Contract.query.from_statement(
'SELECT contract.*, COUNT(agreement.contract_id) as uses FROM contract LEFT JOIN agreement ON agreement.contract_id=contract.id GROUP BY contract.id HAVING contract.usage_limit>uses OR contract.usage_limit IS NULL'
).all()
contract = self.randomize(contracts)
return serialize_contract(contract)
@staticmethod
def randomize(contracts):
ids = map(lambda c: c.id, contracts)
random_idx = random.randint(0, len(ids) - 1)
return contracts[random_idx]