current week

This commit is contained in:
j 2023-10-01 09:08:09 +02:00
parent 502f6fb2b9
commit e59c9d2f41
2 changed files with 11 additions and 7 deletions

View file

@ -59,10 +59,18 @@ class Item(models.Model):
return json.dumps(comments) return json.dumps(comments)
@classmethod @classmethod
def public(cls, now=None): def all_public(cls, now=None):
if now is None: if now is None:
now = timezone.make_aware(datetime.now(), timezone.get_default_timezone()) now = timezone.make_aware(datetime.now(), timezone.get_default_timezone())
qs = cls.objects.exclude(published=None).filter(published__lte=now).order_by('-published') qs = cls.objects.exclude(published=None).filter(published__lte=now).order_by('-published')
archive = qs.annotate(year=ExtractYear('published')).annotate(week=ExtractWeek('published'))
return archive
@classmethod
def public(cls, now=None):
if now is None:
now = timezone.make_aware(datetime.now(), timezone.get_default_timezone())
qs = cls.all_public(now)
cal = now.date().isocalendar() cal = now.date().isocalendar()
monday = now.date() - timedelta(days=now.date().isocalendar().weekday - 1) monday = now.date() - timedelta(days=now.date().isocalendar().weekday - 1)
monday = timezone.datetime(monday.year, monday.month, monday.day, tzinfo=now.tzinfo) monday = timezone.datetime(monday.year, monday.month, monday.day, tzinfo=now.tzinfo)
@ -77,11 +85,7 @@ class Item(models.Model):
else: else:
last_monday = monday - timedelta(days=7) last_monday = monday - timedelta(days=7)
week = qs.filter(published__gt=last_monday) week = qs.filter(published__gt=last_monday)
archive = (qs archive = qs.exclude(id__in=week)
.exclude(id__in=week)
.annotate(year=ExtractYear('published'))
.annotate(week=ExtractWeek('published'))
)
return week, archive return week, archive
def get_week(self): def get_week(self):

View file

@ -77,7 +77,7 @@ def index(request):
def archive(request, year=None, month=None, day=None, week=None): def archive(request, year=None, month=None, day=None, week=None):
context = default_context(request) context = default_context(request)
_, archive = models.Item.public() archive = models.Item.all_public()
if year and month and day: if year and month and day:
date = datetime.strptime('%s-%s-%s' % (year, month, day), '%Y-%m-%d') date = datetime.strptime('%s-%s-%s' % (year, month, day), '%Y-%m-%d')
week = int(date.strftime('%W')) week = int(date.strftime('%W'))