| 136 | |
| 137 | # Get the closest previous post's date for the year, if it exists. |
| 138 | if date.month == 1: |
| 139 | previous_month = None |
| 140 | else: |
| 141 | lookup_kwargs = {'%s__range' % date_field: (date.replace(month=1, day=1), date.replace(month=date.month - 1))} |
| 142 | previous_list = queryset.filter(**lookup_kwargs).order_by('-%s' % date_field) |
| 143 | if previous_list: |
| 144 | previous_month = getattr(previous_list[0], date_field).date() |
| 145 | else: |
| 146 | previous_month = None |
| 147 | |
| 148 | # Get the closest next post's date for the year, if it exists. |
| 149 | if date.month == 12: |
| 150 | next_month = None |
| 151 | else: |
| 152 | lookup_kwargs = {'%s__range' % date_field: (date.replace(month=date.month + 1), date.replace(month=12, day=31))} |
| 153 | next_list = queryset.filter(**lookup_kwargs).order_by(date_field) |
| 154 | if next_list: |
| 155 | next_month = getattr(next_list[0], date_field).date() |
| 156 | else: |
| 157 | next_month = None |
| 158 | |
| 261 | |
| 262 | # Get the closest previous post's date for the month, if it exists. |
| 263 | if date.day == 1: |
| 264 | previous_day = None |
| 265 | else: |
| 266 | lookup_kwargs = {'%s__range' % date_field: (date.replace(day=1), date.replace(day=date.day - 1))} |
| 267 | previous_list = queryset.filter(**lookup_kwargs).order_by('-%s' % date_field) |
| 268 | if previous_list: |
| 269 | previous_day = getattr(previous_list[0], date_field).date() |
| 270 | else: |
| 271 | previous_day = None |
| 272 | |
| 273 | # Get the closest next post's date for the month, if it exists. |
| 274 | if date.month != 12: |
| 275 | last_day = (date.replace(month=date.month + 1, day=1) - datetime.timedelta(days=1)).day |
| 276 | else: |
| 277 | last_day = 31 |
| 278 | |
| 279 | if date.day == last_day: |
| 280 | next_day = None |
| 281 | else: |
| 282 | lookup_kwargs = {'%s__range' % date_field: (date.replace(day=date.day + 1), date.replace(day=last_day))} |
| 283 | next_list = queryset.filter(**lookup_kwargs).order_by(date_field) |
| 284 | if next_list: |
| 285 | next_day = getattr(next_list[0], date_field).date() |
| 286 | else: |
| 287 | next_day = None |
| 288 | |