-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscratchpad.py
97 lines (72 loc) · 2.07 KB
/
scratchpad.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
94
95
96
97
from os.path import dirname, realpath
import sys
import os
import math
import json
ROOT_PATH = dirname(realpath(__file__))
sys.path.append(ROOT_PATH)
os.environ['DJANGO_SETTINGS_MODULE'] = 'wham_gql.settings'
from django.conf import settings
##############################################################################################################################################
# from spotify_wham.models import SpotifyArtist
from test_models.models import Address, Person
from parse_graphql import parse_graphql
# SpotifyArtist.objects.wham_get(pk='234234')
# address = Address.objects.create(street='Sherwood Road', city='Mount Waverley', country='Australia')
# pserson = Person.objects.create(name="Michael", nickname="mike", address=address)
# for a in Address.objects.all():
# print a.pk
def pretty_print(o):
print json.dumps(o, indent=4)
for p in Person.objects.all():
print p.pk
# parse_graphql
pgql = parse_graphql("""
Person(1) {
name,
nickname,
address {
street,
city,
country
}
}
""")
pgql_object = pgql['Person']
id = pgql_object['id']
person = Person.objects.get(pk=id)
def fetch_fields(fields, object):
out = {}
for field in fields:
out[field['field_name']] = fetch_field(field, object)
return out
def fetch_field(field, object):
field_name = field['field_name']
child_fields = field['child_fields']
value = getattr(object, field_name)
if len(child_fields) > 0:
related_object = value
return fetch_fields(child_fields, related_object)
else:
return value
def fetch_object(ast, object):
return fetch_fields(ast['fields'], object)
return out_fields
out = {
id: fetch_object(pgql_object, person)
}
pretty_print(out)
#output:
#{
# "1": {
# "nickname": "mike",
# "name": "Michael",
# "address": {
# "city": "Mount Waverley",
# "street": "Sherwood Road",
# "country": "Australia"
# }
# }
#}
# print out
# print json.dumps(pgql_object, indent=4)