<s> import datetime <EOL> from random import choice <EOL> from django . db import models , IntegrityError <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . contrib . auth . models import User <EOL> from django . template . defaultfilters import slugify <EOL> from lithium . conf import settings <EOL> from lithium . forum . managers import ForumManager , ThreadManager <EOL> t = '<STR_LIT>' <EOL> def generate_extra_id ( length = <NUM_LIT:4> ) : <EOL> return '<STR_LIT>' . join ( [ random . choice ( t ) for i in range ( length ) ] ) <EOL> FORUM_PERMISSIONS = ( <EOL> ( <NUM_LIT:1> , _ ( '<STR_LIT>' ) ) , <EOL> ( <NUM_LIT:2> , _ ( '<STR_LIT>' ) ) , <EOL> ( <NUM_LIT:3> , _ ( '<STR_LIT>' ) ) , <EOL> ( <NUM_LIT:4> , _ ( '<STR_LIT>' ) ) , <EOL> ) <EOL> class Forum ( models . Model ) : <EOL> title = models . CharField ( _ ( '<STR_LIT:title>' ) , max_length = <NUM_LIT:255> ) <EOL> slug = models . SlugField ( _ ( '<STR_LIT>' ) , unique = True ) <EOL> description = models . TextField ( _ ( '<STR_LIT:description>' ) , blank = True ) <EOL> is_category = models . BooleanField ( _ ( '<STR_LIT>' ) , default = False , help_text = _ ( '<STR_LIT>' ) ) <EOL> parent = models . ForeignKey ( '<STR_LIT>' , blank = True , null = True , related_name = '<STR_LIT>' ) <EOL> position = models . PositiveIntegerField ( _ ( '<STR_LIT>' ) , editable = False , default = <NUM_LIT:0> ) <EOL> level = models . PositiveIntegerField ( _ ( '<STR_LIT>' ) , editable = False , default = <NUM_LIT:0> ) <EOL> read = models . IntegerField ( _ ( '<STR_LIT>' ) , choices = FORUM_PERMISSIONS , help_text = _ ( '<STR_LIT>' ) , default = settings . FORUM_DEFAULT_READ_PERMISSION ) <EOL> write = models . IntegerField ( _ ( '<STR_LIT>' ) , choices = FORUM_PERMISSIONS , help_text = _ ( '<STR_LIT>' ) , default = settings . FORUM_DEFAULT_WRITE_PERMISSION ) <EOL> objects = ForumManager ( ) <EOL> class Meta : <EOL> ordering = ( '<STR_LIT>' , ) <EOL> def __str__ ( self ) : <EOL> return self . title <EOL> def siblings ( self , include_self = False ) : <EOL> """<STR_LIT>""" <EOL> qs = Forum . objects . all ( ) <EOL> if not self . parent : <EOL> qs = qs . filter ( parent__isnull = True ) <EOL> else : <EOL> qs = qs . filter ( parent = self . parent ) <EOL> if not include_self : <EOL> qs = qs . exclude ( pk = self . pk ) <EOL> return qs <EOL> def save ( self , * args , ** kwargs ) : <EOL> if not self . pk : <EOL> try : <EOL> self . position = self . siblings ( include_self = True ) . only ( '<STR_LIT>' ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . position + <NUM_LIT:1> <EOL> except IndexError : <EOL> if self . parent : <EOL> self . position = self . parent . position + <NUM_LIT:1> <EOL> else : <EOL> try : <EOL> self . position = Forum . objects . only ( '<STR_LIT>' ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . position + <NUM_LIT:1> <EOL> except IndexError : <EOL> self . position = <NUM_LIT:1> <EOL> if self . position != <NUM_LIT:1> : <EOL> Forum . objects . update_position ( self . position ) <EOL> if self . parent : <EOL> self . level = self . parent . level + <NUM_LIT:1> <EOL> super ( Forum , self ) . save ( * args , ** kwargs ) <EOL> def delete ( self , * args , ** kwargs ) : <EOL> for child in self . children . all ( ) : <EOL> child . delete ( ) <EOL> position = Forum . objects . get ( pk = self . pk ) . position <EOL> super ( Forum , self ) . delete ( * args , ** kwargs ) <EOL> Forum . objects . update_position ( position , increment = False ) <EOL> def get_next_forum ( self ) : <EOL> try : <EOL> return self . siblings ( ) . filter ( position__gt = self . position ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return None <EOL> def get_previous_forum ( self ) : <EOL> try : <EOL> return self . siblings ( ) . filter ( position__lt = self . position ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return None <EOL> def get_position_range ( self ) : <EOL> """<STR_LIT>""" <EOL> position_range = [ self . position ] <EOL> for child in self . children . all ( ) : <EOL> position_range += child . get_position_range ( ) <EOL> return position_range <EOL> def move ( self , direction = '<STR_LIT>' ) : <EOL> swap = None <EOL> if direction == '<STR_LIT>' : <EOL> swap = self . get_previous_forum ( ) <EOL> increment = False <EOL> if direction == '<STR_LIT>' : <EOL> swap = self . get_next_forum ( ) <EOL> increment = True <EOL> if not swap : <EOL> return False <EOL> self_range = self . get_position_range ( ) <EOL> swap_range = swap . get_position_range ( ) <EOL> base = Forum . objects . only ( '<STR_LIT>' ) . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . position * <NUM_LIT:2> <EOL> if direction == '<STR_LIT>' : <EOL> addition = base - len ( self_range ) <EOL> else : <EOL> addition = base + len ( self_range ) <EOL> Forum . objects . update_position ( swap_range , base ) <EOL> Forum . objects . update_position ( self_range , len ( swap_range ) , increment ) <EOL> Forum . objects . update_position ( base , addition , False ) <EOL> return True <EOL> @ models . permalink <EOL> def get_absolute_url ( self ) : <EOL> return ( '<STR_LIT>' , None , { '<STR_LIT>' : self . slug } ) <EOL> @ models . permalink <EOL> def get_create_thread_url ( self ) : <EOL> return ( '<STR_LIT>' , None , { '<STR_LIT>' : self . slug } ) <EOL> def has_children ( self ) : <EOL> return self . children . count ( ) > <NUM_LIT:0> <EOL> def latest_thread ( self ) : <EOL> try : <EOL> return self . thread_set . all ( ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return None <EOL> def get_parent_list ( self ) : <EOL> forum = self <EOL> parent_list = [ ] <EOL> while forum : <EOL> parent_list . append ( forum ) <EOL> forum = forum . parent <EOL> parent_list . reverse ( ) <EOL> return parent_list <EOL> class Thread ( models . Model ) : <EOL> forum = models . ForeignKey ( Forum ) <EOL> title = models . CharField ( _ ( '<STR_LIT:title>' ) , max_length = <NUM_LIT:255> ) <EOL> slug = models . SlugField ( _ ( '<STR_LIT>' ) ) <EOL> is_sticky = models . BooleanField ( _ ( '<STR_LIT>' ) , default = False ) <EOL> is_locked = models . BooleanField ( _ ( '<STR_LIT>' ) , default = False ) <EOL> objects = ThreadManager ( ) <EOL> class Meta : <EOL> unique_together = ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> def __unicode__ ( self ) : <EOL> return self . title <EOL> def save ( self , * args , ** kwargs ) : <EOL> if self . slug == '<STR_LIT>' : <EOL> self . slug = slugify ( self . title ) <EOL> try : <EOL> super ( Thread , self ) . save ( * args , ** kwargs ) <EOL> except IntegrityError : <EOL> self . slug = '<STR_LIT>' % ( self . slug , generate_extra_id ( ) ) <EOL> self . save ( * args , ** kwargs ) <EOL> @ models . permalink <EOL> def get_absolute_url ( self ) : <EOL> return ( '<STR_LIT>' , None , { '<STR_LIT>' : self . forum . slug , '<STR_LIT>' : self . slug } ) <EOL> @ models . permalink <EOL> def get_reply_url ( self ) : <EOL> return ( '<STR_LIT>' , None , { '<STR_LIT>' : self . forum . slug , '<STR_LIT>' : self . slug } ) <EOL> def get_parent_list ( self ) : <EOL> parent_list = self . forum . get_parent_list ( ) <EOL> parent_list . append ( self ) <EOL> return parent_list <EOL> class Post ( models . Model ) : <EOL> content = models . TextField ( _ ( '<STR_LIT:content>' ) ) <EOL> thread = models . ForeignKey ( Thread ) <EOL> author = models . ForeignKey ( User , related_name = '<STR_LIT>' , null = True , blank = True ) <EOL> pub_date = models . DateTimeField ( _ ( '<STR_LIT>' ) , default = datetime . datetime . now ) <EOL> class Meta : <EOL> ordering = ( '<STR_LIT>' , ) <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT:%s>' % self . content [ : <NUM_LIT:20> ] <EOL> def get_absolute_url ( self ) : <EOL> return '<STR_LIT>' % ( self . thread . get_absolute_url ( ) , self . pk ) </s>
