# Path: staticgen/exceptions.py
# class StaticgenError(Exception):
#     pass
# 
# Path: staticgen/staticgen_views.py
# class StaticgenView(object):
#     is_paginated = False
# 
#     def __init__(self, *args, **kwargs):
#         super(StaticgenView, self).__init__(*args, **kwargs)
#         self.client = Client(SERVER_NAME=get_static_site_domain())
# 
#     def items(self):
#         return []
# 
#     def log_error(self, message):
#         logger.error(message)
#         if not settings.STATICGEN_FAIL_SILENTLY:
#             raise StaticgenError(message)
# 
#     def url(self, item):
#         return resolve_url(item)
# 
#     def _get_context_data(self, response):
#         for key in ('context', 'context_data',):
#             context = getattr(response, key, None)
#             if context:
#                 return context
# 
#     def _get_paginator(self, url):
#         response = self.client.get(url)
#         if not is_success(response.status_code):  # pragma: no cover
#             message = _('Error retrieving: {url} - Code: {code}').format(
#                 url=url, code=response.status_code)
#             self.log_error(message)
#         else:
#             context_data = self._get_context_data(response)
#             if context_data:
#                 try:
#                     return context_data['paginator'], context_data['is_paginated']
#                 except KeyError:  # pragma: no cover
#                     pass
#         return None, False
# 
#     def _urls(self, page):
#         urls = []
# 
#         url = self.url(page)
#         urls.append(url)  # first page
# 
#         if self.is_paginated:
#             paginator, is_paginated = self._get_paginator(url)
#             if paginator is not None and is_paginated:
#                 page_range = paginator.page_range
#                 match = resolve(url)
#                 for page_num in page_range:
#                     kwargs = match.kwargs.copy()
#                     kwargs.update({
#                         'page': page_num
#                     })
#                     urls.append(reverse(match.view_name, args=match.args, kwargs=kwargs))
# 
#         return urls
# 
#     def get_urls(self):
#         urls = []
#         for page in self.items():
#             if getattr(self, 'i18n', False):
#                 current_lang_code = translation.get_language()
#                 for lang_code, lang_name in settings.LANGUAGES:
#                     translation.activate(lang_code)
#                     urls += self._urls(page)
#                 translation.activate(current_lang_code)
#             else:
#                 urls += self._urls(page)
# 
#         urls = list(set(urls))
#         urls.sort()
#         return urls
# 
# Path: example/staticgen_views.py
# class ExampleDetailView(StaticgenView):
# 
#     def items(self):
#         return Post.objects.all()
# 
# class ExampleListView(StaticgenView):
#     is_paginated = True
# 
#     def items(self):
#         return ('post_list', )
# 
# class ExampleStaticView(StaticgenView):
# 
#     def items(self):
#         return (
#             'homepage',
#             'error_page',
#             'redirect_home',
#             'django.contrib.sitemaps.views.sitemap',
#         )


from django.test import TestCase, override_settings
from mock import patch
from staticgen.exceptions import StaticgenError
from staticgen.staticgen_views import StaticgenView
from example.staticgen_views import (
    ExampleDetailView,
    ExampleListView,
    ExampleStaticView
)
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import unicode_literals






class TestStaticgenViews(TestCase):

    def test_base_staticgen_view_items(self):
        # base StaticgenView() - this should return empty list.
        staticgen_view = StaticgenView()
        self.assertEqual(len(staticgen_view.items()), 0)

    @patch('staticgen.staticgen_views.logger')
    def test_base_staticgen_log_error_logging(self, logger):
        staticgen_view = StaticgenView()
        message = 'Something bad happened'
        staticgen_view.log_error(message)
        self.assertTrue(logger.error.called)

    @override_settings(STATICGEN_FAIL_SILENTLY=False)
    def test_base_staticgen_log_error_exception(self):

# Path: staticgen/exceptions.py
# class StaticgenError(Exception):
#     pass
# 
# Path: staticgen/staticgen_views.py
# class StaticgenView(object):
#     is_paginated = False
# 
#     def __init__(self, *args, **kwargs):
#         super(StaticgenView, self).__init__(*args, **kwargs)
#         self.client = Client(SERVER_NAME=get_static_site_domain())
# 
#     def items(self):
#         return []
# 
#     def log_error(self, message):
#         logger.error(message)
#         if not settings.STATICGEN_FAIL_SILENTLY:
#             raise StaticgenError(message)
# 
#     def url(self, item):
#         return resolve_url(item)
# 
#     def _get_context_data(self, response):
#         for key in ('context', 'context_data',):
#             context = getattr(response, key, None)
#             if context:
#                 return context
# 
#     def _get_paginator(self, url):
#         response = self.client.get(url)
#         if not is_success(response.status_code):  # pragma: no cover
#             message = _('Error retrieving: {url} - Code: {code}').format(
#                 url=url, code=response.status_code)
#             self.log_error(message)
#         else:
#             context_data = self._get_context_data(response)
#             if context_data:
#                 try:
#                     return context_data['paginator'], context_data['is_paginated']
#                 except KeyError:  # pragma: no cover
#                     pass
#         return None, False
# 
#     def _urls(self, page):
#         urls = []
# 
#         url = self.url(page)
#         urls.append(url)  # first page
# 
#         if self.is_paginated:
#             paginator, is_paginated = self._get_paginator(url)
#             if paginator is not None and is_paginated:
#                 page_range = paginator.page_range
#                 match = resolve(url)
#                 for page_num in page_range:
#                     kwargs = match.kwargs.copy()
#                     kwargs.update({
#                         'page': page_num
#                     })
#                     urls.append(reverse(match.view_name, args=match.args, kwargs=kwargs))
# 
#         return urls
# 
#     def get_urls(self):
#         urls = []
#         for page in self.items():
#             if getattr(self, 'i18n', False):
#                 current_lang_code = translation.get_language()
#                 for lang_code, lang_name in settings.LANGUAGES:
#                     translation.activate(lang_code)
#                     urls += self._urls(page)
#                 translation.activate(current_lang_code)
#             else:
#                 urls += self._urls(page)
# 
#         urls = list(set(urls))
#         urls.sort()
#         return urls
# 
# Path: example/staticgen_views.py
# class ExampleDetailView(StaticgenView):
# 
#     def items(self):
#         return Post.objects.all()
# 
# class ExampleListView(StaticgenView):
#     is_paginated = True
# 
#     def items(self):
#         return ('post_list', )
# 
# class ExampleStaticView(StaticgenView):
# 
#     def items(self):
#         return (
#             'homepage',
#             'error_page',
#             'redirect_home',
#             'django.contrib.sitemaps.views.sitemap',
#         )


# Path: tests/test_views.py
from django.test import TestCase, override_settings
from mock import patch
from staticgen.exceptions import StaticgenError
from staticgen.staticgen_views import StaticgenView
from example.staticgen_views import (
    ExampleDetailView,
    ExampleListView,
    ExampleStaticView
)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import unicode_literals






class TestStaticgenViews(TestCase):

    def test_base_staticgen_view_items(self):
        # base StaticgenView() - this should return empty list.
        staticgen_view = StaticgenView()
        self.assertEqual(len(staticgen_view.items()), 0)

    @patch('staticgen.staticgen_views.logger')
    def test_base_staticgen_log_error_logging(self, logger):
        staticgen_view = StaticgenView()
        message = 'Something bad happened'
        staticgen_view.log_error(message)
        self.assertTrue(logger.error.called)

    @override_settings(STATICGEN_FAIL_SILENTLY=False)
    def test_base_staticgen_log_error_exception(self):

        staticgen_view = StaticgenView()