forked from Justdwiwt/XUEMC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
180 lines (138 loc) · 7.35 KB
/
run.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#-*- coding: utf-8 -*-
from web.app import app, api
from Utils import Util, ProtocolItem
import json, threading, datetime
from DB import orm
from Logic import logic, restful
from flask import Flask, request
from flask_restful import Resource, Api
import flask.ext.restless
from flask.ext.restless import ProcessingException
from web import views
def post_get_one(result=None, **kw):
if result:
logic.SetDefaultImage(result)
return result
def post_get_many(result=None, search_params=None, **kw):
if result and result.has_key(restful.ITEM_OBJECTS):
for obj in result[restful.ITEM_OBJECTS]:
logic.SetDefaultImage(obj)
return result
def backup_pre_put_single_account(instance_id=None, data=None, **kw):
if instance_id is None: return
account = orm.Account.query.filter_by(username=instance_id).first()
if account is None:
account = orm.Account(instance_id, None, None, None,0,0,None)
orm.db.session.add(account)
orm.db.session.commit()
if data.has_key(restful.ITEM_CODE):
terminal = orm.Terminal.query.filter_by(code=data[restful.ITEM_CODE]).first()
if terminal is None:
terminal = orm.Terminal(account.id,data.get(restful.ITEM_OS), data[restful.ITEM_CODE])
orm.db.session.add(terminal)
terminal.account_id = account.id
terminal.os = data.get(restful.ITEM_OS)
orm.db.session.commit()
data.pop(restful.ITEM_CODE,None)
data.pop(restful.ITEM_OS,None)
def pre_put_single_account(instance_id=None, data=None, **kw):
if instance_id is None: return
account = orm.Account.query.get(int(instance_id))
if account is None:
return
if data.has_key(restful.ITEM_CHECKCODE):
dtNow = datetime.datetime.now()
dtValidTime = account.dtcreate if account.dtcreate else datetime.datetime.now()
dtValidTime = dtValidTime + datetime.timedelta(minutes=15)
if account.checkcode == data.get(restful.ITEM_CHECKCODE) and dtNow < dtValidTime:
account.flag_telephone = 1
orm.db.session.commit()
if data.has_key(restful.ITEM_TELEPHONE):
if account.telephone!=data.get(restful.ITEM_TELEPHONE):
account.flag_telephone = 0
orm.db.session.commit()
data.pop(restful.ITEM_FLAG_TELEPHONE,None)
data.pop(restful.ITEM_CHECKCODE,None)
def pre_post_account(data=None, **kw):
"""Accepts a single argument, `data`, which is the dictionary of
fields to set on the new instance of the model.
"""
if not data.has_key(restful.ITEM_USERNAME):
data[restful.ITEM_USERNAME] = data.get(restful.ITEM_TELEPHONE)
data[restful.ITEM_DTCREATE]=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
pass
import random
def post_post_account(result=None, **kw):
"""Accepts a single argument, `result`, which is the dictionary
representation of the created instance of the model.
"""
if result and result.has_key(restful.ITEM_ID):
account = orm.Account.query.get(int(result.get(restful.ITEM_ID)))
account.checkcode = str(random.randint(100001,999999))
orm.db.session.commit()
#send sms verification here
message = '您的验证码为%s, 请勿告诉他人,15分钟有效 【学莫愁】' % account.checkcode
Util.SendSMSByZA(account.telephone, message)
return result
pass
def pre_post_test(data=None, **kw):
if not data.has_key(restful.ITEM_USERNAME):
data[restful.ITEM_USERNAME] = data.get(restful.ITEM_TELEPHONE)
pass
class Account(Resource):
def post(self):
body = json.loads(request.data)
account = orm.Account.query.filter_by(telephone= body.get(restful.ITEM_TELEPHONE)).first()
if account:
#send sms verification here
if account.flag_telephone != 1:
account.dtcreate = datetime.datetime.now()
account.checkcode = str(random.randint(100001,999999))
orm.db.session.commit()
message = '您的验证码为%s, 请勿告诉他人,15分钟有效 【学莫愁】' % account.checkcode
Util.SendSMSByZA(account.telephone, message)
dtcreate = account.dtcreate.strftime("%Y-%m-%dT%H:%M:%S") if account.dtcreate else None
return {restful.ITEM_ID:account.id,
restful.ITEM_FLAG_TELEPHONE:account.flag_telephone,
restful.ITEM_TELEPHONE:account.telephone,
restful.ITEM_USERNAME:account.username,
restful.ITEM_NAME:account.name,
restful.ITEM_DTCREATE: dtcreate,
restful.ITEM_SOURCE:account.source}
else:
return restful.PostAccount(body)
return {'hello': 'world'}
api.add_resource(Account, '/bd/api/v1.0/account')
# Create the Flask-Restless API manager.
orm.db.create_all()
manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db=orm.db)
# Create API endpoints, which will be available at /api/<tablename> by
# default. Allowed HTTP methods can be specified as well.
manager.create_api(orm.Advert, methods=['GET'], url_prefix='/bd/api/v1.0')
manager.create_api(orm.Agespan, methods=['GET'], url_prefix='/bd/api/v1.0')
manager.create_api(orm.Area, methods=['GET'], url_prefix='/bd/api/v1.0')
manager.create_api(orm.Bulletin, methods=['GET'], url_prefix='/bd/api/v1.0', postprocessors={'GET_SINGLE': [post_get_one],'GET_MANY':[post_get_many]})
manager.create_api(orm.Feature, methods=['GET'], url_prefix='/bd/api/v1.0')
manager.create_api(orm.Feetype, methods=['GET'], url_prefix='/bd/api/v1.0')
manager.create_api(orm.Institution, methods=['GET'], url_prefix='/bd/api/v1.0', postprocessors={'GET_SINGLE': [post_get_one],'GET_MANY':[post_get_many]})
manager.create_api(orm.InstitutionFeature, methods=['GET'], url_prefix='/bd/api/v1.0')
manager.create_api(orm.School, results_per_page = 7, methods=['GET'], url_prefix='/bd/api/v1.0', postprocessors={'GET_SINGLE': [post_get_one],'GET_MANY':[post_get_many]})
manager.create_api(orm.SchoolFeature, methods=['GET'], url_prefix='/bd/api/v1.0')
manager.create_api(orm.Schooltype, methods=['GET'], url_prefix='/bd/api/v1.0')
manager.create_api(orm.Account, methods=['GET', 'PUT', 'PATCH'], url_prefix='/bd/api/v1.0', preprocessors={'PATCH_SINGLE':[pre_put_single_account]}, exclude_columns=['checkcode','password'])
manager.create_api(orm.Account, methods=['POST'], url_prefix='/bd/api/v1.0/back', preprocessors={'POST':[pre_post_account]},postprocessors={'POST':[post_post_account]}, exclude_columns=['checkcode','password'])
manager.create_api(orm.Test, methods=['POST', 'PATCH'], preprocessors={'POST':[pre_post_test]}, url_prefix='/bd/api/v1.0')
class Messages(Resource):
def post(self):
pass
print "POST a Message:", request.data,"--"
body = json.loads(request.data)
if body[ProtocolItem.MESSAGES][ProtocolItem.DEST_TYPE] == ProtocolItem.VALUE_IOS:
threading.Thread(target=Util.push_ios,args=([body[ProtocolItem.MESSAGES][ProtocolItem.DEST_ID]], "alarm", body[ProtocolItem.MESSAGES][ProtocolItem.CONTENT])).start()
# Util.push_ios([body[ProtocolItem.MESSAGES][ProtocolItem.DEST_ID]], 'alarm', body[ProtocolItem.MESSAGES][ProtocolItem.CONTENT])
return {'_id':'0'}
def get(self):
return {'get':'None'}
api.add_resource(Messages, '/bd/api/messages')
if __name__ == '__main__':
app.run(debug=True,port= 5001)