forked from sloopstash/kickstart-flask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.py
219 lines (200 loc) · 8.52 KB
/
init.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# Import community modules.
import sys
import json
from elasticapm.contrib.flask import ElasticAPM
from flask import Flask,request,render_template
from werkzeug.exceptions import NotFound,BadRequest
import argparse
from flask import jsonify,render_template,redirect
# Append App specific Python paths.
sys.path.append('includes')
sys.path.append('core')
# Import custom modules.
from database import redis
from config import app_conf, redis_conf
# Intialize App.
app = Flask('CRM App',template_folder='templates')
app.config['ELASTIC_APM'] = {
# Set required service name. Allowed characters:
# a-z, A-Z, 0-9, -, _, and space
'SERVICE_NAME': 'CRM App',
# Use if APM Server requires a token
'SECRET_TOKEN': '',
# Set custom APM Server URL (default: http://localhost:8200)
'SERVER_URL': 'http://elk-apm:8200',
'DEBUG': True
}
apm = ElasticAPM(app,logging=True)
@app.route('/health',methods=['GET'])
def health():
return 'OK'
@app.route('/dashboard',methods=['GET'])
def home():
vars = {}
vars['environment'] = app_conf['environment']
vars['cdn'] = app_conf['cdn']
vars['accounts'] = redis.engine.hgetall(redis_conf['key_prefix']['account'])
for key,value in vars['accounts'].items():
vars['accounts'][key] = json.loads(value)
return render_template('dashboard.html',vars=vars)
@app.route('/account/create',methods=['GET','POST'])
def account_create():
vars = {}
vars['environment'] = app_conf['environment']
vars['cdn'] = app_conf['cdn']
if request.method=='GET':
return render_template('account/create.html',vars=vars)
elif request.method=='POST':
data = {}
data['firstname'] = request.form['firstname']
data['lastname'] = request.form['lastname']
data['organization'] = request.form['organization']
data['email'] = request.form['email']
data['phone'] = request.form['phone']
id = redis.engine.incr(redis_conf['key_prefix']['account_counter'])
response = redis.engine.hset(redis_conf['key_prefix']['account'],id,json.dumps(data))
if response:
return redirect('/dashboard')
else:
vars['message'] = 'Failure'
return render_template('account/create.html',vars=vars)
@app.route('/account/<id>/update',methods=['GET','POST'])
def account_update(id):
vars = {}
vars['environment'] = app_conf['environment']
vars['cdn'] = app_conf['cdn']
if request.method=='GET':
vars['account'] = json.loads(redis.engine.hget(redis_conf['key_prefix']['account'],id))
vars['account']['id'] = id
return render_template('account/update.html',vars=vars)
elif request.method=='POST':
data = {}
data['firstname'] = request.form['firstname']
data['lastname'] = request.form['lastname']
data['organization'] = request.form['organization']
data['email'] = request.form['email']
data['phone'] = request.form['phone']
response = redis.engine.hset(redis_conf['key_prefix']['account'],id,json.dumps(data))
return redirect('/dashboard')
@app.route('/account/<id>/view',methods=['GET'])
def account_view(id):
vars = {}
vars['environment'] = app_conf['environment']
vars['cdn'] = app_conf['cdn']
vars['account'] = json.loads(redis.engine.hget(redis_conf['key_prefix']['account'],id))
vars['account']['id'] = id
return render_template('account/view.html',vars=vars)
@app.route('/account/<act_id>/contacts',methods=['GET'])
def contact_view(act_id):
vars = {}
vars['environment'] = app_conf['environment']
vars['cdn'] = app_conf['cdn']
vars['account'] = json.loads(redis.engine.hget(redis_conf['key_prefix']['account'],act_id))
vars['account']['id'] = act_id
vars['contacts'] = redis.engine.hgetall(redis_conf['key_prefix']['contact']+':'+str(act_id))
for key,value in vars['contacts'].items():
vars['contacts'][key] = json.loads(value)
return render_template('contact/view.html',Active='contacts',vars=vars)
@app.route('/account/<act_id>/contact/create',methods=['GET','POST'])
def contact_create(act_id):
vars = {}
vars['environment'] = app_conf['environment']
vars['cdn'] = app_conf['cdn']
if request.method=='GET':
vars['account'] = json.loads(redis.engine.hget(redis_conf['key_prefix']['account'],act_id))
vars['account']['id'] = act_id
return render_template('contact/create.html',Active='contacts',vars=vars)
elif request.method=='POST':
data = {}
data['email'] = request.form['email']
data['phone'] = request.form['phone']
data['firstname'] = request.form['firstname']
data['lastname'] = request.form['lastname']
id = redis.engine.incr(redis_conf['key_prefix']['contact_counter']+':'+str(act_id))
response = redis.engine.hset(redis_conf['key_prefix']['contact']+':'+str(act_id),id,json.dumps(data))
if response:
return redirect('/account/'+act_id+'/contacts')
else:
vars['message'] = 'Failure'
return render_template('contact/create.html',Active='contacts',vars=vars)
@app.route('/account/<act_id>/contact/<id>/update',methods=['GET','POST'])
def contact_update(act_id,id):
vars = {}
vars['environment'] = app_conf['environment']
vars['cdn'] = app_conf['cdn']
if request.method=='GET':
vars['account'] = json.loads(redis.engine.hget(redis_conf['key_prefix']['account'],act_id))
vars['account']['id'] = act_id
vars['contact'] = json.loads(redis.engine.hget(redis_conf['key_prefix']['contact']+':'+str(act_id),id))
vars['contact']['id'] = id
return render_template('contact/update.html',Active='contacts',vars=vars)
elif request.method=='POST':
data = {}
data['email'] = request.form['email']
data['phone'] = request.form['phone']
data['firstname'] = request.form['firstname']
data['lastname'] = request.form['lastname']
response = redis.engine.hset(redis_conf['key_prefix']['contact']+':'+str(act_id),id,json.dumps(data))
return redirect('/account/'+act_id+'/contacts')
@app.route('/account/<act_id>/leads',methods=['GET'])
def lead_view(act_id):
vars = {}
vars['environment'] = app_conf['environment']
vars['cdn'] = app_conf['cdn']
vars['account'] = json.loads(redis.engine.hget(redis_conf['key_prefix']['account'],act_id))
vars['account']['id'] = act_id
vars['leads'] = redis.engine.hgetall(redis_conf['key_prefix']['lead']+':'+str(act_id))
for key,value in vars['leads'].items():
vars['leads'][key] = json.loads(value)
return render_template('lead/view.html',Active='leads',vars=vars)
@app.route('/account/<act_id>/lead/create',methods=['GET','POST'])
def lead_create(act_id):
vars = {}
vars['environment'] = app_conf['environment']
vars['cdn'] = app_conf['cdn']
if request.method=='GET':
vars['account'] = json.loads(redis.engine.hget(redis_conf['key_prefix']['account'],act_id))
vars['account']['id'] = act_id
return render_template('lead/create.html',Active='leads',vars=vars)
elif request.method=='POST':
data = {}
data['email'] = request.form['email']
data['phone'] = request.form['phone']
data['firstname'] = request.form['firstname']
data['lastname'] = request.form['lastname']
id = redis.engine.incr(redis_conf['key_prefix']['lead_counter']+':'+str(act_id))
response = redis.engine.hset(redis_conf['key_prefix']['lead']+':'+str(act_id),id,json.dumps(data))
if response:
return redirect('/account/'+act_id+'/leads')
else:
vars['message'] = 'Failure'
return render_template('lead/create.html',Active='leads',vars=vars)
@app.route('/account/<act_id>/lead/<id>/update',methods=['GET','POST'])
def lead_update(act_id,id):
vars = {}
vars['environment'] = app_conf['environment']
vars['cdn'] = app_conf['cdn']
if request.method=='GET':
vars['account'] = json.loads(redis.engine.hget(redis_conf['key_prefix']['account'],act_id))
vars['account']['id'] = act_id
vars['lead'] = json.loads(redis.engine.hget(redis_conf['key_prefix']['lead']+':'+str(act_id),id))
vars['lead']['id'] = id
return render_template('lead/update.html',Active='leads',vars=vars)
elif request.method=='POST':
data = {}
data['email'] = request.form['email']
data['phone'] = request.form['phone']
data['firstname'] = request.form['firstname']
data['lastname'] = request.form['lastname']
response = redis.engine.hset(redis_conf['key_prefix']['lead']+':'+str(act_id),id,json.dumps(data))
return redirect('/account/'+act_id+'/leads')
if __name__ == '__main__':
try:
parser = argparse.ArgumentParser()
parser.add_argument('--port','-P',type=int,default=5000)
parser.add_argument('--host','-H',default='0.0.0.0')
args = parser.parse_args()
print('starting app')
app.run(debug=False,host=args.host,port=args.port)
except KeyboardInterrupt:
print('stopping app')