{"action":"created","comment":{"url":"https://api.github.com/repos/translate/pootle/pulls/comments/78823577","id":78823577,"diff_hunk":"@@ -0,0 +1,233 @@\n+# -*- coding: utf-8 -*-\n+#\n+# Copyright (C) Pootle contributors.\n+#\n+# This file is a part of the Pootle project. It is distributed under the GPL3\n+# or later license. See the LICENSE file for a copy of the license and the\n+# AUTHORS file for copyright and authorship information.\n+\n+from fnmatch import fnmatch\n+\n+from pootle.core.url_helpers import split_pootle_path\n+from pootle_fs.utils import PathFilter\n+from pootle_store.models import Store\n+\n+from .models import VirtualFolder, VirtualFolderTreeItem\n+\n+\n+def update_vfolder_tree(vf, store):\n+    \"\"\"For a given VirtualFolder and Store update the VirtualFolderTreeItem\n+    \"\"\"\n+    # Create missing VirtualFolderTreeItem tree structure for affected Stores\n+    # after adding or unobsoleting Units.\n+    created = False\n+    try:\n+        vfolder_treeitem = (\n+            VirtualFolderTreeItem.objects.get(\n+                directory=store.parent, vfolder=vf))\n+    except VirtualFolderTreeItem.DoesNotExist:\n+        parts = split_pootle_path(store.parent.pootle_path)\n+        path_parts = [\"\", parts[0], parts[1], vf.name]\n+        if parts[2]:\n+            path_parts.append(parts[2].strip(\"/\"))\n+        path_parts.append(parts[3])\n+        pootle_path = \"/\".join(path_parts)\n+        vfolder_treeitem = (\n+            VirtualFolderTreeItem.objects.create(\n+                pootle_path=pootle_path,\n+                directory=store.parent,\n+                vfolder=vf))\n+        created = True\n+    if not created:\n+        # The VirtualFolderTreeItem already existed, so\n+        # calculate again the stats up to the root.\n+        vfolder_treeitem.update_all_cache()\n+\n+\n+class VirtualFolderFinder(object):\n+    \"\"\"Find vfs for a new store\"\"\"\n+\n+    def __init__(self, store):\n+        self.store = store\n+\n+    @property\n+    def language(self):\n+        return self.store.translation_project.language\n+\n+    @property\n+    def project(self):\n+        return self.store.translation_project.project\n+\n+    @property\n+    def possible_vfolders(self):\n+        return (\n+            self.project.vfolders.filter(all_languages=True)\n+            | self.language.vfolders.filter(all_projects=True)\n+            | self.project.vfolders.filter(languages=self.language)\n+            | VirtualFolder.objects.filter(\n+                all_languages=True, all_projects=True))\n+\n+    def add_to_vfolders(self):\n+        to_add = []\n+        for vf in self.possible_vfolders:\n+            if vf.path_matcher.should_add_store(self.store):\n+                to_add.append(vf)\n+        if to_add:\n+            self.store.vfolders.add(*to_add)\n+            self.store.set_priority()\n+        for vf in to_add:\n+            update_vfolder_tree(vf, self.store)\n+\n+\n+class VirtualFolderPathMatcher(object):\n+\n+    tp_path = \"/[^/]*/[^/]*/\"\n+\n+    def __init__(self, vf):\n+        self.vf = vf\n+\n+    @property\n+    def existing_stores(self):\n+        \"\"\"Currently associated Stores\"\"\"\n+        return self.vf.stores.all()\n+\n+    @property\n+    def languages(self):\n+        \"\"\"The languages associated with this vfolder\n+        If `all_languages` is set then `None` is returned\n+        \"\"\"\n+        if self.vf.all_languages:\n+            return None\n+        return self.vf.languages.all()\n+\n+    @property\n+    def projects(self):\n+        \"\"\"The projects associated with this vfolder\n+        If `all_projects` is set then `None` is returned\n+        \"\"\"\n+        if self.vf.all_projects:\n+            return None\n+        return self.vf.projects.all()\n+\n+    @property\n+    def matching_stores(self):\n+        \"\"\"Store qs containing all stores that match\n+        project, language, and rules for this vfolder\n+        \"\"\"\n+        return self.filter_from_rules(self.store_qs)\n+\n+    @property\n+    def rules(self):\n+        \"\"\"Glob matching rules\"\"\"\n+        return (\n+            \"%s\" % r.strip()\n+            for r\n+            in self.vf.filter_rules.split(\",\"))\n+\n+    @property\n+    def store_manager(self):\n+        \"\"\"The root object manager for finding/adding stores\"\"\"\n+        return Store.objects\n+\n+    @property\n+    def store_qs(self):\n+        \"\"\"The stores qs without any rule filtering\"\"\"\n+        return self.filter_projects(\n+            self.filter_languages(\n+                self.store_manager))\n+\n+    def update_stores(self):\n+        \"\"\"Add and delete Store associations as necessary, and set the\n+        priority for any affected Stores\n+        \"\"\"\n+        added, removed = self.add_and_remove_stores()\n+        for store in added:\n+            update_vfolder_tree(self.vf, store)\n+            if store.priority != self.vf.priority:\n+                store.set_priority()\n+        for store in removed:\n+            if store.priority == self.vf.priority:\n+                store.set_priority()\n+\n+    def add_and_remove_stores(self):\n+        \"\"\"Add Stores that should be associated but arent, delete Store\n+        associations for Stores that are associated but shouldnt be\n+        \"\"\"\n+        existing_stores = set(self.existing_stores)\n+        matching_stores = set(self.matching_stores)\n+        to_add = matching_stores - existing_stores\n+        to_remove = existing_stores - matching_stores\n+        if to_add:\n+            self.add_stores(to_add)\n+        if to_remove:\n+            self.remove_stores(to_remove)\n+        return to_add, to_remove\n+\n+    def add_stores(self, stores):\n+        \"\"\"Associate a Store\"\"\"\n+        self.vf.stores.add(*stores)\n+\n+    def remove_stores(self, stores):\n+        self.vf.stores.remove(*stores)\n+\n+    def should_add_store(self, store):\n+        if self.store_matches(store) and not self.store_associated(store):\n+            return True\n+        return False\n+\n+    def store_associated(self, store):\n+        return self.vf.stores.through.objects.filter(store_id=store.id).exists()","path":"pootle/apps/virtualfolder/utils.py","position":null,"original_position":179,"commit_id":"2e651c5857ac872d1cf13bf417eb81276859fb54","original_commit_id":"734b75f5d51c0d28320fc0a1ccd7a46ea732caeb","user":{"login":"phlax","id":454682,"avatar_url":"https://avatars.githubusercontent.com/u/454682?v=3","gravatar_id":"","url":"https://api.github.com/users/phlax","html_url":"https://github.com/phlax","followers_url":"https://api.github.com/users/phlax/followers","following_url":"https://api.github.com/users/phlax/following{/other_user}","gists_url":"https://api.github.com/users/phlax/gists{/gist_id}","starred_url":"https://api.github.com/users/phlax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/phlax/subscriptions","organizations_url":"https://api.github.com/users/phlax/orgs","repos_url":"https://api.github.com/users/phlax/repos","events_url":"https://api.github.com/users/phlax/events{/privacy}","received_events_url":"https://api.github.com/users/phlax/received_events","type":"User","site_admin":false},"body":"...although actually the algo is a little wrong there - correcting now...","created_at":"2016-09-14T19:43:10Z","updated_at":"2016-09-14T19:43:10Z","html_url":"https://github.com/translate/pootle/pull/5233#discussion_r78823577","pull_request_url":"https://api.github.com/repos/translate/pootle/pulls/5233","_links":{"self":{"href":"https://api.github.com/repos/translate/pootle/pulls/comments/78823577"},"html":{"href":"https://github.com/translate/pootle/pull/5233#discussion_r78823577"},"pull_request":{"href":"https://api.github.com/repos/translate/pootle/pulls/5233"}}},"pull_request":{"url":"https://api.github.com/repos/translate/pootle/pulls/5233","id":84755746,"html_url":"https://github.com/translate/pootle/pull/5233","diff_url":"https://github.com/translate/pootle/pull/5233.diff","patch_url":"https://github.com/translate/pootle/pull/5233.patch","issue_url":"https://api.github.com/repos/translate/pootle/issues/5233","number":5233,"state":"open","locked":false,"title":"[WIP] Move vfolder location and filter_rules to projects/languages/glob matching","user":{"login":"phlax","id":454682,"avatar_url":"https://avatars.githubusercontent.com/u/454682?v=3","gravatar_id":"","url":"https://api.github.com/users/phlax","html_url":"https://github.com/phlax","followers_url":"https://api.github.com/users/phlax/followers","following_url":"https://api.github.com/users/phlax/following{/other_user}","gists_url":"https://api.github.com/users/phlax/gists{/gist_id}","starred_url":"https://api.github.com/users/phlax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/phlax/subscriptions","organizations_url":"https://api.github.com/users/phlax/orgs","repos_url":"https://api.github.com/users/phlax/repos","events_url":"https://api.github.com/users/phlax/events{/privacy}","received_events_url":"https://api.github.com/users/phlax/received_events","type":"User","site_admin":false},"body":"this PR will\r\n\r\n- add projects/languages m2m tables with vfolder\r\n- glob matching for filter_rules\r\n- migrate existing location/filter_rules fields to projects/locations and update the filter_rules if required","created_at":"2016-09-10T13:36:45Z","updated_at":"2016-09-14T19:43:10Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ea1d7c9f537c2d35a80c462737385e9335bc0a2a","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/translate/pootle/pulls/5233/commits","review_comments_url":"https://api.github.com/repos/translate/pootle/pulls/5233/comments","review_comment_url":"https://api.github.com/repos/translate/pootle/pulls/comments{/number}","comments_url":"https://api.github.com/repos/translate/pootle/issues/5233/comments","statuses_url":"https://api.github.com/repos/translate/pootle/statuses/2e651c5857ac872d1cf13bf417eb81276859fb54","head":{"label":"phlax:vfolders_location_projects_langs","ref":"vfolders_location_projects_langs","sha":"2e651c5857ac872d1cf13bf417eb81276859fb54","user":{"login":"phlax","id":454682,"avatar_url":"https://avatars.githubusercontent.com/u/454682?v=3","gravatar_id":"","url":"https://api.github.com/users/phlax","html_url":"https://github.com/phlax","followers_url":"https://api.github.com/users/phlax/followers","following_url":"https://api.github.com/users/phlax/following{/other_user}","gists_url":"https://api.github.com/users/phlax/gists{/gist_id}","starred_url":"https://api.github.com/users/phlax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/phlax/subscriptions","organizations_url":"https://api.github.com/users/phlax/orgs","repos_url":"https://api.github.com/users/phlax/repos","events_url":"https://api.github.com/users/phlax/events{/privacy}","received_events_url":"https://api.github.com/users/phlax/received_events","type":"User","site_admin":false},"repo":{"id":39886550,"name":"pootle","full_name":"phlax/pootle","owner":{"login":"phlax","id":454682,"avatar_url":"https://avatars.githubusercontent.com/u/454682?v=3","gravatar_id":"","url":"https://api.github.com/users/phlax","html_url":"https://github.com/phlax","followers_url":"https://api.github.com/users/phlax/followers","following_url":"https://api.github.com/users/phlax/following{/other_user}","gists_url":"https://api.github.com/users/phlax/gists{/gist_id}","starred_url":"https://api.github.com/users/phlax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/phlax/subscriptions","organizations_url":"https://api.github.com/users/phlax/orgs","repos_url":"https://api.github.com/users/phlax/repos","events_url":"https://api.github.com/users/phlax/events{/privacy}","received_events_url":"https://api.github.com/users/phlax/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/phlax/pootle","description":"Online translation tool","fork":true,"url":"https://api.github.com/repos/phlax/pootle","forks_url":"https://api.github.com/repos/phlax/pootle/forks","keys_url":"https://api.github.com/repos/phlax/pootle/keys{/key_id}","collaborators_url":"https://api.github.com/repos/phlax/pootle/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/phlax/pootle/teams","hooks_url":"https://api.github.com/repos/phlax/pootle/hooks","issue_events_url":"https://api.github.com/repos/phlax/pootle/issues/events{/number}","events_url":"https://api.github.com/repos/phlax/pootle/events","assignees_url":"https://api.github.com/repos/phlax/pootle/assignees{/user}","branches_url":"https://api.github.com/repos/phlax/pootle/branches{/branch}","tags_url":"https://api.github.com/repos/phlax/pootle/tags","blobs_url":"https://api.github.com/repos/phlax/pootle/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/phlax/pootle/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/phlax/pootle/git/refs{/sha}","trees_url":"https://api.github.com/repos/phlax/pootle/git/trees{/sha}","statuses_url":"https://api.github.com/repos/phlax/pootle/statuses/{sha}","languages_url":"https://api.github.com/repos/phlax/pootle/languages","stargazers_url":"https://api.github.com/repos/phlax/pootle/stargazers","contributors_url":"https://api.github.com/repos/phlax/pootle/contributors","subscribers_url":"https://api.github.com/repos/phlax/pootle/subscribers","subscription_url":"https://api.github.com/repos/phlax/pootle/subscription","commits_url":"https://api.github.com/repos/phlax/pootle/commits{/sha}","git_commits_url":"https://api.github.com/repos/phlax/pootle/git/commits{/sha}","comments_url":"https://api.github.com/repos/phlax/pootle/comments{/number}","issue_comment_url":"https://api.github.com/repos/phlax/pootle/issues/comments{/number}","contents_url":"https://api.github.com/repos/phlax/pootle/contents/{+path}","compare_url":"https://api.github.com/repos/phlax/pootle/compare/{base}...{head}","merges_url":"https://api.github.com/repos/phlax/pootle/merges","archive_url":"https://api.github.com/repos/phlax/pootle/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/phlax/pootle/downloads","issues_url":"https://api.github.com/repos/phlax/pootle/issues{/number}","pulls_url":"https://api.github.com/repos/phlax/pootle/pulls{/number}","milestones_url":"https://api.github.com/repos/phlax/pootle/milestones{/number}","notifications_url":"https://api.github.com/repos/phlax/pootle/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/phlax/pootle/labels{/name}","releases_url":"https://api.github.com/repos/phlax/pootle/releases{/id}","deployments_url":"https://api.github.com/repos/phlax/pootle/deployments","created_at":"2015-07-29T10:00:35Z","updated_at":"2016-01-14T21:46:37Z","pushed_at":"2016-09-14T19:38:50Z","git_url":"git://github.com/phlax/pootle.git","ssh_url":"git@github.com:phlax/pootle.git","clone_url":"https://github.com/phlax/pootle.git","svn_url":"https://github.com/phlax/pootle","homepage":"http://pootle.translatehouse.org","size":101639,"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":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"translate:master","ref":"master","sha":"5228e3d89df31492b7271836f1f494edc4b47d62","user":{"login":"translate","id":1538178,"avatar_url":"https://avatars.githubusercontent.com/u/1538178?v=3","gravatar_id":"","url":"https://api.github.com/users/translate","html_url":"https://github.com/translate","followers_url":"https://api.github.com/users/translate/followers","following_url":"https://api.github.com/users/translate/following{/other_user}","gists_url":"https://api.github.com/users/translate/gists{/gist_id}","starred_url":"https://api.github.com/users/translate/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/translate/subscriptions","organizations_url":"https://api.github.com/users/translate/orgs","repos_url":"https://api.github.com/users/translate/repos","events_url":"https://api.github.com/users/translate/events{/privacy}","received_events_url":"https://api.github.com/users/translate/received_events","type":"Organization","site_admin":false},"repo":{"id":3769702,"name":"pootle","full_name":"translate/pootle","owner":{"login":"translate","id":1538178,"avatar_url":"https://avatars.githubusercontent.com/u/1538178?v=3","gravatar_id":"","url":"https://api.github.com/users/translate","html_url":"https://github.com/translate","followers_url":"https://api.github.com/users/translate/followers","following_url":"https://api.github.com/users/translate/following{/other_user}","gists_url":"https://api.github.com/users/translate/gists{/gist_id}","starred_url":"https://api.github.com/users/translate/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/translate/subscriptions","organizations_url":"https://api.github.com/users/translate/orgs","repos_url":"https://api.github.com/users/translate/repos","events_url":"https://api.github.com/users/translate/events{/privacy}","received_events_url":"https://api.github.com/users/translate/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/translate/pootle","description":"Online translation tool","fork":false,"url":"https://api.github.com/repos/translate/pootle","forks_url":"https://api.github.com/repos/translate/pootle/forks","keys_url":"https://api.github.com/repos/translate/pootle/keys{/key_id}","collaborators_url":"https://api.github.com/repos/translate/pootle/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/translate/pootle/teams","hooks_url":"https://api.github.com/repos/translate/pootle/hooks","issue_events_url":"https://api.github.com/repos/translate/pootle/issues/events{/number}","events_url":"https://api.github.com/repos/translate/pootle/events","assignees_url":"https://api.github.com/repos/translate/pootle/assignees{/user}","branches_url":"https://api.github.com/repos/translate/pootle/branches{/branch}","tags_url":"https://api.github.com/repos/translate/pootle/tags","blobs_url":"https://api.github.com/repos/translate/pootle/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/translate/pootle/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/translate/pootle/git/refs{/sha}","trees_url":"https://api.github.com/repos/translate/pootle/git/trees{/sha}","statuses_url":"https://api.github.com/repos/translate/pootle/statuses/{sha}","languages_url":"https://api.github.com/repos/translate/pootle/languages","stargazers_url":"https://api.github.com/repos/translate/pootle/stargazers","contributors_url":"https://api.github.com/repos/translate/pootle/contributors","subscribers_url":"https://api.github.com/repos/translate/pootle/subscribers","subscription_url":"https://api.github.com/repos/translate/pootle/subscription","commits_url":"https://api.github.com/repos/translate/pootle/commits{/sha}","git_commits_url":"https://api.github.com/repos/translate/pootle/git/commits{/sha}","comments_url":"https://api.github.com/repos/translate/pootle/comments{/number}","issue_comment_url":"https://api.github.com/repos/translate/pootle/issues/comments{/number}","contents_url":"https://api.github.com/repos/translate/pootle/contents/{+path}","compare_url":"https://api.github.com/repos/translate/pootle/compare/{base}...{head}","merges_url":"https://api.github.com/repos/translate/pootle/merges","archive_url":"https://api.github.com/repos/translate/pootle/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/translate/pootle/downloads","issues_url":"https://api.github.com/repos/translate/pootle/issues{/number}","pulls_url":"https://api.github.com/repos/translate/pootle/pulls{/number}","milestones_url":"https://api.github.com/repos/translate/pootle/milestones{/number}","notifications_url":"https://api.github.com/repos/translate/pootle/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/translate/pootle/labels{/name}","releases_url":"https://api.github.com/repos/translate/pootle/releases{/id}","deployments_url":"https://api.github.com/repos/translate/pootle/deployments","created_at":"2012-03-19T23:06:03Z","updated_at":"2016-09-12T03:04:34Z","pushed_at":"2016-09-14T19:38:51Z","git_url":"git://github.com/translate/pootle.git","ssh_url":"git@github.com:translate/pootle.git","clone_url":"https://github.com/translate/pootle.git","svn_url":"https://github.com/translate/pootle","homepage":"http://pootle.translatehouse.org","size":106417,"stargazers_count":693,"watchers_count":693,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":197,"mirror_url":null,"open_issues_count":445,"forks":197,"open_issues":445,"watchers":693,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/translate/pootle/pulls/5233"},"html":{"href":"https://github.com/translate/pootle/pull/5233"},"issue":{"href":"https://api.github.com/repos/translate/pootle/issues/5233"},"comments":{"href":"https://api.github.com/repos/translate/pootle/issues/5233/comments"},"review_comments":{"href":"https://api.github.com/repos/translate/pootle/pulls/5233/comments"},"review_comment":{"href":"https://api.github.com/repos/translate/pootle/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/translate/pootle/pulls/5233/commits"},"statuses":{"href":"https://api.github.com/repos/translate/pootle/statuses/2e651c5857ac872d1cf13bf417eb81276859fb54"}}}}
{"actor":{"display_login":"phlax"}}