Fix: Find the current last day; Use the correct get now time function
This commit is contained in:
@@ -6,6 +6,7 @@ from django import forms
|
|||||||
from django.core.validators import ValidationError
|
from django.core.validators import ValidationError
|
||||||
from django.db.models import Count, Q
|
from django.db.models import Count, Q
|
||||||
from django.forms import DateInput
|
from django.forms import DateInput
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
from members.models import Member
|
from members.models import Member
|
||||||
|
|
||||||
@@ -412,7 +413,7 @@ class BillAdminForm(forms.ModelForm):
|
|||||||
self.fields["resolution"].queryset = self.fields["resolution"].queryset.filter(
|
self.fields["resolution"].queryset = self.fields["resolution"].queryset.filter(
|
||||||
(
|
(
|
||||||
Q(option=Resolution.Option.FINANCE)
|
Q(option=Resolution.Option.FINANCE)
|
||||||
& Q(date__gt=datetime.datetime.now(tz=datetime.UTC).date() - relativedelta(years=2))
|
& Q(date__gt=timezone.now().date() - relativedelta(years=2))
|
||||||
)
|
)
|
||||||
| Q(option=Resolution.Option.PERMANENT)
|
| Q(option=Resolution.Option.PERMANENT)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import datetime
|
|
||||||
import io
|
import io
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from django.core.files import File
|
from django.core.files import File
|
||||||
|
from django.utils import timezone
|
||||||
from pypdf import PdfReader, PdfWriter
|
from pypdf import PdfReader, PdfWriter
|
||||||
from pypdf.constants import FieldDictionaryAttributes as FA # noqa: N814
|
from pypdf.constants import FieldDictionaryAttributes as FA # noqa: N814
|
||||||
|
|
||||||
@@ -34,7 +34,7 @@ def generate_pdf(wiref):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Get budget year
|
# Get budget year
|
||||||
today = datetime.datetime.now(tz=datetime.UTC).date()
|
today = timezone.now().date()
|
||||||
if today.month < 7:
|
if today.month < 7:
|
||||||
budget_year = f"{today.year - 1}-{today.year}"
|
budget_year = f"{today.year - 1}-{today.year}"
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
|
import calendar
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models import Case, Q, When
|
from django.db.models import Case, Q, When
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
from .choices import PostType, Status
|
from .choices import PostType, Status
|
||||||
|
|
||||||
@@ -81,21 +83,26 @@ class ArticleManager(PublishedManager, models.Manager):
|
|||||||
|
|
||||||
def pinned(self, public=True):
|
def pinned(self, public=True):
|
||||||
# Get date for pinned news that is max 1 month old.
|
# Get date for pinned news that is max 1 month old.
|
||||||
post_date = datetime.datetime.now(tz=datetime.UTC).date()
|
post_date = timezone.now().date()
|
||||||
__month = post_date.month
|
_day = post_date.day
|
||||||
__year = post_date.year
|
_month = post_date.month
|
||||||
|
_year = post_date.year
|
||||||
|
|
||||||
if __month != 1:
|
if _month != 1:
|
||||||
__month -= 1
|
_month -= 1
|
||||||
else:
|
else:
|
||||||
# If the current month is January, you get the date from December of previous year.
|
# If the current month is January, you get the date from December of previous year.
|
||||||
__month = 12
|
_month = 12
|
||||||
__year -= 1
|
_year -= 1
|
||||||
|
|
||||||
post_date = post_date.replace(year=__year, month=__month)
|
# Clamp day to last day of target month (handles 30/31 and Feb)
|
||||||
|
last_day = calendar.monthrange(_year, _month)[1]
|
||||||
|
safe_day = min(_day, last_day)
|
||||||
|
|
||||||
|
post_date = post_date.replace(year=_year, month=_month, day=safe_day)
|
||||||
|
|
||||||
# Get date for event posts that is max 1 day old.
|
# Get date for event posts that is max 1 day old.
|
||||||
event_date = datetime.datetime.now(tz=datetime.UTC).date() - datetime.timedelta(1)
|
event_date = timezone.now().date() - datetime.timedelta(1)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
self.published(public)
|
self.published(public)
|
||||||
@@ -145,7 +152,7 @@ class AllEventManager(PublishedManager, models.Manager):
|
|||||||
return qs.order_by("-date")
|
return qs.order_by("-date")
|
||||||
|
|
||||||
def future_events(self, public=True):
|
def future_events(self, public=True):
|
||||||
date_today = datetime.datetime.now(tz=datetime.UTC).date()
|
date_today = timezone.now().date()
|
||||||
qs = self.published(public).filter(event_start__gt=date_today)
|
qs = self.published(public).filter(event_start__gt=date_today)
|
||||||
return qs.reverse()
|
return qs.reverse()
|
||||||
|
|
||||||
@@ -166,12 +173,12 @@ class EventManager(PublishedManager, models.Manager):
|
|||||||
return qs.order_by("-date")
|
return qs.order_by("-date")
|
||||||
|
|
||||||
def future_events(self, public=True):
|
def future_events(self, public=True):
|
||||||
date_today = datetime.datetime.now(tz=datetime.UTC).date()
|
date_today = timezone.now().date()
|
||||||
qs = self.published(public).filter(event_start__gt=date_today)
|
qs = self.published(public).filter(event_start__gt=date_today)
|
||||||
return qs.reverse()
|
return qs.reverse()
|
||||||
|
|
||||||
def past_events(self, public=True):
|
def past_events(self, public=True):
|
||||||
date_today = datetime.datetime.now(tz=datetime.UTC).date()
|
date_today = timezone.now().date()
|
||||||
qs = self.published(public).filter(event_start__lt=date_today)
|
qs = self.published(public).filter(event_start__lt=date_today)
|
||||||
return qs
|
return qs
|
||||||
|
|
||||||
@@ -191,11 +198,11 @@ class FetMeetingManager(PublishedManager, models.Manager):
|
|||||||
return qs.order_by("-date")
|
return qs.order_by("-date")
|
||||||
|
|
||||||
def future_events(self):
|
def future_events(self):
|
||||||
date_today = datetime.datetime.now(tz=datetime.UTC).date()
|
date_today = timezone.now().date()
|
||||||
qs = self.published().filter(event_start__gt=date_today)
|
qs = self.published().filter(event_start__gt=date_today)
|
||||||
return qs.reverse()
|
return qs.reverse()
|
||||||
|
|
||||||
def past_events(self):
|
def past_events(self):
|
||||||
date_today = datetime.datetime.now(tz=datetime.UTC).date()
|
date_today = timezone.now().date()
|
||||||
qs = self.published().filter(event_start__lt=date_today)
|
qs = self.published().filter(event_start__lt=date_today)
|
||||||
return qs
|
return qs
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ from django.db.models import Q
|
|||||||
from django.http import HttpResponseRedirect
|
from django.http import HttpResponseRedirect
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
from django.utils import timezone
|
||||||
from django.views.generic import ListView, TemplateView
|
from django.views.generic import ListView, TemplateView
|
||||||
from django.views.generic.detail import DetailView
|
from django.views.generic.detail import DetailView
|
||||||
from django.views.generic.edit import CreateView
|
from django.views.generic.edit import CreateView
|
||||||
@@ -64,7 +65,7 @@ def _get_display_period(view_type: str, period: str) -> date:
|
|||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
# Get first day of the current week
|
# Get first day of the current week
|
||||||
today = datetime.datetime.now(tz=datetime.UTC).date()
|
today = timezone.now().date()
|
||||||
display_date = today - datetime.timedelta(days=today.weekday())
|
display_date = today - datetime.timedelta(days=today.weekday())
|
||||||
|
|
||||||
# Handle month view
|
# Handle month view
|
||||||
@@ -76,7 +77,7 @@ def _get_display_period(view_type: str, period: str) -> date:
|
|||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
# Get the first day of the current month
|
# Get the first day of the current month
|
||||||
display_date = datetime.datetime.now(tz=datetime.UTC).date().replace(day=1)
|
display_date = timezone.now().date().replace(day=1)
|
||||||
|
|
||||||
return display_date
|
return display_date
|
||||||
|
|
||||||
@@ -170,7 +171,7 @@ class RentalListView(ListView):
|
|||||||
context["week_num"] = week_num
|
context["week_num"] = week_num
|
||||||
|
|
||||||
# Get the current date for the calendar
|
# Get the current date for the calendar
|
||||||
context["today"] = datetime.datetime.now(tz=datetime.UTC).date()
|
context["today"] = timezone.now().date()
|
||||||
|
|
||||||
# Add rental items to the context for the filter
|
# Add rental items to the context for the filter
|
||||||
context["rentalitems"] = RentalItem.objects.all()
|
context["rentalitems"] = RentalItem.objects.all()
|
||||||
@@ -234,7 +235,7 @@ class RentalCreateView(CreateView):
|
|||||||
selected_items = sorted(items, key=lambda x: x.name.lower())
|
selected_items = sorted(items, key=lambda x: x.name.lower())
|
||||||
|
|
||||||
for i in range(0, len(selected_items), RENTAL_ITEMS_MAX):
|
for i in range(0, len(selected_items), RENTAL_ITEMS_MAX):
|
||||||
batch = selected_items[i:i + RENTAL_ITEMS_MAX]
|
batch = selected_items[i : i + RENTAL_ITEMS_MAX]
|
||||||
|
|
||||||
# Clone base_rental — copying all its field values
|
# Clone base_rental — copying all its field values
|
||||||
new_rental = Rental.objects.create(
|
new_rental = Rental.objects.create(
|
||||||
@@ -266,6 +267,7 @@ class RentalCreateView(CreateView):
|
|||||||
def get_success_url(self):
|
def get_success_url(self):
|
||||||
return reverse("rental:rental_create_done")
|
return reverse("rental:rental_create_done")
|
||||||
|
|
||||||
|
|
||||||
class RentalCreateDoneView(TemplateView):
|
class RentalCreateDoneView(TemplateView):
|
||||||
template_name = "rental/create_done.html"
|
template_name = "rental/create_done.html"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user