-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrud.py
More file actions
62 lines (44 loc) · 1.74 KB
/
crud.py
File metadata and controls
62 lines (44 loc) · 1.74 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from datetime import datetime
from google.cloud import datastore
#from google.appengine.ext import ndb
class Capital:
def __init__(self):
self.ds = datastore.Client(project="hackathon-team-018")
self.kind = "PythonCapital"
self.data = {}
def get_key(self, id):
return self.ds.key(self.kind, id)
def store_capital(self, uniqueid, obj):
key = self.ds.key(self.kind, uniqueid)
entity = datastore.Entity(key)
entity['id'] = int(uniqueid)
entity['location'] = datastore.Entity(key=self.ds.key('EmbeddedKind'))
entity['location']['latitude'] = obj['location']['latitude']
entity['location']['longitude'] = obj['location']['longitude']
entity['country'] = obj['country']
entity['name'] = obj['name']
entity['countryCode'] = obj['countryCode']
entity['continent'] = obj['continent']
key = self.ds.put(entity)
self.data[id] = key
return key
def fetch_capitals(self):
query = self.ds.query(kind=self.kind)
query.order = ['country']
return self.get_query_results(query)
def fetch_capitals_query(self, prop, val):
query = self.ds.query(kind=self.kind)
query.add_filter(prop, "=", val)
return self.get_query_results(query)
def get_query_results(self, query):
results = list()
for entity in list(query.fetch()):
results.append(dict(entity))
return results
def get_capital(self, id):
query = self.ds.query(kind=self.kind)
query.add_filter('id', '=', int(id))
return self.get_query_results(query)
def delete_capital(self, id):
key = self.ds.key(self.kind, id)
self.ds.delete(key)