-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
93 lines (74 loc) · 2.66 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
from pymongo import MongoClient
from bson.objectid import ObjectId
from flask import Flask,render_template,jsonify,json,request
app = Flask(__name__, static_url_path='/static')
app.debug = True
#this is test
client = MongoClient('localhost:27017')
@app.route("/")
def home():
return app.send_static_file("index.html")
@app.route("/data/<name>/<id>")
def details(name,id):
collectionname = name.split('.',1)[1]
databasename =name.split('.',1)[0]
try:
objs = client[databasename][collectionname].find({"_id" : ObjectId(id)})
except Exception as e:
try:
if id.isdigit():
objs = client[databasename][collectionname].find({"_id" : int(id)})
elif id.replace('.','',1).isdigit():
objs = client[databasename][collectionname].find({"_id" : float(id)})
else:
objs = client[databasename][collectionname].find({"_id" : id})
except Exception as e:
return str(e)
return jsonify(details=prepareResponse(objs))
@app.route("/menu")
def menu():
d = dict((db, [db + '.' + collection for collection in client[db].collection_names()])
for db in client.database_names())
return jsonify(list=d)
#test
@app.route("/add/collection/<name>")
def addCollection(name):
collectionname = name.split('.',1)[1]
databasename =name.split('.',1)[0]
post = {}
d = client[databasename][collectionname].insert_one(post).inserted_id
client[databasename][collectionname].createIndex( { "$**": "text" } )
return jsonify(result=prepareResponse(d))
def prepareResponse(objs):
objList = []
try:
for obj in objs:
objItem = {}
for n in obj:
if isinstance(n, ObjectId):
objItem[n] = obj[n]
else:
#objItem["Action"] = "<a href='#' onClick='openRecord(\"" + str(obj[n]) + "\")' class='glyphicon glyphicon-chevron-right'></a>"
objItem[n] = str(obj[n])
objList.append(objItem)
except Exception as e:
1
return objList
@app.route("/data/<name>")
def list(name):
collectionname = name.split('.',1)[1]
databasename =name.split('.',1)[0]
pageno = request.args.get('pageno')
if pageno =='':
pageno = 0
q = request.args.get('q')
query= {}
if len(q)>0:
query = { '$text': { '$search': q, '$caseSensitive': True } }
try:
objs = client[databasename][collectionname].find(query).skip(int(pageno)*10).limit (10)
except Exception as e:
return str(e)
return jsonify(list=prepareResponse(objs))
if __name__ == "__main__":
app.run()