2009-01-05 14:30:08 +02:00
# -*- coding: utf-8 -*-
import re
2009-04-14 14:57:17 +03:00
import os . path
2009-01-05 14:30:08 +02:00
from datetime import datetime
from django import forms
from django . conf import settings
from django . db . models import Q
from django . contrib . auth . models import User
from django . utils . translation import ugettext as _
2009-12-23 17:06:48 +02:00
from djangobb_forum . models import Topic , Post , Profile , Reputation , Report , PrivateMessage , \
2009-08-02 19:51:00 +03:00
Forum , Attachment , TZ_CHOICES , PRIVACY_CHOICES
2010-01-04 23:41:21 +02:00
from djangobb_forum . markups import bbmarkup
2009-12-23 17:06:48 +02:00
from djangobb_forum import settings as forum_settings
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 ) :
2009-04-14 14:57:17 +03:00
name = forms . CharField ( label = _ ( ' Subject ' ) ,
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 )
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
2009-01-05 14:30:08 +02:00
self . fields [ ' body ' ] . widget = forms . Textarea ( attrs = { ' class ' : ' bbcode ' , ' 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
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
post = Post ( topic = topic , user = self . user , user_ip = self . ip ,
markup = ' bbcode ' ,
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 )
file ( path , ' w ' ) . write ( memfile . read ( ) )
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
self . fields [ ' body ' ] . widget = forms . Textarea ( attrs = { ' class ' : ' bbcode ' } )
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
fields = [ ' time_zone ' , ' language ' ]
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
def __init__ ( self , * args , * * kwargs ) :
2009-10-29 21:43:39 +02:00
self . user_view = kwargs . pop ( ' user_view ' , None )
self . user_request = kwargs . pop ( ' user_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 )
2009-10-29 21:43:39 +02:00
self . fields [ ' username ' ] . initial = self . user_view . username
if not self . user_request . is_superuser :
2009-04-21 14:41:20 +03:00
self . fields [ ' username ' ] . widget = forms . HiddenInput ( )
2009-10-29 21:43:39 +02:00
self . fields [ ' email ' ] . initial = self . user_view . 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 :
2009-10-29 21:43:39 +02:00
if self . user_request . is_superuser :
self . user_view . username = self . cleaned_data [ ' username ' ]
self . user_view . 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 ' ]
2009-10-29 21:43:39 +02:00
self . user_view . save ( )
2009-07-02 16:31:48 +03:00
if commit :
self . profile . save ( )
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 ) :
self . user = kwargs . pop ( ' user ' , None )
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 )
self . fields [ ' name ' ] . initial = " %s %s " % ( self . user . first_name , self . 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 :
self . user . first_name , self . user . last_name = cleaned_name . split ( None , 1 )
2009-01-05 14:30:08 +02:00
else :
2009-12-23 18:18:50 +02:00
self . user . first_name = cleaned_name
self . user . last_name = ' '
2009-07-02 16:31:48 +03:00
self . user . save ( )
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 ' ]
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 ' ]
def __init__ ( self , * args , * * kwargs ) :
super ( PersonalityProfileForm , self ) . __init__ ( * args , * * kwargs )
self . fields [ ' signature ' ] . widget = forms . Textarea ( attrs = { ' class ' : ' bbcode ' , ' 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 )
2010-01-04 23:41:21 +02:00
profile . signature = bbmarkup . bbcode ( profile . signature )
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
fields = [ ' theme ' ]
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 ) :
super ( PrivacyProfileForm , self ) . __init__ ( * args , * * kwargs )
self . fields [ ' privacy_permission ' ] . widget = forms . RadioSelect (
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 ' ]
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 ' ]
if sort_by == ' username ' :
if sort_dir == ' ASC ' :
2009-04-15 10:29:12 +03:00
return qs . filter ( username__contains = username , forum_profile__post_count__gte = forum_settings . POST_USER_SEARCH ) . order_by ( ' username ' )
2009-01-05 14:30:08 +02:00
elif sort_dir == ' DESC ' :
2009-04-15 10:29:12 +03:00
return qs . filter ( username__contains = username , forum_profile__post_count__gte = forum_settings . POST_USER_SEARCH ) . order_by ( ' -username ' )
2009-01-05 14:30:08 +02:00
elif sort_by == ' registered ' :
if sort_dir == ' ASC ' :
2009-04-15 10:29:12 +03:00
return qs . filter ( username__contains = username , forum_profile__post_count__gte = forum_settings . POST_USER_SEARCH ) . order_by ( ' date_joined ' )
2009-01-05 14:30:08 +02:00
elif sort_dir == ' DESC ' :
2009-04-15 10:29:12 +03:00
return qs . filter ( username__contains = username , forum_profile__post_count__gte = forum_settings . POST_USER_SEARCH ) . order_by ( ' -date_joined ' )
2009-01-05 14:30:08 +02:00
elif sort_by == ' num_posts ' :
if sort_dir == ' ASC ' :
2009-04-15 10:29:12 +03:00
return qs . filter ( username__contains = username , forum_profile__post_count__gte = forum_settings . POST_USER_SEARCH ) . order_by ( ' forum_profile__post_count ' )
2009-01-05 14:30:08 +02:00
elif sort_dir == ' DESC ' :
2009-04-15 10:29:12 +03:00
return qs . filter ( username__contains = username , forum_profile__post_count__gte = forum_settings . POST_USER_SEARCH ) . 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 ) :
keywords = forms . CharField ( required = False , label = _ ( ' Keyword search ' ) ,
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 ' ) )
sort_dir = forms . ChoiceField ( choices = SORT_DIR_CHOICES , label = _ ( ' Sort order ' ) )
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
fields = [ ' reason ' , ' topic ' , ' 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 )
self . topic = kwargs . pop ( ' topic ' , None )
self . sign = kwargs . pop ( ' sign ' , None )
super ( ReputationForm , self ) . __init__ ( * args , * * kwargs )
self . fields [ ' topic ' ] . widget = forms . HiddenInput ( )
self . fields [ ' sign ' ] . widget = forms . HiddenInput ( )
self . fields [ ' reason ' ] . widget = forms . Textarea ( attrs = { ' class ' : ' bbcode ' } )
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
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
reputation . time = datetime . now ( )
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 ' } ) )
body = forms . CharField ( required = False , label = _ ( ' Message ' ) ,
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
2009-07-02 16:01:22 +03:00
2009-01-05 14:30:08 +02:00
class CreatePMForm ( forms . ModelForm ) :
recipient = forms . CharField ( label = _ ( ' Recipient ' ) )
class Meta :
model = PrivateMessage
fields = [ ' subject ' , ' body ' ]
def __init__ ( self , * args , * * kwargs ) :
self . user = kwargs . pop ( ' user ' , None )
super ( CreatePMForm , self ) . __init__ ( * args , * * kwargs )
self . fields . keyOrder = [ ' recipient ' , ' subject ' , ' body ' ]
self . fields [ ' subject ' ] . widget = widget = forms . TextInput ( attrs = { ' size ' : ' 115 ' } )
self . fields [ ' body ' ] . widget = forms . Textarea ( attrs = { ' class ' : ' bbcode ' } )
def clean_recipient ( self ) :
name = self . cleaned_data [ ' recipient ' ]
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-01-09 16:23:39 +02:00
2009-01-05 14:30:08 +02:00
def save ( self ) :
pm = PrivateMessage ( src_user = self . user , dst_user = self . cleaned_data [ ' recipient ' ] )
pm = forms . save_instance ( self , pm )
2009-08-02 19:51:00 +03:00
return pm