diora-web/radio/models.py

91 lines
3.2 KiB
Python
Raw Normal View History

2026-03-16 19:19:22 +01:00
from django.db import models
from django.contrib.auth.models import User
class SavedStation(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='saved_stations')
name = models.CharField(max_length=200)
url = models.URLField(max_length=500)
bitrate = models.CharField(max_length=20, blank=True)
country = models.CharField(max_length=100, blank=True)
tags = models.CharField(max_length=200, blank=True)
favicon_url = models.URLField(max_length=500, blank=True)
is_favorite = models.BooleanField(default=False)
notes = models.TextField(blank=True)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = ('user', 'url')
ordering = ['-is_favorite', 'name']
def __str__(self):
return f"{self.name} ({self.user.username})"
class StationPlay(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='station_plays')
station_name = models.CharField(max_length=200)
station_url = models.URLField(max_length=500)
started_at = models.DateTimeField(auto_now_add=True)
ended_at = models.DateTimeField(null=True, blank=True)
hour_of_day = models.IntegerField() # 023, set on save
day_of_week = models.IntegerField() # 0=Mon … 6=Sun, set on save
is_weekend = models.BooleanField() # True if day_of_week >= 5
class Meta:
ordering = ['-started_at']
def save(self, *args, **kwargs):
from django.utils import timezone
now = timezone.now()
self.hour_of_day = now.hour
self.day_of_week = now.weekday()
self.is_weekend = self.day_of_week >= 5
super().save(*args, **kwargs)
def __str__(self):
return f"{self.station_name} by {self.user.username} at {self.started_at}"
class FocusSession(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='focus_sessions')
station_name = models.CharField(max_length=200, blank=True)
completed_at = models.DateTimeField(auto_now_add=True)
duration_minutes = models.IntegerField(default=25)
class Meta:
ordering = ['-completed_at']
class FeaturedStation(models.Model):
name = models.CharField(max_length=200)
url = models.URLField(max_length=500)
description = models.CharField(max_length=300, blank=True)
favicon_url = models.URLField(max_length=500, blank=True)
tags = models.CharField(max_length=200, blank=True)
order = models.PositiveIntegerField(default=0)
active = models.BooleanField(default=True)
class Meta:
ordering = ['order', 'name']
def __str__(self):
return self.name
2026-03-16 19:19:22 +01:00
class TrackHistory(models.Model):
user = models.ForeignKey(
User, null=True, blank=True, on_delete=models.SET_NULL, related_name='track_history'
)
session_key = models.CharField(max_length=40, blank=True) # for anonymous users
station_name = models.CharField(max_length=200)
track = models.CharField(max_length=500)
played_at = models.DateTimeField(auto_now_add=True)
scrobbled = models.BooleanField(default=False)
class Meta:
ordering = ['-played_at']
def __str__(self):
return f"{self.track} @ {self.station_name}"