def test_jsonDictAllHP(self): p = Proposal.objects.all()[0] pa = ProposalHttpAdapter(proposal = p) # here's what the json for this session looks like when # we use the ORM ormJson = pa.jsonDict() # here's what the json for this session looks like when # we use the high performance query of the DB sqlJsons = pa.jsonDictAllHP() self.assertEqual(1, len(sqlJsons)) sqlJson = sqlJsons[0] #keys = ormJson.keys() #print "ORM fields: " #for k in sorted(keys): # if k != 'abstract': # print k, " : ", ormJson[k] #print "********************" #print "SQL fields: " #keys = sqlJson.keys() #for k in sorted(keys): # if k != 'abstract': # print k, " : ", sqlJson[k] # still some issues w/ time accounting to figure out #self.assertEqual(ormJson, sqlJson) fields = sorted(ormJson.keys()) ignoreFields = [] for f in fields: if f not in ignoreFields: self.assertEqual(ormJson.get(f) , sqlJson.get(f))
def test_from_post(self): self.assertEqual(1, len(Proposal.objects.all())) p = Proposal.objects.all()[0] pa = ProposalHttpAdapter(proposal = p) ormJson = pa.jsonDict() ormJson['pcode'] = u'GBT12A-003' pa.initFromPost(ormJson) self.assertEqual(2, len(Proposal.objects.all()))
def test_copy(self): proposal = Proposal.objects.get(pcode = 'GBT12A-002') # Fix up test session for s in proposal.session_set.all(): n = SessionNextSemester() n.save() s.next_semester = n s.save() adapter = ProposalHttpAdapter(proposal) new_proposal = adapter.copy('GBT12A-003') self.assertTrue(new_proposal.source_set.all() > 0) self.assertTrue(new_proposal.session_set.all() > 0)
def read(self, request, *args, **kws): if len(args) == 1: pcode, = args adapter = ProposalHttpAdapter(Proposal.objects.get(pcode = pcode)) return HttpResponse(json.dumps(adapter.jsonDict()) , content_type = 'application/json') else: # Keep this here for when we start doing pagenation """ start = request.GET.get('start', 0) limit = request.GET.get('limit', 25) end = int(start) + int(limit) """ propJson = ProposalHttpAdapter.jsonDictAllHP() return HttpResponse(json.dumps({"success" : "ok" , "proposals" : propJson , "total" : len(propJson) }) , content_type = 'application/json')
def update(self, request, *args, **kws): pcode, = args adapter = ProposalHttpAdapter(Proposal.objects.get(pcode = pcode)) adapter.updateFromPost(json.loads(request.raw_post_data)) return HttpResponse(json.dumps({"success" : "ok"}) , content_type = 'application/json')