func=(f'INSERT INTO {LogEntry._meta.db_table}(level) VALUES (\'ROW\');' ' RETURN NULL;'), ), ) class ToLogModel(models.Model): """For testing triggers that log records at statement and row level""" field = models.CharField(max_length=16) class CharPk(models.Model): custom_pk = models.CharField(primary_key=True, max_length=32) @pgtrigger.register( pgtrigger.Protect(name='protect_delete', operation=pgtrigger.Delete), pgtrigger.Trigger( name='protect_misc_insert', when=pgtrigger.Before, operation=pgtrigger.Insert, func="RAISE EXCEPTION 'no no no!';", condition=pgtrigger.Q(new__field='misc_insert'), ), ) class TestTrigger(models.Model): """ For testing triggers """ field = models.CharField(max_length=16) int_field = models.IntegerField(default=0)
"externalId": track.external_id, "imageUrl": img_url, } def upload_to_tracks_audios(*args, **kwargs): return f"django-storage/music/tracks/audios/" f"{unique_upload(*args, **kwargs)}" def upload_to_tracks_imgs(*args, **kwargs): return f"django-storage/music/tracks/imgs/" f"{unique_upload(*args, **kwargs)}" @pgtrigger.register( pgtrigger.Protect( name="protect_delete", operation=pgtrigger.Delete, )) class Track(models.Model): """ A singular piece of streamable media. All queue root nodes point to tracks. """ objects = TrackManager() class Meta: unique_together = [ "provider", "external_id", ] FORMAT_TRACK = GLOBAL_FORMAT_TRACK
import pgtrigger from django.db import models from django.utils.translation import gettext_lazy as _ @pgtrigger.register( pgtrigger.Trigger( name="uppercase_college_code", operation=pgtrigger.Insert | pgtrigger.Update, when=pgtrigger.Before, func="NEW.code=upper(NEW.code); RETURN NEW;", ), pgtrigger.Protect( name="protect_college_deletes", operation=pgtrigger.Delete, ), ) class College(models.Model): class Region(models.TextChoices): NORTH = "N", _("North") SOUTH = "S", _("South") EAST = "E", _("East") WEST = "W", _("West") code = models.CharField(max_length=10, primary_key=True) name = models.CharField(max_length=100) address = models.CharField(max_length=500) region = models.CharField( max_length=1, choices=Region.choices, default=Region.NORTH )
import uuid import pgtrigger from django.db import models @pgtrigger.register( pgtrigger.Protect( name="protect_inserts", operation=pgtrigger.Insert, )) @pgtrigger.register( pgtrigger.Protect( name="append_only", operation=(pgtrigger.Update | pgtrigger.Delete), )) class Request(models.Model): """ Logs for outgoing requests. There is a pgtrigger to protect inserts to promote using the following interface: jukebox_radio.networking.actions.make_request """ TYPE_GET = "get" TYPE_POST = "post" TYPE_CHOICES = ( (TYPE_GET, "GET"), (TYPE_POST, "POST"), )
} class VoiceRecordingQuerySet(models.QuerySet): def context_filter(self, track_uuid, user): """ Get all relevant voice recording given a track and a user. """ return (self.select_related("user", "track").filter( track__uuid=track_uuid, user=user, deleted_at__isnull=True).order_by("timestamp_ms")) @pgtrigger.register( pgtrigger.Protect( name="protect_deletes", operation=pgtrigger.Delete, )) @pgtrigger.register( pgtrigger.Protect( name="protect_updates", operation=pgtrigger.Update, )) class VoiceRecording(models.Model): """ Voice recordings that are pinned to a specific time on a track. """ objects = VoiceRecordingManager.from_queryset(VoiceRecordingQuerySet)() uuid = models.UUIDField(primary_key=True, default=uuid.uuid4,
from django.db import models import pghistory import pgtrigger @pgtrigger.register(pgtrigger.Protect(operation=pgtrigger.Delete)) class CannotDelete(models.Model): """This model cannot be deleted. The ``pgtrigger.Protect`` trigger protects the deletion operation from happening """ @pgtrigger.register( pgtrigger.Protect(operation=(pgtrigger.Update | pgtrigger.Delete))) class AppendOnly(models.Model): """This model can only be appended. The ``pgtrigger.Protect`` trigger protects the update or delete operations from happening, making this an "append-only" model. """ int_field = models.IntegerField() @pgtrigger.register( pgtrigger.Protect(operation=pgtrigger.Update, condition=pgtrigger.Q( old__created_at__df=pgtrigger.F('new__created_at')))) class ReadOnlyField(models.Model): """
class TestModel(models.Model): int_field = models.IntegerField(null=True, unique=True) char_field = models.CharField(max_length=128, null=True) float_field = models.FloatField(null=True) class Meta: unique_together = ('int_field', 'char_field') class CharPk(models.Model): custom_pk = models.CharField(primary_key=True, max_length=32) @pgtrigger.register(pgtrigger.Protect(operation=pgtrigger.Delete)) class TestTrigger(models.Model): """ For testing triggers """ field = models.CharField(max_length=16) int_field = models.IntegerField(default=0) dt_field = models.DateTimeField(default=timezone.now) nullable = models.CharField(null=True, default=None, max_length=16) fk_field = models.ForeignKey('auth.User', null=True, on_delete=models.CASCADE) char_pk_fk_field = models.ForeignKey(CharPk, null=True, on_delete=models.CASCADE)
"format": collection.format, "service": collection.provider, "name": collection.name, "artistName": collection.artist_name, "externalId": collection.external_id, "imageUrl": collection.img_url, } def upload_to_collections_imgs(*args, **kwargs): return f"django-storage/music/collections/imgs/" f"{unique_upload(*args, **kwargs)}" @pgtrigger.register( pgtrigger.Protect( name="append_only", operation=(pgtrigger.Update | pgtrigger.Delete), )) class Collection(models.Model): """ Typically an album or a playlist, this model is a singular interface for all collections of tracks. """ objects = CollectionManager() class Meta: unique_together = [ "provider", "external_id", ]
"""""" queue_head = Queue.objects.get_head(stream) if not queue_head: return Queue.objects.none() return self.filter( index__gte=queue_head.index - 10, index__lt=queue_head.index, stream=stream, is_abstract=False, deleted_at__isnull=True, ).order_by("index") @pgtrigger.register( pgtrigger.Protect(name="protect_deletes", operation=pgtrigger.Delete)) @pgtrigger.register( pgtrigger.Protect(name="protect_inserts", operation=pgtrigger.Insert)) class Queue(models.Model): """ A queue is a piece of content (either a track or collection) selected to play in a stream. There are two important data structures used here. 1: LIST - This is responsible for the order in which tracks are played. The list of queues for a stream are ordered by the index field. 2: TREE - This is responsbile for the tree like structure to each queued item. For example, when I queue up an album (collection) it creates a queue item for the album and for each track on the album. This allows the user to remove either the album or individual tracks from the