| Conditions | 2 |
| Total Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | # coding=utf-8 |
||
| 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 |