-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
41 lines (36 loc) · 1.43 KB
/
database.py
File metadata and controls
41 lines (36 loc) · 1.43 KB
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
from pymongo import MongoClient
from config import Config
import datetime
class Mongo:
def __init__(self):
'''
Establish connection to the MongoDB database.
'''
try:
config = Config()
self.client = MongoClient(host=config.dbhost,
port=config.dbport,
username=config.dbuser,
password=config.dbpass,
authSource=config.dbauth)
self.db = self.client[config.dbname]
except Exception:
raise RuntimeError("Could not connect to database.")
return None
def find_result(self, latitude, longitude):
try:
result = self.db.results.findOne({
"location": {"$nearSphere": {"$geometry": {
"type": "Point", "coordinates": [longitude, latitude]},
"$maxDistance": Config.max_distance_meters}}})
return result.get('result')
except Exception:
return None
def store_result(self, latitude, longitude, result):
try:
self.db.results.insert_one({"location": {
"type": "Point", "coordinates": [longitude, latitude]},
"result": result, "timestamp": datetime.utcnow()})
return True
except Exception:
return False