2009-01-05 14:30:08 +02:00
import math
import re
import datetime
2009-01-21 01:13:46 +02:00
from django . shortcuts import get_object_or_404
2009-04-07 17:46:22 +03:00
from django . http import Http404 , HttpResponse , HttpResponseRedirect , HttpResponseForbidden
2009-01-05 14:30:08 +02:00
from django . contrib . auth . models import User
from django . contrib . auth . decorators import login_required
from django . conf import settings
from django . core . urlresolvers import reverse
from django . db import connection
from django . core . cache import cache
2009-03-03 18:30:41 +02:00
from django . utils import translation
2009-05-24 22:51:42 +03:00
from django . db . models import Q , Sum
2009-01-05 14:30:08 +02:00
2009-03-31 00:03:16 +03:00
from forum . util import render_to , paged , build_form , paginate , set_language
2009-04-03 23:38:12 +03:00
from forum . models import Category , Forum , Topic , Post , Profile , Read , \
2009-04-14 14:57:17 +03:00
Reputation , Report , PrivateMessage , Attachment
2009-04-03 23:38:12 +03:00
from forum . forms import AddPostForm , EditPostForm , UserSearchForm , \
PostSearchForm , ReputationForm , MailToForm , EssentialsProfileForm , \
PersonalProfileForm , MessagingProfileForm , PersonalityProfileForm , \
DisplayProfileForm , PrivacyProfileForm , ReportForm , UploadAvatarForm , CreatePMForm
2009-03-31 00:03:16 +03:00
from forum . markups import mypostmarkup
from forum . templatetags import forum_extras
from forum import settings as forum_settings
from forum . util import urlize , smiles
from forum . index import post_indexer
2009-01-05 14:30:08 +02:00
@render_to ( ' forum/index.html ' )
2009-02-09 20:31:19 +02:00
def index ( request , full = True ) :
2009-04-03 23:38:12 +03:00
users_cached = cache . get ( ' users_online ' , { } )
2009-04-03 23:47:38 +03:00
users_online = users_cached and User . objects . filter ( id__in = users_cached . keys ( ) ) or [ ]
2009-04-03 23:38:12 +03:00
guests_cached = cache . get ( ' guests_online ' , { } )
guest_count = len ( guests_cached )
users_count = len ( users_online )
2009-04-07 18:43:04 +03:00
2009-01-05 14:30:08 +02:00
cats = { }
forums = { }
2009-04-07 17:46:22 +03:00
user_groups = request . user . groups . all ( )
2009-04-07 21:27:45 +03:00
if request . user . is_anonymous ( ) : # in django 1.1 EmptyQuerySet raise exception
user_groups = [ ]
2009-05-24 22:51:42 +03:00
for forum in Forum . objects . filter ( Q ( category__groups__in = user_groups ) | Q ( category__groups__isnull = True ) ) . select_related ( ' last_post__topic ' , ' last_post__user ' , ' category ' ) :
2009-01-05 14:30:08 +02:00
cat = cats . setdefault ( forum . category . id ,
{ ' cat ' : forum . category , ' forums ' : [ ] } )
cat [ ' forums ' ] . append ( forum )
forums [ forum . id ] = forum
cmpdef = lambda a , b : cmp ( a [ ' cat ' ] . position , b [ ' cat ' ] . position )
cats = sorted ( cats . values ( ) , cmpdef )
2009-05-24 18:50:57 +03:00
2009-02-09 20:31:19 +02:00
if full :
return { ' cats ' : cats ,
' posts ' : Post . objects . count ( ) ,
' topics ' : Topic . objects . count ( ) ,
' users ' : User . objects . count ( ) ,
' users_online ' : users_online ,
2009-04-03 23:38:12 +03:00
' online_count ' : users_count ,
2009-02-09 20:31:19 +02:00
' guest_count ' : guest_count ,
' last_user ' : User . objects . order_by ( ' -date_joined ' ) [ 0 ] ,
}
else :
return { ' cats ' : cats ,
' posts ' : Post . objects . count ( ) ,
' topics ' : Topic . objects . count ( ) ,
' users ' : User . objects . count ( ) ,
' users_online ' : users_online ,
2009-04-03 23:38:12 +03:00
' online_count ' : users_count ,
2009-02-09 20:31:19 +02:00
' guest_count ' : guest_count ,
' last_user ' : User . objects . order_by ( ' -date_joined ' ) [ 0 ] ,
} , ' forum/lofi/index.html '
2009-01-05 14:30:08 +02:00
@render_to ( ' forum/moderate.html ' )
2009-01-17 17:56:19 +02:00
@paged ( ' topics ' , forum_settings . FORUM_PAGE_SIZE )
2009-01-05 14:30:08 +02:00
def moderate ( request , forum_id ) :
forum = Forum . objects . get ( pk = forum_id )
topics = forum . topics . filter ( sticky = False ) . select_related ( )
if request . user . is_superuser or request . user in forum . moderators . all ( ) :
if ' move_topics ' in request . POST :
for topic in request . POST :
topic_match = re . match ( ' topic_id \ [( \ d+) \ ] ' , topic )
2009-04-07 17:46:22 +03:00
2009-01-05 14:30:08 +02:00
if topic_match :
topic_id = topic_match . group ( 1 )
reverse ( ' move_topic ' , args = [ topic_id ] )
2009-01-21 01:13:46 +02:00
2009-01-05 14:30:08 +02:00
return HttpResponseRedirect ( reverse ( ' index ' ) )
elif ' delete_topics ' in request . POST :
for topic in request . POST :
topic_match = re . match ( ' topic_id \ [( \ d+) \ ] ' , reputation )
2009-01-21 01:13:46 +02:00
2009-01-05 14:30:08 +02:00
if topic_match :
topic_id = topic_match . group ( 1 )
topic = get_object_or_404 ( Topic , pk = topic_id )
topic . delete ( )
return HttpResponseRedirect ( reverse ( ' index ' ) )
elif ' open_topics ' in request . POST :
for topic in request . POST :
topic_match = re . match ( ' topic_id \ [( \ d+) \ ] ' , topic )
2009-01-21 01:13:46 +02:00
2009-01-05 14:30:08 +02:00
if topic_match :
topic_id = topic_match . group ( 1 )
open_topic ( request , topic_id )
2009-01-21 01:13:46 +02:00
2009-01-05 14:30:08 +02:00
return HttpResponseRedirect ( reverse ( ' index ' ) )
elif ' close_topics ' in request . POST :
for topic in request . POST :
topic_match = re . match ( ' topic_id \ [( \ d+) \ ] ' , topic )
2009-01-21 01:13:46 +02:00
2009-01-05 14:30:08 +02:00
if topic_match :
topic_id = topic_match . group ( 1 )
close_topic ( request , topic_id )
return HttpResponseRedirect ( reverse ( ' index ' ) )
2009-01-21 01:13:46 +02:00
2009-01-05 14:30:08 +02:00
return { ' forum ' : forum ,
' topics ' : topics ,
' sticky_topics ' : forum . topics . filter ( sticky = True ) ,
' paged_qs ' : topics ,
' posts ' : forum . posts . count ( ) ,
}
else :
raise Http404
2009-01-21 01:13:46 +02:00
@render_to ( ' forum/search_topics.html ' )
2009-01-05 14:30:08 +02:00
def search ( request ) :
2009-04-07 18:43:04 +03:00
# TODO: move to form
2009-01-05 14:30:08 +02:00
if ' action ' in request . GET :
2009-01-21 01:13:46 +02:00
action = request . GET [ ' action ' ]
if action == ' show_24h ' :
2009-01-05 14:30:08 +02:00
date = datetime . datetime . today ( ) - datetime . timedelta ( 1 )
topics = Topic . objects . filter ( created__gte = date ) . order_by ( ' created ' )
2009-01-21 01:13:46 +02:00
elif action == ' show_new ' :
2009-04-07 18:43:04 +03:00
#TODO: FIXME
2009-01-05 14:30:08 +02:00
topics = Topic . objects . all ( ) . order_by ( ' created ' )
2009-04-07 18:43:04 +03:00
topics = [ topic for topic in topics if forum_extras . has_unreads ( topic , request . user ) ]
2009-01-21 01:13:46 +02:00
elif action == ' show_unanswered ' :
2009-01-05 14:30:08 +02:00
topics = Topic . objects . filter ( post_count = 1 )
2009-01-21 01:13:46 +02:00
elif action == ' show_subscriptions ' :
2009-01-05 14:30:08 +02:00
topics = Topic . objects . filter ( subscribers = request . user )
2009-01-21 01:13:46 +02:00
elif action == ' show_user ' :
2009-01-05 14:30:08 +02:00
user_id = request . GET [ ' user_id ' ]
posts = Post . objects . filter ( user__id = user_id )
topics = [ post . topic for post in posts ]
2009-04-07 18:43:04 +03:00
elif action == ' search ' :
2009-03-26 21:57:57 +02:00
keywords = request . GET . get ( ' keywords ' )
author = request . GET . get ( ' author ' )
forum = request . GET . get ( ' forum ' )
search_in = request . GET . get ( ' search_in ' )
sort_by = request . GET . get ( ' sort_by ' )
sort_dir = request . GET . get ( ' sort_dir ' )
2009-04-07 18:43:04 +03:00
2009-03-26 21:57:57 +02:00
if keywords and author :
if search_in == ' all ' :
if forum == ' 0 ' :
query = ' user: %s AND (topic: %s OR body: %s ) ' % ( author , keywords , keywords )
else :
query = ' user: %s AND forum: %s AND (topic: %s OR body: %s ) ' % ( author , forum , keywords , keywords )
elif search_in == ' message ' :
if forum == ' 0 ' :
query = ' user: %s AND body: %s ' % ( author , keywords )
else :
query = ' user: %s AND forum: %s AND body: %s ' % ( author , forum , keywords )
elif search_in == ' topic ' :
if forum == ' 0 ' :
query = ' user: %s AND topic: %s ' % ( author , keywords )
else :
query = ' user: %s AND forum: %s AND topic: %s ' % ( author , forum , keywords )
elif keywords :
if search_in == ' all ' :
if forum == ' 0 ' :
query = ' topic: %s OR body: %s ' % ( keywords , keywords )
else :
query = ' forum: %s AND (topic: %s OR body: %s ) ' % ( forum , keywords , keywords )
elif search_in == ' message ' :
if forum == ' 0 ' :
query = ' body: %s ' % ( keywords )
else :
query = ' forum: %s AND body: %s ' % ( forum , keywords )
elif search_in == ' topic ' :
if forum == ' 0 ' :
query = ' topic: %s ' % ( keywords )
else :
query = ' forum: %s AND topic: %s ' % ( forum , keywords )
elif author :
if forum == ' 0 ' :
query = ' user: %s ' % ( author )
else :
query = ' forum: %s AND user: %s ' % ( forum , author )
if sort_by == ' 0 ' :
order = ' created '
elif sort_by == ' 1 ' :
order = ' user '
elif sort_by == ' 2 ' :
order = ' topic '
elif sort_by == ' 3 ' :
order = ' forum '
if sort_dir == ' DESC ' :
order = ' - ' + order
posts = post_indexer . search ( query ) . order_by ( order )
if ' topics ' in request . GET [ ' show_as ' ] :
topics = [ ]
for post in posts :
if post . instance . topic not in topics :
topics . append ( post . instance . topic )
return { ' topics ' : topics } , ' forum/search_topics.html '
2009-01-05 14:30:08 +02:00
elif ' posts ' in request . GET [ ' show_as ' ] :
2009-01-21 01:13:46 +02:00
return { ' posts ' : posts } , ' forum/search_posts.html '
return { ' topics ' : topics }
else :
2009-01-05 14:30:08 +02:00
form = PostSearchForm ( )
2009-01-21 01:13:46 +02:00
return { ' categories ' : Category . objects . all ( ) ,
' form ' : form ,
} , ' forum/search_form.html '
2009-01-20 23:55:09 +02:00
2009-01-05 14:30:08 +02:00
@login_required
2009-01-21 01:13:46 +02:00
@render_to ( ' forum/report.html ' )
2009-01-05 14:30:08 +02:00
def misc ( request ) :
if ' action ' in request . GET :
2009-01-21 01:13:46 +02:00
action = request . GET [ ' action ' ]
if action == ' markread ' :
2009-01-05 14:30:08 +02:00
for category in Category . objects . all ( ) :
for topic in category . topics :
read , new = Read . objects . get_or_create ( user = request . user , topic = topic )
if not new :
read . time = datetime . datetime . now ( )
read . save ( )
return HttpResponseRedirect ( reverse ( ' index ' ) )
2009-01-21 01:13:46 +02:00
elif action == ' report ' :
if request . GET . get ( ' post_id ' , ' ' ) :
2009-01-05 14:30:08 +02:00
post_id = request . GET [ ' post_id ' ]
post = get_object_or_404 ( Post , id = post_id )
form = build_form ( ReportForm , request , reported_by = request . user , post = post_id )
2009-01-21 01:13:46 +02:00
if request . method == ' POST ' and form . is_valid ( ) :
2009-01-05 14:30:08 +02:00
form . save ( )
return HttpResponseRedirect ( post . get_absolute_url ( ) )
2009-01-21 01:13:46 +02:00
return { ' form ' : form }
2009-01-05 14:30:08 +02:00
elif ' submit ' in request . POST and ' mail_to ' in request . GET :
form = MailToForm ( request . POST )
if form . is_valid ( ) :
user = get_object_or_404 ( User , username = request . GET [ ' mail_to ' ] )
subject = form . cleaned_data [ ' subject ' ]
body = form . cleaned_data [ ' body ' ]
user . email_user ( subject , body , request . user . email )
return HttpResponseRedirect ( reverse ( ' index ' ) )
elif ' mail_to ' in request . GET :
user = get_object_or_404 ( User , username = request . GET [ ' mail_to ' ] )
form = MailToForm ( )
2009-01-21 01:13:46 +02:00
return { ' form ' : form ,
' user ' : user ,
} , ' forum/mail_to.html '
2009-01-05 14:30:08 +02:00
2009-05-24 22:51:42 +03:00
2009-01-05 14:30:08 +02:00
@render_to ( ' forum/forum.html ' )
2009-01-17 17:56:19 +02:00
@paged ( ' topics ' , forum_settings . FORUM_PAGE_SIZE )
2009-02-09 20:31:19 +02:00
def show_forum ( request , forum_id , full = True ) :
2009-01-05 14:30:08 +02:00
forum = Forum . objects . get ( pk = forum_id )
2009-04-07 17:46:22 +03:00
if not forum . category . has_access ( request . user ) :
return HttpResponseForbidden ( )
2009-04-21 19:20:31 +03:00
topics = forum . topics . order_by ( ' -sticky ' , ' -updated ' ) . select_related ( )
2009-01-05 14:30:08 +02:00
moderator = request . user . is_superuser or \
request . user in forum . moderators . all ( )
2009-02-09 20:31:19 +02:00
if full :
return { ' categories ' : Category . objects . all ( ) ,
' forum ' : forum ,
' topics ' : topics ,
' paged_qs ' : topics ,
2009-05-24 22:51:42 +03:00
' posts ' : forum . post_count ,
' topics ' : forum . topic_count ,
2009-02-09 20:31:19 +02:00
' moderator ' : moderator ,
}
else :
pages , paginator , paged_list_name = paginate ( topics , request , forum_settings . FORUM_PAGE_SIZE )
return { ' categories ' : Category . objects . all ( ) ,
' forum ' : forum ,
2009-05-24 22:51:42 +03:00
' posts ' : forum . post_count ,
2009-02-09 20:31:19 +02:00
' moderator ' : moderator ,
' pages ' : pages ,
' paginator ' : paginator ,
' topics ' : paged_list_name ,
} , ' forum/lofi/forum.html '
2009-01-05 14:30:08 +02:00
@render_to ( ' forum/topic.html ' )
2009-01-17 17:56:19 +02:00
@paged ( ' posts ' , forum_settings . TOPIC_PAGE_SIZE )
2009-02-09 20:31:19 +02:00
def show_topic ( request , topic_id , full = True ) :
2009-01-05 14:30:08 +02:00
topic = Topic . objects . select_related ( ) . get ( pk = topic_id )
2009-04-07 17:46:22 +03:00
if not topic . forum . category . has_access ( request . user ) :
return HttpResponseForbidden ( )
2009-01-05 14:30:08 +02:00
topic . views + = 1
topic . save ( )
2009-05-24 22:51:42 +03:00
last_post = topic . last_post
2009-01-05 14:30:08 +02:00
if request . user . is_authenticated ( ) :
topic . update_read ( request . user )
posts = topic . posts . all ( ) . select_related ( )
2009-05-24 22:51:42 +03:00
users = set ( post . user . id for post in posts )
profiles = Profile . objects . filter ( user__pk__in = users )
profiles = dict ( ( profile . user_id , profile ) for profile in profiles )
2009-01-05 14:30:08 +02:00
for post in posts :
post . user . forum_profile = profiles [ post . user . id ]
2009-05-24 22:51:42 +03:00
if forum_settings . REPUTATION_SUPPORT :
replies_list = Reputation . objects . filter ( to_user__pk__in = users ) . values ( ' to_user_id ' ) . annotate ( sign = Sum ( ' sign ' ) ) #values_list buggy?
if replies_list :
replies = { }
for r in replies_list :
replies [ r [ ' to_user_id ' ] ] = r [ ' sign ' ]
for post in posts :
post . user . forum_profile . reply_total = replies . get ( post . user . id , 0 )
print replies
2009-01-05 14:30:08 +02:00
initial = { }
if request . user . is_authenticated ( ) :
initial = { ' markup ' : request . user . forum_profile . markup }
form = AddPostForm ( topic = topic , initial = initial )
moderator = request . user . is_superuser or \
request . user in topic . forum . moderators . all ( )
if request . user . is_authenticated ( ) and request . user in topic . subscribers . all ( ) :
subscribed = True
else :
subscribed = False
2009-03-25 17:41:38 +02:00
highlight_word = request . GET . get ( ' hw ' , ' ' )
2009-02-09 20:31:19 +02:00
if full :
return { ' categories ' : Category . objects . all ( ) ,
' topic ' : topic ,
' last_post ' : last_post ,
' form ' : form ,
' moderator ' : moderator ,
' subscribed ' : subscribed ,
' paged_qs ' : posts ,
2009-03-25 17:41:38 +02:00
' highlight_word ' : highlight_word ,
2009-02-09 20:31:19 +02:00
}
else :
pages , paginator , paged_list_name = paginate ( posts , request , forum_settings . TOPIC_PAGE_SIZE )
2009-04-14 14:57:17 +03:00
load_related ( page . object_list , Attachment . objects . all ( ) , ' post ' )
2009-02-09 20:31:19 +02:00
return { ' categories ' : Category . objects . all ( ) ,
' topic ' : topic ,
#'last_post': last_post,
#'form': form,
#'moderator': moderator,
#'subscribed': subscribed,
#'paged_qs': posts,
' pages ' : pages ,
' paginator ' : paginator ,
' posts ' : paged_list_name ,
} , ' forum/lofi/topic.html '
2009-01-05 14:30:08 +02:00
@login_required
@render_to ( ' forum/add_post.html ' )
def add_post ( request , forum_id , topic_id ) :
forum = None
topic = None
posts = None
if forum_id :
forum = get_object_or_404 ( Forum , pk = forum_id )
2009-04-13 11:55:41 +03:00
if not forum . category . has_access ( request . user ) :
return HttpResponseForbidden ( )
2009-01-05 14:30:08 +02:00
elif topic_id :
topic = get_object_or_404 ( Topic , pk = topic_id )
posts = topic . posts . all ( ) . select_related ( )
2009-04-13 11:55:41 +03:00
if not topic . forum . category . has_access ( request . user ) :
return HttpResponseForbidden ( )
2009-01-05 14:30:08 +02:00
if topic and topic . closed :
return HttpResponseRedirect ( topic . get_absolute_url ( ) )
2009-04-14 14:57:17 +03:00
2009-01-05 14:30:08 +02:00
ip = request . META . get ( ' REMOTE_ADDR ' , ' ' )
form = build_form ( AddPostForm , request , topic = topic , forum = forum ,
user = request . user , ip = ip ,
initial = { ' markup ' : request . user . forum_profile . markup } )
if ' post_id ' in request . GET :
post_id = request . GET [ ' post_id ' ]
post = get_object_or_404 ( Post , pk = post_id )
form . fields [ ' body ' ] . initial = " [quote= %s ] %s [/quote] " % ( post . user , post . body )
if form . is_valid ( ) :
post = form . save ( ) ;
return HttpResponseRedirect ( post . get_absolute_url ( ) )
return { ' form ' : form ,
' posts ' : posts ,
' topic ' : topic ,
' forum ' : forum ,
}
2009-01-21 01:13:46 +02:00
@render_to ( ' forum/user.html ' )
2009-01-05 14:30:08 +02:00
def user ( request , username ) :
user = get_object_or_404 ( User , username = username )
if request . user . is_authenticated ( ) and user == request . user or request . user . is_superuser :
if ' section ' in request . GET :
2009-01-21 01:13:46 +02:00
section = request . GET [ ' section ' ]
if section == ' privacy ' :
2009-01-05 14:30:08 +02:00
form = build_form ( PrivacyProfileForm , request , instance = user . forum_profile )
2009-01-21 01:13:46 +02:00
if request . method == ' POST ' and form . is_valid ( ) :
2009-01-05 14:30:08 +02:00
form . save ( )
2009-01-21 01:13:46 +02:00
return { ' active_menu ' : ' privacy ' ,
' profile ' : user ,
' form ' : form ,
} , ' forum/profile/profile_privacy.html '
elif section == ' display ' :
2009-01-05 14:30:08 +02:00
form = build_form ( DisplayProfileForm , request , instance = user . forum_profile )
2009-01-21 01:13:46 +02:00
if request . method == ' POST ' and form . is_valid ( ) :
2009-01-05 14:30:08 +02:00
form . save ( )
2009-01-21 01:13:46 +02:00
return { ' active_menu ' : ' display ' ,
' profile ' : user ,
' form ' : form ,
} , ' forum/profile/profile_display.html '
elif section == ' personality ' :
2009-01-05 14:30:08 +02:00
form = build_form ( PersonalityProfileForm , request , instance = user . forum_profile )
2009-01-21 01:13:46 +02:00
if request . method == ' POST ' and form . is_valid ( ) :
2009-01-05 14:30:08 +02:00
form . save ( )
2009-01-21 01:13:46 +02:00
return { ' active_menu ' : ' personality ' ,
' profile ' : user ,
' form ' : form ,
} , ' forum/profile/profile_personality.html '
elif section == ' messaging ' :
2009-01-05 14:30:08 +02:00
form = build_form ( MessagingProfileForm , request , instance = user . forum_profile )
2009-01-21 01:13:46 +02:00
if request . method == ' POST ' and form . is_valid ( ) :
2009-01-05 14:30:08 +02:00
form . save ( )
2009-01-21 01:13:46 +02:00
return { ' active_menu ' : ' messaging ' ,
' profile ' : user ,
' form ' : form ,
} , ' forum/profile/profile_messaging.html '
elif section == ' personal ' :
2009-01-05 14:30:08 +02:00
form = build_form ( PersonalProfileForm , request , instance = user . forum_profile , user = user )
2009-01-21 01:13:46 +02:00
if request . method == ' POST ' and form . is_valid ( ) :
2009-01-05 14:30:08 +02:00
form . save ( )
2009-01-21 01:13:46 +02:00
return { ' active_menu ' : ' personal ' ,
' profile ' : user ,
' form ' : form ,
} , ' forum/profile/profile_personal.html '
elif section == ' essentials ' :
2009-01-05 14:30:08 +02:00
form = build_form ( EssentialsProfileForm , request , instance = user . forum_profile , user = user )
2009-01-21 01:13:46 +02:00
if request . method == ' POST ' and form . is_valid ( ) :
2009-03-03 18:30:41 +02:00
profile = form . save ( )
set_language ( request , profile . language )
2009-01-21 01:13:46 +02:00
return { ' active_menu ' : ' essentials ' ,
' profile ' : user ,
' form ' : form ,
} , ' forum/profile/profile_essentials.html '
2009-01-05 14:30:08 +02:00
elif ' action ' in request . GET :
2009-01-21 01:13:46 +02:00
action = request . GET [ ' action ' ]
if action == ' upload_avatar ' :
2009-01-05 14:30:08 +02:00
form = build_form ( UploadAvatarForm , request , instance = user . forum_profile )
2009-01-21 01:13:46 +02:00
if request . method == ' POST ' and form . is_valid ( ) :
2009-01-05 14:30:08 +02:00
form . save ( )
return HttpResponseRedirect ( reverse ( ' forum_profile ' , args = [ user . username ] ) )
2009-01-21 01:13:46 +02:00
return { ' form ' : form ,
2009-02-03 16:07:02 +02:00
' avatar_width ' : forum_settings . AVATAR_WIDTH ,
' avatar_height ' : forum_settings . AVATAR_HEIGHT ,
2009-01-21 01:13:46 +02:00
} , ' forum/upload_avatar.html '
elif action == ' delete_avatar ' :
2009-01-05 14:30:08 +02:00
profile = get_object_or_404 ( Profile , user = request . user )
profile . avatar = None
profile . save ( )
return HttpResponseRedirect ( reverse ( ' forum_profile ' , args = [ user . username ] ) )
else :
form = build_form ( EssentialsProfileForm , request , instance = user . forum_profile , user = user )
2009-01-21 01:13:46 +02:00
if request . method == ' POST ' and form . is_valid ( ) :
2009-03-03 18:30:41 +02:00
profile = form . save ( )
set_language ( request , profile . language )
2009-01-21 01:13:46 +02:00
return { ' active_menu ' : ' essentials ' ,
' profile ' : user ,
' form ' : form ,
} , ' forum/profile/profile_essentials.html '
2009-01-05 14:30:08 +02:00
else :
topic_count = Topic . objects . filter ( user = user ) . count ( )
2009-04-15 10:29:12 +03:00
if user . forum_profile . post_count < forum_settings . POST_USER_SEARCH and not request . user . is_authenticated ( ) :
return HttpResponseRedirect ( reverse ( ' auth_login ' ) + ' ?next= %s ' % request . path )
2009-01-21 01:13:46 +02:00
return { ' profile ' : user ,
' topic_count ' : topic_count ,
}
2009-01-05 14:30:08 +02:00
2009-01-21 01:13:46 +02:00
@render_to ( ' forum/reputation.html ' )
2009-01-05 14:30:08 +02:00
def reputation ( request , username ) :
user = get_object_or_404 ( User , username = username )
form = build_form ( ReputationForm , request , from_user = request . user , to_user = user )
if ' action ' in request . GET :
if ' topic_id ' in request . GET :
sign = 0
topic_id = request . GET [ ' topic_id ' ]
form . fields [ ' topic ' ] . initial = topic_id
if request . GET [ ' action ' ] == ' plus ' :
form . fields [ ' sign ' ] . initial = 1
elif request . GET [ ' action ' ] == ' minus ' :
form . fields [ ' sign ' ] . initial = - 1
2009-01-21 01:13:46 +02:00
return { ' form ' : form } , ' forum/reputation_form.html '
2009-01-05 14:30:08 +02:00
else :
raise Http404
2009-01-21 01:13:46 +02:00
elif request . method == ' POST ' :
2009-01-05 14:30:08 +02:00
if ' del_reputation ' in request . POST :
for reputation in request . POST :
reputation_match = re . match ( ' reputation_id \ [( \ d+) \ ] ' , reputation )
if reputation_match :
reputation_id = reputation_match . group ( 1 )
reputation = get_object_or_404 ( Reputation , pk = reputation_id )
reputation . delete ( )
return HttpResponseRedirect ( reverse ( ' index ' ) )
elif form . is_valid ( ) :
form . save ( )
topic_id = request . POST [ ' topic ' ]
topic = get_object_or_404 ( Topic , id = topic_id )
return HttpResponseRedirect ( topic . get_absolute_url ( ) )
else :
raise Http404
2009-01-21 01:13:46 +02:00
2009-01-05 14:30:08 +02:00
else :
reputations = Reputation . objects . filter ( to_user = user ) . order_by ( ' -time ' ) . select_related ( )
2009-01-21 01:13:46 +02:00
return { ' reputations ' : reputations ,
2009-01-05 14:30:08 +02:00
' profile ' : user . forum_profile ,
2009-01-21 01:13:46 +02:00
}
2009-01-05 14:30:08 +02:00
def show_post ( request , post_id ) :
post = get_object_or_404 ( Post , pk = post_id )
count = post . topic . posts . filter ( created__lt = post . created ) . count ( ) + 1
2009-01-17 17:56:19 +02:00
page = math . ceil ( count / float ( forum_settings . TOPIC_PAGE_SIZE ) )
2009-01-05 14:30:08 +02:00
url = ' %s ?page= %d #post- %d ' % ( reverse ( ' topic ' , args = [ post . topic . id ] ) , page , post . id )
return HttpResponseRedirect ( url )
2009-01-21 01:13:46 +02:00
2009-01-05 14:30:08 +02:00
@login_required
@render_to ( ' forum/edit_post.html ' )
def edit_post ( request , post_id ) :
2009-03-31 00:03:16 +03:00
from forum . templatetags . forum_extras import forum_editable_by
2009-01-05 14:30:08 +02:00
post = get_object_or_404 ( Post , pk = post_id )
topic = post . topic
if not forum_editable_by ( post , request . user ) :
return HttpResponseRedirect ( post . get_absolute_url ( ) )
form = build_form ( EditPostForm , request , topic = topic , instance = post )
if form . is_valid ( ) :
post = form . save ( )
return HttpResponseRedirect ( post . get_absolute_url ( ) )
return { ' form ' : form ,
' post ' : post ,
}
@login_required
@render_to ( ' forum/delete_posts.html ' )
2009-01-17 17:56:19 +02:00
@paged ( ' posts ' , forum_settings . TOPIC_PAGE_SIZE )
2009-01-05 14:30:08 +02:00
def delete_posts ( request , topic_id ) :
2009-03-31 00:03:16 +03:00
from forum . templatetags . forum_extras import forum_moderated_by
2009-01-05 14:30:08 +02:00
topic = Topic . objects . select_related ( ) . get ( pk = topic_id )
topic . views + = 1
topic . save ( )
2009-01-21 01:13:46 +02:00
2009-01-05 14:30:08 +02:00
if forum_moderated_by ( topic , request . user ) :
deleted = False
for post in request . POST :
if not deleted :
deleted = True
post_id = re . match ( ' post \ [( \ d+) \ ] ' , post ) . group ( 1 )
delete_post ( request , post_id )
if deleted :
return HttpResponseRedirect ( topic . get_absolute_url ( ) )
last_post = topic . posts . order_by ( ' -created ' ) [ 0 ]
if request . user . is_authenticated ( ) :
topic . update_read ( request . user )
posts = topic . posts . all ( ) . select_related ( )
profiles = Profile . objects . filter ( user__pk__in = set ( x . user . id for x in posts ) )
profiles = dict ( ( x . user_id , x ) for x in profiles )
for post in posts :
post . user . forum_profile = profiles [ post . user . id ]
initial = { }
if request . user . is_authenticated ( ) :
initial = { ' markup ' : request . user . forum_profile . markup }
form = AddPostForm ( topic = topic , initial = initial )
moderator = request . user . is_superuser or \
request . user in topic . forum . moderators . all ( )
if request . user . is_authenticated ( ) and request . user in topic . subscribers . all ( ) :
subscribed = True
else :
subscribed = False
return {
' topic ' : topic ,
' last_post ' : last_post ,
' form ' : form ,
' moderator ' : moderator ,
' subscribed ' : subscribed ,
' paged_qs ' : posts ,
}
@login_required
@render_to ( ' forum/move_topic.html ' )
def move_topic ( request , topic_id ) :
2009-03-31 00:03:16 +03:00
from forum . templatetags . forum_extras import forum_moderated_by
2009-01-05 14:30:08 +02:00
topic = get_object_or_404 ( Topic , pk = topic_id )
if forum_moderated_by ( topic , request . user ) :
if ' to_forum ' in request . GET :
topic . forum_id = request . GET [ ' to_forum ' ]
topic . save ( )
return HttpResponseRedirect ( topic . forum . get_absolute_url ( ) )
return { ' categories ' : Category . objects . all ( ) ,
}
@login_required
def stick_topic ( request , topic_id ) :
2009-03-31 00:03:16 +03:00
from forum . templatetags . forum_extras import forum_moderated_by
2009-01-05 14:30:08 +02:00
topic = get_object_or_404 ( Topic , pk = topic_id )
if forum_moderated_by ( topic , request . user ) :
if not topic . sticky :
topic . sticky = True
topic . save ( )
return HttpResponseRedirect ( topic . get_absolute_url ( ) )
@login_required
def unstick_topic ( request , topic_id ) :
2009-03-31 00:03:16 +03:00
from forum . templatetags . forum_extras import forum_moderated_by
2009-01-05 14:30:08 +02:00
topic = get_object_or_404 ( Topic , pk = topic_id )
if forum_moderated_by ( topic , request . user ) :
if topic . sticky :
topic . sticky = False
topic . save ( )
return HttpResponseRedirect ( topic . get_absolute_url ( ) )
@login_required
@render_to ( ' forum/delete_post.html ' )
def delete_post ( request , post_id ) :
post = get_object_or_404 ( Post , pk = post_id )
last_post = post . topic . posts . order_by ( ' -created ' ) [ 0 ]
topic = post . topic
2009-02-03 16:42:34 +02:00
forum = post . topic . forum
2009-01-05 14:30:08 +02:00
allowed = False
if request . user . is_superuser or \
request . user in post . topic . forum . moderators . all ( ) or \
( post . user == request . user and post == last_post ) :
allowed = True
if not allowed :
return HttpResponseRedirect ( post . get_absolute_url ( ) )
2009-02-03 16:42:34 +02:00
2009-01-05 14:30:08 +02:00
post . delete ( )
2009-02-03 16:33:41 +02:00
profile = get_object_or_404 ( Profile , user = post . user )
profile . post_count = Post . objects . filter ( user = post . user ) . count ( )
profile . save ( )
2009-02-03 16:42:34 +02:00
2009-01-05 14:30:08 +02:00
try :
Topic . objects . get ( pk = topic . id )
except Topic . DoesNotExist :
2009-02-03 16:42:34 +02:00
#removed latest post in topic
2009-01-05 14:30:08 +02:00
return HttpResponseRedirect ( forum . get_absolute_url ( ) )
else :
return HttpResponseRedirect ( topic . get_absolute_url ( ) )
@login_required
def close_topic ( request , topic_id ) :
2009-03-31 00:03:16 +03:00
from forum . templatetags . forum_extras import forum_moderated_by
2009-01-05 14:30:08 +02:00
topic = get_object_or_404 ( Topic , pk = topic_id )
if forum_moderated_by ( topic , request . user ) :
if not topic . closed :
topic . closed = True
topic . save ( )
return HttpResponseRedirect ( topic . get_absolute_url ( ) )
@login_required
def open_topic ( request , topic_id ) :
2009-03-31 00:03:16 +03:00
from forum . templatetags . forum_extras import forum_moderated_by
2009-01-05 14:30:08 +02:00
topic = get_object_or_404 ( Topic , pk = topic_id )
if forum_moderated_by ( topic , request . user ) :
if topic . closed :
topic . closed = False
topic . save ( )
return HttpResponseRedirect ( topic . get_absolute_url ( ) )
@render_to ( ' forum/users.html ' )
2009-01-17 17:56:19 +02:00
@paged ( ' users ' , forum_settings . USERS_PAGE_SIZE )
2009-01-05 14:30:08 +02:00
def users ( request ) :
2009-04-15 10:29:12 +03:00
users = User . objects . filter ( forum_profile__post_count__gte = forum_settings . POST_USER_SEARCH ) . order_by ( ' username ' )
2009-01-05 14:30:08 +02:00
form = UserSearchForm ( request . GET )
users = form . filter ( users )
return { ' paged_qs ' : users ,
' form ' : form ,
}
@login_required
@render_to ( ' forum/pm/create_pm.html ' )
def create_pm ( request ) :
recipient = request . GET . get ( ' recipient ' , ' ' )
form = build_form ( CreatePMForm , request , user = request . user ,
initial = { ' markup ' : request . user . forum_profile . markup ,
' recipient ' : recipient } )
if form . is_valid ( ) :
post = form . save ( ) ;
return HttpResponseRedirect ( reverse ( ' forum_pm_outbox ' ) )
return { ' active_menu ' : ' create ' ,
' form ' : form ,
}
@login_required
@render_to ( ' forum/pm/outbox.html ' )
def pm_outbox ( request ) :
messages = PrivateMessage . objects . filter ( src_user = request . user )
return { ' active_menu ' : ' outbox ' ,
' messages ' : messages ,
}
@login_required
@render_to ( ' forum/pm/inbox.html ' )
def pm_inbox ( request ) :
messages = PrivateMessage . objects . filter ( dst_user = request . user )
return { ' active_menu ' : ' inbox ' ,
' messages ' : messages ,
}
@login_required
@render_to ( ' forum/pm/message.html ' )
def show_pm ( request , pm_id ) :
msg = get_object_or_404 ( PrivateMessage , pk = pm_id )
if not request . user in [ msg . dst_user , msg . src_user ] :
return HttpRedirectException ( ' / ' )
if request . user == msg . dst_user :
inbox = True
post_user = msg . src_user
else :
inbox = False
post_user = msg . dst_user
2009-01-21 17:43:08 +02:00
if not msg . read :
msg . read = True
msg . save ( )
2009-01-05 14:30:08 +02:00
return { ' msg ' : msg ,
' inbox ' : inbox ,
' post_user ' : post_user ,
}
@login_required
def delete_subscription ( request , topic_id ) :
topic = get_object_or_404 ( Topic , pk = topic_id )
topic . subscribers . remove ( request . user )
if ' from_topic ' in request . GET :
return HttpResponseRedirect ( reverse ( ' topic ' , args = [ topic . id ] ) )
else :
return HttpResponseRedirect ( reverse ( ' edit_profile ' ) )
@login_required
def add_subscription ( request , topic_id ) :
topic = get_object_or_404 ( Topic , pk = topic_id )
topic . subscribers . add ( request . user )
return HttpResponseRedirect ( reverse ( ' topic ' , args = [ topic . id ] ) )
2009-04-14 14:57:17 +03:00
@login_required
def show_attachment ( request , hash ) :
attachment = get_object_or_404 ( Attachment , hash = hash )
file_obj = file ( attachment . get_absolute_path ( ) )
return HttpResponse ( file_obj , content_type = attachment . content_type )
2009-02-05 13:45:44 +02:00
#TODO: check markup
@render_to ( ' forum/post_preview.html ' )
2009-01-05 14:30:08 +02:00
def post_preview ( request ) :
''' Preview for markitup '''
2009-02-05 13:45:44 +02:00
data = mypostmarkup . markup ( request . POST . get ( ' data ' , ' ' ) , auto_urls = False )
data = urlize ( data )
data = smiles ( data )
return { ' data ' : data }