From ccdb20eda714247b617316696175e69549d2526d Mon Sep 17 00:00:00 2001 From: Juan Madurga Date: Tue, 15 Mar 2016 12:38:59 +0100 Subject: [PATCH] add telegram Update to context. Useful for chat and user id to respond in some use cases --- microbot/models/handler.py | 3 ++- microbot/models/telegram_api.py | 18 +++++++++++++++++- tests/test_microbot.py | 24 +++++++++++++++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/microbot/models/handler.py b/microbot/models/handler.py index 89dc093..ecbd4cb 100644 --- a/microbot/models/handler.py +++ b/microbot/models/handler.py @@ -122,7 +122,8 @@ def process(self, bot, update, **url_context): for env_var in bot.env_vars.all(): env.update(env_var.as_json()) context = {'url': url_context, - 'env': env} + 'env': env, + 'update': update.to_dict()} r = self.request.process(**context) logger.debug("Handler %s get request %s" % (self, r)) response_text_template = Template(self.response_text_template) diff --git a/microbot/models/telegram_api.py b/microbot/models/telegram_api.py index d3423a9..729084d 100644 --- a/microbot/models/telegram_api.py +++ b/microbot/models/telegram_api.py @@ -2,6 +2,7 @@ from django.db import models from django.utils.encoding import python_2_unicode_compatible from django.utils.translation import ugettext_lazy as _ +from django.forms.models import model_to_dict @python_2_unicode_compatible class User(models.Model): @@ -16,6 +17,9 @@ class Meta: def __str__(self): return "%s" % self.first_name + + def to_dict(self): + return model_to_dict(self) @python_2_unicode_compatible class Chat(models.Model): @@ -42,6 +46,9 @@ class Meta: def __str__(self): return "%s" % (self.title or self.username) + + def to_dict(self): + return model_to_dict(self) @python_2_unicode_compatible class Message(models.Model): @@ -63,6 +70,12 @@ class Meta: def __str__(self): return "(%s,%s)" % (self.from_user, self.text or '(no text)') + def to_dict(self): + message_dict = model_to_dict(self, exclude=['from_user', 'chat']) + message_dict.update({'from_user': self.from_user.to_dict(), + 'chat': self.chat.to_dict()}) + return message_dict + class Update(models.Model): update_id = models.BigIntegerField(_('Id'), primary_key=True) @@ -74,4 +87,7 @@ class Meta: verbose_name_plural = 'Updates' def __str__(self): - return "%s" % self.update_id + return "%s" % self.update_id + + def to_dict(self): + return {'update_id': self.update_id, 'message': self.message.to_dict()} diff --git a/tests/test_microbot.py b/tests/test_microbot.py index e357348..3af05ee 100644 --- a/tests/test_microbot.py +++ b/tests/test_microbot.py @@ -169,6 +169,13 @@ class TestRequests(LiveServerTestCase, testcases.BaseTestBot): } } + update_as_part_of_context = {'in': '/authors@1', + 'out': {'parse_mode': 'HTML', + 'reply_markup': '', + 'text': 'author2 updated by first_name_' + } + } + def test_get_request(self): Author.objects.create(name="author1") self.request = factories.RequestFactory(url_template=self.live_server_url + '/api/authors/', @@ -352,4 +359,19 @@ def test_put_data_template(self): response_text_template='{{response.name}} updated', response_keyboard_template='') self._test_message(self.author_put_data_template) - self.assertEqual(Author.objects.all()[0].name, 'author2') \ No newline at end of file + self.assertEqual(Author.objects.all()[0].name, 'author2') + + def test_update_as_part_of_context(self): + Author.objects.create(name="author1") + self.request = factories.RequestFactory(url_template=self.live_server_url + '/api/authors/{{url.id}}/', + method=Request.PUT, + data='{"name": "author2"}') + self.handler = factories.HandlerFactory(bot=self.bot, + pattern='/authors@(?P\d+)', + request=self.request, + response_text_template='{{response.name}} updated by {{update.message.from_user.first_name}}', + response_keyboard_template='') + self._test_message(self.update_as_part_of_context) + self.assertEqual(Author.objects.count(), 1) + author = Author.objects.all()[0] + self.assertEqual(author.name, "author2")