Git Product home page Git Product logo

django-gatekeeper's Introduction

django-gatekeeper

Django application for moderation of model instances.

Provides convenience methods and an admin interface for moderating instances of registered Django models.

django-gatekeeper is a project of Sunlight Labs (c) 2010. Writen by Jeremy Carbaugh <[email protected]> and others (see AUTHORS)

All code is under a BSD-style license, see LICENSE for details.

Homepage: http://pypi.python.org/pypi/django-gatekeeper/

Source: http://github.com/sunlightlabs/django-gatekeeper/

Requirements

python >= 2.4

django >= 1.1

Quick Setup

settings.py

Add to INSTALLED_APPS:

gatekeeper

Be sure to place gatekeeper above any application which contains models that will be moderated.

If you are using old-style automoderation (GATEKEEPER_ENABLE_AUTOMODERATION) be sure to add GatekeeperMiddleware to MIDDLEWARE_CLASSES:

gatekeeper.middleware.GatekeeperMiddleware

Register Models

>>> from django.db import models >>> import gatekeeper >>> class MyModel(models.Model): ... pass >>> gatekeeper.register(MyModel)

gatekeeper.register optionally takes some extra parameters for advanced configuration, see advanced register options for details.

Accessing Moderated Models

Models registered with gatekeeper.register are given a custom manager that adds several extra fields. (As documented in advanced register options all of these fields can be renamed via special parameters to gatekeeper.register but for simplicity they will be referred to by their default names throughout this document.)

Model Instance Convenience Fields

Every model instance accessed via the custom manager ('objects' by default) has a few extra read-only fields tacked on:

  • moderation_status - current moderation status (APPROVED_STATUS, PENDING_STATUS, or REJECTED_STATUS)
  • flagged - flag status (True or False)

In addition, moderation_object is provided as a shortcut to access the actual ModeratedObject instance. The first access of this attribute incurs a database hit but the instance is then cached for the object lifetime.

GatekeeperQuerySet

The custom manager returns a special GatekeeperQuerySet with a few extra filters:

>>> MyModel.objects.all().approved() # approved by moderator >>> MyModel.objects.all().pending() # pending in moderation queue >>> MyModel.objects.all().rejected() # rejected by moderator >>> MyModel.objects.all().flagged() # flagged >>> MyModel.objects.all().not_flagged() # not flagged

These are implemented on the GatekeeperQuerySet itself so that they can be chained:

>>> MyModel.objects.filter(spam=eggs).rejected().flagged()

Deletion of Moderated Objects

When a moderated object instance is deleted, the associated ModeratedObject instance is deleted as well.

Advanced Usage

Auto-Moderation

Gatekeeper provides two methods of auto-moderation. The recommended form of auto-moderation allows a moderation method to be written. This method should return True to approve, False to reject, or None to pass on for manual moderation. The auto-moderation function is passed as an argument when registering a Model.

>>> class MyModel(models.Model): ... pass >>> def myautomod(obj): ... pass >>> gatekeeper.register(MyModel, auto_moderator=myautomod)

If all you want is for users with permission to moderate (gatekeeper.change_moderatedobject permission) to have their objects auto-approved set: GATEKEEPER_ENABLE_AUTOMODERATION = True in settings.py.

If you enable old-style automoderation be sure to enable gatekeeper.middleware.GatekeeperMiddleware

This form of auto-moderation will be attempted if an auto-moderation function returns None or is not specified for a model.

Long Description

When registering a model, a long_desc parameter may be specified that is used to render descriptive text about the instance that is being moderated. The long description is used in emails and in the admin interface.

>>> class Book(models.Model): ... title = models.CharField(max_length=128) ... author = models.CharField(max_length=128) >>> def booklongdesc(obj): ... return u"%s written by %s" % (obj.title, obj.author) >>> gatekeeper.register(MyModel, long_desc=booklongdesc)

The long_desc parameter accepts either a method or a string. If a method is passed, it will be invoked with the object as the only parameter. If a string is used, gatekeeper will first look on the object for a method with the same name, then an attribute if no method is found. If neither are found, or no long_desc parameter is specified, the objects __unicode__() method will be called.

Default Moderation

By default, moderated model instances will be marked as pending and placed on the moderation queue when created. This behavior can be overridden by specifying GATEKEEPER_DEFAULT_STATUS in settings.py.

  • gatekeeper.PENDING_STATUS - mark objects as pending and place on the moderation queue
  • gatekeeper.APPROVED_STATUS - mark objects as approved and bypass the moderation queue
  • gatekeeper.REJECTED_STATUS - mark objects as rejected and bypass the moderation queue

Moderation On Flagging

Flagging is provided as a simple mechanism to allow for users to flag content generally for further review. By default flagging an object does not change it's moderation status, but GATEKEEPER_STATUS_ON_FLAG is available toalter this behavior. If GATEKEEPER_STATUS_ON_FLAG is set to one of the status constants (see Default Moderation) the given status will be set for an object when ModeratedObject.flag is called.

Import Unmoderated Objects

If gatekeeper is added to an existing application, objects already in the database will not be registered with gatekeeper. You can register existing objects with gatekeeper by passing true to the import_unmoderated parameter of the registration method. The imported objects will be set to the state specified by GATEKEEPER_DEFAULT_STATUS in settings.py or pending if GATEKEEPER_DEFAULT_STATUS is not set.

>>> gatekeeper.register(MyModel, import_unmoderated=True)

Moderation Queue Notifications

Gatekeep will send a notification email to a list of recipients when a new object is placed on the moderation queue. Specify GATEKEEPER_MODERATOR_LIST in settings.py to enable notifications.

GATEKEEPER_MODERATOR_LIST = ['[email protected]','[email protected]']

Post-moderation Signal

Many applications will want to execute certain tasks once an object is moderated. Gatekeeper provides a signal that is fired when an object is manually or automatically moderated.

gatekeeper.post_moderation

Advanced Register Options

gatekeeper.register takes a few optional parameters:

manager_name:

name of manager to add/replace on model (defaults to objects)

status_name:

name of moderation status field to add to model instances (defaults to moderation_status)

flagged_name:

name of flagged field to add to model instances (defaults to flagged)

moderation_object_name:

name of moderation_object accessor to add to model instances (defaults to moderation_object)

base_manager:

Manager for contributed manager placed at manager_name to inherit from. Default behavior is to attempt to inherit from same class as the manager already in place or fall back to models.Manager if no manager exists.

django-gatekeeper's People

Contributors

copiesofcopies avatar jamesturk avatar jcarbaugh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-gatekeeper's Issues

syncdb throws error: "gatekeeper.moderatedobject: Accessor for field 'content_type' clashes with related field 'ContentType.moderatedobject_set'. Add a related_name argument to the definition for 'content_type'."

If i run python manage.py syncdb i get the following error:

Error: One or more models did not validate:
gatekeeper.moderatedobject: Accessor for field 'content_type' clashes with related field 'ContentType.moderatedobject_set'. Add a related_name argument to the definition for 'content_type'.

Any idea why? (Using the current version 0.4.0a)

Use UTC for all timestamps

Sorry, one last thought: It'd be great to use datetime.now(pytz.UTC) for all timestamps to create tz-aware timestamps.

missing closing tag in template

Hello,

check line 34 and 35 in moderate.html, there's a missing >:

                        <li {% if flagged_only %}class="selected"{% endif %}<a href="?flagged_only=1">flagged</a></li>
                        <li {% if not flagged_only %}class="selected"{% endif %}<a href="?flagged_only=0">all</a></li>

should be:

                        <li {% if flagged_only %}class="selected"{% endif %}><a href="?flagged_only=1">flagged</a></li>
                        <li {% if not flagged_only %}class="selected"{% endif %}><a href="?flagged_only=0">all</a></li>

Error in __init__.py save_handler

line 218 in init.py has

 message += "\n\nTo moderate, go to http://%s/admin/gatekeeper/moderatedobject/?ot=desc&o=2" % (message, domain)

should be

message += "\n\nTo moderate, go to http://%s/admin/gatekeeper/moderatedobject/?ot=desc&o=2" % (domain)

Default approve(), reject() methods overwrite moderation_reason

As there is only one moderation_object per instance, and this moderation object can go from Pending->Approved->Pending->Approved etc., it would be great if the moderation_reason was not overwritten on each call to approve() (in models._moderate()

Current work-around is to provide a custom approve()/reject() on your model.

ModerationObject always created even for default APPROVED

The current implementation always creates a moderation object when an instance of the registered class is created. This makes perfect sense when items will be pending moderation by default. But if the policy is to display items by default and only moderate an object if it is flagged, then it would make sense in this case to create a moderation object only if/when an item is flagged (or if the automoderation is positive).

Would it be possible to have a general policy of create moderation object only if an item is moderated (ie. default or autoapproval status is not APPROVED).

I'd be happy to provide a patch if you think it's worthwhile - let me know your thoughts.

Cannot see multiple flaggings of the same item

If

  1. An item is flagged (and switched to PENDING)
  2. The item is moderated and set to APPROVED
  3. The item is flagged by someone else

then AFAICS, the record of (1) is gone. Another similar situation is when flagging an item does not put it pending, and you'd like to see all flagged items ordered by how many times they have been flagged.

Is it worth considering splitting the flagging data from ModerationObject to a separate model with where FlaggedData M:1 ModerationObject. One solution would be that when an item is flagged, it is related to the ModerationObject... it should then be easy to add logic such as the moderation object goes back to PENDING after 3 new flaggings if desired (or even provide a custom function to make the decision).

Again, let me know if you think this would be worthwhile or out of scope for what you had in mind for django-gatekeeper.

Cheers,
Michael.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.