def context(self, user, **kw): request_handler = kw.get('request_handler') if not request_handler.request.body: return self.render_to_json( { 'status': 'error', 'msg': "Context Message is Empty." }, request_handler) data = simplejson.loads(request_handler.request.body) profile = user.get_profile(user_id=user.id) tokens = simplejson.loads(profile.tokens) for app_name in data['application']: application = Application().get_by(name=app_name) if application: #TODO - verificar se a aplicacao assina o contexto for description in data['context'].keys(): context_type = ContextType().get_by( description=description) if context_type: context = Context() context.user_id = user.id context.context_type_id = context_type.id context.context = data['context'][description] context.updated = datetime.now() context.save() context_application = ContextApplication() context_application.application = application context_application.context = context context_application.save() else: return self.render_to_json( { 'status': 'error', 'msg': "Context Not Sent. Invalid context-type." }, request_handler) ContextQueue().add(application.name, data['context'], tokens[app_name], application.callback_url) else: return self.render_to_json( { 'status': 'error', 'msg': "Context Not Sent. Application not registered." }, request_handler) return self.render_to_json({ 'status': 'ok', 'msg': "Context Sent." }, request_handler)
def test_send_context(self): user = create_logged_user(username='******') app1 = create_application(name="twitter") text = ''.join([random.choice(string.letters) for x in xrange(5)]) parameters = { 'application': [app1.name], 'context': { 'location': '-22.95835442222223,-43.196200622222214', 'status': text } } request = HTTPRequest( url=self.get_url('/context?auth=should-be-user-auth'), method='POST', body=simplejson.dumps(parameters)) self.http_client.fetch(request, self.stop) response = self.wait() self.failIf(response.error) assert simplejson.loads(response.body)['status'] == 'ok' context1 = Context().fetch_by(context=text).first() context2 = Context().fetch_by( context='-22.95835442222223,-43.196200622222214').first() context_applicaton1 = ContextApplication.fetch_by( context_id=context1.id).first() context_applicaton2 = ContextApplication.fetch_by( context_id=context2.id).first() assert context1.context == text assert context2.context == '-22.95835442222223,-43.196200622222214' assert context_applicaton1.context_id == context1.id assert context_applicaton2.context_id == context2.id context_applicaton1.delete() context_applicaton2.delete() context1.delete() context2.delete() user.delete() app1.delete()
def context(self, user, **kw): request_handler = kw.get('request_handler') if not request_handler.request.body: return self.render_to_json({'status': 'error', 'msg': "Context Message is Empty."}, request_handler) data = simplejson.loads(request_handler.request.body) profile = user.get_profile(user_id=user.id) tokens = simplejson.loads(profile.tokens) for app_name in data['application']: application = Application().get_by(name=app_name) if application: #TODO - verificar se a aplicacao assina o contexto for description in data['context'].keys(): context_type = ContextType().get_by(description=description) if context_type: context = Context() context.user_id = user.id context.context_type_id = context_type.id context.context = data['context'][description] context.updated = datetime.now() context.save() context_application = ContextApplication() context_application.application = application context_application.context = context context_application.save() else: return self.render_to_json({'status': 'error', 'msg': "Context Not Sent. Invalid context-type."}, request_handler) ContextQueue().add(application.name, data['context'], tokens[app_name], application.callback_url) else: return self.render_to_json({'status': 'error', 'msg': "Context Not Sent. Application not registered."}, request_handler) return self.render_to_json({'status': 'ok', 'msg': "Context Sent."}, request_handler)
def test_send_context(self): user = create_logged_user(username='******') app1 = create_application(name="twitter") text = ''.join([random.choice(string.letters) for x in xrange(5)]) parameters = {'application': [app1.name], 'context': {'location': '-22.95835442222223,-43.196200622222214','status': text}} request = HTTPRequest(url=self.get_url('/context?auth=should-be-user-auth'), method='POST',body=simplejson.dumps(parameters)) self.http_client.fetch(request, self.stop) response = self.wait() self.failIf(response.error) assert simplejson.loads(response.body)['status'] == 'ok' context1 = Context().fetch_by(context=text).first() context2 = Context().fetch_by(context='-22.95835442222223,-43.196200622222214').first() context_applicaton1 = ContextApplication.fetch_by(context_id=context1.id).first() context_applicaton2 = ContextApplication.fetch_by(context_id=context2.id).first() assert context1.context == text assert context2.context == '-22.95835442222223,-43.196200622222214' assert context_applicaton1.context_id == context1.id assert context_applicaton2.context_id == context2.id context_applicaton1.delete() context_applicaton2.delete() context1.delete() context2.delete() user.delete() app1.delete()