def get(self, id=None): if id is None: refs = Ref.query(Ref.level == 1).fetch() res = [to_dict(r, False) for r in refs] else: #Id may have int type for automatically assigned keys #and str type for explicity created keys such as for Root Ref id = int(id) if re.match(r'\d+',id) else id r = Ref.get_by_id(id) res = to_dict(r) if r is not None else None return Response(json.dumps(res), mimetype='application/json')
def setUp(self): super(RefTestCase, self).setUp() #0 level profs = Ref(name=u'Professions', description=u'Contains grouped professions') self.profs_k = profs.put() #1 it = Ref(name=u'IT', code='100E') it.set_parent(profs) self.it_k = it.put() #2 web_programmist = Ref(name=u'Web programmist', code='101B', skills=['Python', 'Javascript', 'HTML'], is_leaf=True) web_programmist.set_parent(it) self.web_programmist_k = web_programmist.put() system_programmist = Ref(name=u'System programmist', code='101B', skills=['C', 'C++', 'GTK'], is_leaf=True) system_programmist.set_parent(it) self.system_programmist_k = system_programmist.put() #1 edu = Ref(name=u'Education', code='200C') edu.set_parent(profs) self.edu_k = edu.put()
def fill_datastore(self): # 0 level self.r_k = root_key = ndb.Key(Ref, "root") root_ref = Ref(key=root_key, name="Refs", is_group=True) root_ref.put() # 1 profs = Ref( name=u"Professions", description=u"Contains grouped professions", is_group=True, structure=[{"name": "code", "type": "string"}, {"name": "description", "type": "text"}], ) profs.set_parent(root_ref) self.profs_k = profs.put() # 2 it = Ref(name=u"IT", code="100E", is_group=True) it.set_parent(profs) self.it_k = it.put() # 3 web_programmist = Ref(name=u"Web programmist", code="101B", skills=["Python", "Javascript", "HTML"]) web_programmist.set_parent(it) self.wp_k = web_programmist.put() system_programmist = Ref(name=u"System programmist", code="101B", skills=["C", "C++", "GTK"]) system_programmist.set_parent(it) self.sp_k = system_programmist.put() # 2 edu = Ref(name=u"Education", code="200C", is_group=True) edu.set_parent(profs) edu.put()