forgotten allauth templates
[wolnelektury.git] / apps / catalogue / test_utils.py
1 from django.conf import settings
2 from django.test import TestCase
3 import shutil
4 import tempfile
5 from slughifi import slughifi
6 from librarian import WLURI
7
8 class WLTestCase(TestCase):
9     """
10         Generic base class for tests. Adds settings freeze and clears MEDIA_ROOT.
11     """
12     longMessage = True
13
14     def setUp(self):
15         self._MEDIA_ROOT, settings.MEDIA_ROOT = settings.MEDIA_ROOT, tempfile.mkdtemp(prefix='djangotest_')
16         settings.NO_SEARCH_INDEX = settings.NO_BUILD_PDF = settings.NO_BUILD_MOBI = settings.NO_BUILD_EPUB = settings.NO_BUILD_TXT = settings.NO_BUILD_FB2 = True
17         settings.CELERY_ALWAYS_EAGER = True
18         self._CACHES, settings.CACHES = settings.CACHES, {
19             'default': {
20                 'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
21             }
22         }
23
24     def tearDown(self):
25         shutil.rmtree(settings.MEDIA_ROOT, True)
26         settings.MEDIA_ROOT = self._MEDIA_ROOT
27         settings.CACHES = self._CACHES
28
29
30 class PersonStub(object):
31
32     def __init__(self, first_names, last_name):
33         self.first_names = first_names
34         self.last_name = last_name
35
36     def readable(self):
37         return " ".join(self.first_names + (self.last_name,))
38
39
40 class BookInfoStub(object):
41     _empty_fields = ['cover_url', 'variant_of']
42     # allow single definition for multiple-value fields
43     _salias = {
44         'authors': 'author',
45     }
46
47     def __init__(self, **kwargs):
48         self.__dict = kwargs
49
50     def __setattr__(self, key, value):
51         if not key.startswith('_'):
52             self.__dict[key] = value
53         return object.__setattr__(self, key, value)
54
55     def __getattr__(self, key):
56         try:
57             return self.__dict[key]
58         except KeyError:
59             if key in self._empty_fields:
60                 return None
61             elif key in self._salias:
62                 return [getattr(self, self._salias[key])]
63             else:
64                 raise
65
66     def to_dict(self):
67         return dict((key, unicode(value)) for key, value in self.__dict.items())
68
69
70 def info_args(title, language=None):
71     """ generate some keywords for comfortable BookInfoCreation  """
72     slug = unicode(slughifi(title))
73     if language is None:
74         language = u'pol'
75     return {
76         'title': unicode(title),
77         'url': WLURI.from_slug(slug),
78         'about': u"http://wolnelektury.pl/example/URI/%s" % slug,
79         'language': language,
80     }