-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastore_queries.py
49 lines (38 loc) · 1.22 KB
/
datastore_queries.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
from google.cloud import datastore
from datastore_queries_admin import get_configs
def get_config_dictionary():
configs = get_configs()
result = {}
for c in configs:
result[c.key.id_or_name] = c['value']
return result
def get_all_posts(post_type=None, limit=None):
"""
:param post_type: 'post' or 'page'
:param limit: max nr of posts to retrieve
"""
client = datastore.Client()
query = client.query(kind='Post', order=('-date',))
if post_type in ['post', 'page']:
query.add_filter('post_type', '=', post_type)
return query.fetch(limit=limit)
def get_posts_by_archive(year, month):
client = datastore.Client()
query = client.query(kind='Post', order=('-date',))
query.add_filter('year', '=', year)
query.add_filter('month', '=', month)
return query.fetch()
def get_post(slug):
client = datastore.Client()
query = client.query(kind='Post')
query.add_filter('slug', '=', slug)
result = list(query.fetch(1))
if len(result) < 1:
return None
else:
return result[0]
def get_archives():
client = datastore.Client()
query = client.query(kind='Archive')
query.order = ['-year', '-month']
return query.fetch()