Conditions | 11 |
Total Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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:
Complex classes like get_object_from_classbased_instance() 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 | # coding=utf-8 |
||
61 | def get_object_from_classbased_instance( |
||
62 | instance, queryset, request, *args, **kwargs): |
||
63 | """ |
||
64 | Get object from an instance of classbased generic view |
||
65 | |||
66 | Parameters |
||
67 | ---------- |
||
68 | instance : instance |
||
69 | An instance of classbased generic view |
||
70 | queryset : instance |
||
71 | A queryset instance |
||
72 | request : instance |
||
73 | A instance of HttpRequest |
||
74 | |||
75 | Returns |
||
76 | ------- |
||
77 | instance |
||
78 | An instance of model object or None |
||
79 | """ |
||
80 | from django.views.generic.edit import BaseCreateView |
||
81 | # initialize request, args, kwargs of classbased_instance |
||
82 | # most of methods of classbased view assumed these attributes |
||
83 | # but these attributes is initialized in ``dispatch`` method. |
||
84 | instance.request = request |
||
85 | instance.args = args |
||
86 | instance.kwargs = kwargs |
||
87 | |||
88 | # get queryset from class if ``queryset_or_model`` is not specified |
||
89 | if hasattr(instance, 'get_queryset') and not queryset: |
||
90 | queryset = instance.get_queryset() |
||
91 | elif hasattr(instance, 'queryset') and not queryset: |
||
92 | queryset = instance.queryset |
||
93 | elif hasattr(instance, 'model') and not queryset: |
||
94 | queryset = instance.model._default_manager.all() |
||
95 | |||
96 | # get object |
||
97 | if hasattr(instance, 'get_object'): |
||
98 | try: |
||
99 | obj = instance.get_object(queryset) |
||
100 | except AttributeError as e: |
||
101 | # CreateView has ``get_object`` method but CreateView |
||
102 | # should not have any object before thus simply set |
||
103 | # None |
||
104 | if isinstance(instance, BaseCreateView): |
||
105 | obj = None |
||
106 | else: |
||
107 | raise e |
||
108 | elif hasattr(instance, 'object'): |
||
109 | obj = instance.object |
||
110 | else: |
||
111 | obj = None |
||
112 | return obj |
||
113 | |||
114 |