{"action":"created","comment":{"url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/pulls/comments/70813219","id":70813219,"diff_hunk":"@@ -18,85 +18,191 @@\n from openerp.addons.base.ir.ir_qweb import QWebContext\n \n _logger = logging.getLogger('# Telegram')\n+_logger.setLevel(logging.DEBUG)\n # telebot.logger.setLevel(logging.DEBUG)\n-SAFE_EVAL_BASE = {\n+\n+\n+def get_parameter(db_name, key):\n+    db = openerp.sql_db.db_connect(db_name)\n+    registry = openerp.registry(db_name)\n+    result = None\n+    with openerp.api.Environment.manage(), db.cursor() as cr:\n+        res = registry['ir.config_parameter'].search(cr, SUPERUSER_ID, [('key', '=', key)])\n+        if len(res) == 1:\n+            val = registry['ir.config_parameter'].browse(cr, SUPERUSER_ID, res[0])\n+            result = val.value\n+        elif len(res) < 1:\n+            _logger.debug('# WARNING. No value for key %s' % key)\n+            return None\n+    return result\n+\n+\n+globals_dict = {\n     'datetime': datetime,\n     'dateutil': dateutil,\n     'time': time,\n+    'get_parameter': get_parameter,\n+    '_logger': _logger,\n }\n \n \n class TelegramCommand(models.Model):\n     \"\"\"\n         Model represents Telegram commands that may be proceeded.\n         Other modules can add new commands by adding some records of telegram.command model.\n-        Command must have:\n-          - python_code to execute;\n-          - response_code to handle odoo response on executed python_code as optional;\n-          - and web controllers if it is needed.\n+        Short commands gives result right after response_code is done.\n+        Long commands gives result after job is done, when appropriate notification appears in bus.\n+\n+        Members:\n+          response_code - python code to execute task. Launched by telegram_listener\n+          response_template - Template of message, that user will receive immediately after he send command\n+          notify_code - python code to get data, computed after executed action code. Launched by odoo_listener (bus)\n+          notify_template - Template of message, that user will receive after job is done\n+          update_cache_code - python code to update cache. Launched by ir.actions.server\n+          group_ids - Who can use this command\n+          model_ids - These models changes initiates cache updates for this command.\n+\n     \"\"\"\n     _name = \"telegram.command\"\n \n     name = fields.Char()\n-    python_code = fields.Char()\n+    description = fields.Char()\n+    cacheable = fields.Boolean()\n     response_code = fields.Char()\n-    group_ids = fields.One2many('res.groups', 'telegram_command_id')\n     response_template = fields.Char()\n+    notify_code = fields.Char()\n     notify_template = fields.Char()\n+    group_ids = fields.One2many('res.groups', 'telegram_command_id')\n+    model_ids = fields.Many2many('ir.model', 'command_to_model_rel', 'command_id', 'model_id')\n \n     @api.model\n     def telegram_listener(self, messages, bot):\n         # python_code execution method\n-        for m in messages:  # messages from telegram server\n-            res = self.env['telegram.command'].search([('name', '=', m.text)], limit=1)\n+        for tele_message in messages:  # messages from telegram server\n+            res = self.env['telegram.command'].search([('name', '=', tele_message.text)], limit=1)\n+            command = res[0] if len(res) > 0 else False\n             if len(res) == 1:\n-                locals_dict = {'self': self, 'bot': bot, 'm': m,\n-                               'TelegramUser': TelegramUser,\n-                               'get_parameter': get_parameter}\n-                safe_eval(res[0].python_code, SAFE_EVAL_BASE, locals_dict, mode=\"exec\", nocopy=True)\n-                self.render_and_send(bot, res[0].response_template, locals_dict, telegram_message=m)\n+                tele_user = self.env['telegram.user'].search([('chat_id', '=', tele_message.chat.id)])\n+                if command.name not in ['/login', '/help']:\n+                    if not tele_user.logged_in:\n+                        bot.send_message(tele_message.chat.id, 'You have to /login first.')\n+                        return\n+                    if not self.access_granted(command, tele_message.chat.id):\n+                        bot.send_message(tele_message.chat.id, 'Access denied. Command:  %s  .' % tele_message.text)\n+                        return\n+                if command.name == '/login' and tele_user.logged_in:\n+                    bot.send_message(tele_message.chat.id, 'You already logged in.')\n+                    return\n+                locals_dict = {'env': self.env, 'bot': bot, 'tele_message': tele_message, 'TelegramUser': TelegramUser}\n+                need_computed_answer = True\n+                if command.id in bot.cache.vals:","path":"telegram/telegram.py","position":95,"original_position":95,"commit_id":"2617e5aca7c9127a77c1872de7fc8ccdee286d64","original_commit_id":"2617e5aca7c9127a77c1872de7fc8ccdee286d64","user":{"login":"yelizariev","id":186131,"avatar_url":"https://avatars.githubusercontent.com/u/186131?v=3","gravatar_id":"","url":"https://api.github.com/users/yelizariev","html_url":"https://github.com/yelizariev","followers_url":"https://api.github.com/users/yelizariev/followers","following_url":"https://api.github.com/users/yelizariev/following{/other_user}","gists_url":"https://api.github.com/users/yelizariev/gists{/gist_id}","starred_url":"https://api.github.com/users/yelizariev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yelizariev/subscriptions","organizations_url":"https://api.github.com/users/yelizariev/orgs","repos_url":"https://api.github.com/users/yelizariev/repos","events_url":"https://api.github.com/users/yelizariev/events{/privacy}","received_events_url":"https://api.github.com/users/yelizariev/received_events","type":"User","site_admin":false},"body":"user ``get_value`` instead of directly accessing to vals","created_at":"2016-07-14T14:19:24Z","updated_at":"2016-07-14T14:19:24Z","html_url":"https://github.com/it-projects-llc/odoo-telegram/pull/10#discussion_r70813219","pull_request_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/pulls/10","_links":{"self":{"href":"https://api.github.com/repos/it-projects-llc/odoo-telegram/pulls/comments/70813219"},"html":{"href":"https://github.com/it-projects-llc/odoo-telegram/pull/10#discussion_r70813219"},"pull_request":{"href":"https://api.github.com/repos/it-projects-llc/odoo-telegram/pulls/10"}}},"pull_request":{"url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/pulls/10","id":77460778,"html_url":"https://github.com/it-projects-llc/odoo-telegram/pull/10","diff_url":"https://github.com/it-projects-llc/odoo-telegram/pull/10.diff","patch_url":"https://github.com/it-projects-llc/odoo-telegram/pull/10.patch","issue_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/issues/10","number":10,"state":"open","locked":false,"title":"Cache","user":{"login":"ilyasProgrammer","id":15200087,"avatar_url":"https://avatars.githubusercontent.com/u/15200087?v=3","gravatar_id":"","url":"https://api.github.com/users/ilyasProgrammer","html_url":"https://github.com/ilyasProgrammer","followers_url":"https://api.github.com/users/ilyasProgrammer/followers","following_url":"https://api.github.com/users/ilyasProgrammer/following{/other_user}","gists_url":"https://api.github.com/users/ilyasProgrammer/gists{/gist_id}","starred_url":"https://api.github.com/users/ilyasProgrammer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ilyasProgrammer/subscriptions","organizations_url":"https://api.github.com/users/ilyasProgrammer/orgs","repos_url":"https://api.github.com/users/ilyasProgrammer/repos","events_url":"https://api.github.com/users/ilyasProgrammer/events{/privacy}","received_events_url":"https://api.github.com/users/ilyasProgrammer/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-07-14T13:59:46Z","updated_at":"2016-07-14T14:19:24Z","closed_at":null,"merged_at":null,"merge_commit_sha":"662219a65112cf06b0a3530777213bb785d9fad1","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/pulls/10/commits","review_comments_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/pulls/10/comments","review_comment_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/pulls/comments{/number}","comments_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/issues/10/comments","statuses_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/statuses/2617e5aca7c9127a77c1872de7fc8ccdee286d64","head":{"label":"ilyasProgrammer:cache","ref":"cache","sha":"2617e5aca7c9127a77c1872de7fc8ccdee286d64","user":{"login":"ilyasProgrammer","id":15200087,"avatar_url":"https://avatars.githubusercontent.com/u/15200087?v=3","gravatar_id":"","url":"https://api.github.com/users/ilyasProgrammer","html_url":"https://github.com/ilyasProgrammer","followers_url":"https://api.github.com/users/ilyasProgrammer/followers","following_url":"https://api.github.com/users/ilyasProgrammer/following{/other_user}","gists_url":"https://api.github.com/users/ilyasProgrammer/gists{/gist_id}","starred_url":"https://api.github.com/users/ilyasProgrammer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ilyasProgrammer/subscriptions","organizations_url":"https://api.github.com/users/ilyasProgrammer/orgs","repos_url":"https://api.github.com/users/ilyasProgrammer/repos","events_url":"https://api.github.com/users/ilyasProgrammer/events{/privacy}","received_events_url":"https://api.github.com/users/ilyasProgrammer/received_events","type":"User","site_admin":false},"repo":{"id":59206563,"name":"odoo-telegram","full_name":"ilyasProgrammer/odoo-telegram","owner":{"login":"ilyasProgrammer","id":15200087,"avatar_url":"https://avatars.githubusercontent.com/u/15200087?v=3","gravatar_id":"","url":"https://api.github.com/users/ilyasProgrammer","html_url":"https://github.com/ilyasProgrammer","followers_url":"https://api.github.com/users/ilyasProgrammer/followers","following_url":"https://api.github.com/users/ilyasProgrammer/following{/other_user}","gists_url":"https://api.github.com/users/ilyasProgrammer/gists{/gist_id}","starred_url":"https://api.github.com/users/ilyasProgrammer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ilyasProgrammer/subscriptions","organizations_url":"https://api.github.com/users/ilyasProgrammer/orgs","repos_url":"https://api.github.com/users/ilyasProgrammer/repos","events_url":"https://api.github.com/users/ilyasProgrammer/events{/privacy}","received_events_url":"https://api.github.com/users/ilyasProgrammer/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ilyasProgrammer/odoo-telegram","description":"","fork":true,"url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram","forks_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/forks","keys_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/teams","hooks_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/hooks","issue_events_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/issues/events{/number}","events_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/events","assignees_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/assignees{/user}","branches_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/branches{/branch}","tags_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/tags","blobs_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/git/refs{/sha}","trees_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/statuses/{sha}","languages_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/languages","stargazers_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/stargazers","contributors_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/contributors","subscribers_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/subscribers","subscription_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/subscription","commits_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/commits{/sha}","git_commits_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/git/commits{/sha}","comments_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/comments{/number}","issue_comment_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/issues/comments{/number}","contents_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/contents/{+path}","compare_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/merges","archive_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/downloads","issues_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/issues{/number}","pulls_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/pulls{/number}","milestones_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/milestones{/number}","notifications_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/labels{/name}","releases_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/releases{/id}","deployments_url":"https://api.github.com/repos/ilyasProgrammer/odoo-telegram/deployments","created_at":"2016-05-19T12:59:24Z","updated_at":"2016-05-19T13:01:25Z","pushed_at":"2016-07-14T14:02:50Z","git_url":"git://github.com/ilyasProgrammer/odoo-telegram.git","ssh_url":"git@github.com:ilyasProgrammer/odoo-telegram.git","clone_url":"https://github.com/ilyasProgrammer/odoo-telegram.git","svn_url":"https://github.com/ilyasProgrammer/odoo-telegram","homepage":null,"size":83,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"it-projects-llc:9.0","ref":"9.0","sha":"4fd5ed7fcdd9847ca8b2769563471cbf8d2b324e","user":{"login":"it-projects-llc","id":17959306,"avatar_url":"https://avatars.githubusercontent.com/u/17959306?v=3","gravatar_id":"","url":"https://api.github.com/users/it-projects-llc","html_url":"https://github.com/it-projects-llc","followers_url":"https://api.github.com/users/it-projects-llc/followers","following_url":"https://api.github.com/users/it-projects-llc/following{/other_user}","gists_url":"https://api.github.com/users/it-projects-llc/gists{/gist_id}","starred_url":"https://api.github.com/users/it-projects-llc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/it-projects-llc/subscriptions","organizations_url":"https://api.github.com/users/it-projects-llc/orgs","repos_url":"https://api.github.com/users/it-projects-llc/repos","events_url":"https://api.github.com/users/it-projects-llc/events{/privacy}","received_events_url":"https://api.github.com/users/it-projects-llc/received_events","type":"Organization","site_admin":false},"repo":{"id":58188910,"name":"odoo-telegram","full_name":"it-projects-llc/odoo-telegram","owner":{"login":"it-projects-llc","id":17959306,"avatar_url":"https://avatars.githubusercontent.com/u/17959306?v=3","gravatar_id":"","url":"https://api.github.com/users/it-projects-llc","html_url":"https://github.com/it-projects-llc","followers_url":"https://api.github.com/users/it-projects-llc/followers","following_url":"https://api.github.com/users/it-projects-llc/following{/other_user}","gists_url":"https://api.github.com/users/it-projects-llc/gists{/gist_id}","starred_url":"https://api.github.com/users/it-projects-llc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/it-projects-llc/subscriptions","organizations_url":"https://api.github.com/users/it-projects-llc/orgs","repos_url":"https://api.github.com/users/it-projects-llc/repos","events_url":"https://api.github.com/users/it-projects-llc/events{/privacy}","received_events_url":"https://api.github.com/users/it-projects-llc/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/it-projects-llc/odoo-telegram","description":"Telegram.org and Odoo.com integration","fork":false,"url":"https://api.github.com/repos/it-projects-llc/odoo-telegram","forks_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/forks","keys_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/keys{/key_id}","collaborators_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/teams","hooks_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/hooks","issue_events_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/issues/events{/number}","events_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/events","assignees_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/assignees{/user}","branches_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/branches{/branch}","tags_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/tags","blobs_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/git/refs{/sha}","trees_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/git/trees{/sha}","statuses_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/statuses/{sha}","languages_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/languages","stargazers_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/stargazers","contributors_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/contributors","subscribers_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/subscribers","subscription_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/subscription","commits_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/commits{/sha}","git_commits_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/git/commits{/sha}","comments_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/comments{/number}","issue_comment_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/issues/comments{/number}","contents_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/contents/{+path}","compare_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/compare/{base}...{head}","merges_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/merges","archive_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/downloads","issues_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/issues{/number}","pulls_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/pulls{/number}","milestones_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/milestones{/number}","notifications_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/labels{/name}","releases_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/releases{/id}","deployments_url":"https://api.github.com/repos/it-projects-llc/odoo-telegram/deployments","created_at":"2016-05-06T07:23:04Z","updated_at":"2016-06-13T18:45:53Z","pushed_at":"2016-07-14T14:02:50Z","git_url":"git://github.com/it-projects-llc/odoo-telegram.git","ssh_url":"git@github.com:it-projects-llc/odoo-telegram.git","clone_url":"https://github.com/it-projects-llc/odoo-telegram.git","svn_url":"https://github.com/it-projects-llc/odoo-telegram","homepage":"https://www.it-projects.info/","size":182,"stargazers_count":3,"watchers_count":3,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":1,"forks":4,"open_issues":1,"watchers":3,"default_branch":"9.0"}},"_links":{"self":{"href":"https://api.github.com/repos/it-projects-llc/odoo-telegram/pulls/10"},"html":{"href":"https://github.com/it-projects-llc/odoo-telegram/pull/10"},"issue":{"href":"https://api.github.com/repos/it-projects-llc/odoo-telegram/issues/10"},"comments":{"href":"https://api.github.com/repos/it-projects-llc/odoo-telegram/issues/10/comments"},"review_comments":{"href":"https://api.github.com/repos/it-projects-llc/odoo-telegram/pulls/10/comments"},"review_comment":{"href":"https://api.github.com/repos/it-projects-llc/odoo-telegram/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/it-projects-llc/odoo-telegram/pulls/10/commits"},"statuses":{"href":"https://api.github.com/repos/it-projects-llc/odoo-telegram/statuses/2617e5aca7c9127a77c1872de7fc8ccdee286d64"}}}}
{"actor":{"display_login":"yelizariev"}}