<s> from __future__ import unicode_literals <EOL> import math <EOL> from datetime import timedelta <EOL> from django . utils import timezone <EOL> from django . contrib import messages <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . contrib . auth import get_user_model <EOL> from django . contrib . sites . models import Site <EOL> from django . core . cache import cache <EOL> from django . core . exceptions import SuspiciousOperation , PermissionDenied <EOL> from django . core . urlresolvers import reverse <EOL> from django . db import transaction <EOL> from django . db . models import Q , F <EOL> from django . http import Http404 , HttpResponse , HttpResponseRedirect , HttpResponseForbidden <EOL> from django . shortcuts import get_object_or_404 , render <EOL> from django . utils . encoding import smart_str <EOL> from django . utils . translation import ugettext as _ <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from django . conf import settings <EOL> from haystack . query import SearchQuerySet , SQ <EOL> from djangobb_forum import settings as forum_settings <EOL> from djangobb_forum . forms import AddPostForm , EditPostForm , UserSearchForm , PostSearchForm , ReputationForm , MailToForm , EssentialsProfileForm , ReportForm , VotePollForm , PollForm <EOL> from djangobb_forum . models import Category , Forum , Topic , Post , Reputation , Attachment , PostTracking <EOL> from djangobb_forum . templatetags import forum_extras <EOL> from djangobb_forum . templatetags . forum_extras import forum_moderated_by <EOL> from djangobb_forum . util import build_form , smiles , convert_text_to_html , get_page <EOL> User = get_user_model ( ) <EOL> def index ( request , full = True ) : <EOL> users_cached = cache . get ( '<STR_LIT>' , { } ) <EOL> users_online = users_cached and User . objects . filter ( id__in = users_cached . keys ( ) ) or [ ] <EOL> guests_cached = cache . get ( '<STR_LIT>' , { } ) <EOL> guest_count = len ( guests_cached ) <EOL> users_count = len ( users_online ) <EOL> _forums = Forum . objects . all ( ) <EOL> user = request . user <EOL> if not user . is_superuser : <EOL> user_groups = user . groups . all ( ) or [ ] <EOL> _forums = _forums . filter ( Q ( category__groups__in = user_groups ) | Q ( category__groups__isnull = True ) ) <EOL> _forums = _forums . select_related ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> cats = { } <EOL> forums = { } <EOL> for forum in _forums : <EOL> cat = cats . setdefault ( forum . category . id , <EOL> { '<STR_LIT:id>' : forum . category . id , '<STR_LIT>' : forum . category , '<STR_LIT>' : [ ] } ) <EOL> cat [ '<STR_LIT>' ] . append ( forum ) <EOL> forums [ forum . id ] = forum <EOL> cmpkey = lambda x : x [ '<STR_LIT>' ] . position <EOL> cats = sorted ( cats . values ( ) , key = cmpkey ) <EOL> to_return = { '<STR_LIT>' : cats , <EOL> '<STR_LIT>' : Post . objects . count ( ) , <EOL> '<STR_LIT>' : Topic . objects . count ( ) , <EOL> '<STR_LIT>' : User . objects . count ( ) , <EOL> '<STR_LIT>' : users_online , <EOL> '<STR_LIT>' : users_count , <EOL> '<STR_LIT>' : guest_count , <EOL> '<STR_LIT>' : User . objects . latest ( '<STR_LIT>' ) , <EOL> } <EOL> if full : <EOL> return render ( request , '<STR_LIT>' , to_return ) <EOL> else : <EOL> return render ( request , '<STR_LIT>' , to_return ) <EOL> @ transaction . atomic <EOL> def moderate ( request , forum_id ) : <EOL> forum = get_object_or_404 ( Forum , pk = forum_id ) <EOL> topics = forum . topics . order_by ( '<STR_LIT>' , '<STR_LIT>' ) . select_related ( ) <EOL> if request . user . is_superuser or request . user in forum . moderators . all ( ) : <EOL> topic_ids = request . POST . getlist ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in request . POST : <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : Category . objects . all ( ) , <EOL> '<STR_LIT>' : topic_ids , <EOL> '<STR_LIT>' : forum , <EOL> } ) <EOL> elif '<STR_LIT>' in request . POST : <EOL> for topic_id in topic_ids : <EOL> topic = get_object_or_404 ( Topic , pk = topic_id ) <EOL> topic . delete ( ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> elif '<STR_LIT>' in request . POST : <EOL> for topic_id in topic_ids : <EOL> open_close_topic ( request , topic_id , '<STR_LIT:o>' ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> elif '<STR_LIT>' in request . POST : <EOL> for topic_id in topic_ids : <EOL> open_close_topic ( request , topic_id , '<STR_LIT:c>' ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : forum , <EOL> '<STR_LIT>' : get_page ( topics , request , forum_settings . FORUM_PAGE_SIZE ) , <EOL> '<STR_LIT>' : forum . posts . count ( ) , <EOL> } ) <EOL> else : <EOL> raise Http404 <EOL> def search ( request ) : <EOL> def _render_search_form ( form = None ) : <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : Category . objects . all ( ) , <EOL> '<STR_LIT>' : form , <EOL> } ) <EOL> if not '<STR_LIT:action>' in request . GET : <EOL> return _render_search_form ( form = PostSearchForm ( ) ) <EOL> if request . GET . get ( "<STR_LIT>" ) == "<STR_LIT>" : <EOL> show_as_posts = True <EOL> template_name = '<STR_LIT>' <EOL> else : <EOL> show_as_posts = False <EOL> template_name = '<STR_LIT>' <EOL> context = { } <EOL> viewable_category = Category . objects . all ( ) <EOL> topics = Topic . objects . all ( ) . order_by ( "<STR_LIT>" ) <EOL> posts = Post . objects . all ( ) . order_by ( '<STR_LIT>' ) <EOL> user = request . user <EOL> if not user . is_superuser : <EOL> user_groups = user . groups . all ( ) or [ ] <EOL> viewable_category = viewable_category . filter ( Q ( groups__in = user_groups ) | Q ( groups__isnull = True ) ) <EOL> topics = Topic . objects . filter ( forum__category__in = viewable_category ) <EOL> posts = Post . objects . filter ( topic__forum__category__in = viewable_category ) <EOL> base_url = None <EOL> _generic_context = True <EOL> action = request . GET [ '<STR_LIT:action>' ] <EOL> if action == '<STR_LIT>' : <EOL> date = timezone . now ( ) - timedelta ( days = <NUM_LIT:1> ) <EOL> if show_as_posts : <EOL> context [ "<STR_LIT>" ] = posts . filter ( Q ( created__gte = date ) | Q ( updated__gte = date ) ) <EOL> else : <EOL> context [ "<STR_LIT>" ] = topics . filter ( Q ( last_post__created__gte = date ) | Q ( last_post__updated__gte = date ) ) <EOL> _generic_context = False <EOL> elif action == '<STR_LIT>' : <EOL> if not user . is_authenticated ( ) : <EOL> raise Http404 ( "<STR_LIT>" ) <EOL> try : <EOL> last_read = PostTracking . objects . get ( user = user ) . last_read <EOL> except PostTracking . DoesNotExist : <EOL> last_read = None <EOL> if last_read : <EOL> if show_as_posts : <EOL> context [ "<STR_LIT>" ] = posts . filter ( Q ( created__gte = last_read ) | Q ( updated__gte = last_read ) ) <EOL> else : <EOL> context [ "<STR_LIT>" ] = topics . filter ( Q ( last_post__created__gte = last_read ) | Q ( last_post__updated__gte = last_read ) ) <EOL> _generic_context = False <EOL> else : <EOL> topics_id = [ topic . id for topic in topics [ : forum_settings . SEARCH_PAGE_SIZE ] if forum_extras . has_unreads ( topic , user ) ] <EOL> topics = Topic . objects . filter ( id__in = topics_id ) <EOL> elif action == '<STR_LIT>' : <EOL> topics = topics . filter ( post_count = <NUM_LIT:1> ) <EOL> elif action == '<STR_LIT>' : <EOL> topics = topics . filter ( subscribers__id = user . id ) <EOL> elif action == '<STR_LIT>' : <EOL> if not user . is_authenticated ( ) : <EOL> raise Http404 ( "<STR_LIT>" ) <EOL> user_id = request . GET . get ( "<STR_LIT>" , user . id ) <EOL> try : <EOL> user_id = int ( user_id ) <EOL> except ValueError : <EOL> raise SuspiciousOperation ( ) <EOL> if user_id != user . id : <EOL> try : <EOL> search_user = User . objects . get ( id = user_id ) <EOL> except User . DoesNotExist : <EOL> messages . error ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( request . path ) <EOL> messages . info ( request , _ ( "<STR_LIT>" ) % { '<STR_LIT:username>' : search_user . username } ) <EOL> if show_as_posts : <EOL> posts = posts . filter ( user__id = user_id ) <EOL> else : <EOL> topics = topics . filter ( posts__user__id = user_id ) . order_by ( "<STR_LIT>" ) . distinct ( ) <EOL> base_url = "<STR_LIT>" % user_id <EOL> elif action == '<STR_LIT>' : <EOL> form = PostSearchForm ( request . GET ) <EOL> if not form . is_valid ( ) : <EOL> return _render_search_form ( form ) <EOL> keywords = form . cleaned_data [ '<STR_LIT>' ] <EOL> author = form . cleaned_data [ '<STR_LIT>' ] <EOL> forum = form . cleaned_data [ '<STR_LIT>' ] <EOL> search_in = form . cleaned_data [ '<STR_LIT>' ] <EOL> sort_by = form . cleaned_data [ '<STR_LIT>' ] <EOL> sort_dir = form . cleaned_data [ '<STR_LIT>' ] <EOL> query = SearchQuerySet ( ) . models ( Post ) <EOL> if author : <EOL> query = query . filter ( author__username = author ) <EOL> if forum != '<STR_LIT:0>' : <EOL> query = query . filter ( forum__id = forum ) <EOL> if keywords : <EOL> if search_in == '<STR_LIT:all>' : <EOL> query = query . filter ( SQ ( topic = keywords ) | SQ ( text = keywords ) ) <EOL> elif search_in == '<STR_LIT:message>' : <EOL> query = query . filter ( text = keywords ) <EOL> elif search_in == '<STR_LIT>' : <EOL> query = query . filter ( topic = keywords ) <EOL> order = { '<STR_LIT:0>' : '<STR_LIT>' , <EOL> '<STR_LIT:1>' : '<STR_LIT>' , <EOL> '<STR_LIT:2>' : '<STR_LIT>' , <EOL> '<STR_LIT:3>' : '<STR_LIT>' } . get ( sort_by , '<STR_LIT>' ) <EOL> if sort_dir == '<STR_LIT>' : <EOL> order = '<STR_LIT:->' + order <EOL> post_pks = query . values_list ( "<STR_LIT>" , flat = True ) <EOL> if not show_as_posts : <EOL> context [ "<STR_LIT>" ] = topics . filter ( posts__in = post_pks ) . distinct ( ) <EOL> else : <EOL> posts = posts . filter ( pk__in = post_pks ) . order_by ( order ) <EOL> context [ "<STR_LIT>" ] = posts <EOL> get_query_dict = request . GET . copy ( ) <EOL> get_query_dict . pop ( "<STR_LIT>" ) <EOL> base_url = "<STR_LIT>" % get_query_dict . urlencode ( ) <EOL> _generic_context = False <EOL> if _generic_context : <EOL> if show_as_posts : <EOL> context [ "<STR_LIT>" ] = posts . filter ( topic__in = topics ) . order_by ( '<STR_LIT>' ) <EOL> else : <EOL> context [ "<STR_LIT>" ] = topics <EOL> if base_url is None : <EOL> base_url = "<STR_LIT>" % action <EOL> if show_as_posts : <EOL> context [ '<STR_LIT>' ] = get_page ( context [ '<STR_LIT>' ] , request , forum_settings . SEARCH_PAGE_SIZE ) <EOL> context [ "<STR_LIT>" ] = base_url + "<STR_LIT>" <EOL> post_count = context [ "<STR_LIT>" ] . count ( ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) % post_count ) <EOL> else : <EOL> context [ '<STR_LIT>' ] = get_page ( context [ '<STR_LIT>' ] , request , forum_settings . SEARCH_PAGE_SIZE ) <EOL> context [ "<STR_LIT>" ] = base_url + "<STR_LIT>" <EOL> topic_count = context [ "<STR_LIT>" ] . count ( ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) % topic_count ) <EOL> return render ( request , template_name , context ) <EOL> @ login_required <EOL> def misc ( request ) : <EOL> if '<STR_LIT:action>' in request . GET : <EOL> action = request . GET [ '<STR_LIT:action>' ] <EOL> if action == '<STR_LIT>' : <EOL> user = request . user <EOL> PostTracking . objects . filter ( user__id = user . id ) . update ( last_read = timezone . now ( ) , topics = None ) <EOL> messages . info ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> elif action == '<STR_LIT>' : <EOL> if request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> post_id = request . GET [ '<STR_LIT>' ] <EOL> post = get_object_or_404 ( Post , id = post_id ) <EOL> form = build_form ( ReportForm , request , reported_by = request . user , post = post_id ) <EOL> if request . method == '<STR_LIT:POST>' and form . is_valid ( ) : <EOL> form . save ( ) <EOL> messages . info ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( post . get_absolute_url ( ) ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : form } ) <EOL> elif '<STR_LIT>' in request . POST and '<STR_LIT>' in request . GET : <EOL> if not forum_settings . USER_TO_USER_EMAIL and not request . user . is_superuser : <EOL> raise PermissionDenied <EOL> form = MailToForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> user = get_object_or_404 ( User , username = request . GET [ '<STR_LIT>' ] ) <EOL> subject = form . cleaned_data [ '<STR_LIT>' ] <EOL> body = form . cleaned_data [ '<STR_LIT:body>' ] + '<STR_LIT>' % ( Site . objects . get_current ( ) . domain , <EOL> request . user . username , <EOL> request . user . email ) <EOL> try : <EOL> user . email_user ( subject , body , request . user . email ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> except Exception : <EOL> messages . error ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> elif '<STR_LIT>' in request . GET : <EOL> if not forum_settings . USER_TO_USER_EMAIL and not request . user . is_superuser : <EOL> raise PermissionDenied <EOL> mailto = get_object_or_404 ( User , username = request . GET [ '<STR_LIT>' ] ) <EOL> form = MailToForm ( ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : form , <EOL> '<STR_LIT>' : mailto } <EOL> ) <EOL> def show_forum ( request , forum_id , full = True ) : <EOL> forum = get_object_or_404 ( Forum , pk = forum_id ) <EOL> if not forum . category . has_access ( request . user ) : <EOL> raise PermissionDenied <EOL> topics = forum . topics . order_by ( '<STR_LIT>' , '<STR_LIT>' ) . select_related ( ) <EOL> moderator = request . user . is_superuser or request . user in forum . moderators . all ( ) <EOL> categories = [ ] <EOL> for category in Category . objects . all ( ) : <EOL> if category . has_access ( request . user ) : <EOL> categories . append ( category ) <EOL> to_return = { <EOL> '<STR_LIT>' : categories , <EOL> '<STR_LIT>' : forum , <EOL> '<STR_LIT>' : forum . post_count , <EOL> '<STR_LIT>' : get_page ( topics , request , forum_settings . FORUM_PAGE_SIZE ) , <EOL> '<STR_LIT>' : moderator , <EOL> } <EOL> if full : <EOL> return render ( request , '<STR_LIT>' , to_return ) <EOL> else : <EOL> return render ( request , '<STR_LIT>' , to_return ) <EOL> @ transaction . atomic <EOL> def show_topic ( request , topic_id , full = True ) : <EOL> """<STR_LIT>""" <EOL> post_request = request . method == "<STR_LIT:POST>" <EOL> user_is_authenticated = request . user . is_authenticated ( ) <EOL> if post_request and not user_is_authenticated : <EOL> raise PermissionDenied <EOL> topic = get_object_or_404 ( Topic . objects . select_related ( ) , pk = topic_id ) <EOL> if not topic . forum . category . has_access ( request . user ) : <EOL> raise PermissionDenied <EOL> Topic . objects . filter ( pk = topic . id ) . update ( views = F ( '<STR_LIT>' ) + <NUM_LIT:1> ) <EOL> last_post = topic . last_post <EOL> if request . user . is_authenticated ( ) : <EOL> topic . update_read ( request . user ) <EOL> posts = topic . posts . all ( ) . select_related ( ) <EOL> moderator = request . user . is_superuser or request . user in topic . forum . moderators . all ( ) <EOL> if user_is_authenticated and request . user in topic . subscribers . all ( ) : <EOL> subscribed = True <EOL> else : <EOL> subscribed = False <EOL> reply_form = None <EOL> form_url = None <EOL> back_url = None <EOL> if user_is_authenticated and not topic . closed : <EOL> form_url = request . path + "<STR_LIT>" <EOL> back_url = request . path <EOL> ip = request . META . get ( '<STR_LIT>' , None ) <EOL> post_form_kwargs = { "<STR_LIT>" : topic , "<STR_LIT:user>" : request . user , "<STR_LIT>" : ip } <EOL> if post_request and AddPostForm . FORM_NAME in request . POST : <EOL> reply_form = AddPostForm ( request . POST , request . FILES , ** post_form_kwargs ) <EOL> if reply_form . is_valid ( ) : <EOL> post = reply_form . save ( ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( post . get_absolute_url ( ) ) <EOL> else : <EOL> reply_form = AddPostForm ( <EOL> initial = { <EOL> '<STR_LIT>' : request . user . forum_profile . markup , <EOL> '<STR_LIT>' : request . user . forum_profile . auto_subscribe , <EOL> } , <EOL> ** post_form_kwargs <EOL> ) <EOL> poll_form = None <EOL> polls = topic . poll_set . all ( ) <EOL> if not polls : <EOL> poll = None <EOL> else : <EOL> poll = polls [ <NUM_LIT:0> ] <EOL> if user_is_authenticated : <EOL> poll . deactivate_if_expired ( ) <EOL> has_voted = request . user in poll . users . all ( ) <EOL> if not post_request or not VotePollForm . FORM_NAME in request . POST : <EOL> if poll . active and not has_voted : <EOL> poll_form = VotePollForm ( poll ) <EOL> else : <EOL> if not poll . active : <EOL> messages . error ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( topic . get_absolute_url ( ) ) <EOL> elif has_voted : <EOL> messages . error ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( topic . get_absolute_url ( ) ) <EOL> poll_form = VotePollForm ( poll , request . POST ) <EOL> if poll_form . is_valid ( ) : <EOL> ids = poll_form . cleaned_data [ "<STR_LIT>" ] <EOL> queryset = poll . choices . filter ( id__in = ids ) <EOL> queryset . update ( votes = F ( '<STR_LIT>' ) + <NUM_LIT:1> ) <EOL> poll . users . add ( request . user ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( topic . get_absolute_url ( ) ) <EOL> highlight_word = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> view_data = { <EOL> '<STR_LIT>' : Category . objects . all ( ) , <EOL> '<STR_LIT>' : topic , <EOL> '<STR_LIT>' : get_page ( posts , request , forum_settings . TOPIC_PAGE_SIZE ) , <EOL> '<STR_LIT>' : poll , <EOL> '<STR_LIT>' : poll_form , <EOL> } <EOL> if full : <EOL> view_data . update ( { <EOL> '<STR_LIT>' : last_post , <EOL> '<STR_LIT>' : form_url , <EOL> '<STR_LIT>' : reply_form , <EOL> '<STR_LIT>' : back_url , <EOL> '<STR_LIT>' : moderator , <EOL> '<STR_LIT>' : subscribed , <EOL> '<STR_LIT>' : highlight_word , <EOL> } ) <EOL> return render ( request , '<STR_LIT>' , view_data ) <EOL> else : <EOL> return render ( request , '<STR_LIT>' , view_data ) <EOL> @ login_required <EOL> @ transaction . atomic <EOL> def add_topic ( request , forum_id ) : <EOL> """<STR_LIT>""" <EOL> forum = get_object_or_404 ( Forum , pk = forum_id ) <EOL> if not forum . category . has_access ( request . user ) : <EOL> raise PermissionDenied <EOL> ip = request . META . get ( '<STR_LIT>' , None ) <EOL> post_form_kwargs = { "<STR_LIT>" : forum , "<STR_LIT:user>" : request . user , "<STR_LIT>" : ip , } <EOL> poll_form = None <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = AddPostForm ( request . POST , request . FILES , ** post_form_kwargs ) <EOL> if form . is_valid ( ) : <EOL> all_valid = True <EOL> else : <EOL> all_valid = False <EOL> if forum_settings . ENABLE_POLLS : <EOL> poll_form = PollForm ( request . POST ) <EOL> if not poll_form . has_data ( ) : <EOL> poll_form = PollForm ( ) <EOL> elif not poll_form . is_valid ( ) : <EOL> all_valid = False <EOL> if all_valid : <EOL> post = form . save ( ) <EOL> if poll_form and poll_form . has_data ( ) : <EOL> poll_form . save ( post ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> else : <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( post . get_absolute_url ( ) ) <EOL> else : <EOL> form = AddPostForm ( <EOL> initial = { <EOL> '<STR_LIT>' : request . user . forum_profile . markup , <EOL> '<STR_LIT>' : request . user . forum_profile . auto_subscribe , <EOL> } , <EOL> ** post_form_kwargs <EOL> ) <EOL> if forum_settings . ENABLE_POLLS and forum_id : <EOL> poll_form = PollForm ( ) <EOL> context = { <EOL> '<STR_LIT>' : forum , <EOL> '<STR_LIT>' : poll_form , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : request . path , <EOL> '<STR_LIT>' : forum . get_absolute_url ( ) , <EOL> } <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> @ transaction . atomic <EOL> def upload_avatar ( request , username , template = None , form_class = None ) : <EOL> user = get_object_or_404 ( User , username = username ) <EOL> if request . user . is_authenticated ( ) and user == request . user or request . user . is_superuser : <EOL> form = build_form ( form_class , request , instance = user . forum_profile ) <EOL> if request . method == '<STR_LIT:POST>' and form . is_valid ( ) : <EOL> form . save ( ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = [ user . username ] ) ) <EOL> return render ( request , template , { '<STR_LIT>' : form , <EOL> '<STR_LIT>' : forum_settings . AVATAR_WIDTH , <EOL> '<STR_LIT>' : forum_settings . AVATAR_HEIGHT , <EOL> } ) <EOL> else : <EOL> topic_count = Topic . objects . filter ( user__id = user . id ) . count ( ) <EOL> if user . forum_profile . post_count < forum_settings . POST_USER_SEARCH and not request . user . is_authenticated ( ) : <EOL> messages . error ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( settings . LOGIN_URL + '<STR_LIT>' % request . path ) <EOL> return render ( request , template , { '<STR_LIT>' : user , <EOL> '<STR_LIT>' : topic_count , <EOL> } ) <EOL> @ transaction . atomic <EOL> def user ( request , username , section = '<STR_LIT>' , action = None , template = '<STR_LIT>' , form_class = EssentialsProfileForm ) : <EOL> user = get_object_or_404 ( User , username = username ) <EOL> if request . user . is_authenticated ( ) and user == request . user or request . user . is_superuser : <EOL> form = build_form ( form_class , request , instance = user . forum_profile , extra_args = { '<STR_LIT>' : request } ) <EOL> if request . method == '<STR_LIT:POST>' and form . is_valid ( ) : <EOL> form . save ( ) <EOL> profile_url = reverse ( '<STR_LIT>' % section , args = [ user . username ] ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( profile_url ) <EOL> return render ( request , template , { '<STR_LIT>' : section , <EOL> '<STR_LIT>' : user , <EOL> '<STR_LIT>' : form , <EOL> } ) <EOL> else : <EOL> template = '<STR_LIT>' <EOL> topic_count = Topic . objects . filter ( user__id = user . id ) . count ( ) <EOL> if user . forum_profile . post_count < forum_settings . POST_USER_SEARCH and not request . user . is_authenticated ( ) : <EOL> messages . error ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( settings . LOGIN_URL + '<STR_LIT>' % request . path ) <EOL> return render ( request , template , { '<STR_LIT>' : user , <EOL> '<STR_LIT>' : topic_count , <EOL> } ) <EOL> @ login_required <EOL> @ transaction . atomic <EOL> def reputation ( request , username ) : <EOL> user = get_object_or_404 ( User , username = username ) <EOL> form = build_form ( ReputationForm , request , from_user = request . user , to_user = user ) <EOL> if '<STR_LIT:action>' in request . GET : <EOL> if request . user == user : <EOL> return HttpResponseForbidden ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in request . GET : <EOL> post_id = request . GET [ '<STR_LIT>' ] <EOL> form . fields [ '<STR_LIT>' ] . initial = post_id <EOL> if request . GET [ '<STR_LIT:action>' ] == '<STR_LIT>' : <EOL> form . fields [ '<STR_LIT>' ] . initial = <NUM_LIT:1> <EOL> elif request . GET [ '<STR_LIT:action>' ] == '<STR_LIT>' : <EOL> form . fields [ '<STR_LIT>' ] . initial = - <NUM_LIT:1> <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : form } ) <EOL> else : <EOL> raise Http404 <EOL> elif request . method == '<STR_LIT:POST>' : <EOL> if '<STR_LIT>' in request . POST and request . user . is_superuser : <EOL> reputation_list = request . POST . getlist ( '<STR_LIT>' ) <EOL> for reputation_id in reputation_list : <EOL> reputation = get_object_or_404 ( Reputation , pk = reputation_id ) <EOL> reputation . delete ( ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> elif form . is_valid ( ) : <EOL> form . save ( ) <EOL> post_id = request . POST [ '<STR_LIT>' ] <EOL> post = get_object_or_404 ( Post , id = post_id ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( post . get_absolute_url ( ) ) <EOL> else : <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : form } ) <EOL> else : <EOL> reputations = Reputation . objects . filter ( to_user__id = user . id ) . order_by ( '<STR_LIT>' ) . select_related ( ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : reputations , <EOL> '<STR_LIT>' : user . forum_profile , <EOL> } ) <EOL> def show_post ( request , post_id ) : <EOL> post = get_object_or_404 ( Post , pk = post_id ) <EOL> count = post . topic . posts . filter ( created__lt = post . created ) . count ( ) + <NUM_LIT:1> <EOL> page = math . ceil ( count / float ( forum_settings . TOPIC_PAGE_SIZE ) ) <EOL> url = '<STR_LIT>' % ( reverse ( '<STR_LIT>' , args = [ post . topic . id ] ) , page , post . id ) <EOL> return HttpResponseRedirect ( url ) <EOL> @ login_required <EOL> @ transaction . atomic <EOL> def edit_post ( request , post_id ) : <EOL> from djangobb_forum . templatetags . forum_extras import forum_editable_by <EOL> post = get_object_or_404 ( Post , pk = post_id ) <EOL> topic = post . topic <EOL> if not forum_editable_by ( post , request . user ) : <EOL> messages . error ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( post . get_absolute_url ( ) ) <EOL> form = build_form ( EditPostForm , request , topic = topic , instance = post ) <EOL> if form . is_valid ( ) : <EOL> post = form . save ( commit = False ) <EOL> post . updated_by = request . user <EOL> post . save ( ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( post . get_absolute_url ( ) ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : form , <EOL> '<STR_LIT>' : post , <EOL> } ) <EOL> @ login_required <EOL> @ transaction . atomic <EOL> def delete_posts ( request , topic_id ) : <EOL> topic = Topic . objects . select_related ( ) . get ( pk = topic_id ) <EOL> if forum_moderated_by ( topic , request . user ) : <EOL> deleted = False <EOL> post_list = request . POST . getlist ( '<STR_LIT>' ) <EOL> for post_id in post_list : <EOL> if not deleted : <EOL> deleted = True <EOL> delete_post ( request , post_id ) <EOL> if deleted : <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( topic . get_absolute_url ( ) ) <EOL> last_post = topic . posts . latest ( ) <EOL> if request . user . is_authenticated ( ) : <EOL> topic . update_read ( request . user ) <EOL> posts = topic . posts . all ( ) . select_related ( ) <EOL> initial = { } <EOL> if request . user . is_authenticated ( ) : <EOL> initial = { '<STR_LIT>' : request . user . forum_profile . markup } <EOL> form = AddPostForm ( topic = topic , initial = initial ) <EOL> moderator = request . user . is_superuser or request . user in topic . forum . moderators . all ( ) <EOL> if request . user . is_authenticated ( ) and request . user in topic . subscribers . all ( ) : <EOL> subscribed = True <EOL> else : <EOL> subscribed = False <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : topic , <EOL> '<STR_LIT>' : last_post , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : moderator , <EOL> '<STR_LIT>' : subscribed , <EOL> '<STR_LIT>' : get_page ( posts , request , forum_settings . TOPIC_PAGE_SIZE ) , <EOL> } ) <EOL> @ login_required <EOL> @ transaction . atomic <EOL> def move_topic ( request ) : <EOL> if '<STR_LIT>' in request . GET : <EOL> topic_ids = [ request . GET [ '<STR_LIT>' ] ] <EOL> else : <EOL> topic_ids = request . POST . getlist ( '<STR_LIT>' ) <EOL> first_topic = topic_ids [ <NUM_LIT:0> ] <EOL> topic = get_object_or_404 ( Topic , pk = first_topic ) <EOL> from_forum = topic . forum <EOL> if '<STR_LIT>' in request . POST : <EOL> to_forum_id = int ( request . POST [ '<STR_LIT>' ] ) <EOL> to_forum = get_object_or_404 ( Forum , pk = to_forum_id ) <EOL> for topic_id in topic_ids : <EOL> topic = get_object_or_404 ( Topic , pk = topic_id ) <EOL> if topic . forum != to_forum : <EOL> if forum_moderated_by ( topic , request . user ) : <EOL> topic . forum = to_forum <EOL> topic . save ( ) <EOL> try : <EOL> last_post = Post . objects . filter ( topic__forum__id = from_forum . id ) . latest ( ) <EOL> except Post . DoesNotExist : <EOL> last_post = None <EOL> from_forum . last_post = last_post <EOL> from_forum . topic_count = from_forum . topics . count ( ) <EOL> from_forum . post_count = from_forum . posts . count ( ) <EOL> from_forum . save ( ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( to_forum . get_absolute_url ( ) ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : Category . objects . all ( ) , <EOL> '<STR_LIT>' : topic_ids , <EOL> '<STR_LIT>' : from_forum , <EOL> } ) <EOL> @ login_required <EOL> @ transaction . atomic <EOL> def stick_unstick_topic ( request , topic_id , action ) : <EOL> topic = get_object_or_404 ( Topic , pk = topic_id ) <EOL> if forum_moderated_by ( topic , request . user ) : <EOL> if action == '<STR_LIT:s>' : <EOL> topic . sticky = True <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> elif action == '<STR_LIT:u>' : <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> topic . sticky = False <EOL> topic . save ( ) <EOL> return HttpResponseRedirect ( topic . get_absolute_url ( ) ) <EOL> @ login_required <EOL> @ transaction . atomic <EOL> def delete_post ( request , post_id ) : <EOL> post = get_object_or_404 ( Post , pk = post_id ) <EOL> last_post = post . topic . last_post <EOL> topic = post . topic <EOL> forum = post . topic . forum <EOL> if not ( request . user . is_superuser or request . user in post . topic . forum . moderators . all ( ) or ( post . user == request . user ) ) : <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( post . get_absolute_url ( ) ) <EOL> post . delete ( ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> try : <EOL> Topic . objects . get ( pk = topic . id ) <EOL> except Topic . DoesNotExist : <EOL> return HttpResponseRedirect ( forum . get_absolute_url ( ) ) <EOL> else : <EOL> return HttpResponseRedirect ( topic . get_absolute_url ( ) ) <EOL> @ login_required <EOL> @ transaction . atomic <EOL> def open_close_topic ( request , topic_id , action ) : <EOL> topic = get_object_or_404 ( Topic , pk = topic_id ) <EOL> if forum_moderated_by ( topic , request . user ) : <EOL> if action == '<STR_LIT:c>' : <EOL> topic . closed = True <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> elif action == '<STR_LIT:o>' : <EOL> topic . closed = False <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> topic . save ( ) <EOL> return HttpResponseRedirect ( topic . get_absolute_url ( ) ) <EOL> def users ( request ) : <EOL> users = User . objects . filter ( forum_profile__post_count__gte = forum_settings . POST_USER_SEARCH ) . order_by ( '<STR_LIT:username>' ) <EOL> form = UserSearchForm ( request . GET ) <EOL> users = form . filter ( users ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : get_page ( users , request , forum_settings . USERS_PAGE_SIZE ) , <EOL> '<STR_LIT>' : form , <EOL> } ) <EOL> @ login_required <EOL> @ transaction . atomic <EOL> def delete_subscription ( request , topic_id ) : <EOL> topic = get_object_or_404 ( Topic , pk = topic_id ) <EOL> topic . subscribers . remove ( request . user ) <EOL> messages . info ( request , _ ( "<STR_LIT>" ) ) <EOL> if '<STR_LIT>' in request . GET : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = [ topic . id ] ) ) <EOL> else : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = [ request . user . username ] ) ) <EOL> @ login_required <EOL> @ transaction . atomic <EOL> def add_subscription ( request , topic_id ) : <EOL> topic = get_object_or_404 ( Topic , pk = topic_id ) <EOL> topic . subscribers . add ( request . user ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = [ topic . id ] ) ) <EOL> @ login_required <EOL> def show_attachment ( request , hash ) : <EOL> attachment = get_object_or_404 ( Attachment , hash = hash ) <EOL> file_data = open ( attachment . get_absolute_path ( ) , '<STR_LIT:rb>' ) . read ( ) <EOL> response = HttpResponse ( file_data , content_type = attachment . content_type ) <EOL> response [ '<STR_LIT>' ] = smart_str ( '<STR_LIT>' % attachment . name ) <EOL> return response <EOL> @ login_required <EOL> @ csrf_exempt <EOL> def post_preview ( request ) : <EOL> '''<STR_LIT>''' <EOL> markup = request . user . forum_profile . markup <EOL> data = request . POST . get ( '<STR_LIT:data>' , '<STR_LIT>' ) <EOL> data = convert_text_to_html ( data , markup ) <EOL> if forum_settings . SMILES_SUPPORT : <EOL> data = smiles ( data ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT:data>' : data } ) </s>
