{"action":"created","comment":{"url":"https://api.github.com/repos/flavour/eden/pulls/comments/40584754","id":40584754,"diff_hunk":"@@ -616,4 +718,230 @@ def _update_subscription(self, subscription):\n         subscription[\"filter_id\"] = filter_id\n         return subscription\n \n+    # -------------------------------------------------------------------------\n+    def _update_sync(self, resource, filters, selected_repository_id, properties):\n+        \"\"\" \n+            Update synchronization settings \n+            \n+            @param resource: available resources config\n+            @param filters: filter applied on the resource\n+            @param selected_repository_id: repository that is under current selection\n+            @param properties: comment field of the pr_subscription; used to \n+                               store the ids of FTP Sync\n+            \n+        \"\"\"\n+        \n+        db = current.db\n+        s3db = current.s3db\n+        user_id = current.auth.user.id\n+        utcnow = current.request.utcnow\n+        \n+        if properties:\n+            old_repository_id = json.loads(properties)[\"repository_id\"]\n+        else:\n+            # First Run\n+            old_repository_id = None\n+        \n+        # Insert/Update repository_id    \n+        if old_repository_id:\n+            if old_repository_id == selected_repository_id:\n+                # Do Nothing\n+                pass\n+            else:\n+                # Update\n+                properties = json.loads(properties)\n+                properties[\"repository_id\"] = selected_repository_id\n+                properties = json.dumps(properties)\n+        else:\n+            # First Run\n+            repository_id_dict = {\"repository_id\": selected_repository_id}\n+            properties = json.dumps(repository_id_dict)\n+            old_repository_id = selected_repository_id\n+        \n+                   \n+        # Sync Task\n+        sync_task_table = s3db.sync_task\n+        \n+        # Check if task already exists\n+        query = (sync_task_table.deleted != True) & \\\n+                (sync_task_table.owned_by_user == user_id) & \\\n+                (sync_task_table.repository_id == old_repository_id)\n+                \n+        row = db(query).select(sync_task_table.id,\n+                               sync_task_table.repository_id,\n+                               limitby=(0, 1)).first()\n+        if row:\n+            # Check if update?\n+            if row.repository_id == selected_repository_id:\n+                # Do Nothing\n+                pass\n+            else:\n+                # Update\n+                db(sync_task_table.repository_id == old_repository_id).\\\n+                                update(repository_id = selected_repository_id)\n+            sync_task_id = row.id        \n+        else:\n+            # First Run\n+            sync_task_data = {\"repository_id\": selected_repository_id,\n+                              \"resource_name\": resource,\n+                              \"mode\": 2, #Push\n+                              \"strategy\": [\"create\"], # Alert updates are done\n+                                                      # as extra info elements\n+                              \"representation\": \"cap\",\n+                              \"multiple_file\": True,\n+                              \"last_push\": utcnow, # since used for notifications,\n+                                                   # so don't send old alerts\n+                              }\n+            sync_task_id = sync_task_table.insert(**sync_task_data)        \n+        \n+        \n+        # Sync Resource Filter\n+            \n+        # Normally a filter looks like this\n+        # [[\"event_type_id__belongs\",\"24,3\"],[\"priority__belongs\",null]]\n+        # Get only those that have value and ignore null one\n+        filters = json.loads(filters)\n+        filters = [filter for filter in filters if filter[1] is not None]\n+        current_filter_ids = []\n+        sync_resource_filter_table = s3db.sync_resource_filter\n+        if len(filters)>0:           \n+            if properties and \\\n+               \"resource_filter_ids\" in json.loads(properties):\n+                resource_filter_id_list = json.loads(properties) \\\n+                                            [\"resource_filter_ids\"]\n+            else:\n+                resource_filter_id_list = []\n+                \n+            for filter in filters:\n+                # Get the query\n+                query_part = str(filter[0].split(\"__belongs\",1)[0]).strip(\"[]\")\n+                # Get the value for the query\n+                value_ = str(filter[1])\n+                if query_part in [\"event_type_id\", \"priority\", \"language\"]:\n+                    tablename = \"cap_info\"\n+                else:\n+                    tablename = \"gis_location\"\n+                    \n+                values = value_.split(\",\")\n+    \n+                for value in values:\n+                    filter_string = \"%s=%s\" % (query_part, value)\n+                    if properties and \\\n+                       \"resource_filter_ids\" in json.loads(properties):\n+                        rows = db(sync_resource_filter_table.id.belongs\\\n+                                 (resource_filter_id_list)).select()\n+                        for row in rows:\n+                            if row.owned_by_user == user_id and \\\n+                               row.task_id == sync_task_id and \\\n+                               row.tablename == tablename and \\\n+                               row.filter_string == filter_string:\n+                                current_filter_ids.append(row.id)\n+                                break\n+                        else:\n+                            resource_filter_data = {\"task_id\": sync_task_id,\n+                                                    \"tablename\": tablename,\n+                                                    \"filter_string\": filter_string,\n+                                                    \"modified_on\": utcnow,\n+                                                    }\n+                            resource_filter_id = sync_resource_filter_table. \\\n+                                                    insert(**resource_filter_data)                        \n+                            resource_filter_id_list.append(resource_filter_id)\n+                            current_filter_ids.append(resource_filter_id)                    \n+                    else:\n+                        # First Run                       \n+                        resource_filter_data = {\"task_id\": sync_task_id,\n+                                                \"tablename\": tablename,\n+                                                \"filter_string\": filter_string,\n+                                                \"modified_on\": utcnow,\n+                                                }\n+                        resource_filter_id = sync_resource_filter_table.\\","path":"modules/templates/SAMBRO/controllers.py","position":470,"original_position":470,"commit_id":"a6313bd73686fdbd86741f3136789b6e9724850d","original_commit_id":"a6313bd73686fdbd86741f3136789b6e9724850d","user":{"login":"nursix","id":1257183,"avatar_url":"https://avatars.githubusercontent.com/u/1257183?v=3","gravatar_id":"","url":"https://api.github.com/users/nursix","html_url":"https://github.com/nursix","followers_url":"https://api.github.com/users/nursix/followers","following_url":"https://api.github.com/users/nursix/following{/other_user}","gists_url":"https://api.github.com/users/nursix/gists{/gist_id}","starred_url":"https://api.github.com/users/nursix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nursix/subscriptions","organizations_url":"https://api.github.com/users/nursix/orgs","repos_url":"https://api.github.com/users/nursix/repos","events_url":"https://api.github.com/users/nursix/events{/privacy}","received_events_url":"https://api.github.com/users/nursix/received_events","type":"User","site_admin":false},"body":"Not calling set_record_owner here nor onaccept here :/","created_at":"2015-09-28T18:07:58Z","updated_at":"2015-09-28T18:07:58Z","html_url":"https://github.com/flavour/eden/pull/1162#discussion_r40584754","pull_request_url":"https://api.github.com/repos/flavour/eden/pulls/1162","_links":{"self":{"href":"https://api.github.com/repos/flavour/eden/pulls/comments/40584754"},"html":{"href":"https://github.com/flavour/eden/pull/1162#discussion_r40584754"},"pull_request":{"href":"https://api.github.com/repos/flavour/eden/pulls/1162"}}},"pull_request":{"url":"https://api.github.com/repos/flavour/eden/pulls/1162","id":46177876,"html_url":"https://github.com/flavour/eden/pull/1162","diff_url":"https://github.com/flavour/eden/pull/1162.diff","patch_url":"https://github.com/flavour/eden/pull/1162.patch","issue_url":"https://api.github.com/repos/flavour/eden/issues/1162","number":1162,"state":"open","locked":false,"title":"SAMBRO: FTP Sync","user":{"login":"biplovbhandari","id":4207677,"avatar_url":"https://avatars.githubusercontent.com/u/4207677?v=3","gravatar_id":"","url":"https://api.github.com/users/biplovbhandari","html_url":"https://github.com/biplovbhandari","followers_url":"https://api.github.com/users/biplovbhandari/followers","following_url":"https://api.github.com/users/biplovbhandari/following{/other_user}","gists_url":"https://api.github.com/users/biplovbhandari/gists{/gist_id}","starred_url":"https://api.github.com/users/biplovbhandari/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/biplovbhandari/subscriptions","organizations_url":"https://api.github.com/users/biplovbhandari/orgs","repos_url":"https://api.github.com/users/biplovbhandari/repos","events_url":"https://api.github.com/users/biplovbhandari/events{/privacy}","received_events_url":"https://api.github.com/users/biplovbhandari/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-09-28T16:26:13Z","updated_at":"2015-09-28T18:07:58Z","closed_at":null,"merged_at":null,"merge_commit_sha":"313d17661d0abb76c6f483de666376c20bc56533","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/flavour/eden/pulls/1162/commits","review_comments_url":"https://api.github.com/repos/flavour/eden/pulls/1162/comments","review_comment_url":"https://api.github.com/repos/flavour/eden/pulls/comments{/number}","comments_url":"https://api.github.com/repos/flavour/eden/issues/1162/comments","statuses_url":"https://api.github.com/repos/flavour/eden/statuses/a6313bd73686fdbd86741f3136789b6e9724850d","head":{"label":"biplovbhandari:master","ref":"master","sha":"a6313bd73686fdbd86741f3136789b6e9724850d","user":{"login":"biplovbhandari","id":4207677,"avatar_url":"https://avatars.githubusercontent.com/u/4207677?v=3","gravatar_id":"","url":"https://api.github.com/users/biplovbhandari","html_url":"https://github.com/biplovbhandari","followers_url":"https://api.github.com/users/biplovbhandari/followers","following_url":"https://api.github.com/users/biplovbhandari/following{/other_user}","gists_url":"https://api.github.com/users/biplovbhandari/gists{/gist_id}","starred_url":"https://api.github.com/users/biplovbhandari/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/biplovbhandari/subscriptions","organizations_url":"https://api.github.com/users/biplovbhandari/orgs","repos_url":"https://api.github.com/users/biplovbhandari/repos","events_url":"https://api.github.com/users/biplovbhandari/events{/privacy}","received_events_url":"https://api.github.com/users/biplovbhandari/received_events","type":"User","site_admin":false},"repo":{"id":37774689,"name":"eden","full_name":"biplovbhandari/eden","owner":{"login":"biplovbhandari","id":4207677,"avatar_url":"https://avatars.githubusercontent.com/u/4207677?v=3","gravatar_id":"","url":"https://api.github.com/users/biplovbhandari","html_url":"https://github.com/biplovbhandari","followers_url":"https://api.github.com/users/biplovbhandari/followers","following_url":"https://api.github.com/users/biplovbhandari/following{/other_user}","gists_url":"https://api.github.com/users/biplovbhandari/gists{/gist_id}","starred_url":"https://api.github.com/users/biplovbhandari/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/biplovbhandari/subscriptions","organizations_url":"https://api.github.com/users/biplovbhandari/orgs","repos_url":"https://api.github.com/users/biplovbhandari/repos","events_url":"https://api.github.com/users/biplovbhandari/events{/privacy}","received_events_url":"https://api.github.com/users/biplovbhandari/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/biplovbhandari/eden","description":"Sahana Eden is an Open Source Humanitarian Platform which can be used to provide solutions for Disaster Management, Development, and Environmental Management sectors.. Please sign CLA when submitting pull requests: http://bit.ly/SSF-eCLA","fork":true,"url":"https://api.github.com/repos/biplovbhandari/eden","forks_url":"https://api.github.com/repos/biplovbhandari/eden/forks","keys_url":"https://api.github.com/repos/biplovbhandari/eden/keys{/key_id}","collaborators_url":"https://api.github.com/repos/biplovbhandari/eden/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/biplovbhandari/eden/teams","hooks_url":"https://api.github.com/repos/biplovbhandari/eden/hooks","issue_events_url":"https://api.github.com/repos/biplovbhandari/eden/issues/events{/number}","events_url":"https://api.github.com/repos/biplovbhandari/eden/events","assignees_url":"https://api.github.com/repos/biplovbhandari/eden/assignees{/user}","branches_url":"https://api.github.com/repos/biplovbhandari/eden/branches{/branch}","tags_url":"https://api.github.com/repos/biplovbhandari/eden/tags","blobs_url":"https://api.github.com/repos/biplovbhandari/eden/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/biplovbhandari/eden/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/biplovbhandari/eden/git/refs{/sha}","trees_url":"https://api.github.com/repos/biplovbhandari/eden/git/trees{/sha}","statuses_url":"https://api.github.com/repos/biplovbhandari/eden/statuses/{sha}","languages_url":"https://api.github.com/repos/biplovbhandari/eden/languages","stargazers_url":"https://api.github.com/repos/biplovbhandari/eden/stargazers","contributors_url":"https://api.github.com/repos/biplovbhandari/eden/contributors","subscribers_url":"https://api.github.com/repos/biplovbhandari/eden/subscribers","subscription_url":"https://api.github.com/repos/biplovbhandari/eden/subscription","commits_url":"https://api.github.com/repos/biplovbhandari/eden/commits{/sha}","git_commits_url":"https://api.github.com/repos/biplovbhandari/eden/git/commits{/sha}","comments_url":"https://api.github.com/repos/biplovbhandari/eden/comments{/number}","issue_comment_url":"https://api.github.com/repos/biplovbhandari/eden/issues/comments{/number}","contents_url":"https://api.github.com/repos/biplovbhandari/eden/contents/{+path}","compare_url":"https://api.github.com/repos/biplovbhandari/eden/compare/{base}...{head}","merges_url":"https://api.github.com/repos/biplovbhandari/eden/merges","archive_url":"https://api.github.com/repos/biplovbhandari/eden/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/biplovbhandari/eden/downloads","issues_url":"https://api.github.com/repos/biplovbhandari/eden/issues{/number}","pulls_url":"https://api.github.com/repos/biplovbhandari/eden/pulls{/number}","milestones_url":"https://api.github.com/repos/biplovbhandari/eden/milestones{/number}","notifications_url":"https://api.github.com/repos/biplovbhandari/eden/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/biplovbhandari/eden/labels{/name}","releases_url":"https://api.github.com/repos/biplovbhandari/eden/releases{/id}","created_at":"2015-06-20T15:32:51Z","updated_at":"2015-06-20T15:33:04Z","pushed_at":"2015-09-28T16:23:32Z","git_url":"git://github.com/biplovbhandari/eden.git","ssh_url":"git@github.com:biplovbhandari/eden.git","clone_url":"https://github.com/biplovbhandari/eden.git","svn_url":"https://github.com/biplovbhandari/eden","homepage":"http://eden.sahanafoundation.org","size":163046,"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":"flavour:master","ref":"master","sha":"be35351deed67d88cb11a974d0ff78afdf55417e","user":{"login":"flavour","id":389592,"avatar_url":"https://avatars.githubusercontent.com/u/389592?v=3","gravatar_id":"","url":"https://api.github.com/users/flavour","html_url":"https://github.com/flavour","followers_url":"https://api.github.com/users/flavour/followers","following_url":"https://api.github.com/users/flavour/following{/other_user}","gists_url":"https://api.github.com/users/flavour/gists{/gist_id}","starred_url":"https://api.github.com/users/flavour/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flavour/subscriptions","organizations_url":"https://api.github.com/users/flavour/orgs","repos_url":"https://api.github.com/users/flavour/repos","events_url":"https://api.github.com/users/flavour/events{/privacy}","received_events_url":"https://api.github.com/users/flavour/received_events","type":"User","site_admin":false},"repo":{"id":3021325,"name":"eden","full_name":"flavour/eden","owner":{"login":"flavour","id":389592,"avatar_url":"https://avatars.githubusercontent.com/u/389592?v=3","gravatar_id":"","url":"https://api.github.com/users/flavour","html_url":"https://github.com/flavour","followers_url":"https://api.github.com/users/flavour/followers","following_url":"https://api.github.com/users/flavour/following{/other_user}","gists_url":"https://api.github.com/users/flavour/gists{/gist_id}","starred_url":"https://api.github.com/users/flavour/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flavour/subscriptions","organizations_url":"https://api.github.com/users/flavour/orgs","repos_url":"https://api.github.com/users/flavour/repos","events_url":"https://api.github.com/users/flavour/events{/privacy}","received_events_url":"https://api.github.com/users/flavour/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/flavour/eden","description":"Sahana Eden is an Open Source Humanitarian Platform which can be used to provide solutions for Disaster Management, Development, and Environmental Management sectors.. Please sign CLA when submitting pull requests: http://bit.ly/SSF-eCLA","fork":false,"url":"https://api.github.com/repos/flavour/eden","forks_url":"https://api.github.com/repos/flavour/eden/forks","keys_url":"https://api.github.com/repos/flavour/eden/keys{/key_id}","collaborators_url":"https://api.github.com/repos/flavour/eden/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/flavour/eden/teams","hooks_url":"https://api.github.com/repos/flavour/eden/hooks","issue_events_url":"https://api.github.com/repos/flavour/eden/issues/events{/number}","events_url":"https://api.github.com/repos/flavour/eden/events","assignees_url":"https://api.github.com/repos/flavour/eden/assignees{/user}","branches_url":"https://api.github.com/repos/flavour/eden/branches{/branch}","tags_url":"https://api.github.com/repos/flavour/eden/tags","blobs_url":"https://api.github.com/repos/flavour/eden/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/flavour/eden/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/flavour/eden/git/refs{/sha}","trees_url":"https://api.github.com/repos/flavour/eden/git/trees{/sha}","statuses_url":"https://api.github.com/repos/flavour/eden/statuses/{sha}","languages_url":"https://api.github.com/repos/flavour/eden/languages","stargazers_url":"https://api.github.com/repos/flavour/eden/stargazers","contributors_url":"https://api.github.com/repos/flavour/eden/contributors","subscribers_url":"https://api.github.com/repos/flavour/eden/subscribers","subscription_url":"https://api.github.com/repos/flavour/eden/subscription","commits_url":"https://api.github.com/repos/flavour/eden/commits{/sha}","git_commits_url":"https://api.github.com/repos/flavour/eden/git/commits{/sha}","comments_url":"https://api.github.com/repos/flavour/eden/comments{/number}","issue_comment_url":"https://api.github.com/repos/flavour/eden/issues/comments{/number}","contents_url":"https://api.github.com/repos/flavour/eden/contents/{+path}","compare_url":"https://api.github.com/repos/flavour/eden/compare/{base}...{head}","merges_url":"https://api.github.com/repos/flavour/eden/merges","archive_url":"https://api.github.com/repos/flavour/eden/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/flavour/eden/downloads","issues_url":"https://api.github.com/repos/flavour/eden/issues{/number}","pulls_url":"https://api.github.com/repos/flavour/eden/pulls{/number}","milestones_url":"https://api.github.com/repos/flavour/eden/milestones{/number}","notifications_url":"https://api.github.com/repos/flavour/eden/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/flavour/eden/labels{/name}","releases_url":"https://api.github.com/repos/flavour/eden/releases{/id}","created_at":"2011-12-20T17:49:16Z","updated_at":"2015-09-14T20:01:24Z","pushed_at":"2015-09-28T18:06:29Z","git_url":"git://github.com/flavour/eden.git","ssh_url":"git@github.com:flavour/eden.git","clone_url":"https://github.com/flavour/eden.git","svn_url":"https://github.com/flavour/eden","homepage":"http://eden.sahanafoundation.org","size":205653,"stargazers_count":172,"watchers_count":172,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":429,"mirror_url":null,"open_issues_count":60,"forks":429,"open_issues":60,"watchers":172,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/flavour/eden/pulls/1162"},"html":{"href":"https://github.com/flavour/eden/pull/1162"},"issue":{"href":"https://api.github.com/repos/flavour/eden/issues/1162"},"comments":{"href":"https://api.github.com/repos/flavour/eden/issues/1162/comments"},"review_comments":{"href":"https://api.github.com/repos/flavour/eden/pulls/1162/comments"},"review_comment":{"href":"https://api.github.com/repos/flavour/eden/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/flavour/eden/pulls/1162/commits"},"statuses":{"href":"https://api.github.com/repos/flavour/eden/statuses/a6313bd73686fdbd86741f3136789b6e9724850d"}}}}