|
1
|
|
|
# coding=utf-8 |
|
2
|
|
|
""" |
|
3
|
|
|
""" |
|
4
|
|
|
__author__ = 'Alisue <[email protected]>' |
|
5
|
|
|
from roughpages.backends.base import TemplateFilenameBackendBase |
|
6
|
|
|
from roughpages.backends.decorators import prepare_filename_decorator |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class AuthTemplateFilenameBackend(TemplateFilenameBackendBase): |
|
10
|
|
|
""" |
|
11
|
|
|
A TemplateFilenameBackend which return filename with authenticated state |
|
12
|
|
|
suffix. |
|
13
|
|
|
""" |
|
14
|
|
|
|
|
15
|
|
|
@prepare_filename_decorator |
|
16
|
|
|
def prepare_filenames(self, normalized_url, request): |
|
17
|
|
|
""" |
|
18
|
|
|
Prepare template filename list based on the user authenticated state |
|
19
|
|
|
|
|
20
|
|
|
If user is authenticated user, it use '_authenticated' as a suffix. |
|
21
|
|
|
Otherwise it use '_anonymous' as a suffix to produce the template |
|
22
|
|
|
filename list. The list include original filename at the end of the |
|
23
|
|
|
list. |
|
24
|
|
|
|
|
25
|
|
|
Args: |
|
26
|
|
|
normalized_url (str): A normalized url |
|
27
|
|
|
request (instance): An instance of HttpRequest |
|
28
|
|
|
|
|
29
|
|
|
Returns: |
|
30
|
|
|
list |
|
31
|
|
|
|
|
32
|
|
|
Examples: |
|
33
|
|
|
>>> from mock import MagicMock |
|
34
|
|
|
>>> request = MagicMock() |
|
35
|
|
|
>>> backend = AuthTemplateFilenameBackend() |
|
36
|
|
|
>>> request.user.is_authenticated.return_value = True |
|
37
|
|
|
>>> filenames = backend.prepare_filenames('foo/bar/hogehoge', |
|
38
|
|
|
... request) |
|
39
|
|
|
>>> assert filenames == [ |
|
40
|
|
|
... 'foo/bar/hogehoge_authenticated.html', |
|
41
|
|
|
... 'foo/bar/hogehoge.html' |
|
42
|
|
|
... ] |
|
43
|
|
|
>>> request.user.is_authenticated.return_value = False |
|
44
|
|
|
>>> filenames = backend.prepare_filenames('foo/bar/hogehoge', |
|
45
|
|
|
... request) |
|
46
|
|
|
>>> assert filenames == [ |
|
47
|
|
|
... 'foo/bar/hogehoge_anonymous.html', |
|
48
|
|
|
... 'foo/bar/hogehoge.html' |
|
49
|
|
|
... ] |
|
50
|
|
|
>>> request.user.is_authenticated.return_value = True |
|
51
|
|
|
>>> filenames = backend.prepare_filenames('', |
|
52
|
|
|
... request) |
|
53
|
|
|
>>> assert filenames == [ |
|
54
|
|
|
... 'index_authenticated.html', |
|
55
|
|
|
... 'index.html' |
|
56
|
|
|
... ] |
|
57
|
|
|
>>> request.user.is_authenticated.return_value = False |
|
58
|
|
|
>>> filenames = backend.prepare_filenames('', |
|
59
|
|
|
... request) |
|
60
|
|
|
>>> assert filenames == [ |
|
61
|
|
|
... 'index_anonymous.html', |
|
62
|
|
|
... 'index.html' |
|
63
|
|
|
... ] |
|
64
|
|
|
""" |
|
65
|
|
|
filenames = [normalized_url] |
|
66
|
|
|
if request.user.is_authenticated(): |
|
67
|
|
|
filenames.insert(0, normalized_url + ".authenticated") |
|
68
|
|
|
else: |
|
69
|
|
|
filenames.insert(0, normalized_url + ".anonymous") |
|
70
|
|
|
return filenames |
|
71
|
|
|
|