Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 'lang' to unique constraint for featured topics #2318

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions django_topics/migrations/0009_auto_20250210_0647.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.29 on 2025-02-10 10:47
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('django_topics', '0008_auto_20241217_0702'),
]

operations = [
migrations.AlterUniqueTogether(
name='topicoftheday',
unique_together=set([('topic', 'start_date', 'lang')]),
),
]
62 changes: 62 additions & 0 deletions django_topics/models/featured_topic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from django.db import models
from datetime import datetime
from django.utils.timezone import now
from django_topics.models import Topic


class FeaturedTopicManager(models.Manager):

def get_featured_topic(self, lang: str, date: datetime = None) -> 'TopicOfTheDay':
"""
Return featured topic for given date or closest date that is less than or equal to given date
@param lang: language code, "en" or "he"
@param date: datetime object
@return:
"""
date = date or now().date()
return (
self.filter(start_date__lte=date, lang=lang)
.order_by('-start_date')
.first()
)


class TopicOfTheDay(models.Model):
topic = models.ForeignKey(
Topic,
on_delete=models.CASCADE,
related_name='topic_of_the_day'
)
start_date = models.DateField()
lang = models.CharField(max_length=2, choices=[('en', 'English'), ('he', 'Hebrew')])
objects = FeaturedTopicManager()

class Meta:
unique_together = ('topic', 'start_date', 'lang')
verbose_name = "Landing Page - Featured Topic"
verbose_name_plural = "Landing Page - Featured Topic"

def __str__(self):
return f"{self.topic.slug} ({self.start_date})"


class FeaturedTopicEnglish(TopicOfTheDay):
class Meta:
proxy = True
verbose_name = "Landing Page - Featured Topic (EN)"
verbose_name_plural = "Landing Page - Featured Topic (EN)"

def save(self, *args, **kwargs):
self.lang = "en"
super().save(*args, **kwargs)


class FeaturedTopicHebrew(TopicOfTheDay):
class Meta:
proxy = True
verbose_name = "Landing Page - Featured Topic (HE)"
verbose_name_plural = "Landing Page - Featured Topic (HE)"

def save(self, *args, **kwargs):
self.lang = "he"
super().save(*args, **kwargs)
Loading