def test_get(self): with app.test_request_context(): r = json.loads(EventREST().get().data) self.assertEquals(len(r), 17) eid = Event.query( Event.watchword == u'Ундервуд в Улан-Удэ' ).get().key.id() r = json.loads(EventREST().get(eid).data) self.assertEquals(r['watchword'], u'Ундервуд в Улан-Удэ')
def get(self, id=None): if id is None: args = request.args count = int(args.get("count", 15)) offset = int(args.get("offset", 0)) d = args.get("date", date.today().strftime(DF)).split("-") dt = date(int(d[0]), int(d[1]), int(d[2])) tagids = args.getlist("tags") if tagids: tagkeys = [ndb.Key("Tag", int(id)) for id in tagids] events = Event.query(ndb.AND(Event.intervals.start_date == dt, Event.tags.IN(tagkeys))).fetch() else: events = Event.query(Event.intervals.start_date == dt).fetch(count + 1, offset=offset) r = [to_dict(e, dt) for e in events] more = len(r) > count # Flag shows there are more results to display res = {"more": more, "events": r[:-1] if more else r} else: res = to_dict(Event.get_by_id(int(id))) return Response(json.dumps(res), mimetype="application/json")
def test_to_dict(self): from backend.views.events import to_dict e = Event.query(Event.watchword == u'Ундервуд в Улан-Удэ').get() self.assertEquals(to_dict(e), { 'item_id': e.key.id(), 'watchword': e.watchword, 'description': e.description, 'intervals': [{ 'start_date': i.start_date.strftime('%Y-%m-%d'), 'start_time': i.start_time.strftime( '%H:%M') if i.start_time is not None else None, 'end_date': i.start_date.strftime( '%Y-%m-%d') if i.end_date is not None else None, 'end_time': i.start_time.strftime( '%H:%M') if i.end_time is not None else None, } for i in e.intervals], 'company': e.company.id(), 'tags': [ c.id() for c in e.tags], });