Conditions | 9 |
Total Lines | 65 |
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:
1 | # coding=utf-8 |
||
107 | def get_object_from_date_based_view(request, *args, **kwargs): # noqa |
||
108 | """ |
||
109 | Get object from generic date_based.detail view |
||
110 | |||
111 | Parameters |
||
112 | ---------- |
||
113 | request : instance |
||
114 | An instance of HttpRequest |
||
115 | |||
116 | Returns |
||
117 | ------- |
||
118 | instance |
||
119 | An instance of model object or None |
||
120 | """ |
||
121 | import time |
||
122 | import datetime |
||
123 | from django.http import Http404 |
||
124 | from django.db.models.fields import DateTimeField |
||
125 | try: |
||
126 | from django.utils import timezone |
||
127 | datetime_now = timezone.now |
||
128 | except ImportError: |
||
129 | datetime_now = datetime.datetime.now |
||
130 | year, month, day = kwargs['year'], kwargs['month'], kwargs['day'] |
||
131 | month_format = kwargs.get('month_format', '%b') |
||
132 | day_format = kwargs.get('day_format', '%d') |
||
133 | date_field = kwargs['date_field'] |
||
134 | queryset = kwargs['queryset'] |
||
135 | object_id = kwargs.get('object_id', None) |
||
136 | slug = kwargs.get('slug', None) |
||
137 | slug_field = kwargs.get('slug_field', 'slug') |
||
138 | |||
139 | try: |
||
140 | tt = time.strptime( |
||
141 | '%s-%s-%s' % (year, month, day), |
||
142 | '%s-%s-%s' % ('%Y', month_format, day_format) |
||
143 | ) |
||
144 | date = datetime.date(*tt[:3]) |
||
145 | except ValueError: |
||
146 | raise Http404 |
||
147 | |||
148 | model = queryset.model |
||
149 | |||
150 | if isinstance(model._meta.get_field(date_field), DateTimeField): |
||
151 | lookup_kwargs = { |
||
152 | '%s__range' % date_field: ( |
||
153 | datetime.datetime.combine(date, datetime.time.min), |
||
154 | datetime.datetime.combine(date, datetime.time.max), |
||
155 | )} |
||
156 | else: |
||
157 | lookup_kwargs = {date_field: date} |
||
158 | |||
159 | now = datetime_now() |
||
160 | if date >= now.date() and not kwargs.get('allow_future', False): |
||
161 | lookup_kwargs['%s__lte' % date_field] = now |
||
162 | if object_id: |
||
163 | lookup_kwargs['pk'] = object_id |
||
164 | elif slug and slug_field: |
||
165 | lookup_kwargs['%s__exact' % slug_field] = slug |
||
166 | else: |
||
167 | raise AttributeError( |
||
168 | "Generic detail view must be called with either an " |
||
169 | "object_id or a slug/slug_field." |
||
170 | ) |
||
171 | return get_object_or_404(queryset, **lookup_kwargs) |
||
172 | def _get_object_from_date_based_view_validation(request, *args, **kwargs): |
||
182 |