Skip to content

Instantly share code, notes, and snippets.

@pejorativefox
Created April 7, 2013 14:27
Show Gist options
  • Select an option

  • Save pejorativefox/5330711 to your computer and use it in GitHub Desktop.

Select an option

Save pejorativefox/5330711 to your computer and use it in GitHub Desktop.

Revisions

  1. pejorativefox created this gist Apr 7, 2013.
    92 changes: 92 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,92 @@
    """
    Models and managers for the action system
    """

    ## Setup logger
    import logging
    log = logging.getLogger('action.models')

    ## Django imports
    from django.db import models
    from django.core.exceptions import ObjectDoesNotExist
    # Content types
    from django.contrib.contenttypes.models import ContentType
    from django.contrib.contenttypes import generic
    ## Local imports
    from . signals import action_created


    class VerbModelManager(models.Manager):
    """
    """
    def resolve_verb_string(self, verb_string):
    """ Resolve a string to a verb """
    try:
    verb = self.get(name=verb_string)
    except ObjectDoesNotExist as e:
    log.error(
    'Verb string did not resolve properly: {0}'.format(e.message))
    raise e
    else:
    return verb


    class VerbModel(models.Model):
    """
    """
    #: The name of the verb
    name = models.CharField(max_length=64, unique=True)
    #: A slug for the name, for url use
    slug = models.SlugField(auto_created=True)
    #: The default visibility. Should be false for backend actions.
    public = models.BooleanField(default=False)
    #: Display string template. Just a string useable by str.format
    display_string_template = models.TextField(
    default='src:{0}, verb:{1}, target{2}')
    #: Content type for the meta data: eg, facebook action id etc.
    meta_data_type = models.ForeignKey(ContentType, null=True)

    #: Reverse relationship for all actions associated with the verb
    actions = generic.GenericRelation('ActionModel')
    #: Custom model manager
    objects = VerbModelManager()



    class ActionModelManager(models.Manager):
    """
    """
    def new_action(self, source, target, verb_string):
    verb = VerbModel.objects.resolve_verb_string(verb_string)
    action = None # TODO stub
    action_created.send(action, source=source, target=target, verb=verb)


    class ActionModel(models.Model):
    """
    """
    #: A verb to signify what the action did.
    verb = models.ForeignKey('VerbModel')
    #: The date and time of the action, defaults to creation time.
    datetime = models.DateTimeField(auto_now=True)

    #: The content type of the target object
    target_type = models.ForeignKey(ContentType)
    #: The id of the target object
    target_id = models.PositiveIntegerField()
    #: A generic object that the action targeted
    target = generic.GenericForeignKey('target_type', 'target_id')

    #: The content type of the source object
    source_type = models.ForeignKey(ContentType)
    #: The id of the source object
    source_id = models.PositiveIntegerField()
    #: A generic object that was the actor of the action
    source = generic.GenericForeignKey('source_type', 'source_id')

    #: The id of the meta data model
    meta_data = models.PositiveIntegerField()

    #: Custom object manager
    objects = ActionModelManager()