import logging
import requests

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


logger = logging.getLogger(__name__)
__all__ = ('FigshareFile', 'FigshareFolder', 'FigshareFileNode')


class FigshareFileNode(FileNode):
    provider = 'figshare'


class FigshareFolder(FigshareFileNode, Folder):
    pass


class FigshareFile(FigshareFileNode, File):
    def touch(self, revision=None, **kwargs):
        """Figshare does not support versioning.
        Always pass revision as None to avoid conflict.
        """

        resp = requests.get(self.generate_waterbutler_url(meta=True, **kwargs))
        if resp.status_code != 200:
            logger.warning('Unable to find {} got status code {}'.format(self, resp.status_code))
            return None

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

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

        # Draft files are not renderable
        if data['extra']['status'] == 'drafts':
            return (version, u'''
            <div class="alert alert-info" role="alert">
            The file "{name}" is still a draft on figshare. <br>
            To view it  on the OSF <a href="http://figshare.com/faqs">publish</a> it on figshare.
            </div>
            '''.format(name=escape_html(self.name)))

        return version

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


__all__ = ('FigshareFile', 'FigshareFolder', 'FigshareFileNode')


class FigshareFileNode(FileNode):
    provider = 'figshare'


class FigshareFolder(FigshareFileNode, Folder):
    pass


class FigshareFile(FigshareFileNode, File):

    def touch(self, bearer, revision=None, **kwargs):
        return super(FigshareFile, self).touch(bearer, revision=None, **kwargs)

    def update(self, revision, data):
        """Figshare does not support versioning.
        Always pass revision as None to avoid conflict.
        """
        self.name = data['name']
        self.materialized_path = data['materialized']
        self.save()

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

        # Draft files are not renderable
        if data['extra']['status'] == 'drafts':
            return (version, u'''
            <div class="alert alert-info" role="alert">
            The file "{name}" is still a draft on figshare. <br>
            To view it  on the OSF <a href="http://figshare.com/faqs">publish</a> it on figshare.
            </div>
            '''.format(name=escape_html(self.name)))

        return version

Change figshare's touch override to an update override