Sunday, 2 September 2018

Django signal handling (and recording) for CRUD events on specific models

I am using django 2.0.8 and Python 3.5. I am trying to create a points awarding system based on two classes Foo and Foobar.

I want to do two things:

  1. Set up a points table which maps an (object specific) CRUD event to a point value
  2. Write signal handlers for when the CRUD signals are fired for classes deriving from Foo and FooBar.

This is what I have so far:

myapp/models.py

from django.db import models
from django.contrib.contenttypes.models import ContentType

class Foo(models.Model):
    name = models.CharField(max_length=16)

    class Meta:
        abstract = True   


class FooBar(models.Model):
    name = models.CharField(max_length=16)

    class Meta:
        abstract = True  


class Activity(models.Model):
    ACTIVITY_TYPE_CREATE = 6
    ACTIVITY_TYPE_MODIFY = 7
    ACTIVITY_TYPE_DELETE = 8

    ACTIVITY_TYPES = (
            (ACTIVITY_TYPE_CREATE, 'Create'),
            (ACTIVITY_TYPE_MODIFY, 'Modify'),
            (ACTIVITY_TYPE_DELETE, 'Delete'),
        )

    # Generic relation fields
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    activity_type = models.PositiveSmallIntegerField(choices=ACTIVITY_TYPES)
    points_value = models.SmallIntegerField(null=False,blank=False,default=0)


class FoodooChile(Foo):
    pass


class FooBarChile(FooBar):
    pass

myapp/signals.py

# Not at all sure that this is correct ...
@receiver(post_save, sender='myapp.FoodooChile', dispatch_uid='fooch_save')
def fooch_save(sender, instance=None, created=None, update_fields=None, **kwargs):
   pass

myapp/app.py

class MyappConfig(AppConfig):
name = 'myapp'
label = 'myapp'

def ready(self):
    import myapp.signals

My questions are:

  • How do I modify myapp/models.py and myapp/signals.py in order to listen to and handle CRUD events relating to FoodooChile and FooBarChile?

  • (Assuming the schema for Activity is correct), what would a fixtures file containing point values associated with FoodooChile and FooBarChile Crud events look like?



from Django signal handling (and recording) for CRUD events on specific models

No comments:

Post a Comment