# Path: privagal/gallery/factories.py
# class GalleryFactory(PageFactory):
# 
#     class Meta:
#         model = Gallery
# 
#     date = fuzzy.FuzzyDate(datetime.date(2016, 1, 1))
#     body = factory.LazyAttribute(
#         lambda x: "<p>{}</p>".format(faker.paragraph()))
# 
#     @factory.post_generation
#     def images(self, created, extracted, **kwargs):
#         """
#         Usage example:
#         >>> GalleryFactory(images__images=[ImageFactory()])
#         """
#         images = kwargs.pop('images', [])
#         if not images:
#             images = [ImageFactory() for i in range(0, 3)]
# 
#         self.images = [ImageGalleryFactory.build(page=self, image=i) for i in images]
# 
# class ImageFactory(factory.DjangoModelFactory):
# 
#     class Meta:
#         model = get_image_model()
# 
#     title = factory.Faker('sentence', locale=locale)
#     file = factory.django.ImageField()
# 
# Path: privagal/timeline/models.py
# class Timeline(AuthTokenPageMixin, Page):
# 
#     parent_page_types = []
#     subpage_types = ['gallery.Gallery']
# 
#     paginate_by = 20
# 
#     class Meta:
#         db_table = 'privagal_timeline'
# 
#     def get_context(self, request, *args, **kwargs):
#         context = super(Timeline, self).get_context(request, *args, **kwargs)
# 
#         search_query = request.GET.get('query', None)
#         context['search_query'] = search_query
# 
#         paginator, page = self.get_galleries(request, search_query)
#         context['page_obj'] = page
#         context['object_list'] = page.object_list
#         context['is_paginated'] = page.has_other_pages()
# 
#         return context
# 
#     def get_galleries(self, request, search_query):
#         qs = (
#             Gallery.objects
#             .live()
#             .prefetch_related(
#                 Prefetch(
#                     'images',
#                     queryset=ImageGallery.objects.select_related('image'),
#                 )
#             )
#             .order_by('-date')
#         )
# 
#         if search_query:
#             qs = qs.search(search_query, order_by_relevance=False)
# 
#         return paginate(request, qs, per_page=self.paginate_by)
# 
# Path: privagal/core/utils.py
# class PrivagalTestCase(TestCase):
#     client_class = PrivagalClient
#     login_template_name = None
# 
#     @classmethod
#     def setUpClass(cls):
#         super(PrivagalTestCase, cls).setUpClass()
#         if settings.PRIVAGAL_AUTH_TOKEN_REQUIRED:
#             cls.login_template_name = 'token_required.html'
#         else:
#             cls.login_template_name = 'password_required.html'
# 
#     @classmethod
#     def setUpTestData(cls):
#         cls.timeline = Page.objects.get(slug='timeline').specific
# 
#     def assertPageLogged(self, response):
#         self.assertTemplateNotUsed(
#             response, self.login_template_name, msg_prefix='Not page logged in')
# 
#     def assertNotPageLogged(self, response):
#         self.assertTemplateUsed(
#             response, self.login_template_name, msg_prefix='Page logged in')


import os
import shutil
    import mock
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from django.test import TestCase, override_settings
from wagtail.wagtailimages.views.serve import generate_signature
from privagal.gallery.factories import GalleryFactory, ImageFactory
from privagal.timeline.models import Timeline
from ..utils import PrivagalTestCase
    from unittest import mock  # pragma: no cover
            'foo', password='foo', is_staff=True)
        cls.url = reverse(
            'backoffice_serve',
            kwargs={'filepath': os.path.join('tests', 'django.png')},
        )
        cls.target_dir = os.path.join(settings.MEDIA_ROOT, 'tests')
        try:
            os.makedirs(cls.target_dir)
        except os.error:
            pass

        src = os.path.join(settings.PROJECT_DIR, 'core', 'tests', 'assets', 'django.png')
        shutil.copy(src, cls.target_dir)

    @classmethod
    def tearDownClass(cls):
        shutil.rmtree(cls.target_dir)
        super(BackOfficeSendFileViewTestCase, cls).tearDownClass()

    def test_is_not_staff(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)

    def test_serve(self):
        self.client.force_login(self.user)
        response = self.client.get(self.url)
        self.assertTrue(response.streaming)
        self.assertEqual(response['content-type'], 'image/png')



# Path: privagal/gallery/factories.py
# class GalleryFactory(PageFactory):
# 
#     class Meta:
#         model = Gallery
# 
#     date = fuzzy.FuzzyDate(datetime.date(2016, 1, 1))
#     body = factory.LazyAttribute(
#         lambda x: "<p>{}</p>".format(faker.paragraph()))
# 
#     @factory.post_generation
#     def images(self, created, extracted, **kwargs):
#         """
#         Usage example:
#         >>> GalleryFactory(images__images=[ImageFactory()])
#         """
#         images = kwargs.pop('images', [])
#         if not images:
#             images = [ImageFactory() for i in range(0, 3)]
# 
#         self.images = [ImageGalleryFactory.build(page=self, image=i) for i in images]
# 
# class ImageFactory(factory.DjangoModelFactory):
# 
#     class Meta:
#         model = get_image_model()
# 
#     title = factory.Faker('sentence', locale=locale)
#     file = factory.django.ImageField()
# 
# Path: privagal/timeline/models.py
# class Timeline(AuthTokenPageMixin, Page):
# 
#     parent_page_types = []
#     subpage_types = ['gallery.Gallery']
# 
#     paginate_by = 20
# 
#     class Meta:
#         db_table = 'privagal_timeline'
# 
#     def get_context(self, request, *args, **kwargs):
#         context = super(Timeline, self).get_context(request, *args, **kwargs)
# 
#         search_query = request.GET.get('query', None)
#         context['search_query'] = search_query
# 
#         paginator, page = self.get_galleries(request, search_query)
#         context['page_obj'] = page
#         context['object_list'] = page.object_list
#         context['is_paginated'] = page.has_other_pages()
# 
#         return context
# 
#     def get_galleries(self, request, search_query):
#         qs = (
#             Gallery.objects
#             .live()
#             .prefetch_related(
#                 Prefetch(
#                     'images',
#                     queryset=ImageGallery.objects.select_related('image'),
#                 )
#             )
#             .order_by('-date')
#         )
# 
#         if search_query:
#             qs = qs.search(search_query, order_by_relevance=False)
# 
#         return paginate(request, qs, per_page=self.paginate_by)
# 
# Path: privagal/core/utils.py
# class PrivagalTestCase(TestCase):
#     client_class = PrivagalClient
#     login_template_name = None
# 
#     @classmethod
#     def setUpClass(cls):
#         super(PrivagalTestCase, cls).setUpClass()
#         if settings.PRIVAGAL_AUTH_TOKEN_REQUIRED:
#             cls.login_template_name = 'token_required.html'
#         else:
#             cls.login_template_name = 'password_required.html'
# 
#     @classmethod
#     def setUpTestData(cls):
#         cls.timeline = Page.objects.get(slug='timeline').specific
# 
#     def assertPageLogged(self, response):
#         self.assertTemplateNotUsed(
#             response, self.login_template_name, msg_prefix='Not page logged in')
# 
#     def assertNotPageLogged(self, response):
#         self.assertTemplateUsed(
#             response, self.login_template_name, msg_prefix='Page logged in')


# Path: privagal/core/tests/test_views.py
import os
import shutil
    import mock
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from django.test import TestCase, override_settings
from wagtail.wagtailimages.views.serve import generate_signature
from privagal.gallery.factories import GalleryFactory, ImageFactory
from privagal.timeline.models import Timeline
from ..utils import PrivagalTestCase
    from unittest import mock  # pragma: no cover

            'foo', password='foo', is_staff=True)
        cls.url = reverse(
            'backoffice_serve',
            kwargs={'filepath': os.path.join('tests', 'django.png')},
        )
        cls.target_dir = os.path.join(settings.MEDIA_ROOT, 'tests')
        try:
            os.makedirs(cls.target_dir)
        except os.error:
            pass

        src = os.path.join(settings.PROJECT_DIR, 'core', 'tests', 'assets', 'django.png')
        shutil.copy(src, cls.target_dir)

    @classmethod
    def tearDownClass(cls):
        shutil.rmtree(cls.target_dir)
        super(BackOfficeSendFileViewTestCase, cls).tearDownClass()

    def test_is_not_staff(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)

    def test_serve(self):
        self.client.force_login(self.user)
        response = self.client.get(self.url)
        self.assertTrue(response.streaming)
        self.assertEqual(response['content-type'], 'image/png')



class TimelineViewTestCase(PrivagalTestCase):