Newer
Older
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
import RegularExpressionParser.Parser as parser
import DataBase.mongoDB as db
import re
from flask import Flask
from flask import request
from urllib.parse import urlparse, parse_qs
app = Flask(__name__)
app.config["DEBUG"] = True
@app.route("/", methods=['GET'])
def home():
return "200: successfully connected to home page\n"
@app.route("/api/<collection>/", methods=["GET", "PUT", "POST", "DELETE"])
def data(collection):
if request.method == "GET":
if collection == "books" or collection == "book":
identifier = request.args.to_dict()
print(identifier)
print(type(identifier))
return "200: find"
elif collection == "authors" or collection == "author":
print(request.url)
return "200: find"
elif collection == "search":
url_parsed = urlparse(request.url)
qs_parsed = parse_qs(url_parsed.query)
return search_document(qs_parsed["q"][0].split("&"))
else:
return "404 not found"
# elif request.method == "PUT":
def search_document(identifiers):
if len(identifiers) == 1:
json_idt = parser.parse_query_to_json(identifiers[0])
if re.search("^book.*", identifiers[0]):
return db.get_documents_json(0, json_idt)
else:
return db.get_documents_json(1, json_idt)
elif len(identifiers) == 3:
if re.search("^book.*", identifiers[0]):
if re.search("^author.*", identifiers[2]):
print("Failed to find documentation: two statements are not pointing to the same collection")
return {}
else:
opt = 0
else:
if re.search("^book.*",identifiers[2]):
print("Failed to find documentation: two statements are not pointing to the same collection")
return {}
else:
opt = 1
json_idt1 = parser.parse_query_to_json(identifiers[0])
json_idt2 = parser.parse_query_to_json(identifiers[2])
if identifiers[1] == "AND":
exp = {"$and": [json_idt1, json_idt2]}
elif identifiers[1] == "OR":
exp = {"$or": [json_idt1,json_idt2]}
else:
print("Failed to parse query: unknown operator for identifiers[1]")
return {}
return db.get_documents_json(opt, exp)
else:
return "Error, unknown identifiers"
if __name__ == "__main__":
app.run()