<s> import re <EOL> from django import forms <EOL> from django . conf import settings <EOL> from django . template . defaultfilters import slugify <EOL> from django . utils . translation import ugettext_lazy as _lazy <EOL> from kitsune . products . models import Product , Topic <EOL> from kitsune . sumo . form_fields import MultiUsernameField , StrippedCharField <EOL> from kitsune . wiki . config import SIGNIFICANCES , CATEGORIES <EOL> from kitsune . wiki . models import ( <EOL> Document , Revision , DraftRevision , MAX_REVISION_COMMENT_LENGTH ) <EOL> from kitsune . wiki . tasks import add_short_links <EOL> from kitsune . wiki . widgets import ( <EOL> RadioFieldRendererWithHelpText , <EOL> ProductTopicsAndSubtopicsWidget , <EOL> RelatedDocumentsWidget ) <EOL> TITLE_REQUIRED = _lazy ( u'<STR_LIT>' ) <EOL> TITLE_SHORT = _lazy ( u'<STR_LIT>' <EOL> u'<STR_LIT>' ) <EOL> TITLE_LONG = _lazy ( u'<STR_LIT>' <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' ) <EOL> SLUG_REQUIRED = _lazy ( u'<STR_LIT>' ) <EOL> SLUG_INVALID = _lazy ( u'<STR_LIT>' ) <EOL> SLUG_SHORT = _lazy ( u'<STR_LIT>' <EOL> u'<STR_LIT>' ) <EOL> SLUG_LONG = _lazy ( u'<STR_LIT>' <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' ) <EOL> SUMMARY_REQUIRED = _lazy ( u'<STR_LIT>' ) <EOL> SUMMARY_SHORT = _lazy ( u'<STR_LIT>' <EOL> u'<STR_LIT>' ) <EOL> SUMMARY_LONG = _lazy ( u'<STR_LIT>' <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' ) <EOL> CONTENT_REQUIRED = _lazy ( u'<STR_LIT>' ) <EOL> CONTENT_SHORT = _lazy ( u'<STR_LIT>' <EOL> u'<STR_LIT>' ) <EOL> CONTENT_LONG = _lazy ( u'<STR_LIT>' <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' ) <EOL> COMMENT_LONG = _lazy ( u'<STR_LIT>' <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' ) <EOL> PRODUCT_REQUIRED = _lazy ( u'<STR_LIT>' ) <EOL> TOPIC_REQUIRED = _lazy ( u'<STR_LIT>' ) <EOL> class DocumentForm ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> can_archive = kwargs . pop ( '<STR_LIT>' , False ) <EOL> can_edit_needs_change = kwargs . pop ( '<STR_LIT>' , False ) <EOL> initial_title = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> super ( DocumentForm , self ) . __init__ ( * args , ** kwargs ) <EOL> title_field = self . fields [ '<STR_LIT:title>' ] <EOL> title_field . initial = initial_title <EOL> slug_field = self . fields [ '<STR_LIT>' ] <EOL> slug_field . initial = slugify ( initial_title ) <EOL> topics_field = self . fields [ '<STR_LIT>' ] <EOL> topics_field . choices = Topic . objects . values_list ( '<STR_LIT:id>' , '<STR_LIT:title>' ) <EOL> products_field = self . fields [ '<STR_LIT>' ] <EOL> products_field . choices = Product . objects . values_list ( '<STR_LIT:id>' , '<STR_LIT:title>' ) <EOL> related_documents_field = self . fields [ '<STR_LIT>' ] <EOL> related_documents_field . choices = Document . objects . values_list ( '<STR_LIT:id>' , '<STR_LIT:title>' ) <EOL> if not can_archive : <EOL> del self . fields [ '<STR_LIT>' ] <EOL> if not can_edit_needs_change : <EOL> del self . fields [ '<STR_LIT>' ] <EOL> del self . fields [ '<STR_LIT>' ] <EOL> title = StrippedCharField ( <EOL> min_length = <NUM_LIT:5> , max_length = <NUM_LIT:255> , <EOL> widget = forms . TextInput ( ) , <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> help_text = _lazy ( u'<STR_LIT>' ) , <EOL> error_messages = { '<STR_LIT>' : TITLE_REQUIRED , <EOL> '<STR_LIT>' : TITLE_SHORT , <EOL> '<STR_LIT:max_length>' : TITLE_LONG } ) <EOL> slug = StrippedCharField ( <EOL> min_length = <NUM_LIT:3> , max_length = <NUM_LIT:255> , <EOL> widget = forms . TextInput ( ) , <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> help_text = _lazy ( u'<STR_LIT>' ) , <EOL> error_messages = { '<STR_LIT>' : SLUG_REQUIRED , <EOL> '<STR_LIT>' : SLUG_SHORT , <EOL> '<STR_LIT:max_length>' : SLUG_LONG } ) <EOL> products = forms . MultipleChoiceField ( <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> required = False , <EOL> widget = forms . CheckboxSelectMultiple ( ) ) <EOL> is_localizable = forms . BooleanField ( <EOL> initial = True , <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> required = False ) <EOL> is_archived = forms . BooleanField ( <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> required = False ) <EOL> allow_discussion = forms . BooleanField ( <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> initial = True , <EOL> required = False ) <EOL> category = forms . ChoiceField ( <EOL> choices = CATEGORIES , <EOL> required = False , <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> help_text = _lazy ( u'<STR_LIT>' ) ) <EOL> topics = forms . MultipleChoiceField ( <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> required = False , <EOL> widget = ProductTopicsAndSubtopicsWidget ( ) ) <EOL> related_documents = forms . MultipleChoiceField ( <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> required = False , <EOL> widget = RelatedDocumentsWidget ( ) ) <EOL> locale = forms . CharField ( widget = forms . HiddenInput ( ) ) <EOL> needs_change = forms . BooleanField ( <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> initial = False , <EOL> required = False ) <EOL> needs_change_comment = forms . CharField ( <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> widget = forms . Textarea ( ) , <EOL> required = False ) <EOL> def clean_slug ( self ) : <EOL> slug = self . cleaned_data [ '<STR_LIT>' ] <EOL> if not re . compile ( r'<STR_LIT>' ) . match ( slug ) : <EOL> raise forms . ValidationError ( SLUG_INVALID ) <EOL> return slug <EOL> def clean ( self ) : <EOL> c = super ( DocumentForm , self ) . clean ( ) <EOL> locale = c . get ( '<STR_LIT>' ) <EOL> products = c . get ( '<STR_LIT>' ) <EOL> if ( locale == settings . WIKI_DEFAULT_LANGUAGE and <EOL> ( not products or len ( products ) < <NUM_LIT:1> ) ) : <EOL> raise forms . ValidationError ( PRODUCT_REQUIRED ) <EOL> topics = c . get ( '<STR_LIT>' ) <EOL> if ( locale == settings . WIKI_DEFAULT_LANGUAGE and <EOL> ( not topics or len ( topics ) < <NUM_LIT:1> ) ) : <EOL> raise forms . ValidationError ( TOPIC_REQUIRED ) <EOL> return c <EOL> class Meta : <EOL> model = Document <EOL> fields = ( '<STR_LIT:title>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def save ( self , parent_doc , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> doc = super ( DocumentForm , self ) . save ( commit = False , ** kwargs ) <EOL> doc . parent = parent_doc <EOL> if not doc . needs_change : <EOL> doc . needs_change_comment = '<STR_LIT>' <EOL> doc . save ( ) <EOL> if ( doc . category in settings . IA_DEFAULT_CATEGORIES and <EOL> not doc . share_link ) : <EOL> add_short_links . delay ( [ doc . pk ] ) <EOL> self . save_m2m ( ) <EOL> if parent_doc : <EOL> doc . products . remove ( * [ p for p in doc . products . all ( ) ] ) <EOL> return doc <EOL> class RevisionForm ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> keywords = StrippedCharField ( required = False , <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> help_text = _lazy ( u'<STR_LIT>' ) ) <EOL> summary = StrippedCharField ( <EOL> min_length = <NUM_LIT:5> , max_length = <NUM_LIT:1000> , widget = forms . Textarea ( ) , <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> help_text = _lazy ( u'<STR_LIT>' ) , <EOL> error_messages = { '<STR_LIT>' : SUMMARY_REQUIRED , <EOL> '<STR_LIT>' : SUMMARY_SHORT , <EOL> '<STR_LIT:max_length>' : SUMMARY_LONG } ) <EOL> content = StrippedCharField ( <EOL> min_length = <NUM_LIT:5> , max_length = <NUM_LIT> , <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> widget = forms . Textarea ( ) , <EOL> error_messages = { '<STR_LIT>' : CONTENT_REQUIRED , <EOL> '<STR_LIT>' : CONTENT_SHORT , <EOL> '<STR_LIT:max_length>' : CONTENT_LONG } ) <EOL> expires = forms . DateField ( <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> required = False ) <EOL> comment = StrippedCharField ( required = False , label = _lazy ( u'<STR_LIT>' ) ) <EOL> class Meta ( object ) : <EOL> model = Revision <EOL> fields = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:content>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( RevisionForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields [ '<STR_LIT>' ] . widget = forms . HiddenInput ( ) <EOL> self . fields [ '<STR_LIT>' ] . widget = forms . TextInput ( <EOL> attrs = { '<STR_LIT>' : MAX_REVISION_COMMENT_LENGTH } ) <EOL> def save ( self , creator , document , based_on_id = None , base_rev = None , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> new_rev = super ( RevisionForm , self ) . save ( commit = False , ** kwargs ) <EOL> new_rev . document = document <EOL> new_rev . creator = creator <EOL> if based_on_id : <EOL> new_rev . based_on_id = based_on_id <EOL> if base_rev and not document . allows ( creator , '<STR_LIT>' ) : <EOL> new_rev . keywords = base_rev . keywords <EOL> new_rev . save ( ) <EOL> return new_rev <EOL> class DraftRevisionForm ( forms . ModelForm ) : <EOL> class Meta ( object ) : <EOL> model = DraftRevision <EOL> fields = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:content>' , '<STR_LIT>' , '<STR_LIT:title>' , '<STR_LIT>' ) <EOL> def save ( self , request ) : <EOL> """<STR_LIT>""" <EOL> creator = request . user <EOL> doc_data = self . cleaned_data <EOL> parent_doc = doc_data [ '<STR_LIT>' ] . document <EOL> locale = request . LANGUAGE_CODE <EOL> draft , created = DraftRevision . objects . update_or_create ( <EOL> creator = creator , document = parent_doc , locale = locale , defaults = doc_data ) <EOL> return draft <EOL> class ReviewForm ( forms . Form ) : <EOL> comment = StrippedCharField ( max_length = <NUM_LIT> , widget = forms . Textarea ( ) , <EOL> required = False , label = _lazy ( u'<STR_LIT>' ) , <EOL> error_messages = { '<STR_LIT:max_length>' : COMMENT_LONG } ) <EOL> _widget = forms . RadioSelect ( renderer = RadioFieldRendererWithHelpText ) <EOL> significance = forms . TypedChoiceField ( <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> choices = SIGNIFICANCES , <EOL> initial = SIGNIFICANCES [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , <EOL> required = False , <EOL> widget = _widget , <EOL> coerce = int , empty_value = SIGNIFICANCES [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> is_ready_for_localization = forms . BooleanField ( <EOL> initial = False , <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> required = False ) <EOL> needs_change = forms . BooleanField ( <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> initial = False , <EOL> required = False ) <EOL> needs_change_comment = forms . CharField ( <EOL> label = _lazy ( u'<STR_LIT>' ) , <EOL> widget = forms . Textarea ( ) , <EOL> required = False ) <EOL> class AddContributorForm ( forms . Form ) : <EOL> """<STR_LIT>""" <EOL> users = MultiUsernameField ( <EOL> widget = forms . TextInput ( attrs = { '<STR_LIT>' : _lazy ( u'<STR_LIT:username>' ) , <EOL> '<STR_LIT:class>' : '<STR_LIT>' } ) ) <EOL> languages = [ ( '<STR_LIT>' , '<STR_LIT>' ) ] + [ ( l [ <NUM_LIT:0> ] , u'<STR_LIT>' . format ( * l ) ) <EOL> for l in settings . LANGUAGE_CHOICES ] <EOL> class RevisionFilterForm ( forms . Form ) : <EOL> """<STR_LIT>""" <EOL> locale = forms . ChoiceField ( label = _lazy ( u'<STR_LIT>' ) , choices = languages , <EOL> required = False ) <EOL> users = MultiUsernameField ( label = _lazy ( u'<STR_LIT>' ) , required = False ) <EOL> start = forms . DateField ( label = _lazy ( u'<STR_LIT>' ) , required = False ) <EOL> end = forms . DateField ( label = _lazy ( u'<STR_LIT>' ) , required = False ) </s>
