<s> from __future__ import unicode_literals <EOL> from django import forms <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . db . models import F <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from machina . conf import settings as machina_settings <EOL> from machina . core . db . models import get_model <EOL> from machina . core . loading import get_class <EOL> Post = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Topic = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> TopicPoll = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> PermissionHandler = get_class ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> get_anonymous_user_forum_key = get_class ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> class PostForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = Post <EOL> fields = [ '<STR_LIT>' , '<STR_LIT:content>' , '<STR_LIT:username>' , '<STR_LIT>' , ] <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . user = kwargs . pop ( '<STR_LIT:user>' , None ) <EOL> self . user_ip = kwargs . pop ( '<STR_LIT>' , None ) <EOL> self . forum = kwargs . pop ( '<STR_LIT>' , None ) <EOL> self . topic = kwargs . pop ( '<STR_LIT>' , None ) <EOL> self . perm_handler = PermissionHandler ( ) <EOL> super ( PostForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields [ '<STR_LIT>' ] . widget . attrs [ '<STR_LIT>' ] = _ ( '<STR_LIT>' ) <EOL> self . fields [ '<STR_LIT:content>' ] . label = _ ( '<STR_LIT>' ) <EOL> self . fields [ '<STR_LIT:content>' ] . widget . attrs [ '<STR_LIT>' ] = _ ( '<STR_LIT>' ) <EOL> if self . user and self . user . is_anonymous ( ) : <EOL> self . fields [ '<STR_LIT:username>' ] . required = True <EOL> else : <EOL> del self . fields [ '<STR_LIT:username>' ] <EOL> if not self . instance . pk and self . topic : <EOL> self . fields [ '<STR_LIT>' ] . initial = '<STR_LIT>' . format ( <EOL> machina_settings . TOPIC_ANSWER_SUBJECT_PREFIX , <EOL> self . topic . subject ) <EOL> if not self . instance . pk : <EOL> del self . fields [ '<STR_LIT>' ] <EOL> def clean ( self ) : <EOL> if not self . user . is_anonymous ( ) : <EOL> self . instance . poster = self . user <EOL> else : <EOL> self . instance . anonymous_key = get_anonymous_user_forum_key ( self . user ) <EOL> return super ( PostForm , self ) . clean ( ) <EOL> def save ( self , commit = True ) : <EOL> if self . instance . pk : <EOL> post = super ( PostForm , self ) . save ( commit = False ) <EOL> post . updated_by = self . user <EOL> post . updates_count = F ( '<STR_LIT>' ) + <NUM_LIT:1> <EOL> else : <EOL> post = Post ( <EOL> topic = self . topic , <EOL> poster_ip = self . user_ip , <EOL> subject = self . cleaned_data [ '<STR_LIT>' ] , <EOL> approved = self . perm_handler . can_post_without_approval ( self . forum , self . user ) , <EOL> content = self . cleaned_data [ '<STR_LIT:content>' ] ) <EOL> if not self . user . is_anonymous ( ) : <EOL> post . poster = self . user <EOL> else : <EOL> post . username = self . cleaned_data [ '<STR_LIT:username>' ] <EOL> post . anonymous_key = get_anonymous_user_forum_key ( self . user ) <EOL> if commit : <EOL> post . save ( ) <EOL> return post <EOL> class TopicForm ( PostForm ) : <EOL> topic_type = forms . ChoiceField ( <EOL> label = _ ( '<STR_LIT>' ) , choices = Topic . TYPE_CHOICES , required = False ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( TopicForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . can_add_stickies = self . perm_handler . can_add_stickies ( self . forum , self . user ) <EOL> self . can_add_announcements = self . perm_handler . can_add_announcements ( self . forum , self . user ) <EOL> self . can_create_polls = self . perm_handler . can_create_polls ( self . forum , self . user ) <EOL> if not self . can_add_stickies : <EOL> choices = filter ( <EOL> lambda t : t [ <NUM_LIT:0> ] != Topic . TOPIC_STICKY , <EOL> self . fields [ '<STR_LIT>' ] . choices ) <EOL> self . fields [ '<STR_LIT>' ] . choices = choices <EOL> if not self . can_add_announcements : <EOL> choices = filter ( <EOL> lambda t : t [ <NUM_LIT:0> ] != Topic . TOPIC_ANNOUNCE , <EOL> self . fields [ '<STR_LIT>' ] . choices ) <EOL> self . fields [ '<STR_LIT>' ] . choices = choices <EOL> if self . can_create_polls : <EOL> self . fields [ '<STR_LIT>' ] = forms . CharField ( <EOL> label = _ ( '<STR_LIT>' ) , required = False , <EOL> help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> max_length = TopicPoll . _meta . get_field ( '<STR_LIT>' ) . max_length ) <EOL> self . fields [ '<STR_LIT>' ] = forms . IntegerField ( <EOL> label = _ ( '<STR_LIT>' ) , required = False , <EOL> help_text = _ ( '<STR_LIT>' ) , <EOL> validators = TopicPoll . _meta . get_field ( '<STR_LIT>' ) . validators , <EOL> initial = <NUM_LIT:1> ) <EOL> self . fields [ '<STR_LIT>' ] = forms . IntegerField ( <EOL> label = _ ( '<STR_LIT>' ) , required = False , <EOL> help_text = _ ( '<STR_LIT>' ) , <EOL> min_value = <NUM_LIT:0> , initial = <NUM_LIT:0> ) <EOL> self . fields [ '<STR_LIT>' ] = forms . BooleanField ( <EOL> label = _ ( '<STR_LIT>' ) , required = False , <EOL> help_text = _ ( '<STR_LIT>' ) , <EOL> initial = False ) <EOL> try : <EOL> if hasattr ( self . instance , '<STR_LIT>' ) : <EOL> self . fields [ '<STR_LIT>' ] . initial = self . instance . topic . type <EOL> if self . can_create_polls and self . instance . topic . poll is not None : <EOL> self . fields [ '<STR_LIT>' ] . initial = self . instance . topic . poll . question <EOL> self . fields [ '<STR_LIT>' ] . initial = self . instance . topic . poll . max_options <EOL> self . fields [ '<STR_LIT>' ] . initial = self . instance . topic . poll . duration <EOL> self . fields [ '<STR_LIT>' ] . initial = self . instance . topic . poll . user_changes <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> def save ( self , commit = True ) : <EOL> if not self . instance . pk : <EOL> if '<STR_LIT>' in self . cleaned_data and len ( self . cleaned_data [ '<STR_LIT>' ] ) : <EOL> topic_type = self . cleaned_data [ '<STR_LIT>' ] <EOL> else : <EOL> topic_type = Topic . TOPIC_POST <EOL> topic = Topic ( <EOL> forum = self . forum , <EOL> subject = self . cleaned_data [ '<STR_LIT>' ] , <EOL> type = topic_type , <EOL> status = Topic . TOPIC_UNLOCKED , <EOL> approved = self . perm_handler . can_post_without_approval ( self . forum , self . user ) ) <EOL> if not self . user . is_anonymous ( ) : <EOL> topic . poster = self . user <EOL> self . topic = topic <EOL> if commit : <EOL> topic . save ( ) <EOL> else : <EOL> if '<STR_LIT>' in self . cleaned_data and len ( self . cleaned_data [ '<STR_LIT>' ] ) : <EOL> if self . instance . topic . type != self . cleaned_data [ '<STR_LIT>' ] : <EOL> self . instance . topic . type = self . cleaned_data [ '<STR_LIT>' ] <EOL> self . instance . topic . _simple_save ( ) <EOL> return super ( TopicForm , self ) . save ( commit ) </s>
