old_contents: from django.db import models
  
  from jsonfield import JSONField
  from model_utils import Choices
  from model_utils.models import TimeStampedModel
  
  
  class Event(TimeStampedModel):
  
      ACTIONS = Choices(
              ("package_create", "Package Created"),
              ("package_delete", "Package Deleted"),
              ("release_create", "Release Created"),
              ("release_delete", "Release Deleted"),
              ("file_add", "File Added"),
              ("file_remove", "File Removed"),
          )
  
      package = models.SlugField(max_length=150)
      version = models.CharField(max_length=512)
  
      action = models.CharField(max_length=25, choices=ACTIONS)
  
      data = JSONField()
  
new_contents: from django.db import models
  from django.db.models.signals import post_delete, post_save
  from django.dispatch import receiver
  
  from jsonfield import JSONField
  from model_utils import Choices
  from model_utils.models import TimeStampedModel
  
  from packages.models import Package, Release, ReleaseFile
  
  
  class Event(TimeStampedModel):
  
      ACTIONS = Choices(
              ("package_create", "Package Created"),
              ("package_delete", "Package Deleted"),
              ("release_create", "Release Created"),
              ("release_delete", "Release Deleted"),
              ("file_add", "File Added"),
              ("file_remove", "File Removed"),
          )
  
      package = models.SlugField(max_length=150)
      version = models.CharField(max_length=512, blank=True)
  
      action = models.CharField(max_length=25, choices=ACTIONS)
  
      data = JSONField()
  
  
  @receiver(post_save, sender=Package)
  def history_package_create(instance, created, **kwargs):
      if created:
          Event.objects.create(
              package=instance.name,
              action=Event.ACTIONS.package_create
          )
  
  
  @receiver(post_delete, sender=Package)
  def history_package_delete(instance, **kwargs):
      Event.objects.create(
          package=instance.name,
          action=Event.ACTIONS.package_delete
      )
  
  
  @receiver(post_save, sender=Release)
  def history_release_update(instance, created, **kwargs):
      if created:
          Event.objects.create(
              package=instance.package.name,
              release=instance.version,
              action=Event.ACTIONS.release_create
          )
  
      if instance.has_changed("hidden"):
          if instance.hidden:
              Event.objects.create(
                  package=instance.package.name,
                  release=instance.version,
                  action=Event.ACTIONS.release_delete
              )
          else:
              Event.objects.create(
                  package=instance.package.name,
                  release=instance.version,
                  action=Event.ACTIONS.release_create
              )
  
  
  @receiver(post_save, sender=ReleaseFile)
  def history_releasefile_update(instance, created, **kwargs):
      if created:
          e = Event.objects.create(
              package=instance.release.package.name,
              release=instance.release.version,
              action=Event.ACTIONS.release_create
          )
  
      if instance.has_changed("hidden"):
          if instance.hidden:
              e = Event.objects.create(
                  package=instance.release.name,
                  release=instance.release.version,
                  action=Event.ACTIONS.release_delete
              )
          else:
              e = Event.objects.create(
                  package=instance.release.name,
                  release=instance.release.version,
                  action=Event.ACTIONS.release_create
              )
  
      e.data = {
          "filename": instance.filename,
          "digest": instance.digest,
      }
      e.save()
  
subject: add signals to track when things change