2012-08-10 11:12:52 +03:00
# coding: utf-8
2009-04-14 14:57:17 +03:00
import os . path
2012-08-10 11:12:52 +03:00
from datetime import datetime , timedelta
2009-01-05 14:30:08 +02:00
from django import forms
from django . conf import settings
from django . contrib . auth . models import User
2012-08-10 11:12:52 +03:00
from django . db . models . expressions import F
2012-02-17 00:30:03 +02:00
from django . utils . translation import ugettext_lazy as _
2009-01-05 14:30:08 +02:00
2010-10-30 09:03:50 +03:00
from djangobb_forum . models import Topic , Post , Profile , Reputation , Report , \
2012-08-10 11:12:52 +03:00
Attachment , Poll , PollChoice
2009-12-23 17:06:48 +02:00
from djangobb_forum import settings as forum_settings
2012-03-24 10:48:48 +02:00
from djangobb_forum . util import convert_text_to_html , set_language
2009-08-02 19:51:00 +03:00
2009-01-05 14:30:08 +02:00
SORT_USER_BY_CHOICES = (
( ' username ' , _ ( u ' Username ' ) ) ,
( ' registered ' , _ ( u ' Registered ' ) ) ,
( ' num_posts ' , _ ( u ' No. of posts ' ) ) ,
)
SORT_POST_BY_CHOICES = (
( ' 0 ' , _ ( u ' Post time ' ) ) ,
( ' 1 ' , _ ( u ' Author ' ) ) ,
( ' 2 ' , _ ( u ' Subject ' ) ) ,
( ' 3 ' , _ ( u ' Forum ' ) ) ,
)
SORT_DIR_CHOICES = (
( ' ASC ' , _ ( u ' Ascending ' ) ) ,
( ' DESC ' , _ ( u ' Descending ' ) ) ,
)
SHOW_AS_CHOICES = (
( ' topics ' , _ ( u ' Topics ' ) ) ,
( ' posts ' , _ ( u ' Posts ' ) ) ,
)
SEARCH_IN_CHOICES = (
( ' all ' , _ ( u ' Message text and topic subject ' ) ) ,
( ' message ' , _ ( u ' Message text only ' ) ) ,
( ' topic ' , _ ( u ' Topic subject only ' ) ) ,
)
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
class AddPostForm ( forms . ModelForm ) :
2012-08-10 11:12:52 +03:00
FORM_NAME = " AddPostForm " # used in view and template submit button
2010-01-05 16:28:33 +02:00
name = forms . CharField ( label = _ ( ' Subject ' ) , max_length = 255 ,
2009-01-05 14:30:08 +02:00
widget = forms . TextInput ( attrs = { ' size ' : ' 115 ' } ) )
2009-04-21 20:45:03 +03:00
attachment = forms . FileField ( label = _ ( ' Attachment ' ) , required = False )
2012-08-07 16:44:16 +03:00
subscribe = forms . BooleanField ( label = _ ( ' Subscribe ' ) , help_text = _ ( " Subscribe this topic. " ) , required = False )
2009-01-05 14:30:08 +02:00
class Meta :
model = Post
fields = [ ' body ' ]
def __init__ ( self , * args , * * kwargs ) :
self . user = kwargs . pop ( ' user ' , None )
self . topic = kwargs . pop ( ' topic ' , None )
self . forum = kwargs . pop ( ' forum ' , None )
self . ip = kwargs . pop ( ' ip ' , None )
super ( AddPostForm , self ) . __init__ ( * args , * * kwargs )
2009-05-25 13:56:21 +03:00
2009-01-05 14:30:08 +02:00
if self . topic :
self . fields [ ' name ' ] . widget = forms . HiddenInput ( )
self . fields [ ' name ' ] . required = False
2009-05-25 13:56:21 +03:00
2010-11-28 17:57:54 +02:00
self . fields [ ' body ' ] . widget = forms . Textarea ( attrs = { ' class ' : ' markup ' , ' rows ' : ' 20 ' , ' cols ' : ' 95 ' } )
2009-05-25 13:56:21 +03:00
2009-04-14 14:57:17 +03:00
if not forum_settings . ATTACHMENT_SUPPORT :
self . fields [ ' attachment ' ] . widget = forms . HiddenInput ( )
self . fields [ ' attachment ' ] . required = False
2009-01-05 14:30:08 +02:00
2011-05-16 14:13:36 +03:00
def clean ( self ) :
'''
checking is post subject and body contains not only space characters
'''
errmsg = _ ( ' Can \' t be empty nor contain only whitespace characters ' )
cleaned_data = self . cleaned_data
body = cleaned_data . get ( ' body ' )
subject = cleaned_data . get ( ' name ' )
2011-05-17 10:37:25 +03:00
if subject :
2011-05-16 14:13:36 +03:00
if not subject . strip ( ) :
self . _errors [ ' name ' ] = self . error_class ( [ errmsg ] )
del cleaned_data [ ' name ' ]
2011-05-17 10:37:25 +03:00
if body :
2011-05-16 14:13:36 +03:00
if not body . strip ( ) :
self . _errors [ ' body ' ] = self . error_class ( [ errmsg ] )
del cleaned_data [ ' body ' ]
return cleaned_data
2009-04-14 14:57:17 +03:00
def clean_attachment ( self ) :
if self . cleaned_data [ ' attachment ' ] :
memfile = self . cleaned_data [ ' attachment ' ]
if memfile . size > forum_settings . ATTACHMENT_SIZE_LIMIT :
raise forms . ValidationError ( _ ( ' Attachment is too big ' ) )
return self . cleaned_data [ ' attachment ' ]
2009-01-05 14:30:08 +02:00
2009-11-20 16:25:14 +02:00
def save ( self ) :
2009-01-05 14:30:08 +02:00
if self . forum :
topic = Topic ( forum = self . forum ,
user = self . user ,
name = self . cleaned_data [ ' name ' ] )
topic . save ( )
else :
topic = self . topic
2012-08-07 16:44:16 +03:00
if self . cleaned_data [ ' subscribe ' ] :
# User would like to subscripe to this topic
topic . subscribers . add ( self . user )
2009-01-05 14:30:08 +02:00
post = Post ( topic = topic , user = self . user , user_ip = self . ip ,
2010-11-28 17:57:54 +02:00
markup = self . user . forum_profile . markup ,
2009-01-05 14:30:08 +02:00
body = self . cleaned_data [ ' body ' ] )
2009-11-20 16:25:14 +02:00
post . save ( )
2009-04-14 14:57:17 +03:00
if forum_settings . ATTACHMENT_SUPPORT :
self . save_attachment ( post , self . cleaned_data [ ' attachment ' ] )
2009-01-05 14:30:08 +02:00
return post
2009-07-02 16:01:22 +03:00
2009-11-20 16:25:14 +02:00
2009-04-14 14:57:17 +03:00
def save_attachment ( self , post , memfile ) :
if memfile :
obj = Attachment ( size = memfile . size , content_type = memfile . content_type ,
name = memfile . name , post = post )
dir = os . path . join ( settings . MEDIA_ROOT , forum_settings . ATTACHMENT_UPLOAD_TO )
fname = ' %d .0 ' % post . id
path = os . path . join ( dir , fname )
2010-02-18 11:04:35 +02:00
file ( path , ' wb ' ) . write ( memfile . read ( ) )
2009-04-14 14:57:17 +03:00
obj . path = fname
obj . save ( )
2009-07-02 16:01:22 +03:00
2009-12-17 22:30:17 +02:00
class EditPostForm ( forms . ModelForm ) :
name = forms . CharField ( required = False , label = _ ( ' Subject ' ) ,
widget = forms . TextInput ( attrs = { ' size ' : ' 115 ' } ) )
class Meta :
model = Post
fields = [ ' body ' ]
def __init__ ( self , * args , * * kwargs ) :
self . topic = kwargs . pop ( ' topic ' , None )
super ( EditPostForm , self ) . __init__ ( * args , * * kwargs )
self . fields [ ' name ' ] . initial = self . topic
2010-11-28 17:57:54 +02:00
self . fields [ ' body ' ] . widget = forms . Textarea ( attrs = { ' class ' : ' markup ' } )
2009-12-17 22:30:17 +02:00
def save ( self , commit = True ) :
post = super ( EditPostForm , self ) . save ( commit = False )
post . updated = datetime . now ( )
topic_name = self . cleaned_data [ ' name ' ]
if topic_name :
post . topic . name = topic_name
if commit :
post . topic . save ( )
post . save ( )
return post
2009-01-05 14:30:08 +02:00
class EssentialsProfileForm ( forms . ModelForm ) :
username = forms . CharField ( label = _ ( ' Username ' ) )
email = forms . CharField ( label = _ ( ' E-mail ' ) )
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
class Meta :
model = Profile
2012-08-07 16:44:16 +03:00
fields = [ ' auto_subscribe ' , ' time_zone ' , ' language ' ]
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
def __init__ ( self , * args , * * kwargs ) :
2012-03-05 01:51:00 +02:00
extra_args = kwargs . pop ( ' extra_args ' , { } )
2012-03-05 12:07:15 +02:00
self . request = extra_args . pop ( ' request ' , None )
2009-07-02 16:01:22 +03:00
self . profile = kwargs [ ' instance ' ]
2009-01-05 14:30:08 +02:00
super ( EssentialsProfileForm , self ) . __init__ ( * args , * * kwargs )
2012-03-05 12:07:15 +02:00
self . fields [ ' username ' ] . initial = self . profile . user . username
if not self . request . user . is_superuser :
2009-04-21 14:41:20 +03:00
self . fields [ ' username ' ] . widget = forms . HiddenInput ( )
2012-03-05 12:07:15 +02:00
self . fields [ ' email ' ] . initial = self . profile . user . email
2009-07-02 16:01:22 +03:00
2009-07-02 16:31:48 +03:00
def save ( self , commit = True ) :
2009-01-05 14:30:08 +02:00
if self . cleaned_data :
2012-03-05 12:07:15 +02:00
if self . request . user . is_superuser :
self . profile . user . username = self . cleaned_data [ ' username ' ]
self . profile . user . email = self . cleaned_data [ ' email ' ]
2009-07-02 16:01:22 +03:00
self . profile . time_zone = self . cleaned_data [ ' time_zone ' ]
self . profile . language = self . cleaned_data [ ' language ' ]
2012-03-05 12:07:15 +02:00
self . profile . user . save ( )
2009-07-02 16:31:48 +03:00
if commit :
self . profile . save ( )
2012-03-05 12:07:15 +02:00
set_language ( self . request , self . profile . language )
2009-07-02 16:01:22 +03:00
return self . profile
2009-01-05 14:30:08 +02:00
class PersonalProfileForm ( forms . ModelForm ) :
2010-01-05 16:16:05 +02:00
name = forms . CharField ( label = _ ( ' Real name ' ) , required = False )
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
class Meta :
model = Profile
fields = [ ' status ' , ' location ' , ' site ' ]
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
def __init__ ( self , * args , * * kwargs ) :
2012-03-05 01:51:00 +02:00
extra_args = kwargs . pop ( ' extra_args ' , { } )
2009-07-02 16:31:48 +03:00
self . profile = kwargs [ ' instance ' ]
2009-01-05 14:30:08 +02:00
super ( PersonalProfileForm , self ) . __init__ ( * args , * * kwargs )
2012-03-05 12:07:15 +02:00
self . fields [ ' name ' ] . initial = " %s %s " % ( self . profile . user . first_name , self . profile . user . last_name )
2009-07-02 16:01:22 +03:00
2009-07-02 16:31:48 +03:00
def save ( self , commit = True ) :
self . profile . status = self . cleaned_data [ ' status ' ]
self . profile . location = self . cleaned_data [ ' location ' ]
self . profile . site = self . cleaned_data [ ' site ' ]
2009-01-05 14:30:08 +02:00
if self . cleaned_data [ ' name ' ] :
2009-12-23 18:18:50 +02:00
cleaned_name = self . cleaned_data [ ' name ' ] . strip ( )
if ' ' in cleaned_name :
2012-03-05 12:07:15 +02:00
self . profile . user . first_name , self . profile . user . last_name = cleaned_name . split ( None , 1 )
2009-01-05 14:30:08 +02:00
else :
2012-03-05 12:07:15 +02:00
self . profile . user . first_name = cleaned_name
self . profile . user . last_name = ' '
self . profile . user . save ( )
2009-07-02 16:31:48 +03:00
if commit :
self . profile . save ( )
return self . profile
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
class MessagingProfileForm ( forms . ModelForm ) :
class Meta :
model = Profile
fields = [ ' jabber ' , ' icq ' , ' msn ' , ' aim ' , ' yahoo ' ]
2012-03-05 01:51:00 +02:00
def __init__ ( self , * args , * * kwargs ) :
extra_args = kwargs . pop ( ' extra_args ' , { } )
super ( MessagingProfileForm , self ) . __init__ ( * args , * * kwargs )
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
class PersonalityProfileForm ( forms . ModelForm ) :
class Meta :
model = Profile
fields = [ ' show_avatar ' , ' signature ' ]
2012-08-10 11:12:52 +03:00
2009-01-05 14:30:08 +02:00
def __init__ ( self , * args , * * kwargs ) :
2012-03-05 01:51:00 +02:00
extra_args = kwargs . pop ( ' extra_args ' , { } )
2012-03-05 12:07:15 +02:00
self . profile = kwargs [ ' instance ' ]
2009-01-05 14:30:08 +02:00
super ( PersonalityProfileForm , self ) . __init__ ( * args , * * kwargs )
2010-11-28 17:57:54 +02:00
self . fields [ ' signature ' ] . widget = forms . Textarea ( attrs = { ' class ' : ' markup ' , ' rows ' : ' 10 ' , ' cols ' : ' 75 ' } )
2009-07-02 16:01:22 +03:00
2009-07-02 16:31:48 +03:00
def save ( self , commit = True ) :
2009-01-05 14:30:08 +02:00
profile = super ( PersonalityProfileForm , self ) . save ( commit = False )
2012-03-05 12:07:15 +02:00
profile . signature_html = convert_text_to_html ( profile . signature , self . profile . markup )
2009-07-02 16:31:48 +03:00
if commit :
profile . save ( )
2009-01-05 14:30:08 +02:00
return profile
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
class DisplayProfileForm ( forms . ModelForm ) :
class Meta :
model = Profile
2010-11-28 17:57:54 +02:00
fields = [ ' theme ' , ' markup ' , ' show_smilies ' ]
2009-07-02 16:01:22 +03:00
2012-03-05 01:51:00 +02:00
def __init__ ( self , * args , * * kwargs ) :
extra_args = kwargs . pop ( ' extra_args ' , { } )
super ( DisplayProfileForm , self ) . __init__ ( * args , * * kwargs )
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
class PrivacyProfileForm ( forms . ModelForm ) :
class Meta :
model = Profile
fields = [ ' privacy_permission ' ]
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
def __init__ ( self , * args , * * kwargs ) :
2012-03-05 01:51:00 +02:00
extra_args = kwargs . pop ( ' extra_args ' , { } )
2009-01-05 14:30:08 +02:00
super ( PrivacyProfileForm , self ) . __init__ ( * args , * * kwargs )
2012-08-10 11:12:52 +03:00
self . fields [ ' privacy_permission ' ] . widget = forms . RadioSelect (
2009-01-05 14:30:08 +02:00
choices = self . fields [ ' privacy_permission ' ] . choices
)
2009-04-15 12:12:20 +03:00
2009-01-05 14:30:08 +02:00
class UploadAvatarForm ( forms . ModelForm ) :
class Meta :
model = Profile
fields = [ ' avatar ' ]
2012-03-05 01:51:00 +02:00
def __init__ ( self , * args , * * kwargs ) :
extra_args = kwargs . pop ( ' extra_args ' , { } )
super ( UploadAvatarForm , self ) . __init__ ( * args , * * kwargs )
2009-04-15 12:12:20 +03:00
2009-01-05 14:30:08 +02:00
class UserSearchForm ( forms . Form ) :
2009-01-12 15:02:48 +02:00
username = forms . CharField ( required = False , label = _ ( ' Username ' ) )
2009-01-05 14:30:08 +02:00
#show_group = forms.ChoiceField(choices=SHOW_GROUP_CHOICES)
2009-01-12 15:02:48 +02:00
sort_by = forms . ChoiceField ( choices = SORT_USER_BY_CHOICES , label = _ ( ' Sort by ' ) )
sort_dir = forms . ChoiceField ( choices = SORT_DIR_CHOICES , label = _ ( ' Sort order ' ) )
2009-01-05 14:30:08 +02:00
def filter ( self , qs ) :
if self . is_valid ( ) :
username = self . cleaned_data [ ' username ' ]
#show_group = self.cleaned_data['show_group']
sort_by = self . cleaned_data [ ' sort_by ' ]
sort_dir = self . cleaned_data [ ' sort_dir ' ]
2012-04-25 14:04:46 +03:00
qs = qs . filter ( username__contains = username , forum_profile__post_count__gte = forum_settings . POST_USER_SEARCH )
2012-08-10 11:12:52 +03:00
if sort_by == ' username ' :
if sort_dir == ' ASC ' :
2012-04-25 14:04:46 +03:00
return qs . order_by ( ' username ' )
2012-08-10 11:12:52 +03:00
elif sort_dir == ' DESC ' :
2012-04-25 14:04:46 +03:00
return qs . order_by ( ' -username ' )
2012-08-10 11:12:52 +03:00
elif sort_by == ' registered ' :
if sort_dir == ' ASC ' :
2012-04-25 14:04:46 +03:00
return qs . order_by ( ' date_joined ' )
2012-08-10 11:12:52 +03:00
elif sort_dir == ' DESC ' :
2012-04-25 14:04:46 +03:00
return qs . order_by ( ' -date_joined ' )
2012-08-10 11:12:52 +03:00
elif sort_by == ' num_posts ' :
if sort_dir == ' ASC ' :
2012-04-25 14:04:46 +03:00
return qs . order_by ( ' forum_profile__post_count ' )
2012-08-10 11:12:52 +03:00
elif sort_dir == ' DESC ' :
2012-04-25 14:04:46 +03:00
return qs . order_by ( ' -forum_profile__post_count ' )
2009-01-05 14:30:08 +02:00
else :
return qs
2009-04-03 15:09:08 +03:00
class PostSearchForm ( forms . Form ) :
2012-08-10 11:12:52 +03:00
keywords = forms . CharField ( required = False , label = _ ( ' Keyword search ' ) ,
2009-04-03 15:09:08 +03:00
widget = forms . TextInput ( attrs = { ' size ' : ' 40 ' , ' maxlength ' : ' 100 ' } ) )
author = forms . CharField ( required = False , label = _ ( ' Author search ' ) ,
widget = forms . TextInput ( attrs = { ' size ' : ' 25 ' , ' maxlength ' : ' 25 ' } ) )
forum = forms . CharField ( required = False , label = _ ( ' Forum ' ) )
search_in = forms . ChoiceField ( choices = SEARCH_IN_CHOICES , label = _ ( ' Search in ' ) )
sort_by = forms . ChoiceField ( choices = SORT_POST_BY_CHOICES , label = _ ( ' Sort by ' ) )
2010-11-04 12:49:41 +02:00
sort_dir = forms . ChoiceField ( choices = SORT_DIR_CHOICES , initial = ' DESC ' , label = _ ( ' Sort order ' ) )
2009-04-03 15:09:08 +03:00
show_as = forms . ChoiceField ( choices = SHOW_AS_CHOICES , label = _ ( ' Show results as ' ) )
2009-07-02 16:01:22 +03:00
2009-04-03 15:09:08 +03:00
2009-01-05 14:30:08 +02:00
class ReputationForm ( forms . ModelForm ) :
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
class Meta :
model = Reputation
2010-02-23 14:19:20 +02:00
fields = [ ' reason ' , ' post ' , ' sign ' ]
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
def __init__ ( self , * args , * * kwargs ) :
self . from_user = kwargs . pop ( ' from_user ' , None )
self . to_user = kwargs . pop ( ' to_user ' , None )
2010-02-23 14:19:20 +02:00
self . post = kwargs . pop ( ' post ' , None )
2009-01-05 14:30:08 +02:00
self . sign = kwargs . pop ( ' sign ' , None )
super ( ReputationForm , self ) . __init__ ( * args , * * kwargs )
2010-02-23 14:19:20 +02:00
self . fields [ ' post ' ] . widget = forms . HiddenInput ( )
2009-01-05 14:30:08 +02:00
self . fields [ ' sign ' ] . widget = forms . HiddenInput ( )
2010-11-28 17:57:54 +02:00
self . fields [ ' reason ' ] . widget = forms . Textarea ( attrs = { ' class ' : ' markup ' } )
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
def clean_to_user ( self ) :
name = self . cleaned_data [ ' to_user ' ]
try :
user = User . objects . get ( username = name )
except User . DoesNotExist :
raise forms . ValidationError ( _ ( ' User with login %s does not exist ' ) % name )
else :
return user
2009-07-02 16:01:22 +03:00
2010-02-23 14:19:20 +02:00
def clean ( self ) :
try :
Reputation . objects . get ( from_user = self . from_user , post = self . cleaned_data [ ' post ' ] )
except Reputation . DoesNotExist :
pass
else :
raise forms . ValidationError ( _ ( ' You already voted for this post ' ) )
2012-08-10 11:12:52 +03:00
2012-06-16 12:45:32 +03:00
# check if this post really belong to `from_user`
if not Post . objects . filter ( pk = self . cleaned_data [ ' post ' ] . id , user = self . to_user ) . exists ( ) :
raise forms . ValidationError ( _ ( ' This post does \' t belong to this user ' ) )
2010-02-23 15:45:47 +02:00
return self . cleaned_data
2010-02-23 14:19:20 +02:00
2009-07-02 16:31:48 +03:00
def save ( self , commit = True ) :
2009-01-05 14:30:08 +02:00
reputation = super ( ReputationForm , self ) . save ( commit = False )
reputation . from_user = self . from_user
reputation . to_user = self . to_user
2009-07-02 16:31:48 +03:00
if commit :
reputation . save ( )
2009-01-05 14:30:08 +02:00
return reputation
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
class MailToForm ( forms . Form ) :
subject = forms . CharField ( label = _ ( ' Subject ' ) ,
widget = forms . TextInput ( attrs = { ' size ' : ' 75 ' , ' maxlength ' : ' 70 ' , ' class ' : ' longinput ' } ) )
2012-08-10 11:12:52 +03:00
body = forms . CharField ( required = False , label = _ ( ' Message ' ) ,
2009-01-05 14:30:08 +02:00
widget = forms . Textarea ( attrs = { ' rows ' : ' 10 ' , ' cols ' : ' 75 ' } ) )
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
class ReportForm ( forms . ModelForm ) :
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
class Meta :
model = Report
fields = [ ' reason ' , ' post ' ]
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
def __init__ ( self , * args , * * kwargs ) :
self . reported_by = kwargs . pop ( ' reported_by ' , None )
self . post = kwargs . pop ( ' post ' , None )
super ( ReportForm , self ) . __init__ ( * args , * * kwargs )
self . fields [ ' post ' ] . widget = forms . HiddenInput ( )
self . fields [ ' post ' ] . initial = self . post
self . fields [ ' reason ' ] . widget = forms . Textarea ( attrs = { ' rows ' : ' 10 ' , ' cols ' : ' 75 ' } )
2009-07-02 16:01:22 +03:00
2009-07-02 16:31:48 +03:00
def save ( self , commit = True ) :
2009-01-05 14:30:08 +02:00
report = super ( ReportForm , self ) . save ( commit = False )
report . created = datetime . now ( )
report . reported_by = self . reported_by
2009-07-02 16:31:48 +03:00
if commit :
report . save ( )
2009-01-05 14:30:08 +02:00
return report
2012-08-10 11:12:52 +03:00
class VotePollForm ( forms . Form ) :
"""
Dynamic form for the poll .
"""
FORM_NAME = " VotePollForm " # used in view and template submit button
choice = forms . MultipleChoiceField ( )
def __init__ ( self , poll , * args , * * kwargs ) :
self . poll = poll
super ( VotePollForm , self ) . __init__ ( * args , * * kwargs )
choices = self . poll . choices . all ( ) . values_list ( " id " , " choice " )
if self . poll . choice_count == 1 :
self . fields [ " choice " ] = forms . ChoiceField (
choices = choices , widget = forms . RadioSelect
)
else :
self . fields [ " choice " ] = forms . MultipleChoiceField (
choices = choices , widget = forms . CheckboxSelectMultiple
)
def clean_choice ( self ) :
ids = self . cleaned_data [ " choice " ]
count = len ( ids )
if count > self . poll . choice_count :
raise forms . ValidationError (
_ ( u ' You have selected too many choices! (Only %i allowed.) ' ) % self . poll . choice_count
)
return ids
class PollForm ( forms . ModelForm ) :
answers = forms . CharField ( min_length = 2 , widget = forms . Textarea ,
help_text = _ ( " Write each answer on a new line. " )
)
days = forms . IntegerField ( required = False , min_value = 1 ,
help_text = _ ( " Number of days for this poll to run. Leave empty for never ending poll. " )
)
class Meta :
model = Poll
fields = [ ' question ' , ' choice_count ' ]
def create_poll ( self ) :
"""
return True if one field filled with data - > the user wants to create a poll
"""
return any ( self . data . get ( key ) for key in ( ' question ' , ' answers ' , ' days ' ) )
def clean_answers ( self ) :
# validate if there is more than whitespaces ;)
raw_answers = self . cleaned_data [ " answers " ]
answers = [ answer . strip ( ) for answer in raw_answers . splitlines ( ) if answer . strip ( ) ]
if len ( answers ) == 0 :
raise forms . ValidationError ( _ ( u " There is no valid answer! " ) )
# validate length of all answers
is_max_length = max ( [ len ( answer ) for answer in answers ] )
should_max_length = PollChoice . _meta . get_field ( " choice " ) . max_length
if is_max_length > should_max_length :
raise forms . ValidationError ( _ ( u " One of this answers are too long! " ) )
return answers
def save ( self , post ) :
"""
Create poll and all answers in PollChoice model .
"""
poll = super ( PollForm , self ) . save ( commit = False )
poll . topic = post . topic
days = self . cleaned_data [ " days " ]
if days :
now = datetime . now ( )
poll . deactivate_date = now + timedelta ( days = days )
poll . save ( )
answers = self . cleaned_data [ " answers " ]
for answer in answers :
PollChoice . objects . create ( poll = poll , choice = answer )