Conditions | 19 |
Total Lines | 73 |
Lines | 0 |
Ratio | 0 % |
Changes | 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:
Complex classes like Details.__call__() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """Default example views""" |
||
25 | def __call__( |
||
26 | self, request, path=None, lang=None, delegation=True, |
||
27 | **kwargs): |
||
28 | |||
29 | current_page = False |
||
30 | |||
31 | if path is None: |
||
32 | raise ValueError( |
||
33 | "pages.views.Details class view requires the path argument. " |
||
34 | "Check your urls.py file.") |
||
35 | |||
36 | # for the ones that might have forgotten to pass the language |
||
37 | # the language is now removed from the page path |
||
38 | if settings.PAGE_USE_LANGUAGE_PREFIX and lang is None: |
||
39 | maybe_lang = path.split("/")[0] |
||
40 | if maybe_lang in LANGUAGE_KEYS: |
||
41 | lang = maybe_lang |
||
42 | path = path[(len(lang) + 1):] |
||
43 | |||
44 | lang = self.choose_language(lang, request) |
||
45 | pages_navigation = self.get_navigation(request, path, lang) |
||
46 | |||
47 | context = { |
||
48 | 'path': path, |
||
49 | 'pages_navigation': pages_navigation, |
||
50 | 'lang': lang, |
||
51 | } |
||
52 | |||
53 | is_staff = self.is_user_staff(request) |
||
54 | |||
55 | current_page = self.resolve_page(request, context, is_staff) |
||
56 | |||
57 | # Do redirect to new page (if enabled) |
||
58 | if settings.PAGE_REDIRECT_OLD_SLUG and current_page: |
||
59 | url = current_page.get_absolute_url(language=lang) |
||
60 | slug = current_page.get_complete_slug(language=lang) |
||
61 | current_url = request.get_full_path() |
||
62 | if url != path and url + '/' != current_url and slug != path: |
||
63 | return HttpResponsePermanentRedirect(url) |
||
64 | |||
65 | # if no pages has been found, we will try to find it via an Alias |
||
66 | if not current_page: |
||
67 | redirection = self.resolve_alias(request, path, lang) |
||
68 | if redirection: |
||
69 | return redirection |
||
70 | else: |
||
71 | context['current_page'] = current_page |
||
72 | |||
73 | # If unauthorized to see the pages, raise a 404, That can |
||
74 | # happen with expired pages. |
||
75 | if not is_staff and not current_page.visible: |
||
76 | raise Http404 |
||
77 | |||
78 | redirection = self.resolve_redirection(request, context) |
||
79 | if redirection: |
||
80 | return redirection |
||
81 | |||
82 | self.extra_context(request, context) |
||
83 | |||
84 | if delegation and current_page.delegate_to: |
||
85 | answer = self.delegate(request, context, delegation, **kwargs) |
||
86 | if answer: |
||
87 | return answer |
||
88 | |||
89 | if kwargs.get('only_context', False): |
||
90 | return context |
||
91 | template_name = kwargs.get( |
||
92 | 'template_name', |
||
93 | self.get_template(request, context)) |
||
94 | |||
95 | context['template_name'] = template_name |
||
96 | |||
97 | return render(request, template_name, context) |
||
98 | |||
248 |