# Path: privagal/core/mixins.py
# class PageLoginRequiredMixin(object):
# 
#     def dispatch(self, request, *args, **kwargs):
#         page = Page.objects.get(slug='timeline')
#         check_view_restrictions(request, page)
#         return super(PageLoginRequiredMixin, self).dispatch(request, *args, **kwargs)
# 
# Path: privagal/core/models.py
# class Token(models.Model):
#     """
#     Heavily copied from rest_framework.authtoken.models.Token
#     """
#     key = models.CharField(_("Key"), max_length=40, primary_key=True)
#     created = models.DateTimeField(_("Created"), auto_now_add=True)
# 
#     objects = TokenManager()
# 
#     class Meta:
#         db_table = 'privagal_token'
#         get_latest_by = "created"
# 
#     def save(self, *args, **kwargs):
#         if not self.key:
#             self.key = Token.generate_key()
#         return super(Token, self).save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.key
# 
#     @classmethod
#     def generate_key(cls):
#         return binascii.hexlify(os.urandom(20)).decode()


import os
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.views.generic import View
from wagtail.contrib.modeladmin.views import CreateView
from wagtail.wagtailimages.views.serve import SendFileView
from sendfile import sendfile
from .mixins import PageLoginRequiredMixin
from .models import Token
# -*- coding: utf-8 -*-
from __future__ import unicode_literals







class SendFilePrivateView(PageLoginRequiredMixin, SendFileView):
    pass


class BackOfficeSendFileView(View):

    def dispatch(self, request, filepath, *args, **kwargs):
        if not request.user.is_staff:
            raise PermissionDenied

        # No need to check file to serve: we blindly rely on backend. For
        # example, you need to properly configure nginx to **only** serve
        # private file from that directory. Same goes for Apache.
        path = os.path.join(settings.MEDIA_ROOT, filepath)
        return sendfile(request, path)


class TokenCreateView(CreateView):

    def get_initial(self):
        initial = super(TokenCreateView, self).get_initial()

# Path: privagal/core/mixins.py
# class PageLoginRequiredMixin(object):
# 
#     def dispatch(self, request, *args, **kwargs):
#         page = Page.objects.get(slug='timeline')
#         check_view_restrictions(request, page)
#         return super(PageLoginRequiredMixin, self).dispatch(request, *args, **kwargs)
# 
# Path: privagal/core/models.py
# class Token(models.Model):
#     """
#     Heavily copied from rest_framework.authtoken.models.Token
#     """
#     key = models.CharField(_("Key"), max_length=40, primary_key=True)
#     created = models.DateTimeField(_("Created"), auto_now_add=True)
# 
#     objects = TokenManager()
# 
#     class Meta:
#         db_table = 'privagal_token'
#         get_latest_by = "created"
# 
#     def save(self, *args, **kwargs):
#         if not self.key:
#             self.key = Token.generate_key()
#         return super(Token, self).save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.key
# 
#     @classmethod
#     def generate_key(cls):
#         return binascii.hexlify(os.urandom(20)).decode()


# Path: privagal/core/views.py
import os
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.views.generic import View
from wagtail.contrib.modeladmin.views import CreateView
from wagtail.wagtailimages.views.serve import SendFileView
from sendfile import sendfile
from .mixins import PageLoginRequiredMixin
from .models import Token

# -*- coding: utf-8 -*-
from __future__ import unicode_literals







class SendFilePrivateView(PageLoginRequiredMixin, SendFileView):
    pass


class BackOfficeSendFileView(View):

    def dispatch(self, request, filepath, *args, **kwargs):
        if not request.user.is_staff:
            raise PermissionDenied

        # No need to check file to serve: we blindly rely on backend. For
        # example, you need to properly configure nginx to **only** serve
        # private file from that directory. Same goes for Apache.
        path = os.path.join(settings.MEDIA_ROOT, filepath)
        return sendfile(request, path)


class TokenCreateView(CreateView):

    def get_initial(self):
        initial = super(TokenCreateView, self).get_initial()

        initial['key'] = Token.generate_key()