{"action":"created","comment":{"url":"https://api.github.com/repos/CenterForOpenScience/osf.io/pulls/comments/24831675","id":24831675,"diff_hunk":"@@ -0,0 +1,328 @@\n+# -*- coding: utf-8 -*-\n+import os\n+import base64\n+import hashlib\n+import logging\n+import urllib\n+from datetime import datetime\n+\n+import furl\n+\n+from modularodm import fields, Q\n+from modularodm.exceptions import ModularOdmException\n+\n+from framework.auth import Auth\n+from website.addons.base import exceptions\n+from website.addons.base import AddonUserSettingsBase, AddonNodeSettingsBase, GuidFile\n+\n+from website.addons.box.client import get_node_addon_client\n+from website.addons.box.utils import clean_path, BoxNodeLogger, refresh_creds_if_necessary\n+from website.addons.box import settings\n+\n+from box import CredentialsV2\n+\n+logger = logging.getLogger(__name__)\n+\n+\n+class BoxFile(GuidFile):\n+    \"\"\"A Box file model with a GUID. Created lazily upon viewing a\n+    file's detail page.\n+    \"\"\"\n+\n+    #: Full path to the file, e.g. 'My Pictures/foo.png'\n+    path = fields.StringField(required=True, index=True)\n+\n+    @property\n+    def file_name(self):\n+        if self.revision:\n+            return '{0}_{1}_{2}.html'.format(self._id, self.revision, base64.b64encode(self.folder))\n+        return '{0}_{1}_{2}.html'.format(self._id, self.unique_identifier, base64.b64encode(self.folder))\n+\n+    @property\n+    def waterbutler_path(self):\n+        path = self.path\n+        if self.folder == '/':\n+            return path\n+        return path.replace(self.folder, '', 1)\n+\n+    @property\n+    def folder(self):\n+        return self.node.get_addon('box').folder\n+\n+    @property\n+    def provider(self):\n+        return 'box'\n+\n+    @property\n+    def version_identifier(self):\n+        return 'revision'\n+\n+    @property\n+    def unique_identifier(self):\n+        return self.path.split('/')[1][-5] #self._metadata_cache['extra']['revisionId']\n+\n+    @classmethod\n+    def get_or_create(cls, node, path):\n+        \"\"\"Get or create a new file record. Return a tuple of the form (obj, created)\n+        \"\"\"\n+        cleaned_path = clean_path(path)\n+        try:\n+            new = cls.find_one(\n+                Q('node', 'eq', node) &\n+                Q('path', 'eq', cleaned_path)\n+            )\n+            created = False\n+        except ModularOdmException:\n+            # Create new\n+            new = cls(node=node, path=cleaned_path)\n+            new.save()\n+            created = True\n+        return new, created\n+\n+\n+class BoxUserSettings(AddonUserSettingsBase):\n+    \"\"\"Stores user-specific box information, including the Oauth access\n+    token.\n+    \"\"\"\n+\n+    box_id = fields.StringField(required=False)\n+    access_token = fields.StringField(required=False)\n+    refresh_token = fields.StringField(required=False)\n+    box_info = fields.DictionaryField(required=False)\n+    token_type = fields.StringField(required=False)\n+    restricted_to = fields.DictionaryField(required=False)\n+    last_refreshed = fields.DateTimeField(editable=True)\n+\n+\n+    # TODO(sloria): The `user` param in unnecessary for AddonUserSettings\n+    def to_json(self, user=None):\n+        \"\"\"Return a dictionary representation of the user settings.\n+        The dictionary keys and values will be available as variables in\n+        box_user_settings.mako.\n+        \"\"\"\n+        output = super(BoxUserSettings, self).to_json(self.owner)\n+        output['has_auth'] = self.has_auth\n+        return output\n+\n+    def token_refreshed_callback(self, access_token, refresh_token):\n+        self.access_token = access_token\n+        self.refresh_token = refresh_token\n+        self.last_refreshed = datetime.utcnow()\n+        self.save()\n+\n+    @property\n+    def has_auth(self):\n+        return bool(self.access_token and refresh_creds_if_necessary(self))","path":"website/addons/box/model.py","position":115,"original_position":115,"commit_id":"dc84b36be66b266c1614645210ba28345d5011cc","original_commit_id":"dc84b36be66b266c1614645210ba28345d5011cc","user":{"login":"chrisseto","id":5532905,"avatar_url":"https://avatars.githubusercontent.com/u/5532905?v=3","gravatar_id":"","url":"https://api.github.com/users/chrisseto","html_url":"https://github.com/chrisseto","followers_url":"https://api.github.com/users/chrisseto/followers","following_url":"https://api.github.com/users/chrisseto/following{/other_user}","gists_url":"https://api.github.com/users/chrisseto/gists{/gist_id}","starred_url":"https://api.github.com/users/chrisseto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chrisseto/subscriptions","organizations_url":"https://api.github.com/users/chrisseto/orgs","repos_url":"https://api.github.com/users/chrisseto/repos","events_url":"https://api.github.com/users/chrisseto/events{/privacy}","received_events_url":"https://api.github.com/users/chrisseto/received_events","type":"User","site_admin":false},"body":"Can `refresh_creds_if_necessary` be moved into this class as self is being passed to it anyways?","created_at":"2015-02-17T17:02:57Z","updated_at":"2015-02-17T17:02:57Z","html_url":"https://github.com/CenterForOpenScience/osf.io/pull/1758#discussion_r24831675","pull_request_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/pulls/1758","_links":{"self":{"href":"https://api.github.com/repos/CenterForOpenScience/osf.io/pulls/comments/24831675"},"html":{"href":"https://github.com/CenterForOpenScience/osf.io/pull/1758#discussion_r24831675"},"pull_request":{"href":"https://api.github.com/repos/CenterForOpenScience/osf.io/pulls/1758"}}},"pull_request":{"url":"https://api.github.com/repos/CenterForOpenScience/osf.io/pulls/1758","id":29050739,"html_url":"https://github.com/CenterForOpenScience/osf.io/pull/1758","diff_url":"https://github.com/CenterForOpenScience/osf.io/pull/1758.diff","patch_url":"https://github.com/CenterForOpenScience/osf.io/pull/1758.patch","issue_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/issues/1758","number":1758,"state":"open","locked":false,"title":"[wip] Box addon","user":{"login":"mfraezz","id":5659262,"avatar_url":"https://avatars.githubusercontent.com/u/5659262?v=3","gravatar_id":"","url":"https://api.github.com/users/mfraezz","html_url":"https://github.com/mfraezz","followers_url":"https://api.github.com/users/mfraezz/followers","following_url":"https://api.github.com/users/mfraezz/following{/other_user}","gists_url":"https://api.github.com/users/mfraezz/gists{/gist_id}","starred_url":"https://api.github.com/users/mfraezz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mfraezz/subscriptions","organizations_url":"https://api.github.com/users/mfraezz/orgs","repos_url":"https://api.github.com/users/mfraezz/repos","events_url":"https://api.github.com/users/mfraezz/events{/privacy}","received_events_url":"https://api.github.com/users/mfraezz/received_events","type":"User","site_admin":false},"body":"Code review ready\r\n-------------\r\nInitial pass complete\r\nTests still need work\r\n\r\nPurpose\r\n=======\r\nAdd Box to the supported addons\r\nAccompanies: https://github.com/CenterForOpenScience/waterbutler/pull/9\r\n\r\nChanges\r\n=======\r\n\r\nSide Effects\r\n=========\r\n","created_at":"2015-02-10T22:20:23Z","updated_at":"2015-02-17T17:02:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d661038a39390fe0c8d23fa6110fe9ef34399281","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/pulls/1758/commits","review_comments_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/pulls/1758/comments","review_comment_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/pulls/comments/{number}","comments_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/issues/1758/comments","statuses_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/statuses/dc84b36be66b266c1614645210ba28345d5011cc","head":{"label":"mfraezz:feature/box-addon","ref":"feature/box-addon","sha":"dc84b36be66b266c1614645210ba28345d5011cc","user":{"login":"mfraezz","id":5659262,"avatar_url":"https://avatars.githubusercontent.com/u/5659262?v=3","gravatar_id":"","url":"https://api.github.com/users/mfraezz","html_url":"https://github.com/mfraezz","followers_url":"https://api.github.com/users/mfraezz/followers","following_url":"https://api.github.com/users/mfraezz/following{/other_user}","gists_url":"https://api.github.com/users/mfraezz/gists{/gist_id}","starred_url":"https://api.github.com/users/mfraezz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mfraezz/subscriptions","organizations_url":"https://api.github.com/users/mfraezz/orgs","repos_url":"https://api.github.com/users/mfraezz/repos","events_url":"https://api.github.com/users/mfraezz/events{/privacy}","received_events_url":"https://api.github.com/users/mfraezz/received_events","type":"User","site_admin":false},"repo":{"id":26929858,"name":"osf.io","full_name":"mfraezz/osf.io","owner":{"login":"mfraezz","id":5659262,"avatar_url":"https://avatars.githubusercontent.com/u/5659262?v=3","gravatar_id":"","url":"https://api.github.com/users/mfraezz","html_url":"https://github.com/mfraezz","followers_url":"https://api.github.com/users/mfraezz/followers","following_url":"https://api.github.com/users/mfraezz/following{/other_user}","gists_url":"https://api.github.com/users/mfraezz/gists{/gist_id}","starred_url":"https://api.github.com/users/mfraezz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mfraezz/subscriptions","organizations_url":"https://api.github.com/users/mfraezz/orgs","repos_url":"https://api.github.com/users/mfraezz/repos","events_url":"https://api.github.com/users/mfraezz/events{/privacy}","received_events_url":"https://api.github.com/users/mfraezz/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mfraezz/osf.io","description":"Facilitating Open Science","fork":true,"url":"https://api.github.com/repos/mfraezz/osf.io","forks_url":"https://api.github.com/repos/mfraezz/osf.io/forks","keys_url":"https://api.github.com/repos/mfraezz/osf.io/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mfraezz/osf.io/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mfraezz/osf.io/teams","hooks_url":"https://api.github.com/repos/mfraezz/osf.io/hooks","issue_events_url":"https://api.github.com/repos/mfraezz/osf.io/issues/events{/number}","events_url":"https://api.github.com/repos/mfraezz/osf.io/events","assignees_url":"https://api.github.com/repos/mfraezz/osf.io/assignees{/user}","branches_url":"https://api.github.com/repos/mfraezz/osf.io/branches{/branch}","tags_url":"https://api.github.com/repos/mfraezz/osf.io/tags","blobs_url":"https://api.github.com/repos/mfraezz/osf.io/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mfraezz/osf.io/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mfraezz/osf.io/git/refs{/sha}","trees_url":"https://api.github.com/repos/mfraezz/osf.io/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mfraezz/osf.io/statuses/{sha}","languages_url":"https://api.github.com/repos/mfraezz/osf.io/languages","stargazers_url":"https://api.github.com/repos/mfraezz/osf.io/stargazers","contributors_url":"https://api.github.com/repos/mfraezz/osf.io/contributors","subscribers_url":"https://api.github.com/repos/mfraezz/osf.io/subscribers","subscription_url":"https://api.github.com/repos/mfraezz/osf.io/subscription","commits_url":"https://api.github.com/repos/mfraezz/osf.io/commits{/sha}","git_commits_url":"https://api.github.com/repos/mfraezz/osf.io/git/commits{/sha}","comments_url":"https://api.github.com/repos/mfraezz/osf.io/comments{/number}","issue_comment_url":"https://api.github.com/repos/mfraezz/osf.io/issues/comments/{number}","contents_url":"https://api.github.com/repos/mfraezz/osf.io/contents/{+path}","compare_url":"https://api.github.com/repos/mfraezz/osf.io/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mfraezz/osf.io/merges","archive_url":"https://api.github.com/repos/mfraezz/osf.io/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mfraezz/osf.io/downloads","issues_url":"https://api.github.com/repos/mfraezz/osf.io/issues{/number}","pulls_url":"https://api.github.com/repos/mfraezz/osf.io/pulls{/number}","milestones_url":"https://api.github.com/repos/mfraezz/osf.io/milestones{/number}","notifications_url":"https://api.github.com/repos/mfraezz/osf.io/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mfraezz/osf.io/labels{/name}","releases_url":"https://api.github.com/repos/mfraezz/osf.io/releases{/id}","created_at":"2014-11-20T20:12:10Z","updated_at":"2015-01-28T20:39:13Z","pushed_at":"2015-02-16T22:27:13Z","git_url":"git://github.com/mfraezz/osf.io.git","ssh_url":"git@github.com:mfraezz/osf.io.git","clone_url":"https://github.com/mfraezz/osf.io.git","svn_url":"https://github.com/mfraezz/osf.io","homepage":"http://osf.io","size":75603,"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":"develop"}},"base":{"label":"CenterForOpenScience:develop","ref":"develop","sha":"dd55c4ebaf3051e4460b6226bebc23dbb2deeca6","user":{"login":"CenterForOpenScience","id":3344584,"avatar_url":"https://avatars.githubusercontent.com/u/3344584?v=3","gravatar_id":"","url":"https://api.github.com/users/CenterForOpenScience","html_url":"https://github.com/CenterForOpenScience","followers_url":"https://api.github.com/users/CenterForOpenScience/followers","following_url":"https://api.github.com/users/CenterForOpenScience/following{/other_user}","gists_url":"https://api.github.com/users/CenterForOpenScience/gists{/gist_id}","starred_url":"https://api.github.com/users/CenterForOpenScience/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CenterForOpenScience/subscriptions","organizations_url":"https://api.github.com/users/CenterForOpenScience/orgs","repos_url":"https://api.github.com/users/CenterForOpenScience/repos","events_url":"https://api.github.com/users/CenterForOpenScience/events{/privacy}","received_events_url":"https://api.github.com/users/CenterForOpenScience/received_events","type":"Organization","site_admin":false},"repo":{"id":10199599,"name":"osf.io","full_name":"CenterForOpenScience/osf.io","owner":{"login":"CenterForOpenScience","id":3344584,"avatar_url":"https://avatars.githubusercontent.com/u/3344584?v=3","gravatar_id":"","url":"https://api.github.com/users/CenterForOpenScience","html_url":"https://github.com/CenterForOpenScience","followers_url":"https://api.github.com/users/CenterForOpenScience/followers","following_url":"https://api.github.com/users/CenterForOpenScience/following{/other_user}","gists_url":"https://api.github.com/users/CenterForOpenScience/gists{/gist_id}","starred_url":"https://api.github.com/users/CenterForOpenScience/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CenterForOpenScience/subscriptions","organizations_url":"https://api.github.com/users/CenterForOpenScience/orgs","repos_url":"https://api.github.com/users/CenterForOpenScience/repos","events_url":"https://api.github.com/users/CenterForOpenScience/events{/privacy}","received_events_url":"https://api.github.com/users/CenterForOpenScience/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/CenterForOpenScience/osf.io","description":"Facilitating Open Science","fork":false,"url":"https://api.github.com/repos/CenterForOpenScience/osf.io","forks_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/forks","keys_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/keys{/key_id}","collaborators_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/teams","hooks_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/hooks","issue_events_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/issues/events{/number}","events_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/events","assignees_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/assignees{/user}","branches_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/branches{/branch}","tags_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/tags","blobs_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/git/refs{/sha}","trees_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/git/trees{/sha}","statuses_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/statuses/{sha}","languages_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/languages","stargazers_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/stargazers","contributors_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/contributors","subscribers_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/subscribers","subscription_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/subscription","commits_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/commits{/sha}","git_commits_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/git/commits{/sha}","comments_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/comments{/number}","issue_comment_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/issues/comments/{number}","contents_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/contents/{+path}","compare_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/compare/{base}...{head}","merges_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/merges","archive_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/downloads","issues_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/issues{/number}","pulls_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/pulls{/number}","milestones_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/milestones{/number}","notifications_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/labels{/name}","releases_url":"https://api.github.com/repos/CenterForOpenScience/osf.io/releases{/id}","created_at":"2013-05-21T15:53:37Z","updated_at":"2015-02-17T16:25:13Z","pushed_at":"2015-02-17T16:25:13Z","git_url":"git://github.com/CenterForOpenScience/osf.io.git","ssh_url":"git@github.com:CenterForOpenScience/osf.io.git","clone_url":"https://github.com/CenterForOpenScience/osf.io.git","svn_url":"https://github.com/CenterForOpenScience/osf.io","homepage":"http://osf.io","size":86004,"stargazers_count":70,"watchers_count":70,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":45,"mirror_url":null,"open_issues_count":356,"forks":45,"open_issues":356,"watchers":70,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/CenterForOpenScience/osf.io/pulls/1758"},"html":{"href":"https://github.com/CenterForOpenScience/osf.io/pull/1758"},"issue":{"href":"https://api.github.com/repos/CenterForOpenScience/osf.io/issues/1758"},"comments":{"href":"https://api.github.com/repos/CenterForOpenScience/osf.io/issues/1758/comments"},"review_comments":{"href":"https://api.github.com/repos/CenterForOpenScience/osf.io/pulls/1758/comments"},"review_comment":{"href":"https://api.github.com/repos/CenterForOpenScience/osf.io/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/CenterForOpenScience/osf.io/pulls/1758/commits"},"statuses":{"href":"https://api.github.com/repos/CenterForOpenScience/osf.io/statuses/dc84b36be66b266c1614645210ba28345d5011cc"}}}}