This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
124 lines (100 loc) · 3.88 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
from flask import Flask,render_template,jsonify,request
from flask.ext.pymongo import PyMongo
from bson import json_util
import re
import json
app = Flask(__name__,static_url_path='')
app.config['MONGO_DBNAME'] = 'test' #set mongo database name here
app.config['MONGO_URI'] = 'mongodb://localhost:27017/test' # set mongo connection string
mongo = PyMongo(app)
def splitdata(obj,limit):
splitlist = [list(i) for i in zip(*[iter(obj)]*limit)]
splitlist.append(obj[len(splitlist)*limit:])
return splitlist
def toJson(data):
return json.dumps(data, default=json_util.default)
@app.route('/')
def root():
return app.send_static_file('index.html')
@app.route('/ngoname/<ngoname>')
def ngoname(ngoname):
ngo = mongo.db.ngodata.find({"name":{'$regex': re.compile(r'\b{}\b'.format(ngoname),re.IGNORECASE) ,"$options": "-i"}},{'_id': False})
page = request.args.get('page')
limit = request.args.get('limit')
obj = [x for x in ngo]
if page is None or limit is None:
return toJson(obj)
else:
res = splitdata(obj,int(limit))
for i in range(len(res)):
res[i] = [{"_metadata":{"page":page,"page_count":str(len(res)),"limit":limit}}] + res[i]
try:
return toJson(res[int(page)-1])
except IndexError:
return toJson([])
@app.route('/regno/<regnum>')
def regno(regnum):
ngo = mongo.db.ngodata.find({"regno":{"$regex": regnum}},{'_id': False})
page = request.args.get('page')
limit = request.args.get('limit')
obj = [x for x in ngo]
if page is None or limit is None:
return toJson(obj)
else:
res = splitdata(obj,int(limit))
for i in range(len(res)):
res[i] = [{"_metadata":{"page":page,"page_count":str(len(res)),"limit":limit}}] + res[i]
try:
return toJson(res[int(page)-1])
except IndexError:
return toJson([])
@app.route('/city/<cityname>')
def city(cityname):
ngo = mongo.db.ngodata.find({"city":{"$regex": re.compile(r'\b{}\b'.format(cityname),re.IGNORECASE) ,"$options": "-i"}},{'_id': False})
page = request.args.get('page')
limit = request.args.get('limit')
obj = [x for x in ngo]
if page is None or limit is None:
return toJson(obj)
else:
res = splitdata(obj,int(limit))
for i in range(len(res)):
res[i] = [{"_metadata":{"page":page,"page_count":str(len(res)),"limit":limit}}] + res[i]
try:
return toJson(res[int(page)-1])
except IndexError:
return toJson([])
@app.route('/state/<statename>')
def state(statename):
ngo = mongo.db.ngodata.find({"state":{"$regex": re.compile(r'\b{}\b'.format(statename),re.IGNORECASE) ,"$options": "-i"}},{'_id': False})
page = request.args.get('page')
limit = request.args.get('limit')
obj = [x for x in ngo]
if page is None or limit is None:
return toJson(obj)
else:
res = splitdata(obj,int(limit))
for i in range(len(res)):
res[i] = [{"_metadata":{"page":page,"page_count":str(len(res)),"limit":limit}}] + res[i]
try:
return toJson(res[int(page)-1])
except IndexError:
return toJson([])
@app.route('/sector/<sectorname>')
def sector(sectorname):
ngo = mongo.db.ngodata.find({'sectors':{'$regex': re.compile(r'\b{}\b'.format(sectorname),re.IGNORECASE) ,"$options": "-i"}},{'_id': False})
page = request.args.get('page')
limit = request.args.get('limit')
obj = [x for x in ngo]
if page is None or limit is None:
return toJson(obj)
else:
res = splitdata(obj,int(limit))
for i in range(len(res)):
res[i] = [{"_metadata":{"page":page,"page_count":str(len(res)),"limit":limit}}] + res[i]
try:
return toJson(res[int(page)-1])
except IndexError:
return toJson([])
if __name__ == '__main__':
app.run(debug=True)