2009-01-05 14:30:08 +02:00
import math
2010-11-28 17:57:54 +02:00
from datetime import datetime , timedelta
2009-01-05 14:30:08 +02:00
2012-02-18 00:10:44 +02:00
from django . shortcuts import get_object_or_404 , render
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
2009-09-08 17:35:04 +03:00
from django . contrib . auth . decorators import login_required
2010-02-23 17:04:44 +02:00
from django . contrib . sites . models import Site
2009-01-05 14:30:08 +02:00
from django . core . urlresolvers import reverse
from django . core . cache import cache
2009-08-20 12:46:08 +03:00
from django . db . models import Q , F , Sum
2009-11-20 16:25:14 +02:00
from django . utils . encoding import smart_str
2010-06-09 21:40:09 +03:00
from django . db import transaction
2011-03-24 10:03:01 +02:00
from django . views . decorators . csrf import csrf_exempt
2009-09-04 13:03:15 +03:00
2012-02-18 01:17:32 +02:00
from djangobb_forum . util import build_form , paginate , set_language
2009-12-23 17:06:48 +02:00
from djangobb_forum . models import Category , Forum , Topic , Post , Profile , Reputation , \
2011-08-11 17:15:08 +03:00
Attachment , PostTracking
2009-12-23 17:06:48 +02:00
from djangobb_forum . forms import AddPostForm , EditPostForm , UserSearchForm , \
2009-04-03 23:38:12 +03:00
PostSearchForm , ReputationForm , MailToForm , EssentialsProfileForm , \
PersonalProfileForm , MessagingProfileForm , PersonalityProfileForm , \
2010-10-30 09:03:50 +03:00
DisplayProfileForm , PrivacyProfileForm , ReportForm , UploadAvatarForm
2009-12-23 17:06:48 +02:00
from djangobb_forum . templatetags import forum_extras
from djangobb_forum import settings as forum_settings
2012-06-05 11:54:31 +03:00
from djangobb_forum . util import smiles , convert_text_to_html , TopicFromPostResult
2009-12-24 17:24:39 +02:00
from djangobb_forum . templatetags . forum_extras import forum_moderated_by
2010-11-01 13:42:18 -03:00
from haystack . query import SearchQuerySet , SQ
2009-01-05 14:30:08 +02:00
2009-02-09 20:31:19 +02:00
def index ( request , full = True ) :
2012-04-26 14:27:49 +03:00
users_cached = cache . get ( ' djangobb_users_online ' , { } )
2009-04-03 23:47:38 +03:00
users_online = users_cached and User . objects . filter ( id__in = users_cached . keys ( ) ) or [ ]
2012-04-26 14:27:49 +03:00
guests_cached = cache . get ( ' djangobb_guests_online ' , { } )
2009-04-03 23:38:12 +03:00
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-10-07 13:08:09 +03:00
_forums = Forum . objects . filter (
Q ( category__groups__in = user_groups ) | \
Q ( category__groups__isnull = True ) ) . select_related ( ' last_post__topic ' ,
' last_post__user ' ,
' category ' )
for forum in _forums :
2009-01-05 14:30:08 +02:00
cat = cats . setdefault ( forum . category . id ,
2010-05-23 23:11:30 +03:00
{ ' id ' : forum . category . id , ' cat ' : forum . category , ' forums ' : [ ] } )
2009-01-05 14:30:08 +02:00
cat [ ' forums ' ] . append ( forum )
forums [ forum . id ] = forum
cmpdef = lambda a , b : cmp ( a [ ' cat ' ] . position , b [ ' cat ' ] . position )
cats = sorted ( cats . values ( ) , cmpdef )
2010-11-01 13:42:18 -03:00
2009-10-07 13:08:09 +03:00
to_return = { ' cats ' : cats ,
2009-02-09 20:31:19 +02:00
' 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 ,
2009-10-19 17:23:36 +03:00
' last_user ' : User . objects . latest ( ' date_joined ' ) ,
2009-10-07 13:08:09 +03:00
}
if full :
2012-02-18 00:10:44 +02:00
return render ( request , ' djangobb_forum/index.html ' , to_return )
2009-02-09 20:31:19 +02:00
else :
2012-02-18 00:10:44 +02:00
return render ( request , ' djangobb_forum/lofi/index.html ' , to_return )
2009-01-05 14:30:08 +02:00
2009-12-24 17:24:39 +02:00
2010-06-09 21:40:09 +03:00
@transaction.commit_on_success
2009-01-05 14:30:08 +02:00
def moderate ( request , forum_id ) :
2009-12-24 17:24:39 +02:00
forum = get_object_or_404 ( Forum , pk = forum_id )
2009-12-19 16:40:01 +02:00
topics = forum . topics . order_by ( ' -sticky ' , ' -updated ' ) . select_related ( )
2009-01-05 14:30:08 +02:00
if request . user . is_superuser or request . user in forum . moderators . all ( ) :
2010-02-23 11:19:41 +02:00
topic_ids = request . POST . getlist ( ' topic_id ' )
2009-01-05 14:30:08 +02:00
if ' move_topics ' in request . POST :
2012-02-18 00:10:44 +02:00
return render ( request , ' djangobb_forum/move_topic.html ' , {
2009-12-19 16:40:01 +02:00
' categories ' : Category . objects . all ( ) ,
2009-12-24 17:24:39 +02:00
' topic_ids ' : topic_ids ,
' exclude_forum ' : forum ,
2012-02-18 00:10:44 +02:00
} )
2009-01-05 14:30:08 +02:00
elif ' delete_topics ' in request . POST :
2010-02-23 11:19:41 +02:00
for topic_id in topic_ids :
2009-11-29 17:04:25 +02:00
topic = get_object_or_404 ( Topic , pk = topic_id )
topic . delete ( )
2009-11-29 18:56:11 +02:00
return HttpResponseRedirect ( reverse ( ' djangobb:index ' ) )
2009-01-05 14:30:08 +02:00
elif ' open_topics ' in request . POST :
2010-02-23 11:19:41 +02:00
for topic_id in topic_ids :
2011-08-19 22:30:03 +03:00
open_close_topic ( request , topic_id , ' o ' )
2009-11-29 18:56:11 +02:00
return HttpResponseRedirect ( reverse ( ' djangobb:index ' ) )
2009-01-05 14:30:08 +02:00
elif ' close_topics ' in request . POST :
2010-02-23 11:19:41 +02:00
for topic_id in topic_ids :
2011-08-19 22:30:03 +03:00
open_close_topic ( request , topic_id , ' c ' )
2009-11-29 18:56:11 +02:00
return HttpResponseRedirect ( reverse ( ' djangobb:index ' ) )
2009-01-21 01:13:46 +02:00
2012-02-18 00:10:44 +02:00
return render ( request , ' djangobb_forum/moderate.html ' , { ' forum ' : forum ,
2009-01-05 14:30:08 +02:00
' topics ' : topics ,
2009-12-19 16:40:01 +02:00
#'sticky_topics': forum.topics.filter(sticky=True),
2010-11-01 13:42:18 -03:00
' posts ' : forum . posts . count ( ) ,
2012-02-18 00:10:44 +02:00
} )
2009-01-05 14:30:08 +02:00
else :
raise Http404
2009-12-24 17:24:39 +02:00
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 ' ]
2010-11-06 18:35:26 +02:00
#FIXME: show_user for anonymous raise exception,
#django bug http://code.djangoproject.com/changeset/14087 :|
groups = request . user . groups . all ( ) or [ ] #removed after django > 1.2.3 release
2010-11-01 15:54:06 -03:00
topics = Topic . objects . filter (
2010-11-06 18:35:26 +02:00
Q ( forum__category__groups__in = groups ) | \
2010-11-01 15:54:06 -03:00
Q ( forum__category__groups__isnull = True ) )
2009-01-21 01:13:46 +02:00
if action == ' show_24h ' :
2009-10-22 15:19:26 +03:00
date = datetime . today ( ) - timedelta ( 1 )
2010-11-09 16:23:04 +02:00
topics = topics . filter ( created__gte = date )
2009-01-21 01:13:46 +02:00
elif action == ' show_new ' :
2012-04-02 13:59:35 +02:00
try :
last_read = PostTracking . objects . get ( user = request . user ) . last_read
except PostTracking . DoesNotExist :
last_read = None
2010-11-09 16:23:04 +02:00
if last_read :
topics = topics . filter ( last_post__updated__gte = last_read ) . all ( )
else :
#searching more than forum_settings.SEARCH_PAGE_SIZE in this way - not good idea :]
topics = [ topic for topic in topics [ : forum_settings . SEARCH_PAGE_SIZE ] if forum_extras . has_unreads ( topic , request . user ) ]
2009-01-21 01:13:46 +02:00
elif action == ' show_unanswered ' :
2010-11-01 15:54:06 -03:00
topics = topics . filter ( post_count = 1 )
2009-01-21 01:13:46 +02:00
elif action == ' show_subscriptions ' :
2010-12-27 21:36:40 +02:00
topics = topics . filter ( subscribers__id = request . user . id )
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 )
2010-11-01 15:54:06 -03:00
topics = [ post . topic for post in posts if post . topic in topics ]
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
2010-11-01 13:42:18 -03:00
if not ( keywords or author ) :
return HttpResponseRedirect ( reverse ( ' djangobb:search ' ) )
query = SearchQuerySet ( ) . models ( Post )
if author :
2010-12-28 16:47:13 +02:00
query = query . filter ( author__username = author )
2010-11-01 13:42:18 -03:00
if forum != u ' 0 ' :
2010-12-10 12:40:01 +02:00
query = query . filter ( forum__id = forum )
2010-11-01 13:42:18 -03:00
if keywords :
2009-03-26 21:57:57 +02:00
if search_in == ' all ' :
2010-11-01 13:42:18 -03:00
query = query . filter ( SQ ( topic = keywords ) | SQ ( text = keywords ) )
2011-10-23 12:45:14 +04:00
elif search_in == ' message ' :
2010-11-01 13:42:18 -03:00
query = query . filter ( text = keywords )
2009-03-26 21:57:57 +02:00
elif search_in == ' topic ' :
2010-11-01 13:42:18 -03:00
query = query . filter ( topic = keywords )
2009-03-26 21:57:57 +02:00
2012-06-05 11:54:31 +03:00
# add exlusions for categories user does not have access too
for category in Category . objects . all ( ) :
if not category . has_access ( request . user ) :
query = query . exclude ( category = category )
2009-08-20 12:46:08 +03:00
order = { ' 0 ' : ' created ' ,
2011-10-20 14:46:21 +03:00
' 1 ' : ' author ' ,
2009-08-20 12:46:08 +03:00
' 2 ' : ' topic ' ,
' 3 ' : ' forum ' } . get ( sort_by , ' created ' )
2009-03-26 21:57:57 +02:00
if sort_dir == ' DESC ' :
order = ' - ' + order
2010-11-01 13:42:18 -03:00
posts = query . order_by ( order )
2009-12-10 18:54:39 +02:00
2009-03-26 21:57:57 +02:00
if ' topics ' in request . GET [ ' show_as ' ] :
2012-06-05 11:54:31 +03:00
return render ( request , ' djangobb_forum/search_topics.html ' , {
' results ' : TopicFromPostResult ( posts )
} )
2009-01-05 14:30:08 +02:00
elif ' posts ' in request . GET [ ' show_as ' ] :
2012-05-15 14:26:24 +03:00
return render ( request , ' djangobb_forum/search_posts.html ' , { ' results ' : posts } )
2012-06-05 11:54:31 +03:00
2012-02-18 01:17:32 +02:00
return render ( request , ' djangobb_forum/search_topics.html ' , { ' results ' : topics } )
2009-01-21 01:13:46 +02:00
else :
2009-01-05 14:30:08 +02:00
form = PostSearchForm ( )
2012-02-18 00:10:44 +02:00
return render ( request , ' djangobb_forum/search_form.html ' , { ' categories ' : Category . objects . all ( ) ,
2009-01-21 01:13:46 +02:00
' form ' : form ,
2012-02-18 00:10:44 +02:00
} )
2009-01-20 23:55:09 +02:00
2009-12-24 17:24:39 +02:00
2009-01-05 14:30:08 +02:00
@login_required
def misc ( request ) :
if ' action ' in request . GET :
2009-01-21 01:13:46 +02:00
action = request . GET [ ' action ' ]
if action == ' markread ' :
2009-10-22 15:19:26 +03:00
user = request . user
2010-11-29 00:57:19 +02:00
PostTracking . objects . filter ( user__id = user . id ) . update ( last_read = datetime . now ( ) , topics = None )
2009-11-29 18:56:11 +02:00
return HttpResponseRedirect ( reverse ( ' djangobb:index ' ) )
2009-10-22 15:19:26 +03:00
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 ( ) )
2012-02-21 08:26:30 +02:00
return render ( request , ' djangobb_forum/report.html ' , { ' form ' : form } )
2009-12-10 18:54:39 +02:00
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 ' ]
2010-02-23 17:04:44 +02:00
body = form . cleaned_data [ ' body ' ] + ' \n %s %s [ %s ] ' % ( Site . objects . get_current ( ) . domain ,
request . user . username ,
request . user . email )
2009-01-05 14:30:08 +02:00
user . email_user ( subject , body , request . user . email )
2009-11-29 18:56:11 +02:00
return HttpResponseRedirect ( reverse ( ' djangobb:index ' ) )
2009-12-10 18:54:39 +02:00
2009-01-05 14:30:08 +02:00
elif ' mail_to ' in request . GET :
2011-04-08 10:16:57 +03:00
mailto = get_object_or_404 ( User , username = request . GET [ ' mail_to ' ] )
2009-01-05 14:30:08 +02:00
form = MailToForm ( )
2012-04-23 11:43:01 +03:00
return render ( request , ' djangobb_forum/mail_to.html ' , { ' form ' : form ,
' mailto ' : mailto }
)
2009-01-05 14:30:08 +02:00
2009-05-24 22:51:42 +03:00
2009-02-09 20:31:19 +02:00
def show_forum ( request , forum_id , full = True ) :
2009-06-30 10:02:33 +03:00
forum = get_object_or_404 ( Forum , 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-10-07 13:08:09 +03:00
to_return = { ' categories ' : Category . objects . all ( ) ,
2009-02-09 20:31:19 +02:00
' forum ' : forum ,
2009-05-24 22:51:42 +03:00
' posts ' : forum . post_count ,
2012-02-18 01:17:32 +02:00
' topics ' : topics ,
2009-02-09 20:31:19 +02:00
' moderator ' : moderator ,
}
2009-10-07 13:08:09 +03:00
if full :
2012-02-18 00:10:44 +02:00
return render ( request , ' djangobb_forum/forum.html ' , to_return )
2009-02-09 20:31:19 +02:00
else :
2012-02-18 00:10:44 +02:00
return render ( request , ' djangobb_forum/lofi/forum.html ' , to_return )
2009-01-05 14:30:08 +02:00
2010-06-09 21:40:09 +03:00
@transaction.commit_on_success
2009-02-09 20:31:19 +02:00
def show_topic ( request , topic_id , full = True ) :
2009-06-30 10:02:33 +03:00
topic = get_object_or_404 ( Topic . objects . select_related ( ) , pk = topic_id )
2009-04-07 17:46:22 +03:00
if not topic . forum . category . has_access ( request . user ) :
return HttpResponseForbidden ( )
2009-12-30 18:39:18 +02:00
Topic . objects . filter ( pk = topic . id ) . update ( views = F ( ' views ' ) + 1 )
2009-01-05 14:30:08 +02:00
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 )
2012-02-21 08:26:30 +02:00
posts = topic . posts . all ( ) . select_related ( )
2009-05-24 22:51:42 +03:00
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
2009-09-04 17:58:27 +03:00
highlight_word = request . GET . get ( ' hl ' , ' ' )
2009-02-09 20:31:19 +02:00
if full :
2012-02-18 00:10:44 +02:00
return render ( request , ' djangobb_forum/topic.html ' , { ' categories ' : Category . objects . all ( ) ,
2009-02-09 20:31:19 +02:00
' topic ' : topic ,
' last_post ' : last_post ,
' form ' : form ,
' moderator ' : moderator ,
' subscribed ' : subscribed ,
2011-05-02 17:27:55 +03:00
' posts ' : posts ,
2009-03-25 17:41:38 +02:00
' highlight_word ' : highlight_word ,
2012-02-18 00:10:44 +02:00
} )
2009-02-09 20:31:19 +02:00
else :
2012-02-18 00:10:44 +02:00
return render ( request , ' djangobb_forum/lofi/topic.html ' , { ' categories ' : Category . objects . all ( ) ,
2009-02-09 20:31:19 +02:00
' topic ' : topic ,
2011-05-02 17:38:57 +03:00
' posts ' : posts ,
2012-02-18 00:10:44 +02:00
} )
2009-01-05 14:30:08 +02:00
@login_required
2010-06-09 21:40:09 +03:00
@transaction.commit_on_success
2009-01-05 14:30:08 +02:00
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 ( ) )
2010-11-01 13:42:18 -03:00
2009-11-16 14:50:39 +02:00
ip = request . META . get ( ' REMOTE_ADDR ' , None )
2009-01-05 14:30:08 +02:00
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 )
2012-04-26 17:04:08 +03:00
form . fields [ ' body ' ] . initial = u " [quote= %s ] %s [/quote] " % ( post . user , post . body )
2009-01-05 14:30:08 +02:00
if form . is_valid ( ) :
post = form . save ( ) ;
return HttpResponseRedirect ( post . get_absolute_url ( ) )
2012-02-18 00:10:44 +02:00
return render ( request , ' djangobb_forum/add_post.html ' , { ' form ' : form ,
2009-01-05 14:30:08 +02:00
' posts ' : posts ,
' topic ' : topic ,
' forum ' : forum ,
2012-02-18 00:10:44 +02:00
} )
2009-01-05 14:30:08 +02:00
2009-12-24 17:24:39 +02:00
2010-06-09 21:40:09 +03:00
@transaction.commit_on_success
2012-03-05 11:07:29 +02:00
def upload_avatar ( request , username , template = None , form_class = None ) :
2009-01-05 14:30:08 +02:00
user = get_object_or_404 ( User , username = username )
if request . user . is_authenticated ( ) and user == request . user or request . user . is_superuser :
2012-03-05 11:07:29 +02:00
form = build_form ( form_class , request , instance = user . forum_profile )
if request . method == ' POST ' and form . is_valid ( ) :
form . save ( )
return HttpResponseRedirect ( reverse ( ' djangobb:forum_profile ' , args = [ user . username ] ) )
return render ( request , template , { ' form ' : form ,
' avatar_width ' : forum_settings . AVATAR_WIDTH ,
' avatar_height ' : forum_settings . AVATAR_HEIGHT ,
} )
else :
topic_count = Topic . objects . filter ( user__id = user . id ) . count ( )
if user . forum_profile . post_count < forum_settings . POST_USER_SEARCH and not request . user . is_authenticated ( ) :
return HttpResponseRedirect ( reverse ( ' user_signin ' ) + ' ?next= %s ' % request . path )
return render ( request , template , { ' profile ' : user ,
' topic_count ' : topic_count ,
} )
@transaction.commit_on_success
def user ( request , username , section = ' essentials ' , action = None , template = ' djangobb_forum/profile/profile_essentials.html ' , form_class = EssentialsProfileForm ) :
user = get_object_or_404 ( User , username = username )
if request . user . is_authenticated ( ) and user == request . user or request . user . is_superuser :
profile_url = reverse ( ' djangobb:forum_profile_ %s ' % section , args = [ user . username ] )
2012-03-05 12:07:15 +02:00
form = build_form ( form_class , request , instance = user . forum_profile , extra_args = { ' request ' : request } )
2012-03-05 11:07:29 +02:00
if request . method == ' POST ' and form . is_valid ( ) :
form . save ( )
return HttpResponseRedirect ( profile_url )
return render ( request , template , { ' active_menu ' : section ,
' profile ' : user ,
' form ' : form ,
} )
2009-01-05 14:30:08 +02:00
else :
2012-04-26 10:35:09 +03:00
template = ' djangobb_forum/user.html '
2010-11-29 00:57:19 +02:00
topic_count = Topic . objects . filter ( user__id = user . id ) . 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 ( ) :
2010-11-01 13:42:18 -03:00
return HttpResponseRedirect ( reverse ( ' user_signin ' ) + ' ?next= %s ' % request . path )
2012-03-05 01:51:00 +02:00
return render ( request , template , { ' profile ' : user ,
2009-01-21 01:13:46 +02:00
' topic_count ' : topic_count ,
2012-02-18 00:10:44 +02:00
} )
2009-06-17 12:33:42 +03:00
2009-12-24 17:24:39 +02:00
2009-06-17 12:33:42 +03:00
@login_required
2010-06-09 21:40:09 +03:00
@transaction.commit_on_success
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 :
2010-02-23 14:19:20 +02:00
if request . user == user :
return HttpResponseForbidden ( u ' You can not change the reputation of yourself ' )
if ' post_id ' in request . GET :
post_id = request . GET [ ' post_id ' ]
form . fields [ ' post ' ] . initial = post_id
2009-01-05 14:30:08 +02:00
if request . GET [ ' action ' ] == ' plus ' :
form . fields [ ' sign ' ] . initial = 1
elif request . GET [ ' action ' ] == ' minus ' :
form . fields [ ' sign ' ] . initial = - 1
2012-02-18 00:10:44 +02:00
return render ( request , ' djangobb_forum/reputation_form.html ' , { ' form ' : form } )
2009-01-05 14:30:08 +02:00
else :
raise Http404
2009-01-21 01:13:46 +02:00
elif request . method == ' POST ' :
2010-02-23 14:19:20 +02:00
if ' del_reputation ' in request . POST and request . user . is_superuser :
2009-11-29 17:04:25 +02:00
reputation_list = request . POST . getlist ( ' reputation_id ' )
for reputation_id in reputation_list :
2009-01-05 14:30:08 +02:00
reputation = get_object_or_404 ( Reputation , pk = reputation_id )
reputation . delete ( )
2009-11-29 18:56:11 +02:00
return HttpResponseRedirect ( reverse ( ' djangobb:index ' ) )
2009-01-05 14:30:08 +02:00
elif form . is_valid ( ) :
form . save ( )
2010-02-23 14:19:20 +02:00
post_id = request . POST [ ' post ' ]
post = get_object_or_404 ( Post , id = post_id )
return HttpResponseRedirect ( post . get_absolute_url ( ) )
2009-01-05 14:30:08 +02:00
else :
2012-02-18 00:10:44 +02:00
return render ( request , ' djangobb_forum/reputation_form.html ' , { ' form ' : form } )
2009-01-05 14:30:08 +02:00
else :
2010-11-29 00:57:19 +02:00
reputations = Reputation . objects . filter ( to_user__id = user . id ) . order_by ( ' -time ' ) . select_related ( )
2012-02-18 00:10:44 +02:00
return render ( request , ' djangobb_forum/reputation.html ' , { ' reputations ' : reputations ,
2009-01-05 14:30:08 +02:00
' profile ' : user . forum_profile ,
2012-02-18 00:10:44 +02:00
} )
2009-01-05 14:30:08 +02:00
2009-12-24 17:24:39 +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-11-29 18:56:11 +02:00
url = ' %s ?page= %d #post- %d ' % ( reverse ( ' djangobb:topic ' , args = [ post . topic . id ] ) , page , post . id )
2009-01-05 14:30:08 +02:00
return HttpResponseRedirect ( url )
2009-01-21 01:13:46 +02:00
2009-12-24 17:24:39 +02:00
2009-01-05 14:30:08 +02:00
@login_required
2010-06-09 21:40:09 +03:00
@transaction.commit_on_success
2009-01-05 14:30:08 +02:00
def edit_post ( request , post_id ) :
2009-12-23 17:06:48 +02:00
from djangobb_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 ( ) :
2010-02-17 13:15:27 +02:00
post = form . save ( commit = False )
post . updated_by = request . user
post . save ( )
2009-01-05 14:30:08 +02:00
return HttpResponseRedirect ( post . get_absolute_url ( ) )
2012-02-18 00:10:44 +02:00
return render ( request , ' djangobb_forum/edit_post.html ' , { ' form ' : form ,
2009-01-05 14:30:08 +02:00
' post ' : post ,
2012-02-18 00:10:44 +02:00
} )
2009-01-05 14:30:08 +02:00
2009-12-24 17:24:39 +02:00
2009-01-05 14:30:08 +02:00
@login_required
2010-06-09 21:40:09 +03:00
@transaction.commit_on_success
2009-01-05 14:30:08 +02:00
def delete_posts ( request , topic_id ) :
topic = Topic . objects . select_related ( ) . get ( pk = topic_id )
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
2009-11-29 17:04:25 +02:00
post_list = request . POST . getlist ( ' post ' )
for post_id in post_list :
2009-01-05 14:30:08 +02:00
if not deleted :
deleted = True
delete_post ( request , post_id )
if deleted :
return HttpResponseRedirect ( topic . get_absolute_url ( ) )
2009-10-19 17:23:36 +03:00
last_post = topic . posts . latest ( )
2009-01-05 14:30:08 +02:00
if request . user . is_authenticated ( ) :
topic . update_read ( request . user )
posts = topic . posts . all ( ) . select_related ( )
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
2012-02-18 00:10:44 +02:00
return render ( request , ' djangobb_forum/delete_posts.html ' , {
2009-01-05 14:30:08 +02:00
' topic ' : topic ,
' last_post ' : last_post ,
' form ' : form ,
' moderator ' : moderator ,
' subscribed ' : subscribed ,
2012-02-18 01:17:32 +02:00
' posts ' : posts ,
2012-02-18 00:10:44 +02:00
} )
2009-01-05 14:30:08 +02:00
2009-12-24 17:24:39 +02:00
2009-01-05 14:30:08 +02:00
@login_required
2010-06-09 21:40:09 +03:00
@transaction.commit_on_success
2009-12-19 16:40:01 +02:00
def move_topic ( request ) :
2009-12-24 17:43:16 +02:00
if ' topic_id ' in request . GET :
#if move only 1 topic
topic_ids = [ request . GET [ ' topic_id ' ] ]
else :
topic_ids = request . POST . getlist ( ' topic_id ' )
2009-12-19 16:40:01 +02:00
first_topic = topic_ids [ 0 ]
topic = get_object_or_404 ( Topic , pk = first_topic )
2009-12-24 17:24:39 +02:00
from_forum = topic . forum
if ' to_forum ' in request . POST :
to_forum_id = int ( request . POST [ ' to_forum ' ] )
to_forum = get_object_or_404 ( Forum , pk = to_forum_id )
2009-12-19 16:40:01 +02:00
for topic_id in topic_ids :
topic = get_object_or_404 ( Topic , pk = topic_id )
2009-12-24 17:24:39 +02:00
if topic . forum != to_forum :
2009-12-19 16:40:01 +02:00
if forum_moderated_by ( topic , request . user ) :
2009-12-24 17:24:39 +02:00
topic . forum = to_forum
2009-12-19 16:40:01 +02:00
topic . save ( )
2009-12-24 17:24:39 +02:00
2010-11-01 13:42:18 -03:00
#TODO: not DRY
2009-12-24 17:24:39 +02:00
try :
2010-11-29 00:57:19 +02:00
last_post = Post . objects . filter ( topic__forum__id = from_forum . id ) . latest ( )
2009-12-24 17:24:39 +02:00
except Post . DoesNotExist :
last_post = None
from_forum . last_post = last_post
from_forum . topic_count = from_forum . topics . count ( )
from_forum . post_count = from_forum . posts . count ( )
2009-12-19 16:40:01 +02:00
from_forum . save ( )
return HttpResponseRedirect ( to_forum . get_absolute_url ( ) )
2009-12-24 17:24:39 +02:00
2012-02-18 00:10:44 +02:00
return render ( request , ' djangobb_forum/move_topic.html ' , { ' categories ' : Category . objects . all ( ) ,
2009-12-24 17:43:16 +02:00
' topic_ids ' : topic_ids ,
2009-12-24 17:24:39 +02:00
' exclude_forum ' : from_forum ,
2012-02-18 00:10:44 +02:00
} )
2009-01-05 14:30:08 +02:00
2009-12-24 17:24:39 +02:00
2009-01-05 14:30:08 +02:00
@login_required
2010-06-09 21:40:09 +03:00
@transaction.commit_on_success
2011-08-19 22:30:03 +03:00
def stick_unstick_topic ( request , topic_id , action ) :
2009-01-05 14:30:08 +02:00
topic = get_object_or_404 ( Topic , pk = topic_id )
if forum_moderated_by ( topic , request . user ) :
2011-08-19 22:30:03 +03:00
if action == ' s ' :
topic . sticky = True
elif action == ' u ' :
topic . sticky = False
2009-12-28 21:39:16 +02:00
topic . save ( )
2009-01-05 14:30:08 +02:00
return HttpResponseRedirect ( topic . get_absolute_url ( ) )
@login_required
2010-06-09 21:40:09 +03:00
@transaction.commit_on_success
2009-01-05 14:30:08 +02:00
def delete_post ( request , post_id ) :
post = get_object_or_404 ( Post , pk = post_id )
2009-07-02 16:59:07 +03:00
last_post = post . topic . last_post
2009-01-05 14:30:08 +02:00
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: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
2010-06-09 21:40:09 +03:00
@transaction.commit_on_success
2011-08-19 22:30:03 +03:00
def open_close_topic ( request , topic_id , action ) :
2009-01-05 14:30:08 +02:00
topic = get_object_or_404 ( Topic , pk = topic_id )
if forum_moderated_by ( topic , request . user ) :
2011-08-19 22:30:03 +03:00
if action == ' c ' :
topic . closed = True
elif action == ' o ' :
topic . closed = False
2009-12-28 21:39:16 +02:00
topic . save ( )
2009-01-05 14:30:08 +02:00
return HttpResponseRedirect ( topic . get_absolute_url ( ) )
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 )
2012-02-18 01:17:32 +02:00
return render ( request , ' djangobb_forum/users.html ' , { ' users ' : users ,
2009-01-05 14:30:08 +02:00
' form ' : form ,
2012-02-18 00:10:44 +02:00
} )
2009-08-20 12:46:08 +03:00
2009-12-24 17:24:39 +02:00
2009-01-05 14:30:08 +02:00
@login_required
2010-06-09 21:40:09 +03:00
@transaction.commit_on_success
2009-01-05 14:30:08 +02:00
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 :
2009-11-29 18:56:11 +02:00
return HttpResponseRedirect ( reverse ( ' djangobb:topic ' , args = [ topic . id ] ) )
2009-01-05 14:30:08 +02:00
else :
2011-05-16 10:50:57 +03:00
return HttpResponseRedirect ( reverse ( ' djangobb:forum_profile ' , args = [ request . user . username ] ) )
2009-01-05 14:30:08 +02:00
2009-12-24 17:24:39 +02:00
2009-01-05 14:30:08 +02:00
@login_required
2010-06-09 21:40:09 +03:00
@transaction.commit_on_success
2009-01-05 14:30:08 +02:00
def add_subscription ( request , topic_id ) :
topic = get_object_or_404 ( Topic , pk = topic_id )
topic . subscribers . add ( request . user )
2009-11-29 18:56:11 +02:00
return HttpResponseRedirect ( reverse ( ' djangobb:topic ' , args = [ topic . id ] ) )
2009-01-05 14:30:08 +02:00
2009-12-24 17:24:39 +02:00
2009-04-14 14:57:17 +03:00
@login_required
def show_attachment ( request , hash ) :
attachment = get_object_or_404 ( Attachment , hash = hash )
2010-02-18 11:04:35 +02:00
file_data = file ( attachment . get_absolute_path ( ) , ' rb ' ) . read ( )
2009-11-20 16:25:14 +02:00
response = HttpResponse ( file_data , mimetype = attachment . content_type )
2011-08-23 12:50:55 +03:00
response [ ' Content-Disposition ' ] = ' attachment; filename= " %s " ' % smart_str ( attachment . name )
2009-05-25 13:56:21 +03:00
return response
2009-04-14 14:57:17 +03:00
2010-01-04 14:17:36 +02:00
@login_required
2011-03-24 10:03:01 +02:00
@csrf_exempt
2009-01-05 14:30:08 +02:00
def post_preview ( request ) :
''' Preview for markitup '''
2010-01-04 14:17:36 +02:00
markup = request . user . forum_profile . markup
data = request . POST . get ( ' data ' , ' ' )
2010-11-28 17:57:54 +02:00
data = convert_text_to_html ( data , markup )
2010-01-04 14:17:36 +02:00
if forum_settings . SMILES_SUPPORT :
data = smiles ( data )
2012-02-18 00:10:44 +02:00
return render ( request , ' djangobb_forum/post_preview.html ' , { ' data ' : data } )