<s> from django . db import models <EOL> from django . contrib . auth . models import User <EOL> from django . core . urlresolvers import reverse <EOL> from django . contrib . contenttypes import generic <EOL> from django . contrib . comments . models import Comment <EOL> from django . db . models . signals import post_save <EOL> ALLOW_ALL , REQUIRE_LOGIN , DISALLOW_ALL = range ( <NUM_LIT:3> ) <EOL> SUBSITE_POST_STATUS = ( <EOL> ( ALLOW_ALL , '<STR_LIT>' ) , <EOL> ( REQUIRE_LOGIN , '<STR_LIT>' ) , <EOL> ( DISALLOW_ALL , '<STR_LIT>' ) , <EOL> ) <EOL> class Subsite ( models . Model ) : <EOL> slug = models . SlugField ( max_length = <NUM_LIT:50> , primary_key = True ) <EOL> name = models . CharField ( max_length = <NUM_LIT:50> ) <EOL> description = models . TextField ( ) <EOL> theme = models . CharField ( help_text = '<STR_LIT>' , max_length = <NUM_LIT:100> ) <EOL> ideas_per_page = models . IntegerField ( default = <NUM_LIT:10> ) <EOL> post_status = models . IntegerField ( default = ALLOW_ALL , choices = SUBSITE_POST_STATUS ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> def get_absolute_url ( self ) : <EOL> return reverse ( '<STR_LIT>' , args = [ self . slug ] ) <EOL> def user_can_post ( self , user ) : <EOL> if self . post_status == DISALLOW_ALL : <EOL> return False <EOL> elif self . post_status == ALLOW_ALL : <EOL> return True <EOL> elif self . post_status == REQUIRE_LOGIN : <EOL> return not user . is_anonymous ( ) <EOL> class IdeaManager ( models . Manager ) : <EOL> def with_user_vote ( self , user ) : <EOL> return self . extra ( select = { '<STR_LIT>' : '<STR_LIT>' } , select_params = [ user . id ] ) <EOL> class Idea ( models . Model ) : <EOL> title = models . CharField ( max_length = <NUM_LIT:100> ) <EOL> description = models . TextField ( ) <EOL> score = models . IntegerField ( default = <NUM_LIT:0> ) <EOL> submit_date = models . DateTimeField ( auto_now_add = True ) <EOL> user = models . ForeignKey ( User , null = True , related_name = '<STR_LIT>' ) <EOL> subsite = models . ForeignKey ( Subsite , related_name = '<STR_LIT>' ) <EOL> comments = generic . GenericRelation ( Comment , object_id_field = '<STR_LIT>' ) <EOL> objects = IdeaManager ( ) <EOL> def __unicode__ ( self ) : <EOL> return self . title <EOL> def get_absolute_url ( self ) : <EOL> return reverse ( '<STR_LIT>' , args = [ self . subsite_id , self . id ] ) <EOL> class Vote ( models . Model ) : <EOL> user = models . ForeignKey ( User , related_name = '<STR_LIT>' ) <EOL> idea = models . ForeignKey ( Idea , related_name = '<STR_LIT>' ) <EOL> value = models . IntegerField ( ) <EOL> timestamp = models . DateTimeField ( auto_now_add = True ) <EOL> def __unicode__ ( self ) : <EOL> return '<STR_LIT>' % ( self . user , self . value , self . idea ) <EOL> class Meta : <EOL> unique_together = ( ( '<STR_LIT:user>' , '<STR_LIT>' ) , ) <EOL> def update_idea_votes ( sender , instance , created , ** kwargs ) : <EOL> score = instance . idea . votes . aggregate ( score = models . Sum ( '<STR_LIT:value>' ) ) [ '<STR_LIT>' ] <EOL> instance . idea . score = score <EOL> instance . idea . save ( ) <EOL> post_save . connect ( update_idea_votes , sender = Vote ) </s>
