import requests

from framework.auth.core import _get_current_user

from website.files.models.base import File, Folder, FileNode, FileVersion


__all__ = ('DataverseFile', 'DataverseFolder', 'DataverseFileNode')


class DataverseFileNode(FileNode):
    provider = 'dataverse'


class DataverseFolder(DataverseFileNode, Folder):
    pass


class DataverseFile(DataverseFileNode, File):

    def touch(self, version=None, revision=None, **kwargs):
        """Note: Dataverse only has psuedo versions, don't save them"""
        version = revision or version  # Use revision or version

        resp = requests.get(self.generate_waterbutler_url(meta=True, version=version, **kwargs))
        if resp.status_code != 200:
            return None

        data = resp.json()
        self.name = data['data']['name']
        self.materialized_path = data['data']['materialized']

        version = FileVersion(identifier=version)
        version.update_metadata(data['data'], save=False)

        user = _get_current_user()
        if not user or not self.node.can_edit(user=user):
            try:
                # Users without edit permission can only see published files
                if not data['data']['extra']['hasPublishedVersion']:
                    # Blank out name and path for the render
                    # Dont save because there's no reason to persist the change
                    self.name = ''
                    self.materialized_path = ''
                    return (version, '<div class="alert alert-info" role="alert">This file does not exist.</div>')
            except (KeyError, IndexError):
                pass

from framework.auth.core import _get_current_user

from website.files.models.base import File, Folder, FileNode, FileVersion


__all__ = ('DataverseFile', 'DataverseFolder', 'DataverseFileNode')


class DataverseFileNode(FileNode):
    provider = 'dataverse'


class DataverseFolder(DataverseFileNode, Folder):
    pass


class DataverseFile(DataverseFileNode, File):
    version_identifier = 'version'

    def update(self, revision, data):
        """Note: Dataverse only has psuedo versions, don't save them"""
        self.name = data['name']
        self.materialized_path = data['materialized']

        version = FileVersion(identifier=revision)
        version.update_metadata(data, save=False)

        user = _get_current_user()
        if not user or not self.node.can_edit(user=user):
            try:
                # Users without edit permission can only see published files
                if not data['extra']['hasPublishedVersion']:
                    # Blank out name and path for the render
                    # Dont save because there's no reason to persist the change
                    self.name = ''
                    self.materialized_path = ''
                    return (version, '<div class="alert alert-info" role="alert">This file does not exist.</div>')
            except (KeyError, IndexError):
                pass
        return version

Move override logic into update rather than touch
Move override logic into update rather than touch

- import requests
- 
  from framework.auth.core import _get_current_user
  
  from website.files.models.base import File, Folder, FileNode, FileVersion
  
  
  __all__ = ('DataverseFile', 'DataverseFolder', 'DataverseFileNode')
  
  
  class DataverseFileNode(FileNode):
      provider = 'dataverse'
  
  
  class DataverseFolder(DataverseFileNode, Folder):
      pass
  
  
  class DataverseFile(DataverseFileNode, File):
+     version_identifier = 'version'
  
-     def touch(self, version=None, revision=None, **kwargs):
+     def update(self, revision, data):
          """Note: Dataverse only has psuedo versions, don't save them"""
-         version = revision or version  # Use revision or version
+         self.name = data['name']
+         self.materialized_path = data['materialized']
  
-         resp = requests.get(self.generate_waterbutler_url(meta=True, version=version, **kwargs))
-         if resp.status_code != 200:
-             return None
- 
-         data = resp.json()
-         self.name = data['data']['name']
-         self.materialized_path = data['data']['materialized']
- 
-         version = FileVersion(identifier=version)
+         version = FileVersion(identifier=revision)
-         version.update_metadata(data['data'], save=False)
+         version.update_metadata(data, save=False)
  
          user = _get_current_user()
          if not user or not self.node.can_edit(user=user):
              try:
                  # Users without edit permission can only see published files
-                 if not data['data']['extra']['hasPublishedVersion']:
+                 if not data['extra']['hasPublishedVersion']:
                      # Blank out name and path for the render
                      # Dont save because there's no reason to persist the change
                      self.name = ''
                      self.materialized_path = ''
                      return (version, '<div class="alert alert-info" role="alert">This file does not exist.</div>')
              except (KeyError, IndexError):
                  pass
+         return version
  
Move override logic into update rather than touch
## Code Before:
import requests

from framework.auth.core import _get_current_user

from website.files.models.base import File, Folder, FileNode, FileVersion


__all__ = ('DataverseFile', 'DataverseFolder', 'DataverseFileNode')


class DataverseFileNode(FileNode):
    provider = 'dataverse'


class DataverseFolder(DataverseFileNode, Folder):
    pass


class DataverseFile(DataverseFileNode, File):

    def touch(self, version=None, revision=None, **kwargs):
        """Note: Dataverse only has psuedo versions, don't save them"""
        version = revision or version  # Use revision or version

        resp = requests.get(self.generate_waterbutler_url(meta=True, version=version, **kwargs))
        if resp.status_code != 200:
            return None

        data = resp.json()
        self.name = data['data']['name']
        self.materialized_path = data['data']['materialized']

        version = FileVersion(identifier=version)
        version.update_metadata(data['data'], save=False)

        user = _get_current_user()
        if not user or not self.node.can_edit(user=user):
            try:
                # Users without edit permission can only see published files
                if not data['data']['extra']['hasPublishedVersion']:
                    # Blank out name and path for the render
                    # Dont save because there's no reason to persist the change
                    self.name = ''
                    self.materialized_path = ''
                    return (version, '<div class="alert alert-info" role="alert">This file does not exist.</div>')
            except (KeyError, IndexError):
                pass

## Instruction:
Move override logic into update rather than touch
## Code After:
from framework.auth.core import _get_current_user

from website.files.models.base import File, Folder, FileNode, FileVersion


__all__ = ('DataverseFile', 'DataverseFolder', 'DataverseFileNode')


class DataverseFileNode(FileNode):
    provider = 'dataverse'


class DataverseFolder(DataverseFileNode, Folder):
    pass


class DataverseFile(DataverseFileNode, File):
    version_identifier = 'version'

    def update(self, revision, data):
        """Note: Dataverse only has psuedo versions, don't save them"""
        self.name = data['name']
        self.materialized_path = data['materialized']

        version = FileVersion(identifier=revision)
        version.update_metadata(data, save=False)

        user = _get_current_user()
        if not user or not self.node.can_edit(user=user):
            try:
                # Users without edit permission can only see published files
                if not data['extra']['hasPublishedVersion']:
                    # Blank out name and path for the render
                    # Dont save because there's no reason to persist the change
                    self.name = ''
                    self.materialized_path = ''
                    return (version, '<div class="alert alert-info" role="alert">This file does not exist.</div>')
            except (KeyError, IndexError):
                pass
        return version
