from datetime import datetime
from django.core.files import File
from django.conf import settings

from armstrong.dev.tests.utils import ArmstrongTestCase
from armstrong.dev.tests.utils.concrete import *
from armstrong.dev.tests.utils.users import *

from ..models import Section

import fudge


class ArmSectionsTestCase(ArmstrongTestCase):
    def setUp(self):
        super(ArmSectionsTestCase, self).setUp()
        self.sections = []
        data = [
                ('Local', 'local', 'All about local', None),
                ('Sports', 'sports', 'All about sports', None),
                ('College', 'college', 'All about college sports', 1),
                ('Pro', 'pro', 'All about pro sports', 1),
                ('Weather', 'weather', 'All about weather', None),
                ]
        for title, slug, summary, parent in data:
            if parent is not None:
                parent = self.sections[parent]
            self.sections.append(Section.objects.create(
                    title=title,
                    slug=slug,
                    summary=summary,
                    parent=parent,
                ))


# From django.conf.__init__ and django.test.utils.
# https://github.com/django/django/tree/5acd91d3ec80021d6c8fc25aab3b21098b1e8322
#
# TODO: Move this to armstrong.dev.tests.utils.

class BaseSettings(object):
    """
    Common logic for settings whether set by a module or by the user.
    """
    def __setattr__(self, name, value):
        if name in ("MEDIA_URL", "STATIC_URL") and value and not value.endswith('/'):
            warnings.warn("If set, %s must end with a slash" % name,
                          DeprecationWarning)
        elif name == "ADMIN_MEDIA_PREFIX":
            warnings.warn("The ADMIN_MEDIA_PREFIX setting has been removed; "
                          "use STATIC_URL instead.", DeprecationWarning)
        object.__setattr__(self, name, value)

class UserSettingsHolder(BaseSettings):
    """
    Holder for user configured settings.
    """
    # SETTINGS_MODULE doesn't make much sense in the manually configured
    # (standalone) case.
    SETTINGS_MODULE = None

    def __init__(self, default_settings):
        """
        Requests for configuration variables not in this class are satisfied
        from the module specified in default_settings (if possible).
        """
        self.default_settings = default_settings

    def __getattr__(self, name):
        return getattr(self.default_settings, name)

    def __dir__(self):
        return self.__dict__.keys() + dir(self.default_settings)

    # For Python < 2.6:
    __members__ = property(lambda self: self.__dir__())


class OverrideSettingsHolder(UserSettingsHolder):
    pass


class override_settings(object):
    """
    Acts as either a decorator, or a context manager. If it's a decorator it
    takes a function and returns a wrapped function. If it's a contextmanager
    it's used with the ``with`` statement. In either event entering/exiting
    are called before and after, respectively, the function/block is executed.
    """
    def __init__(self, **kwargs):
        self.options = kwargs
        self.wrapped = settings._wrapped

    def __enter__(self):
        self.enable()

    def __exit__(self, exc_type, exc_value, traceback):
        self.disable()

    def __call__(self, test_func):
        from django.test import TransactionTestCase
        if isinstance(test_func, type) and issubclass(test_func, TransactionTestCase):
            original_pre_setup = test_func._pre_setup
            original_post_teardown = test_func._post_teardown
            def _pre_setup(innerself):
                self.enable()
                original_pre_setup(innerself)
            def _post_teardown(innerself):
                original_post_teardown(innerself)
                self.disable()
            test_func._pre_setup = _pre_setup
            test_func._post_teardown = _post_teardown
            return test_func
        else:
            @wraps(test_func)
            def inner(*args, **kwargs):
                with self:
                    return test_func(*args, **kwargs)
        return inner

    def enable(self):
        override = OverrideSettingsHolder(settings._wrapped)
        for key, new_value in self.options.items():
            setattr(override, key, new_value)
        settings._wrapped = override

    def disable(self):
        settings._wrapped = self.wrapped

@@ -148,16 +148,66 @@
 estCase%0A
+from armstrong.dev.tests.utils.backports import *%0A
 from arm
@@ -239,32 +239,32 @@
 ncrete import *%0A
-
 from armstrong.d
@@ -1164,16 +1164,16 @@
 parent,%0A
+
         
@@ -1187,3309 +1187,4 @@
  ))%0A
-%0A%0A# From django.conf.__init__ and django.test.utils.%0A# https://github.com/django/django/tree/5acd91d3ec80021d6c8fc25aab3b21098b1e8322%0A#%0A# TODO: Move this to armstrong.dev.tests.utils.%0A%0Aclass BaseSettings(object):%0A    %22%22%22%0A    Common logic for settings whether set by a module or by the user.%0A    %22%22%22%0A    def __setattr__(self, name, value):%0A        if name in (%22MEDIA_URL%22, %22STATIC_URL%22) and value and not value.endswith('/'):%0A            warnings.warn(%22If set, %25s must end with a slash%22 %25 name,%0A                          DeprecationWarning)%0A        elif name == %22ADMIN_MEDIA_PREFIX%22:%0A            warnings.warn(%22The ADMIN_MEDIA_PREFIX setting has been removed; %22%0A                          %22use STATIC_URL instead.%22, DeprecationWarning)%0A        object.__setattr__(self, name, value)%0A%0Aclass UserSettingsHolder(BaseSettings):%0A    %22%22%22%0A    Holder for user configured settings.%0A    %22%22%22%0A    # SETTINGS_MODULE doesn't make much sense in the manually configured%0A    # (standalone) case.%0A    SETTINGS_MODULE = None%0A%0A    def __init__(self, default_settings):%0A        %22%22%22%0A        Requests for configuration variables not in this class are satisfied%0A        from the module specified in default_settings (if possible).%0A        %22%22%22%0A        self.default_settings = default_settings%0A%0A    def __getattr__(self, name):%0A        return getattr(self.default_settings, name)%0A%0A    def __dir__(self):%0A        return self.__dict__.keys() + dir(self.default_settings)%0A%0A    # For Python %3C 2.6:%0A    __members__ = property(lambda self: self.__dir__())%0A%0A%0Aclass OverrideSettingsHolder(UserSettingsHolder):%0A    pass%0A%0A%0Aclass override_settings(object):%0A    %22%22%22%0A    Acts as either a decorator, or a context manager. If it's a decorator it%0A    takes a function and returns a wrapped function. If it's a contextmanager%0A    it's used with the %60%60with%60%60 statement. In either event entering/exiting%0A    are called before and after, respectively, the function/block is executed.%0A    %22%22%22%0A    def __init__(self, **kwargs):%0A        self.options = kwargs%0A        self.wrapped = settings._wrapped%0A%0A    def __enter__(self):%0A        self.enable()%0A%0A    def __exit__(self, exc_type, exc_value, traceback):%0A        self.disable()%0A%0A    def __call__(self, test_func):%0A        from django.test import TransactionTestCase%0A        if isinstance(test_func, type) and issubclass(test_func, TransactionTestCase):%0A            original_pre_setup = test_func._pre_setup%0A            original_post_teardown = test_func._post_teardown%0A            def _pre_setup(innerself):%0A                self.enable()%0A                original_pre_setup(innerself)%0A            def _post_teardown(innerself):%0A                original_post_teardown(innerself)%0A                self.disable()%0A            test_func._pre_setup = _pre_setup%0A            test_func._post_teardown = _post_teardown%0A            return test_func%0A        else:%0A            @wraps(test_func)%0A            def inner(*args, **kwargs):%0A                with self:%0A                    return test_func(*args, **kwargs)%0A        return inner%0A%0A    def enable(self):%0A        override = OverrideSettingsHolder(settings._wrapped)%0A        for key, new_value in self.options.items():%0A            setattr(override, key, new_value)%0A        settings._wrapped = override%0A%0A    def disable(self):%0A        settings._wrapped = self.wrapped%0A

Use amrstrong.dev.tests.utils.backports instead of manually backported code