2012-08-10 11:12:52 +03:00
# coding: utf-8
2009-01-05 14:30:08 +02:00
import math
2013-01-14 21:13:32 -05:00
import re
2013-01-18 19:19:17 -05:00
import urllib2
2013-01-30 19:15:52 -05:00
from datetime import timedelta
2009-01-05 14:30:08 +02:00
2012-08-10 11:12:52 +03:00
from django . contrib import messages
2009-09-08 17:35:04 +03:00
from django . contrib . auth . decorators import login_required
2012-08-10 11:12:52 +03:00
from django . contrib . auth . models import User
2013-02-09 02:14:23 -05:00
from django . contrib . auth . forms import AuthenticationForm
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 . cache import cache
2012-08-10 11:12:52 +03:00
from django . core . urlresolvers import reverse
2010-06-09 21:40:09 +03:00
from django . db import transaction
2012-08-10 11:12:52 +03:00
from django . db . models import Q , F
from django . http import Http404 , HttpResponse , HttpResponseRedirect , HttpResponseForbidden
from django . shortcuts import get_object_or_404 , render
from django . utils . encoding import smart_str
2012-08-06 11:47:39 +03:00
from django . utils . translation import ugettext as _
2013-01-30 19:15:52 -05:00
from django . utils import timezone
2012-08-10 11:12:52 +03:00
from django . views . decorators . csrf import csrf_exempt
from haystack . query import SearchQuerySet , SQ
2009-09-04 13:03:15 +03:00
2012-08-08 11:00:28 +03:00
from djangobb_forum import settings as forum_settings
2012-08-06 11:47:39 +03:00
from djangobb_forum . forms import AddPostForm , EditPostForm , UserSearchForm , \
2012-12-17 17:10:33 -05:00
PostSearchForm , ReputationForm , MailToForm , PersonalityProfileForm , \
2012-08-10 11:12:52 +03:00
VotePollForm , ReportForm , VotePollForm , PollForm
from djangobb_forum . models import Category , Forum , Topic , Post , Reputation , \
2012-11-05 14:39:08 -05:00
Report , Attachment , PostTracking
2009-12-23 17:06:48 +02:00
from djangobb_forum . templatetags import forum_extras
2009-12-24 17:24:39 +02:00
from djangobb_forum . templatetags . forum_extras import forum_moderated_by
2013-01-17 22:09:37 -05:00
from djangobb_forum . util import build_form , paginate , set_language , smiles , convert_text_to_html , UnapprovedImageError , can_close_topic
2009-12-24 17:24:39 +02:00
2012-08-10 11:12:52 +03:00
2010-11-01 13:42:18 -03:00
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 ' , { } )
2013-02-21 17:38:49 -05: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 )
2013-02-21 17:32:18 -05:00
online_truncated = users_count > forum_settings . MAX_ONLINE
if online_truncated :
users_online = users_online [ : forum_settings . MAX_ONLINE ]
2009-04-07 18:43:04 +03:00
2012-09-24 14:18:16 +03:00
_forums = Forum . objects . all ( )
user = request . user
if not user . is_superuser :
user_groups = user . groups . all ( ) or [ ] # need 'or []' for anonymous user otherwise: 'EmptyManager' object is not iterable
_forums = _forums . filter ( Q ( category__groups__in = user_groups ) | Q ( category__groups__isnull = True ) )
_forums = _forums . select_related ( ' last_post__topic ' , ' last_post__user ' , ' category ' )
2009-01-05 14:30:08 +02:00
cats = { }
forums = { }
2009-10-07 13:08:09 +03:00
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
' users_online ' : users_online ,
2009-04-03 23:38:12 +03:00
' online_count ' : users_count ,
2013-02-21 17:32:18 -05:00
' online_truncated ' : online_truncated ,
2009-02-09 20:31:19 +02:00
' guest_count ' : guest_count ,
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 :
2013-02-20 17:27:44 -05:00
return render ( request , ' djangobb_forum/mobile/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-08-06 11:47:39 +03: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 ( )
2013-01-14 22:57:35 -05:00
messages . success ( request , _ ( " Topics deleted. " ) )
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 ' )
2013-01-14 22:57:35 -05:00
messages . success ( request , _ ( " Topics opened. " ) )
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 ' )
2013-01-14 22:57:35 -05:00
messages . success ( request , _ ( " Topics closed. " ) )
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
2012-11-05 14:39:08 -05:00
@transaction.commit_on_success
def reports ( request ) :
if request . user . is_superuser or request . user . has_perm ( ' djangobb_forum.change_report ' ) :
2012-12-20 14:18:29 -05:00
if ' action ' in request . GET :
zap_report_id = int ( request . POST [ ' id ' ] )
zap_report = get_object_or_404 ( Report , pk = zap_report_id )
if request . GET [ ' action ' ] == ' zap ' :
zap_report . zapped_by = request . user
zap_report . zapped = True
elif request . GET [ ' action ' ] == ' unzap ' :
zap_report . zapped_by = None
zap_report . zapped = False
zap_report . save ( )
2012-11-05 14:39:08 -05:00
new_reports = Report . objects . filter ( zapped = False ) . order_by ( ' -created ' )
zapped_reports = Report . objects . filter ( zapped = True ) . order_by ( ' -created ' ) [ : 10 ]
2013-04-04 17:25:02 +00:00
return render ( request , ' djangobb_forum/reports.html ' , { ' new_reports ' : new_reports , ' zapped_reports ' : zapped_reports } )
2012-11-05 14:39:08 -05:00
else :
raise Http404
2009-12-24 17:24:39 +02:00
2013-02-09 02:14:23 -05:00
def search ( request , full = True ) :
2012-08-08 11:00:28 +03:00
# TODO: used forms in every search type
2013-02-20 17:27:44 -05:00
template_dir = ' djangobb_forum/ ' if full else ' djangobb_forum/mobile/ '
2012-06-05 11:54:31 +03:00
2012-08-08 11:00:28 +03:00
def _render_search_form ( form = None ) :
2013-04-04 19:01:57 +00:00
# TODO: remove 'in' clause from following query
categories_with_forums = Category . objects . prefetch_related ( ' forums ' )
return render ( request , template_dir + ' search_form.html ' , {
' categories ' : categories_with_forums ,
' form ' : form ,
} )
2009-01-20 23:55:09 +02:00
2012-08-08 11:00:28 +03:00
if not ' action ' in request . GET :
return _render_search_form ( form = PostSearchForm ( ) )
if request . GET . get ( " show_as " ) == " posts " :
show_as_posts = True
2013-02-09 02:14:23 -05:00
template_name = template_dir + ' search_posts.html '
2012-08-08 11:00:28 +03:00
else :
show_as_posts = False
2013-02-09 02:14:23 -05:00
template_name = template_dir + ' search_topics.html '
2012-08-08 11:00:28 +03:00
context = { }
2012-09-24 14:18:16 +03:00
# Create 'user viewable' pre-filtered topics/posts querysets
viewable_category = Category . objects . all ( )
topics = Topic . objects . all ( ) . order_by ( " -last_post__created " )
posts = Post . objects . all ( ) . order_by ( ' -created ' )
2012-09-24 14:18:16 +03:00
user = request . user
2012-09-24 14:18:16 +03:00
if not user . is_superuser :
2013-04-04 17:25:02 +00:00
user_groups = user . groups . all ( ) or [ ] # need 'or []' for anonymous user otherwise: 'EmptyManager' object is not iterable
2012-09-24 14:18:16 +03:00
viewable_category = viewable_category . filter ( Q ( groups__in = user_groups ) | Q ( groups__isnull = True ) )
2013-04-05 12:59:07 +00:00
topics = Topic . objects . filter ( forum__category__in = viewable_category ) \
. select_related ( ' last_post ' , ' last_post__user ' )
2012-09-24 14:18:16 +03:00
posts = Post . objects . filter ( topic__forum__category__in = viewable_category )
2012-08-08 11:00:28 +03:00
base_url = None
_generic_context = True
action = request . GET [ ' action ' ]
if action == ' show_24h ' :
2013-01-30 19:15:52 -05:00
date = timezone . now ( ) - timedelta ( days = 1 )
2012-09-24 14:18:16 +03:00
if show_as_posts :
context [ " posts " ] = posts . filter ( Q ( created__gte = date ) | Q ( updated__gte = date ) )
else :
context [ " topics " ] = topics . filter ( Q ( last_post__created__gte = date ) | Q ( last_post__updated__gte = date ) )
_generic_context = False
2012-08-08 11:00:28 +03:00
elif action == ' show_new ' :
2012-09-24 14:18:16 +03:00
if not user . is_authenticated ( ) :
raise Http404 ( " Search ' show_new ' not available for anonymous user. " )
2012-08-08 11:00:28 +03:00
try :
2012-09-24 14:18:16 +03:00
last_read = PostTracking . objects . get ( user = user ) . last_read
2012-08-08 11:00:28 +03:00
except PostTracking . DoesNotExist :
last_read = None
2012-09-24 14:18:16 +03:00
2012-08-08 11:00:28 +03:00
if last_read :
2012-09-24 14:18:16 +03:00
if show_as_posts :
context [ " posts " ] = posts . filter ( Q ( created__gte = last_read ) | Q ( updated__gte = last_read ) )
else :
context [ " topics " ] = topics . filter ( Q ( last_post__created__gte = last_read ) | Q ( last_post__updated__gte = last_read ) )
_generic_context = False
2012-08-08 11:00:28 +03:00
else :
2013-02-15 18:21:49 -05:00
topic_ids = [ topic . id for topic in topics [ : forum_settings . SEARCH_PAGE_SIZE ] if forum_extras . has_unreads ( topic , user ) ]
topics = Topic . objects . filter ( id__in = topics_id )
2012-08-08 11:00:28 +03:00
elif action == ' show_unanswered ' :
topics = topics . filter ( post_count = 1 )
elif action == ' show_subscriptions ' :
2012-09-24 14:18:16 +03:00
topics = topics . filter ( subscribers__id = user . id )
2012-08-08 11:00:28 +03:00
elif action == ' show_user ' :
# Show all posts from user or topics started by user
2012-09-24 14:18:16 +03:00
if not user . is_authenticated ( ) :
raise Http404 ( " Search ' show_user ' not available for anonymous user. " )
if user . is_staff :
user_id = request . GET . get ( " user_id " , user . id )
user_id = int ( user_id )
if user_id != user . id :
search_user = User . objects . get ( id = user_id )
messages . info ( request , " Filter by user ' %s ' . " % search_user . username )
else :
user_id = user . id
2012-09-24 14:18:16 +03:00
if show_as_posts :
2012-09-24 14:18:16 +03:00
posts = posts . filter ( user__id = user_id )
2012-09-24 14:18:16 +03:00
else :
2012-08-08 11:00:28 +03:00
# show as topic
2012-09-24 14:18:16 +03:00
topics = topics . filter ( posts__user__id = user_id ) . order_by ( " -last_post__created " ) . distinct ( )
2012-09-24 14:18:16 +03:00
base_url = " ?action=show_user&user_id= %s &show_as= " % user_id
2012-08-08 11:00:28 +03:00
elif action == ' search ' :
form = PostSearchForm ( request . GET )
if not form . is_valid ( ) :
return _render_search_form ( form )
keywords = form . cleaned_data [ ' keywords ' ]
author = form . cleaned_data [ ' author ' ]
forum = form . cleaned_data [ ' forum ' ]
search_in = form . cleaned_data [ ' search_in ' ]
sort_by = form . cleaned_data [ ' sort_by ' ]
sort_dir = form . cleaned_data [ ' sort_dir ' ]
query = SearchQuerySet ( ) . models ( Post )
if author :
query = query . filter ( author__username = author )
if forum != u ' 0 ' :
query = query . filter ( forum__id = forum )
if keywords :
if search_in == ' all ' :
query = query . filter ( SQ ( topic = keywords ) | SQ ( text = keywords ) )
elif search_in == ' message ' :
query = query . filter ( text = keywords )
elif search_in == ' topic ' :
query = query . filter ( topic = keywords )
order = { ' 0 ' : ' created ' ,
' 1 ' : ' author ' ,
' 2 ' : ' topic ' ,
' 3 ' : ' forum ' } . get ( sort_by , ' created ' )
if sort_dir == ' DESC ' :
order = ' - ' + order
posts = query . order_by ( order )
if not show_as_posts :
# TODO: We have here a problem to get a list of topics without double entries.
# Maybe we must add a search index over topics?
# Info: If whoosh backend used, setup HAYSTACK_ITERATOR_LOAD_PER_QUERY
# to a higher number to speed up
post_pks = posts . values_list ( " pk " , flat = True )
2012-09-24 14:18:16 +03:00
context [ " topics " ] = topics . filter ( posts__in = post_pks ) . distinct ( )
2012-08-08 11:00:28 +03:00
else :
2012-09-24 14:18:16 +03:00
# FIXME: How to use the pre-filtered query from above?
posts = posts . filter ( topic__forum__category__in = viewable_category )
2012-08-08 11:00:28 +03:00
context [ " posts " ] = posts
get_query_dict = request . GET . copy ( )
get_query_dict . pop ( " show_as " )
base_url = " ? %s &show_as= " % get_query_dict . urlencode ( )
_generic_context = False
if _generic_context :
if show_as_posts :
2012-09-24 14:18:16 +03:00
context [ " posts " ] = posts . filter ( topic__in = topics ) . order_by ( ' -created ' )
2012-08-08 11:00:28 +03:00
else :
context [ " topics " ] = topics
if base_url is None :
base_url = " ?action= %s &show_as= " % action
if show_as_posts :
context [ " as_topic_url " ] = base_url + " topics "
2012-09-24 14:18:16 +03:00
post_count = context [ " posts " ] . count ( )
messages . success ( request , _ ( " Found %i posts. " ) % post_count )
2012-08-08 11:00:28 +03:00
else :
context [ " as_post_url " ] = base_url + " posts "
2012-09-24 14:18:16 +03:00
topic_count = context [ " topics " ] . count ( )
messages . success ( request , _ ( " Found %i topics. " ) % topic_count )
2012-08-08 11:00:28 +03:00
return render ( request , template_name , context )
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 ' ]
2012-08-06 11:47:39 +03:00
if action == ' markread ' :
2009-10-22 15:19:26 +03:00
user = request . user
2013-01-30 19:15:52 -05:00
PostTracking . objects . filter ( user__id = user . id ) . update ( last_read = timezone . now ( ) , topics = None )
2013-01-14 22:57:35 -05:00
messages . info ( request , _ ( " Marked all topics as read. " ) )
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 ( )
2012-08-06 11:47:39 +03:00
messages . info ( request , _ ( " Post reported. " ) )
2009-01-05 14:30:08 +02:00
return HttpResponseRedirect ( post . get_absolute_url ( ) )
2013-02-08 16:19:06 -05:00
return render ( request , ' djangobb_forum/report.html ' , {
' form ' : form ,
' post ' : post ,
} )
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 ' ]
2012-08-30 23:33:19 +02:00
body = form . cleaned_data [ ' body ' ] + u ' \n %s %s [ %s ] ' % ( Site . objects . get_current ( ) . domain ,
2010-02-23 17:04:44 +02:00
request . user . username ,
request . user . email )
2012-08-30 23:35:09 +02:00
try :
user . email_user ( subject , body , request . user . email )
2013-01-14 22:57:35 -05:00
messages . success ( request , _ ( " Email sent. " ) )
2012-08-30 23:35:09 +02:00
except Exception :
messages . error ( request , _ ( " Email could not be sent. " ) )
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 ( )
2013-04-04 15:48:33 +00:00
topics = forum . topics . order_by ( ' -sticky ' , ' -updated ' ) . select_related ( ' last_post__user ' , ' user ' )
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 ,
2013-02-15 17:39:10 -05:00
' can_create_topics ' : ( not forum . moderator_only or request . user . is_superuser or request . user in forum . moderators . all ( ) ) and request . user . is_authenticated ,
2009-02-09 20:31:19 +02:00
}
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 :
2013-02-20 17:27:44 -05:00
return render ( request , ' djangobb_forum/mobile/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 ) :
2012-08-10 11:12:52 +03:00
"""
* Display a topic
* save a reply
* save a poll vote
"""
post_request = request . method == " POST "
user_is_authenticated = request . user . is_authenticated ( )
if post_request and not user_is_authenticated :
# Info: only user that are logged in should get forms in the page.
return HttpResponseForbidden ( )
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-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 )
2013-04-04 18:45:43 +00:00
# without specifying, following query wouldn't select related properly
posts = topic . posts . select_related ( ' user__userprofile ' ,
' user__forum_profile ' ,
' updated_by ' ) . all ( )
# TODO: change the attachments query so it doesn't generate an 'in' clause
posts_with_attachments = { post . id : post . attachments for post in \
topic . posts . prefetch_related ( ' attachments ' ) }
2013-01-30 19:15:52 -05:00
edit_start = timezone . now ( ) - timedelta ( minutes = 1 )
edit_end = timezone . now ( )
2013-01-14 12:10:05 -05:00
editable = posts . filter ( created__range = ( edit_start , edit_end ) ) . filter ( user_id = request . user . id )
can_edit = request . user . has_perm ( ' djangobb_forum.change_post ' )
2012-12-04 18:39:22 -05:00
first_post_number = int ( forum_settings . TOPIC_PAGE_SIZE ) * ( int ( request . GET . get ( ' page ' ) or 1 ) - 1 )
2013-01-17 22:09:37 -05:00
can_close = can_close_topic ( request . user , topic )
2009-05-24 22:51:42 +03:00
2012-08-10 11:12:52 +03:00
moderator = request . user . is_superuser or request . user in topic . forum . moderators . all ( )
if user_is_authenticated and request . user in topic . subscribers . all ( ) :
2009-01-05 14:30:08 +02:00
subscribed = True
else :
subscribed = False
2013-04-04 17:25:02 +00:00
2012-08-10 11:12:52 +03:00
# reply form
reply_form = None
form_url = None
back_url = None
2013-02-19 00:53:36 -05:00
if user_is_authenticated and ( not topic . closed or moderator ) :
2013-01-12 13:00:55 -05:00
form_url = request . path + ' ? ' + request . META [ ' QUERY_STRING ' ] + ' #reply ' # if form validation failed: browser should scroll down to reply form ;)
2012-08-10 11:12:52 +03:00
back_url = request . path
ip = request . META . get ( ' REMOTE_ADDR ' , None )
post_form_kwargs = { " topic " : topic , " user " : request . user , " ip " : ip }
if post_request and AddPostForm . FORM_NAME in request . POST :
2012-12-17 06:00:01 -05:00
2012-08-10 11:12:52 +03:00
reply_form = AddPostForm ( request . POST , request . FILES , * * post_form_kwargs )
2012-12-17 06:00:01 -05:00
if reply_form . is_valid ( ) :
post = reply_form . save ( )
2013-01-14 22:57:35 -05:00
messages . success ( request , _ ( " Reply saved. " ) )
2013-02-20 17:27:44 -05:00
return HttpResponseRedirect ( post . get_absolute_url ( ) if full else post . get_mobile_url ( ) )
2012-08-10 11:12:52 +03:00
else :
reply_form = AddPostForm (
initial = {
' markup ' : request . user . forum_profile . markup ,
' subscribe ' : request . user . forum_profile . auto_subscribe ,
} ,
* * post_form_kwargs
)
# handle poll, if exists
poll_form = None
2013-04-04 17:25:02 +00:00
polls = topic . poll_set . all ( )
2012-08-10 11:12:52 +03:00
if not polls :
poll = None
else :
poll = polls [ 0 ]
if user_is_authenticated : # Only logged in users can vote
poll . auto_deactivate ( )
has_voted = request . user in poll . users . all ( )
if not post_request or not VotePollForm . FORM_NAME in request . POST :
# It's not a POST request or: The reply form was send and not a poll vote
if poll . active and not has_voted :
poll_form = VotePollForm ( poll )
else :
if not poll . active :
2013-01-14 22:57:35 -05:00
messages . error ( request , _ ( " This poll is not active. " ) )
2012-08-10 11:12:52 +03:00
return HttpResponseRedirect ( topic . get_absolute_url ( ) )
elif has_voted :
2013-01-14 22:57:35 -05:00
messages . error ( request , _ ( " You already voted on this poll. " ) )
2012-08-10 11:12:52 +03:00
return HttpResponseRedirect ( topic . get_absolute_url ( ) )
poll_form = VotePollForm ( poll , request . POST )
if poll_form . is_valid ( ) :
ids = poll_form . cleaned_data [ " choice " ]
queryset = poll . choices . filter ( id__in = ids )
queryset . update ( votes = F ( ' votes ' ) + 1 )
poll . users . add ( request . user ) # save that this user has vote
2013-01-14 22:57:35 -05:00
messages . success ( request , _ ( " Vote saved. " ) )
2012-08-10 11:12:52 +03:00
return HttpResponseRedirect ( topic . get_absolute_url ( ) )
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 ,
2012-08-10 11:12:52 +03:00
' form_url ' : form_url ,
' reply_form ' : reply_form ,
' back_url ' : back_url ,
2009-02-09 20:31:19 +02:00
' moderator ' : moderator ,
' subscribed ' : subscribed ,
2011-05-02 17:27:55 +03:00
' posts ' : posts ,
2013-04-04 18:45:43 +00:00
' posts_with_attachments ' : posts_with_attachments ,
2012-12-04 18:39:22 -05:00
' first_post_number ' : first_post_number ,
2009-03-25 17:41:38 +02:00
' highlight_word ' : highlight_word ,
2012-08-10 11:12:52 +03:00
' poll ' : poll ,
' poll_form ' : poll_form ,
2013-01-14 12:10:05 -05:00
' editable ' : editable ,
' can_edit ' : can_edit ,
2013-01-17 22:09:37 -05:00
' can_close ' : can_close
2012-02-18 00:10:44 +02:00
} )
2009-02-09 20:31:19 +02:00
else :
2013-02-20 17:27:44 -05:00
return render ( request , ' djangobb_forum/mobile/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-08-10 11:12:52 +03:00
' poll ' : poll ,
' poll_form ' : poll_form ,
2013-02-09 02:14:23 -05:00
' reply_form ' : reply_form ,
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
2013-02-09 02:14:23 -05:00
def add_topic ( request , forum_id , full = True ) :
2012-08-10 11:12:52 +03:00
"""
create a new topic , with or without poll
"""
forum = get_object_or_404 ( Forum , pk = forum_id )
2013-01-27 17:24:14 -05:00
if not forum . category . has_access ( request . user ) or ( forum . moderator_only and not ( request . user . is_superuser or request . user in forum . moderators . all ( ) ) ) :
2012-08-10 11:12:52 +03:00
return HttpResponseForbidden ( )
2010-11-01 13:42:18 -03:00
2009-11-16 14:50:39 +02:00
ip = request . META . get ( ' REMOTE_ADDR ' , None )
2012-08-10 11:12:52 +03:00
post_form_kwargs = { " forum " : forum , " user " : request . user , " ip " : ip , }
if request . method == ' POST ' :
form = AddPostForm ( request . POST , request . FILES , * * post_form_kwargs )
if form . is_valid ( ) :
all_valid = True
else :
all_valid = False
poll_form = PollForm ( request . POST )
create_poll = poll_form . create_poll ( )
if not create_poll :
# All poll fields are empty: User didn't want to create a poll
# Don't run validation and remove all form error messages
poll_form = PollForm ( ) # create clean form without form errors
elif not poll_form . is_valid ( ) :
all_valid = False
if all_valid :
post = form . save ( )
if create_poll :
poll_form . save ( post )
messages . success ( request , _ ( " Topic with poll saved. " ) )
else :
messages . success ( request , _ ( " Topic saved. " ) )
2013-02-20 17:27:44 -05:00
return HttpResponseRedirect ( post . get_absolute_url ( ) if full else post . get_mobile_url ( ) )
2012-08-10 11:12:52 +03:00
else :
form = AddPostForm (
initial = {
' markup ' : request . user . forum_profile . markup ,
' subscribe ' : request . user . forum_profile . auto_subscribe ,
} ,
* * post_form_kwargs
)
2013-04-04 17:25:02 +00:00
2012-11-08 13:31:54 -05:00
# if creating a new topic and allowed
2013-04-04 17:25:02 +00:00
create_poll_form = forum_id and forum_settings . ALLOW_POLLS
2012-11-08 13:31:54 -05:00
poll_form = PollForm ( )
2012-08-10 11:12:52 +03:00
context = {
' forum ' : forum ,
2012-10-23 06:27:09 -04:00
' create_poll_form ' : create_poll_form ,
' poll_form ' : poll_form ,
2012-08-10 11:12:52 +03:00
' form ' : form ,
' form_url ' : request . path ,
' back_url ' : forum . get_absolute_url ( ) ,
}
2013-02-20 17:27:44 -05:00
return render ( request , ' djangobb_forum/add_topic.html ' if full else ' djangobb_forum/mobile/add_topic.html ' , context )
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 ( )
2013-01-14 22:57:35 -05:00
messages . success ( request , _ ( " Avatar uploaded. " ) )
2013-02-20 16:45:13 -05:00
return HttpResponseRedirect ( reverse ( ' djangobb:forum_settings ' , args = [ user . username ] ) )
2012-03-05 11:07:29 +02:00
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 ( ) :
2012-08-06 11:47:39 +03:00
messages . error ( request , _ ( " Please sign in. " ) )
2012-03-05 11:07:29 +02:00
return HttpResponseRedirect ( reverse ( ' user_signin ' ) + ' ?next= %s ' % request . path )
return render ( request , template , { ' profile ' : user ,
' topic_count ' : topic_count ,
} )
@transaction.commit_on_success
2013-02-20 16:45:13 -05:00
def settings ( request , username ) :
2012-03-05 11:07:29 +02:00
user = get_object_or_404 ( User , username = username )
2013-02-20 16:45:13 -05:00
if request . user . is_authenticated ( ) and ( user == request . user or request . user . is_staff or request . user . has_perm ( ' djangobb_forum.change_report ' ) ) :
form = build_form ( PersonalityProfileForm , request , instance = user . forum_profile , extra_args = { ' request ' : request } )
if request . method == ' POST ' and form . is_valid ( ) :
form . save ( )
messages . success ( request , _ ( " Profile saved. " ) )
return render ( request , ' djangobb_forum/profile/profile_personality.html ' , {
' profile ' : user ,
' form ' : form ,
} )
2012-11-08 13:18:07 -05:00
# anyone else
raise Http404
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 :
2012-08-06 11:47:39 +03:00
reputation = get_object_or_404 ( Reputation , pk = reputation_id )
reputation . delete ( )
messages . success ( request , _ ( " Reputation deleted. " ) )
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 )
2012-08-06 11:47:39 +03:00
messages . success ( request , _ ( " Reputation saved. " ) )
2010-02-23 14:19:20 +02:00
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
2013-02-09 02:14:23 -05:00
def show_post ( request , post_id , full = True ) :
2009-01-05 14:30:08 +02:00
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 ) )
2013-02-20 17:27:44 -05:00
url = ' %s ?page= %d #post- %d ' % ( reverse ( ' djangobb:topic ' if full else ' djangobb:mobile_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
2012-12-14 17:45:20 -05:00
@csrf_exempt
def get_post_source ( request , post_id ) :
' Raw (plain text) post source for quoting '
post = get_object_or_404 ( Post , pk = post_id )
return HttpResponse ( post . body , mimetype = ' text/plain ' )
2009-12-24 17:24:39 +02:00
2013-01-14 21:13:32 -05:00
@csrf_exempt
def get_topic_title ( request , topic_id ) :
' Raw (plain text) topic title for move posts confirmation '
topic = get_object_or_404 ( Topic , pk = topic_id )
return HttpResponse ( topic . name , mimetype = ' text/plain ' )
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 ) :
2013-01-14 22:57:35 -05:00
messages . error ( request , _ ( " You don ' t have permission to edit this post. " ) )
2009-01-05 14:30:08 +02:00
return HttpResponseRedirect ( post . get_absolute_url ( ) )
2013-02-19 01:05:40 -05:00
moderator = request . user . is_superuser or request . user in topic . forum . moderators . all ( )
2009-01-05 14:30:08 +02:00
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 )
2013-02-19 01:05:40 -05:00
if not form . cleaned_data [ ' silent_edit ' ] :
post . updated_by = request . user
post . updated = timezone . now ( )
2010-02-17 13:15:27 +02:00
post . save ( )
2012-08-06 11:47:39 +03:00
messages . success ( request , _ ( " Post updated. " ) )
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 ,
2013-02-19 01:05:40 -05:00
' moderator ' : moderator ,
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 :
2012-08-06 11:47:39 +03:00
messages . success ( request , _ ( " Post deleted. " ) )
2009-01-05 14:30:08 +02:00
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
2012-11-19 11:39:59 -05:00
@login_required
@transaction.commit_on_success
def move_posts ( request , topic_id ) :
topic = Topic . objects . select_related ( ) . get ( pk = topic_id )
from_forum = topic . forum
if forum_moderated_by ( topic , request . user ) :
moved = False
post_list = request . POST . getlist ( ' post ' )
if ' to_topic ' in request . POST :
2013-01-14 21:13:32 -05:00
match = re . match ( r ' .*?( \ d+) ' , request . POST [ ' to_topic ' ] )
if match is None :
2013-01-14 22:57:35 -05:00
messages . error ( request , _ ( " The topic ID must be an integer. " ) )
2012-11-19 11:39:59 -05:00
else :
2013-01-14 21:13:32 -05:00
to_topic_id = int ( match . group ( 1 ) )
2012-11-19 11:39:59 -05:00
try :
to_topic = Topic . objects . select_related ( ) . get ( pk = to_topic_id )
except Topic . DoesNotExist :
2013-01-14 22:57:35 -05:00
messages . error ( request , _ ( " That thread doesn ' t exist. " ) )
2012-11-19 11:39:59 -05:00
else :
2013-02-18 18:19:08 -05:00
if ' move_all ' in request . POST :
2013-02-09 15:04:11 -05:00
Post . objects . filter ( topic = topic ) . update ( topic = to_topic )
2012-11-19 11:39:59 -05:00
topic . delete ( )
2013-02-09 15:04:11 -05:00
moved = True
2012-11-19 11:39:59 -05:00
deleted = True
else :
2013-02-09 15:04:11 -05:00
for post_id in post_list :
if not moved :
moved = True
post = get_object_or_404 ( Post , pk = post_id )
if post . topic != to_topic :
last = ( topic . last_post == post )
if forum_moderated_by ( to_topic , request . user ) :
post . topic = to_topic
post . save ( )
if Post . objects . filter ( topic__id = topic . id ) . count ( ) == 0 :
topic . delete ( )
deleted = True
else :
deleted = False
try :
topic . last_post = Post . objects . filter ( topic__id = topic . id ) . latest ( )
except Post . DoesNotExist :
topic . last_post = None
topic . post_count = Post . objects . filter ( topic__id = topic . id ) . count ( )
topic . save ( )
2012-11-19 11:39:59 -05:00
try :
from_forum . last_post = Post . objects . filter ( topic__forum__id = from_forum . id ) . latest ( )
except Post . DoesNotExist :
from_forum . last_post = None
from_forum . post_count = Post . objects . filter ( topic__forum__id = from_forum . id ) . count ( )
from_forum . topic_count = Topic . objects . filter ( forum__id = from_forum . id ) . count ( )
from_forum . save ( )
to_topic . post_count = Post . objects . filter ( topic__id = to_topic . id ) . count ( )
to_topic . save ( )
if moved :
messages . success ( request , _ ( " Posts moved. " ) )
if not deleted :
return HttpResponseRedirect ( topic . get_absolute_url ( ) )
else :
return HttpResponseRedirect ( from_forum . get_absolute_url ( ) )
last_post = topic . posts . latest ( )
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
return render ( request , ' djangobb_forum/move_posts.html ' , { ' categories ' : Category . objects . all ( ) ,
' exclude_topic ' : from_forum ,
' topic ' : topic ,
' last_post ' : last_post ,
' form ' : form ,
' moderator ' : moderator ,
' subscribed ' : subscribed ,
' posts ' : posts ,
} )
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 ( )
2012-08-06 11:47:39 +03:00
messages . success ( request , _ ( " Topic moved. " ) )
2009-12-19 16:40:01 +02:00
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
2013-01-14 22:57:35 -05:00
messages . success ( request , _ ( " Topic stickied. " ) )
2011-08-19 22:30:03 +03:00
elif action == ' u ' :
2013-01-14 22:57:35 -05:00
messages . success ( request , _ ( " Topic unstickied. " ) )
2011-08-19 22:30:03 +03:00
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
2013-02-03 22:50:59 -05:00
is_head = topic . posts . order_by ( ' created ' ) [ 0 ] . id == post . id
2009-01-05 14:30:08 +02:00
2012-08-06 11:47:39 +03:00
if not ( request . user . is_superuser or \
2009-01-05 14:30:08 +02:00
request . user in post . topic . forum . moderators . all ( ) or \
2012-08-06 11:47:39 +03:00
( post . user == request . user and post == last_post ) ) :
2013-01-14 22:57:35 -05:00
messages . success ( request , _ ( " You don ' t have permission to delete this post. " ) )
2009-01-05 14:30:08 +02:00
return HttpResponseRedirect ( post . get_absolute_url ( ) )
2012-11-13 17:57:39 -05:00
delete_kwargs = { ' staff ' : request . user . is_staff }
post . delete ( * * delete_kwargs )
2012-08-06 11:47:39 +03:00
messages . success ( request , _ ( " Post deleted. " ) )
2009-02-03 16:42:34 +02:00
2013-02-03 22:50:59 -05:00
if is_head :
2009-01-05 14:30:08 +02:00
return HttpResponseRedirect ( forum . get_absolute_url ( ) )
2013-02-03 22:50:59 -05:00
return HttpResponseRedirect ( topic . get_absolute_url ( ) )
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 open_close_topic ( request , topic_id , action ) :
2009-01-05 14:30:08 +02:00
topic = get_object_or_404 ( Topic , pk = topic_id )
2013-01-17 22:09:37 -05:00
if forum_moderated_by ( topic , request . user ) or can_close_topic ( request . user , topic ) :
2011-08-19 22:30:03 +03:00
if action == ' c ' :
topic . closed = True
2012-08-06 11:47:39 +03:00
messages . success ( request , _ ( " Topic closed. " ) )
2011-08-19 22:30:03 +03:00
elif action == ' o ' :
topic . closed = False
2012-08-06 11:47:39 +03:00
messages . success ( request , _ ( " Topic opened. " ) )
2009-12-28 21:39:16 +02:00
topic . save ( )
2009-01-05 14:30:08 +02:00
return HttpResponseRedirect ( topic . get_absolute_url ( ) )
2012-10-26 13:34:16 -04:00
@login_required
2009-01-05 14:30:08 +02:00
def users ( request ) :
2012-10-26 13:34:16 -04:00
if not ( request . user . is_superuser ) :
raise Http404
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 )
2013-01-14 22:57:35 -05:00
messages . info ( request , _ ( " Unsubscribed from topic. " ) )
2009-01-05 14:30:08 +02:00
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 :
2013-02-20 16:45:13 -05:00
return HttpResponseRedirect ( reverse ( ' djangobb:forum_settings ' , 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 )
2013-01-14 22:57:35 -05:00
messages . success ( request , _ ( " Subscribed to topic. " ) )
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
data = request . POST . get ( ' data ' , ' ' )
2012-12-05 21:24:42 -05:00
try :
2013-01-17 20:28:40 -05:00
data = convert_text_to_html ( data , request . user . forum_profile )
2012-12-05 21:24:42 -05:00
except UnapprovedImageError as e :
return render ( request , ' djangobb_forum/post_preview.html ' , {
' data ' : e . user_error ( )
} )
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 } )
2013-01-18 19:19:17 -05:00
def show_youtube_video ( request , video_id ) :
try :
response = urllib2 . urlopen ( ' http://gdata.youtube.com/feeds/api/videos/ %s ?v=2 ' % video_id )
except urllib2 . HTTPError :
title = None
else :
try :
start_tag = ' <media:title type= \' plain \' > '
end_tag = ' </media:title> '
atom = response . read ( )
start = atom . index ( start_tag ) + len ( start_tag )
end = atom . index ( end_tag )
title = atom [ start : end ]
except ValueError :
title = None
return render ( request , ' djangobb_forum/youtube.html ' , {
' video_id ' : video_id ,
' video_title ' : title ,
} )
2013-02-18 18:59:03 -05:00
@login_required
2013-02-20 17:27:44 -05:00
def mobile_reply ( request , post_id ) :
2013-02-18 18:59:03 -05:00
post = get_object_or_404 ( Post , id = post_id )
2013-02-20 17:54:29 -05:00
if not request . user . is_authenticated or post . topic . closed and not ( request . user . is_superuser or request . user in forum . moderators . all ( ) ) :
2013-02-20 17:52:53 -05:00
raise Http404
2013-02-18 18:59:03 -05:00
ip = request . META . get ( ' REMOTE_ADDR ' , None )
post_form_kwargs = { " topic " : post . topic , " user " : request . user , " ip " : ip }
if AddPostForm . FORM_NAME in request . POST :
reply_form = AddPostForm ( request . POST , request . FILES , * * post_form_kwargs )
if reply_form . is_valid ( ) :
post = reply_form . save ( )
messages . success ( request , _ ( " Reply saved. " ) )
2013-02-20 17:27:44 -05:00
return HttpResponseRedirect ( post . get_mobile_url ( ) )
2013-02-18 18:59:03 -05:00
else :
reply_form = AddPostForm (
initial = {
' markup ' : request . user . forum_profile . markup ,
' subscribe ' : request . user . forum_profile . auto_subscribe ,
' body ' : ' [quote= ' + post . user . username + ' ] ' + post . body + ' [/quote] \n ' ,
} ,
* * post_form_kwargs
)
2013-02-20 17:27:44 -05:00
return render ( request , ' djangobb_forum/mobile/reply.html ' , {
2013-02-18 18:59:03 -05:00
' form ' : reply_form ,
' post ' : post
} )