-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
196 lines (156 loc) · 6.05 KB
/
app.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
# -*- coding: utf-8 -*-
from flask import Flask, render_template, send_from_directory
# from raven.contrib.flask import Sentry
from db import *
from openpyxl import load_workbook
from flask_restful import Resource, Api, abort
from parsers import *
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
sqlitedb = 'sqlite:///' + dir_path + os.sep + "database.db"
app = Flask(__name__)
app.config['BUNDLE_ERRORS'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = sqlitedb
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['ERROR_404_HELP'] = False
app.app_context().push()
database.init_app(app)
database.create_all(app=app)
api = Api(app)
# sentry = Sentry(app, dsn='https://f89968f901484fd48ff3f3a50bf3b912:[email protected]/1223183')
# sentry.init_app(app)
# Application route
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/static/mif/metro.ttf')
def metrottf():
return send_from_directory(os.path.join(app.root_path, 'static'), 'metro.ttf', mimetype='application/octet-stream')
@app.route('/static/mif/metro.woff')
def metrowoff():
return send_from_directory(os.path.join(app.root_path, 'static'), 'metro.woff', mimetype='application/octet-stream')
@app.route('/static/js/metro.min.js.map')
def metromap():
return send_from_directory(os.path.join(app.root_path, 'static/js'), 'metro.min.js.map', mimetype='application/octet-stream')
@app.route("/")
def hello():
data = ""
return render_template('hello.html', data=data)
@app.route("/ipmanagement")
def ipmanagement():
data = ""
return render_template('ipmgt.html', data=data)
@app.route("/setting")
def setting():
data = ""
return render_template('setting.html', data=data)
@app.route("/bulksearch")
def bulksearch():
data = ""
return render_template('bulksearch.html', data=data)
@app.route("/bulkinsert")
def bulkinsert():
data = ""
return render_template('bulkinsert.html', data=data)
def abort_if_pool_id_doesnt_exist(pool_id):
existed = isPoolIdExist(pool_id)
if not existed:
abort(404, message="pool_id {} doesn't exist".format(pool_id))
class IpPoolsList(Resource):
def get(self):
filters = filter_arguments.parse_args()
allIpPool = getAllIpPool(filters)
return allIpPool
def post(self):
data = {}
args = addnew_arguments.parse_args()
data['pool'] = args['pool']
data['site'] = args['site']
data['description'] = args['description']
data['detail'] = args['detail']
data['note'] = args['note']
result = addIpPool(data)
if result[0] == "OK":
return args, 201
else:
abort(400, message=result[1])
class IpPools(Resource):
def get(self, pool_id):
# abort_if_pool_id_doesnt_exist(pool_id)
def Standardized(string):
if string is not None:
return string
else:
return ""
if pool_id == "0":
excelfile = dir_path + os.sep + "IP_Planing_Total.xlsx"
wb = load_workbook(excelfile)
sheet = wb['All']
for row in range(2, 2000):
data = {}
data['pool'] = sheet["A" + str(row)].value
data['site'] = Standardized(sheet["B" + str(row)].value)
data['description'] = Standardized(sheet["C" + str(row)].value)
data['detail'] = Standardized(sheet["D" + str(row)].value)
data['note'] = Standardized(sheet["E" + str(row)].value)
if data['pool'] is not None:
data['pool'] = data['pool'].strip()
result = addIpPool(data)
if result[0] != "OK":
abort(400, message=result[1])
else:
pass
else:
continue
return {"value": result[1], "message": "Success"}, 201
else:
return {"value": "Nothing", "message": "Success"}, 201
def delete(self, pool_id):
abort_if_pool_id_doesnt_exist(pool_id)
result = deleteIPPool(pool_id)
if result[0] == "OK":
return {"value": result[1], "message": "Success"}, 201
else:
abort(400, message=result[1])
def put(self, pool_id):
newdata = {}
args = update_arguments.parse_args()
abort_if_pool_id_doesnt_exist(args["poolid"])
newdata['poolid'] = args['poolid']
newdata['pool'] = args['pool']
newdata['site'] = args['site']
newdata['description'] = args['description']
newdata['detail'] = args['detail']
newdata['note'] = args['note']
result = editIpPool(newdata)
if result[0] == "OK":
return args, 201
else:
abort(400, message=result[1])
class Validate(Resource):
def get(self):
args = validate_arguments.parse_args()
function = args['function']
pool = args['param']
poolid = args['id']
db_poolid = isDuplicateIPv4Network(pool)
if db_poolid is not None:
if db_poolid == int(poolid):
return {"message": "valid", "debug": "this pool %s belong to this record" % pool}, 201
else:
return {"message": "invalid", "debug": "Pool %s is duplicate with record id %s" % (pool, str(db_poolid))}, 201
else:
return {"message": "valid", "debug": "Pool %s do not exist in database" % pool}, 201
class Bulksearchip(Resource):
def get(self):
pass
def post(self):
filters = bulksearch_arguments.parse_args()
result = getResultForBulkSearch(filters)
return result, 201
api.add_resource(IpPoolsList, '/api/v1/ippools')
api.add_resource(IpPools, '/api/v1/ippools/<pool_id>')
api.add_resource(Validate, '/api/v1/validate')
api.add_resource(Bulksearchip, '/api/v1/bulksearch')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)