from datetime import datetime

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


STATUS = (
    ("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(default=datetime.now)
    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
    try:
        flagged_content = FlaggedContent.objects.get(content_type=content_type, object_id=object_id)
    except FlaggedContent.DoesNotExist:
        flagged_content = FlaggedContent(content_type=content_type, object_id=object_id, creator=content_creator)
        flagged_content.save()
    
    flag_instance = FlagInstance(flagged_content=flagged_content, user=flagger, comment=comment)
    flag_instance.save()
    
    return flag_instance

@@ -1425,17 +1425,17 @@
 ck if it
-%22
+'
 s alread
@@ -1453,21 +1453,8 @@
 ged%0A
-    try:%0A    
     
@@ -1460,32 +1460,41 @@
  flagged_content
+, created
  = FlaggedConten
@@ -1506,17 +1506,36 @@
 ects.get
-(
+_or_create(%0A        
 content_
@@ -1530,33 +1530,35 @@
     content_type
-=
+ = 
 content_type, ob
@@ -1546,32 +1546,40 @@
  = content_type,
+%0A       
  object_id=objec
@@ -1564,33 +1564,35 @@
        object_id
-=
+ = 
 object_id)%0A    e
@@ -1588,146 +1588,54 @@
 t_id
-)%0A
+,%0A 
     
-except FlaggedContent.DoesNotExist:%0A        flagged_content = FlaggedContent(content_type=content_type, object_id=object_id,
+   defaults = dict(%0A           
  creator
 =con
@@ -1630,17 +1630,19 @@
  creator
-=
+ = 
 content_
@@ -1648,17 +1648,16 @@
 _creator
-)
 %0A       
@@ -1661,29 +1661,14 @@
     
-flagged_content.save(
+)%0A    
 )%0A  
@@ -1703,16 +1703,25 @@
 nstance(
+%0A        
 flagged_
@@ -1727,17 +1727,19 @@
 _content
-=
+ = 
 flagged_
@@ -1750,22 +1750,32 @@
 ent,
+%0A       
  user
-=
+ = 
 flagger,
  com
@@ -1770,24 +1770,32 @@
 flagger,
+%0A       
  comment
 =comment
@@ -1790,16 +1790,23 @@
 ment
-=
+ = 
 comment
+%0A    
 )%0A  

use get_or_create which is far safer than implementing the basic idiom ourselves