You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
3.5 KiB
90 lines
3.5 KiB
|
|
|
|
from django.db import models
|
|
from django.contrib.auth import get_user_model
|
|
|
|
User = get_user_model()
|
|
|
|
class Bookmark(models.Model):
|
|
"""
|
|
Bookmark model for different services like library, podcast, hadith, and video.
|
|
"""
|
|
|
|
class ServiceChoices(models.TextChoices):
|
|
LIBRARY = 'library', 'Library'
|
|
PODCAST = 'podcast', 'Podcast'
|
|
PODCAST_PLAYLIST = 'podcast_playlist', 'Podcast Playlist'
|
|
HADITH = 'hadith', 'Hadith'
|
|
VIDEO = 'video', 'Video'
|
|
VIDEO_PLAYLIST = 'video_playlist', 'Video Playlist'
|
|
ARTICLE = 'article', 'Article'
|
|
|
|
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='bookmarks', verbose_name='User')
|
|
service = models.CharField(max_length=20, choices=ServiceChoices.choices, verbose_name='Service')
|
|
content_id = models.PositiveIntegerField(verbose_name='Content ID')
|
|
status = models.BooleanField(default=True, verbose_name='Status')
|
|
created_at = models.DateTimeField(auto_now_add=True, verbose_name='Created At')
|
|
updated_at = models.DateTimeField(auto_now=True, verbose_name='Updated At')
|
|
|
|
class Meta:
|
|
verbose_name = 'Bookmark'
|
|
verbose_name_plural = 'Bookmarks'
|
|
unique_together = ('user', 'service', 'content_id')
|
|
|
|
def __str__(self):
|
|
return f"{self.user.username} - {self.get_service_display()} - {self.content_id}"
|
|
|
|
@classmethod
|
|
def is_bookmarked(cls, user, service, content_id):
|
|
"""
|
|
Check if a specific content is bookmarked by the user.
|
|
|
|
Args:
|
|
user: User instance
|
|
service: Service name (library, podcast, hadith, video)
|
|
content_id: ID of the content
|
|
|
|
Returns:
|
|
Boolean indicating if the content is bookmarked
|
|
"""
|
|
return cls.objects.filter(
|
|
user=user,
|
|
service=service,
|
|
content_id=content_id,
|
|
status=True
|
|
).exists()
|
|
|
|
@classmethod
|
|
def validate_content_exists(cls, service, content_id):
|
|
"""
|
|
Validate if content with the given ID exists in the specified service.
|
|
|
|
Args:
|
|
service: Service name (library, podcast, hadith, video)
|
|
content_id: ID of the content to validate
|
|
|
|
Returns:
|
|
Boolean indicating if the content exists
|
|
"""
|
|
if service == cls.ServiceChoices.LIBRARY:
|
|
from apps.library.models import Book
|
|
return Book.objects.filter(id=content_id).exists()
|
|
elif service == cls.ServiceChoices.PODCAST:
|
|
from apps.podcast.models import Podcast
|
|
return Podcast.objects.filter(id=content_id).exists()
|
|
elif service == cls.ServiceChoices.PODCAST_PLAYLIST:
|
|
from apps.podcast.models import PodcastPlaylist
|
|
return PodcastPlaylist.objects.filter(id=content_id).exists()
|
|
elif service == cls.ServiceChoices.HADITH:
|
|
from apps.hadith.models import Hadith
|
|
return Hadith.objects.filter(id=content_id).exists()
|
|
elif service == cls.ServiceChoices.VIDEO:
|
|
from apps.video.models import Video
|
|
return Video.objects.filter(id=content_id).exists()
|
|
elif service == cls.ServiceChoices.VIDEO_PLAYLIST:
|
|
from apps.video.models import VideoPlaylist
|
|
return VideoPlaylist.objects.filter(id=content_id).exists()
|
|
elif service == cls.ServiceChoices.ARTICLE:
|
|
from apps.article.models import Article
|
|
return Article.objects.filter(id=content_id).exists()
|
|
return False
|