# Path: nasman/snapshots/models.py
# class IconMapping(models.Model):
#     """
#     Model to manage icon mapping to mimetypes
#     """
#     icon = IconField(
#         'icon',
#         default='fa-file-o'
#     )
#     mime_type = models.CharField(
#         'mime-type',
#         max_length=255,
#         primary_key=True,
#         db_index=True
#     )
# 
#     class Meta:
#         app_label = 'snapshots'
# 
#     def __str__(self):
#         return self.mime_type
# 
# Path: nasman/snapshots/forms.py
# class FileBrowserForm(forms.Form):
#     filesystem = FilesystemField(
#         choices=filesystem_choices,
#         widget=SmallSelectWidget()
#     )
#     snapshot = SnapshotField(widget=forms.HiddenInput)
#     path = PathField(widget=forms.HiddenInput)
# 
# Path: nasman/snapshots/utils/zfs.py
# class ZFSUtil(BaseUtil):
#     """
#     Helper utility for accessing ZFS features
#     """
# 
#     @classmethod
#     def get_filesystems(cls):
#         """
#         Gets a list of filesystems
#         :return: A list of `ZFSFilesystem` objects
#         :rtype: list
#         """
#         parsed = _parse_cmd_output(
#             ['zfs', 'list', '-Hp', '-tfilesystem', '-oname,mountpoint']
#         )
#         if parsed is None:
#             return None
#         filesystems = []
#         for fs in parsed:
#             filesystems.append(
#                 ZFSFilesystem(
#                     fs[0],
#                     fs[1]
#                 )
#             )
#         return filesystems
# 
#     @classmethod
#     def get_snapshots(cls, refresh=False):
#         """
#         Gets a list of snapshots
#         :return: A list of `ZFSSnapshot`
#         :rtype: list
#         """
#         cached = cache.get('zfs-snapshots')
#         if not refresh and cached is not None:
#             logger.info('Returning cached list')
#             return cached
#         parsed = _parse_cmd_output(
#             ['zfs', 'list', '-Hp', '-tsnap', '-oname,creation']
#         )
#         if parsed is None:
#             return None
#         snaps = []
#         for snap in parsed:
#             ts = datetime.fromtimestamp(int(snap[1]))
#             ts = pytz.timezone(cls.timezone_name).localize(ts)
#             snaps.append(
#                 ZFSSnapshot(
#                     snap[0],
#                     ts
#                 )
#             )
#         cache.set('zfs-snapshots', snaps, 43200)
#         logger.info('Refreshed list of snapshots {}'.format(snaps))
#         return snaps
# 
#     @classmethod
#     def get_filesystem(cls, fs_name):
#         """
#         Gets a filesystem by name
#         :param fs_name: Name of the filesystem to get
#         :type fs_name: basestring
#         :return: A `ZFSFilesystem`
#         :rtype: `nasman.snapshots.zfs.ZFSFilesystem`
#         """
#         parsed = _parse_cmd_output(
#             ['zfs',
#              'list',
#              '-Hp',
#              '-tfilesystem',
#              '-oname,mountpoint',
#              fs_name]
#         )
#         if parsed is None:
#             return None
#         return ZFSFilesystem(parsed[0][0], parsed[0][1])
# 
#     @classmethod
#     def get_snapshot(cls, snap_name):
#         """
#         Gets a snapshot by name
#         :param snap_name: Name of the snapshot to get
#         :type snap_name: basestring
#         :return: A `ZFSSnapshot`
#         :rtype: `nasman.snapshots.zfs.ZFSSnapshot`
#         """
#         parsed = _parse_cmd_output(
#             ['zfs',
#              'list',
#              '-Hp',
#              '-tsnap',
#              '-oname,creation',
#              snap_name]
#         )
#         if parsed is None:
#             return None
#         return ZFSSnapshot(parsed[0][0], parsed[0][1])
# 
#     @classmethod
#     def create_snapshot(cls, snap_name, fs_name, recurse):
#         """
#         Creates a snapshot
#         :param snap_name: Name of the snapshot to create
#         :type snap_name: str
#         :param fs_name: Name of the filesystem to take snapshot of
#         :type fs_name: str
#         :param recurse: Whether to recurse filesystems
#         :type recurse: bool
#         :return: The snapshot just created
#         :rtype: `nasman.snapshots.zfs.ZFSSnapshot`
#         """
#         cache.delete('zfs-snapshots')
#         full_name = '{0}@{1}'.format(
#             fs_name,
#             snap_name
#         )
#         cmd = ['zfs',
#                'snapshot',
#                full_name
#                ]
#         if recurse:
#             cmd.append('-r')
#         parsed = _parse_cmd_output(cmd)
#         if parsed is None:
#             return None
#         return cls.get_snapshot(full_name)
# 
# Path: nasman/snapshots/views/base.py
# class BaseView(SetHeadlineMixin):
#     pass


from collections import defaultdict
from datetime import datetime
from pathlib import Path
from django.core.urlresolvers import reverse_lazy
from vanilla import TemplateView, FormView
from ..models import IconMapping
from ..forms import FileBrowserForm
from ..utils.zfs import ZFSUtil
from ..views.base import BaseView
import logging
import magic
                'full_path': x,
                'directory': x.is_dir(),
                'mime_type': mime_type,
                'modified': datetime.fromtimestamp(x.lstat().st_mtime),
                'size': x.lstat().st_size,
                'icon': icon_mapping[mime_type],
            })
        object_list.sort(key=lambda k: (not k['directory'], k['name']))

        # Path breadcrumbs
        breadcrumbs = sorted(path.parents) + [path]

        # The up one '..' link
        up_one = None
        if path != root:
            up_one = path.parent

        # TODO: context relevant snapshot list
        context.update({
            'up_one': up_one,
            'files_list': object_list,
            'path': breadcrumbs,
            'browser_title': breadcrumbs[-1],
        })
        return context

    def form_valid(self, form):
        return self.form_invalid(form)



# Path: nasman/snapshots/models.py
# class IconMapping(models.Model):
#     """
#     Model to manage icon mapping to mimetypes
#     """
#     icon = IconField(
#         'icon',
#         default='fa-file-o'
#     )
#     mime_type = models.CharField(
#         'mime-type',
#         max_length=255,
#         primary_key=True,
#         db_index=True
#     )
# 
#     class Meta:
#         app_label = 'snapshots'
# 
#     def __str__(self):
#         return self.mime_type
# 
# Path: nasman/snapshots/forms.py
# class FileBrowserForm(forms.Form):
#     filesystem = FilesystemField(
#         choices=filesystem_choices,
#         widget=SmallSelectWidget()
#     )
#     snapshot = SnapshotField(widget=forms.HiddenInput)
#     path = PathField(widget=forms.HiddenInput)
# 
# Path: nasman/snapshots/utils/zfs.py
# class ZFSUtil(BaseUtil):
#     """
#     Helper utility for accessing ZFS features
#     """
# 
#     @classmethod
#     def get_filesystems(cls):
#         """
#         Gets a list of filesystems
#         :return: A list of `ZFSFilesystem` objects
#         :rtype: list
#         """
#         parsed = _parse_cmd_output(
#             ['zfs', 'list', '-Hp', '-tfilesystem', '-oname,mountpoint']
#         )
#         if parsed is None:
#             return None
#         filesystems = []
#         for fs in parsed:
#             filesystems.append(
#                 ZFSFilesystem(
#                     fs[0],
#                     fs[1]
#                 )
#             )
#         return filesystems
# 
#     @classmethod
#     def get_snapshots(cls, refresh=False):
#         """
#         Gets a list of snapshots
#         :return: A list of `ZFSSnapshot`
#         :rtype: list
#         """
#         cached = cache.get('zfs-snapshots')
#         if not refresh and cached is not None:
#             logger.info('Returning cached list')
#             return cached
#         parsed = _parse_cmd_output(
#             ['zfs', 'list', '-Hp', '-tsnap', '-oname,creation']
#         )
#         if parsed is None:
#             return None
#         snaps = []
#         for snap in parsed:
#             ts = datetime.fromtimestamp(int(snap[1]))
#             ts = pytz.timezone(cls.timezone_name).localize(ts)
#             snaps.append(
#                 ZFSSnapshot(
#                     snap[0],
#                     ts
#                 )
#             )
#         cache.set('zfs-snapshots', snaps, 43200)
#         logger.info('Refreshed list of snapshots {}'.format(snaps))
#         return snaps
# 
#     @classmethod
#     def get_filesystem(cls, fs_name):
#         """
#         Gets a filesystem by name
#         :param fs_name: Name of the filesystem to get
#         :type fs_name: basestring
#         :return: A `ZFSFilesystem`
#         :rtype: `nasman.snapshots.zfs.ZFSFilesystem`
#         """
#         parsed = _parse_cmd_output(
#             ['zfs',
#              'list',
#              '-Hp',
#              '-tfilesystem',
#              '-oname,mountpoint',
#              fs_name]
#         )
#         if parsed is None:
#             return None
#         return ZFSFilesystem(parsed[0][0], parsed[0][1])
# 
#     @classmethod
#     def get_snapshot(cls, snap_name):
#         """
#         Gets a snapshot by name
#         :param snap_name: Name of the snapshot to get
#         :type snap_name: basestring
#         :return: A `ZFSSnapshot`
#         :rtype: `nasman.snapshots.zfs.ZFSSnapshot`
#         """
#         parsed = _parse_cmd_output(
#             ['zfs',
#              'list',
#              '-Hp',
#              '-tsnap',
#              '-oname,creation',
#              snap_name]
#         )
#         if parsed is None:
#             return None
#         return ZFSSnapshot(parsed[0][0], parsed[0][1])
# 
#     @classmethod
#     def create_snapshot(cls, snap_name, fs_name, recurse):
#         """
#         Creates a snapshot
#         :param snap_name: Name of the snapshot to create
#         :type snap_name: str
#         :param fs_name: Name of the filesystem to take snapshot of
#         :type fs_name: str
#         :param recurse: Whether to recurse filesystems
#         :type recurse: bool
#         :return: The snapshot just created
#         :rtype: `nasman.snapshots.zfs.ZFSSnapshot`
#         """
#         cache.delete('zfs-snapshots')
#         full_name = '{0}@{1}'.format(
#             fs_name,
#             snap_name
#         )
#         cmd = ['zfs',
#                'snapshot',
#                full_name
#                ]
#         if recurse:
#             cmd.append('-r')
#         parsed = _parse_cmd_output(cmd)
#         if parsed is None:
#             return None
#         return cls.get_snapshot(full_name)
# 
# Path: nasman/snapshots/views/base.py
# class BaseView(SetHeadlineMixin):
#     pass


# Path: nasman/snapshots/views/filesystems.py
from collections import defaultdict
from datetime import datetime
from pathlib import Path
from django.core.urlresolvers import reverse_lazy
from vanilla import TemplateView, FormView
from ..models import IconMapping
from ..forms import FileBrowserForm
from ..utils.zfs import ZFSUtil
from ..views.base import BaseView
import logging
import magic

                'full_path': x,
                'directory': x.is_dir(),
                'mime_type': mime_type,
                'modified': datetime.fromtimestamp(x.lstat().st_mtime),
                'size': x.lstat().st_size,
                'icon': icon_mapping[mime_type],
            })
        object_list.sort(key=lambda k: (not k['directory'], k['name']))

        # Path breadcrumbs
        breadcrumbs = sorted(path.parents) + [path]

        # The up one '..' link
        up_one = None
        if path != root:
            up_one = path.parent

        # TODO: context relevant snapshot list
        context.update({
            'up_one': up_one,
            'files_list': object_list,
            'path': breadcrumbs,
            'browser_title': breadcrumbs[-1],
        })
        return context

    def form_valid(self, form):
        return self.form_invalid(form)



class FilesystemList(BaseView, TemplateView):