Skip to content

Commit

Permalink
add telegram Update to context. Useful for chat and user id to respon…
Browse files Browse the repository at this point in the history
…d in some use cases
  • Loading branch information
jlmadurga committed Mar 15, 2016
1 parent 635fc88 commit ccdb20e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
3 changes: 2 additions & 1 deletion microbot/models/handler.py
Expand Up @@ -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)
Expand Down
18 changes: 17 additions & 1 deletion microbot/models/telegram_api.py
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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()}
24 changes: 23 additions & 1 deletion tests/test_microbot.py
Expand Up @@ -169,6 +169,13 @@ class TestRequests(LiveServerTestCase, testcases.BaseTestBot):
}
}

update_as_part_of_context = {'in': '/authors@1',
'out': {'parse_mode': 'HTML',
'reply_markup': '',
'text': '<b>author2</b> 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/',
Expand Down Expand Up @@ -352,4 +359,19 @@ def test_put_data_template(self):
response_text_template='<b>{{response.name}}</b> updated',
response_keyboard_template='')
self._test_message(self.author_put_data_template)
self.assertEqual(Author.objects.all()[0].name, 'author2')
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<id>\d+)',
request=self.request,
response_text_template='<b>{{response.name}}</b> 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")

0 comments on commit ccdb20e

Please sign in to comment.