Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
69 lines (50 sloc) 2.18 KB
from datetime import datetime
from django.conf import settings
from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.utils.translation import ugettext_lazy as _
from flag import signals
STATUS = getattr(settings, "FLAG_STATUSES", [
("1", _("flagged")),
("2", _("flag rejected by moderator")),
("3", _("creator notified")),
("4", _("content removed by creator")),
("5", _("content removed by moderator")),
class FlaggedContent(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey("content_type", "object_id")
creator = models.ForeignKey(User, related_name="flagged_content") # user who created flagged content -- this is kept in model so it outlives content
status = models.CharField(max_length=1, choices=STATUS, default="1")
moderator = models.ForeignKey(User, null=True, related_name="moderated_content") # moderator responsible for last status change
class FlagInstance(models.Model):
flagged_content = models.ForeignKey(FlaggedContent)
user = models.ForeignKey(User) # user flagging the content
when_added = models.DateTimeField(
when_recalled = models.DateTimeField(null=True) # if recalled at all
comment = models.TextField() # comment by the flagger
def add_flag(flagger, content_type, object_id, content_creator, comment):
# check if it's already been flagged
flagged_content, created = FlaggedContent.objects.get_or_create(
content_type = content_type,
object_id = object_id,
defaults = dict(
creator = content_creator
flag_instance = FlagInstance(
flagged_content = flagged_content,
user = flagger,
comment = comment
if created:
sender = FlaggedContent,
flagged_content = flagged_content,
flagged_instance = flag_instance,
return flag_instance
Jump to Line
Something went wrong with that request. Please try again.