<s> import json <EOL> import logging <EOL> import random <EOL> import time <EOL> from datetime import date , datetime , timedelta <EOL> from django . conf import settings <EOL> from django . contrib import auth <EOL> from django . contrib import messages <EOL> from django . contrib . auth . forms import AuthenticationForm <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . contrib . sites . models import Site <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . core . paginator import EmptyPage , PageNotAnInteger <EOL> from django . db . models import Q <EOL> from django . http import ( HttpResponseRedirect , HttpResponse , Http404 , <EOL> HttpResponseBadRequest , HttpResponseForbidden ) <EOL> from django . shortcuts import get_object_or_404 , render , redirect <EOL> from django . template . loader import render_to_string <EOL> from django . utils . translation import ugettext as _ , ugettext_lazy as _lazy <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from django . views . decorators . http import require_POST , require_GET , require_http_methods <EOL> import waffle <EOL> from ordereddict import OrderedDict <EOL> from mobility . decorators import mobile_template <EOL> from session_csrf import anonymous_csrf <EOL> from statsd import statsd <EOL> from taggit . models import Tag <EOL> from tidings . events import ActivationRequestFailed <EOL> from tidings . models import Watch <EOL> from kitsune . access . decorators import permission_required , login_required <EOL> from kitsune . community . utils import top_contributors_questions <EOL> from kitsune . products . api import ProductSerializer , TopicSerializer <EOL> from kitsune . products . models import Product , Topic <EOL> from kitsune . questions import config <EOL> from kitsune . questions . events import QuestionReplyEvent , QuestionSolvedEvent <EOL> from kitsune . questions . feeds import ( <EOL> QuestionsFeed , AnswersFeed , TaggedQuestionsFeed ) <EOL> from kitsune . questions . forms import ( <EOL> NewQuestionForm , EditQuestionForm , AnswerForm , WatchQuestionForm , <EOL> FREQUENCY_CHOICES , MarketplaceAaqForm , MarketplaceRefundForm , <EOL> MarketplaceDeveloperRequestForm , StatsForm ) <EOL> from kitsune . questions . marketplace import ( <EOL> MARKETPLACE_CATEGORIES , ZendeskError ) <EOL> from kitsune . questions . models import ( <EOL> Question , Answer , QuestionVote , AnswerVote , QuestionMappingType , <EOL> QuestionLocale ) <EOL> from kitsune . questions . signals import tag_added <EOL> from kitsune . search . es_utils import ( ES_EXCEPTIONS , Sphilastic , F , <EOL> es_query_with_analyzer ) <EOL> from kitsune . search . utils import locale_or_default , clean_excerpt <EOL> from kitsune . sumo . api_utils import JSONRenderer <EOL> from kitsune . sumo . decorators import ssl_required , ratelimit <EOL> from kitsune . sumo . templatetags . jinja_helpers import urlparams <EOL> from kitsune . sumo . urlresolvers import reverse , split_path <EOL> from kitsune . sumo . utils import paginate , simple_paginate , build_paged_url , is_ratelimited <EOL> from kitsune . tags . utils import add_existing_tag <EOL> from kitsune . upload . api import ImageAttachmentSerializer <EOL> from kitsune . upload . models import ImageAttachment <EOL> from kitsune . upload . views import upload_imageattachment <EOL> from kitsune . users . forms import RegisterForm <EOL> from kitsune . users . templatetags . jinja_helpers import display_name <EOL> from kitsune . users . models import Setting <EOL> from kitsune . users . utils import handle_login , handle_register <EOL> from kitsune . wiki . facets import documents_for , topics_for <EOL> from kitsune . wiki . models import Document , DocumentMappingType <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> UNAPPROVED_TAG = _lazy ( u'<STR_LIT>' ) <EOL> NO_TAG = _lazy ( u'<STR_LIT>' ) <EOL> FILTER_GROUPS = { <EOL> '<STR_LIT:all>' : OrderedDict ( [ <EOL> ( '<STR_LIT>' , _lazy ( '<STR_LIT>' ) ) , <EOL> ] ) , <EOL> '<STR_LIT>' : OrderedDict ( [ <EOL> ( '<STR_LIT>' , _lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _lazy ( '<STR_LIT>' ) ) , <EOL> ] ) , <EOL> '<STR_LIT>' : OrderedDict ( [ <EOL> ( '<STR_LIT>' , _lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _lazy ( '<STR_LIT>' ) ) , <EOL> ] ) , <EOL> '<STR_LIT>' : OrderedDict ( [ <EOL> ( '<STR_LIT>' , _lazy ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _lazy ( '<STR_LIT>' ) ) , <EOL> ] ) , <EOL> } <EOL> ORDER_BY = OrderedDict ( [ <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , _lazy ( '<STR_LIT>' ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , _lazy ( '<STR_LIT>' ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , _lazy ( '<STR_LIT>' ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , _lazy ( '<STR_LIT>' ) ) ) , <EOL> ] ) <EOL> @ mobile_template ( '<STR_LIT>' ) <EOL> def product_list ( request , template ) : <EOL> """<STR_LIT>""" <EOL> return render ( request , template , { <EOL> '<STR_LIT>' : Product . objects . filter ( questions_locales__locale = request . LANGUAGE_CODE ) <EOL> } ) <EOL> @ mobile_template ( '<STR_LIT>' ) <EOL> def question_list ( request , template , product_slug ) : <EOL> """<STR_LIT>""" <EOL> filter_ = request . GET . get ( '<STR_LIT>' ) <EOL> owner = request . GET . get ( <EOL> '<STR_LIT>' , request . session . get ( '<STR_LIT>' , '<STR_LIT:all>' ) ) <EOL> show = request . GET . get ( '<STR_LIT>' ) <EOL> if show not in FILTER_GROUPS : <EOL> show = '<STR_LIT>' <EOL> escalated = request . GET . get ( '<STR_LIT>' ) <EOL> tagged = request . GET . get ( '<STR_LIT>' ) <EOL> tags = None <EOL> topic_slug = request . GET . get ( '<STR_LIT>' ) <EOL> order = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if order not in ORDER_BY : <EOL> order == '<STR_LIT>' <EOL> sort = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> product_slugs = product_slug . split ( '<STR_LIT:U+002C>' ) <EOL> products = [ ] <EOL> if len ( product_slugs ) > <NUM_LIT:1> or product_slugs [ <NUM_LIT:0> ] != '<STR_LIT:all>' : <EOL> for slug in product_slugs : <EOL> products . append ( get_object_or_404 ( Product , slug = slug ) ) <EOL> multiple = len ( products ) > <NUM_LIT:1> <EOL> else : <EOL> products = None <EOL> multiple = True <EOL> if topic_slug and not multiple : <EOL> try : <EOL> topic = Topic . objects . get ( slug = topic_slug , product = products [ <NUM_LIT:0> ] ) <EOL> except Topic . DoesNotExist : <EOL> topic = None <EOL> else : <EOL> topic = None <EOL> question_qs = Question . objects <EOL> if filter_ not in FILTER_GROUPS [ show ] : <EOL> filter_ = None <EOL> if escalated : <EOL> filter_ = None <EOL> if filter_ == '<STR_LIT>' : <EOL> question_qs = question_qs . new ( ) <EOL> elif filter_ == '<STR_LIT>' : <EOL> question_qs = question_qs . unhelpful_answers ( ) <EOL> elif filter_ == '<STR_LIT>' : <EOL> question_qs = question_qs . needs_info ( ) <EOL> elif filter_ == '<STR_LIT>' : <EOL> question_qs = question_qs . solution_provided ( ) <EOL> elif filter_ == '<STR_LIT>' : <EOL> question_qs = question_qs . solved ( ) <EOL> elif filter_ == '<STR_LIT>' : <EOL> question_qs = question_qs . locked ( ) <EOL> elif filter_ == '<STR_LIT>' : <EOL> question_qs = question_qs . recently_unanswered ( ) <EOL> else : <EOL> if show == '<STR_LIT>' : <EOL> question_qs = question_qs . needs_attention ( ) <EOL> if show == '<STR_LIT>' : <EOL> question_qs = question_qs . responded ( ) <EOL> if show == '<STR_LIT>' : <EOL> question_qs = question_qs . done ( ) <EOL> if escalated : <EOL> question_qs = question_qs . filter ( <EOL> tags__name__in = [ config . ESCALATE_TAG_NAME ] ) <EOL> question_qs = question_qs . select_related ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> question_qs = question_qs . prefetch_related ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> question_qs = question_qs . filter ( creator__is_active = <NUM_LIT:1> ) <EOL> if not request . user . has_perm ( '<STR_LIT>' ) : <EOL> question_qs = question_qs . filter ( is_spam = False ) <EOL> if owner == '<STR_LIT>' and request . user . is_authenticated ( ) : <EOL> criteria = Q ( answers__creator = request . user ) | Q ( creator = request . user ) <EOL> question_qs = question_qs . filter ( criteria ) . distinct ( ) <EOL> else : <EOL> owner = None <EOL> feed_urls = ( ( urlparams ( reverse ( '<STR_LIT>' ) , <EOL> product = product_slug , topic = topic_slug ) , <EOL> QuestionsFeed ( ) . title ( ) ) , ) <EOL> if tagged : <EOL> tag_slugs = tagged . split ( '<STR_LIT:U+002C>' ) <EOL> tags = Tag . objects . filter ( slug__in = tag_slugs ) <EOL> if tags : <EOL> for t in tags : <EOL> question_qs = question_qs . filter ( tags__name__in = [ t . name ] ) <EOL> if len ( tags ) == <NUM_LIT:1> : <EOL> feed_urls += ( ( reverse ( '<STR_LIT>' , <EOL> args = [ tags [ <NUM_LIT:0> ] . slug ] ) , <EOL> TaggedQuestionsFeed ( ) . title ( tags [ <NUM_LIT:0> ] ) ) , ) <EOL> else : <EOL> question_qs = Question . objects . none ( ) <EOL> oldest_date = date . today ( ) - timedelta ( days = <NUM_LIT> ) <EOL> question_qs = question_qs . exclude ( created__lt = oldest_date , num_answers = <NUM_LIT:0> ) <EOL> if products : <EOL> question_qs = question_qs . filter ( product__in = products ) . distinct ( ) <EOL> if topic : <EOL> question_qs = question_qs . filter ( topic__id = topic . id ) <EOL> if request . LANGUAGE_CODE in QuestionLocale . objects . locales_list ( ) : <EOL> forum_locale = request . LANGUAGE_CODE <EOL> locale_query = Q ( locale = request . LANGUAGE_CODE ) <EOL> else : <EOL> forum_locale = settings . WIKI_DEFAULT_LANGUAGE <EOL> locale_query = Q ( locale = request . LANGUAGE_CODE ) <EOL> locale_query |= Q ( locale = settings . WIKI_DEFAULT_LANGUAGE ) <EOL> question_qs = question_qs . filter ( locale_query ) <EOL> order_by = ORDER_BY [ order ] [ <NUM_LIT:0> ] <EOL> question_qs = question_qs . order_by ( <EOL> order_by if sort == '<STR_LIT>' else '<STR_LIT>' % order_by ) <EOL> try : <EOL> with statsd . timer ( '<STR_LIT>' % filter_ ) : <EOL> questions_page = simple_paginate ( <EOL> request , question_qs , per_page = config . QUESTIONS_PER_PAGE ) <EOL> except ( PageNotAnInteger , EmptyPage ) : <EOL> if request . GET . get ( '<STR_LIT>' , '<STR_LIT:1>' ) != '<STR_LIT:1>' : <EOL> url = build_paged_url ( request ) <EOL> return HttpResponseRedirect ( urlparams ( url , page = <NUM_LIT:1> ) ) <EOL> extra_filters = locale_query <EOL> if products : <EOL> extra_filters &= Q ( product__in = products ) <EOL> recent_asked_count = Question . recent_asked_count ( extra_filters ) <EOL> recent_unanswered_count = Question . recent_unanswered_count ( extra_filters ) <EOL> if recent_asked_count : <EOL> recent_answered_percent = int ( <EOL> ( float ( recent_asked_count - recent_unanswered_count ) / <EOL> recent_asked_count ) * <NUM_LIT:100> ) <EOL> else : <EOL> recent_answered_percent = <NUM_LIT:0> <EOL> product_list = Product . objects . filter ( visible = True ) <EOL> if products and not multiple : <EOL> topic_list = Topic . objects . filter ( <EOL> visible = True , product = products [ <NUM_LIT:0> ] ) [ : <NUM_LIT:10> ] <EOL> else : <EOL> topic_list = [ ] <EOL> if request . user . is_authenticated ( ) : <EOL> request . session [ '<STR_LIT>' ] = owner <EOL> if products and len ( products ) == <NUM_LIT:1> : <EOL> product = products [ <NUM_LIT:0> ] <EOL> else : <EOL> product = None <EOL> try : <EOL> top_contributors , _ = top_contributors_questions ( <EOL> locale = forum_locale , product = product ) <EOL> except ES_EXCEPTIONS : <EOL> top_contributors = [ ] <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> log . exception ( '<STR_LIT>' ) <EOL> data = { '<STR_LIT>' : questions_page , <EOL> '<STR_LIT>' : feed_urls , <EOL> '<STR_LIT>' : filter_ , <EOL> '<STR_LIT>' : owner , <EOL> '<STR_LIT>' : show , <EOL> '<STR_LIT>' : FILTER_GROUPS [ show ] , <EOL> '<STR_LIT>' : order , <EOL> '<STR_LIT>' : ORDER_BY , <EOL> '<STR_LIT>' : sort , <EOL> '<STR_LIT>' : escalated , <EOL> '<STR_LIT>' : tags , <EOL> '<STR_LIT>' : tagged , <EOL> '<STR_LIT>' : recent_asked_count , <EOL> '<STR_LIT>' : recent_unanswered_count , <EOL> '<STR_LIT>' : recent_answered_percent , <EOL> '<STR_LIT>' : product_list , <EOL> '<STR_LIT>' : products , <EOL> '<STR_LIT>' : product_slug , <EOL> '<STR_LIT>' : multiple , <EOL> '<STR_LIT>' : product_slug == '<STR_LIT:all>' , <EOL> '<STR_LIT>' : top_contributors , <EOL> '<STR_LIT>' : topic_list , <EOL> '<STR_LIT>' : topic } <EOL> with statsd . timer ( '<STR_LIT>' ) : <EOL> return render ( request , template , data ) <EOL> def parse_troubleshooting ( troubleshooting_json ) : <EOL> """<STR_LIT>""" <EOL> if not troubleshooting_json : <EOL> return None <EOL> try : <EOL> parsed = json . loads ( troubleshooting_json ) <EOL> except ValueError : <EOL> return None <EOL> spec = ( <EOL> ( ( ) , dict ) , <EOL> ( ( '<STR_LIT>' , ) , dict ) , <EOL> ( ( '<STR_LIT>' , '<STR_LIT>' ) , bool ) , <EOL> ( ( '<STR_LIT>' , ) , dict ) , <EOL> ( ( '<STR_LIT>' , '<STR_LIT:name>' ) , basestring ) , <EOL> ( ( '<STR_LIT>' , '<STR_LIT>' ) , basestring ) , <EOL> ( ( '<STR_LIT>' , '<STR_LIT>' ) , basestring ) , <EOL> ( ( '<STR_LIT>' , '<STR_LIT:version>' ) , basestring ) , <EOL> ( ( '<STR_LIT>' , ) , list ) , <EOL> ( ( '<STR_LIT>' , ) , dict ) , <EOL> ( ( '<STR_LIT>' , ) , dict ) , <EOL> ( ( '<STR_LIT>' , ) , dict ) , <EOL> ( ( '<STR_LIT>' , ) , dict ) , <EOL> ( ( '<STR_LIT>' , '<STR_LIT>' ) , bool ) , <EOL> ) <EOL> for path , type_ in spec : <EOL> item = parsed <EOL> for piece in path : <EOL> item = item . get ( piece ) <EOL> if item is None : <EOL> return None <EOL> if not isinstance ( item , type_ ) : <EOL> return None <EOL> parsed [ '<STR_LIT>' ] = dict ( <EOL> ( key , val ) for ( key , val ) in parsed [ '<STR_LIT>' ] . items ( ) <EOL> if not key . startswith ( '<STR_LIT>' ) ) <EOL> return parsed <EOL> @ mobile_template ( '<STR_LIT>' ) <EOL> @ anonymous_csrf <EOL> def question_details ( request , template , question_id , form = None , <EOL> watch_form = None , answer_preview = None , ** extra_kwargs ) : <EOL> """<STR_LIT>""" <EOL> ans_ = _answers_data ( request , question_id , form , watch_form , <EOL> answer_preview ) <EOL> question = ans_ [ '<STR_LIT>' ] <EOL> if question . is_spam and not request . user . has_perm ( '<STR_LIT>' ) : <EOL> raise Http404 ( '<STR_LIT>' ) <EOL> troubleshooting_json = question . metadata . get ( '<STR_LIT>' ) <EOL> question . metadata [ '<STR_LIT>' ] = ( <EOL> parse_troubleshooting ( troubleshooting_json ) ) <EOL> if request . user . is_authenticated ( ) : <EOL> ct = ContentType . objects . get_for_model ( request . user ) <EOL> ans_ [ '<STR_LIT>' ] = ImageAttachment . objects . filter ( creator = request . user , <EOL> content_type = ct ) <EOL> extra_kwargs . update ( ans_ ) <EOL> products = Product . objects . filter ( visible = True ) <EOL> topics = topics_for ( product = question . product ) <EOL> related_documents = question . related_documents <EOL> related_questions = question . related_questions <EOL> extra_kwargs . update ( { '<STR_LIT>' : products , '<STR_LIT>' : topics , <EOL> '<STR_LIT>' : question . product , '<STR_LIT>' : question . topic , <EOL> '<STR_LIT>' : related_documents , <EOL> '<STR_LIT>' : related_questions } ) <EOL> if not request . MOBILE : <EOL> if not question . solution_id : <EOL> extra_kwargs . update ( robots_noindex = True ) <EOL> return render ( request , template , extra_kwargs ) <EOL> @ require_POST <EOL> @ permission_required ( '<STR_LIT>' ) <EOL> def edit_details ( request , question_id ) : <EOL> try : <EOL> product = Product . objects . get ( id = request . POST . get ( '<STR_LIT>' ) ) <EOL> topic = Topic . objects . get ( id = request . POST . get ( '<STR_LIT>' ) , <EOL> product = product ) <EOL> locale = request . POST . get ( '<STR_LIT>' ) <EOL> tuple ( QuestionLocale . objects . locales_list ( ) ) . index ( locale ) <EOL> except ( Product . DoesNotExist , Topic . DoesNotExist , ValueError ) : <EOL> return HttpResponseBadRequest ( ) <EOL> question = get_object_or_404 ( Question , pk = question_id ) <EOL> question . product = product <EOL> question . topic = topic <EOL> question . locale = locale <EOL> question . save ( ) <EOL> return redirect ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : question_id } ) ) <EOL> @ ssl_required <EOL> @ anonymous_csrf <EOL> def aaq_react ( request ) : <EOL> request . session [ '<STR_LIT>' ] = True <EOL> to_json = JSONRenderer ( ) . render <EOL> products = ProductSerializer ( <EOL> Product . objects . filter ( questions_locales__locale = request . LANGUAGE_CODE ) , <EOL> many = True ) <EOL> topics = TopicSerializer ( Topic . objects . filter ( in_aaq = True ) , many = True ) <EOL> ctx = { <EOL> '<STR_LIT>' : to_json ( products . data ) , <EOL> '<STR_LIT>' : to_json ( topics . data ) , <EOL> } <EOL> if request . user . is_authenticated ( ) : <EOL> user_ct = ContentType . objects . get_for_model ( request . user ) <EOL> images = ImageAttachmentSerializer ( <EOL> ImageAttachment . objects . filter ( creator = request . user , content_type = user_ct ) , many = True ) <EOL> ctx [ '<STR_LIT>' ] = to_json ( images . data ) <EOL> else : <EOL> ctx [ '<STR_LIT>' ] = to_json ( [ ] ) <EOL> return render ( request , '<STR_LIT>' , ctx ) <EOL> @ ssl_required <EOL> @ mobile_template ( '<STR_LIT>' ) <EOL> @ anonymous_csrf <EOL> def aaq ( request , product_key = None , category_key = None , showform = False , <EOL> template = None , step = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if waffle . flag_is_active ( request , '<STR_LIT>' ) : <EOL> return aaq_react ( request ) <EOL> request . session [ '<STR_LIT>' ] = True <EOL> if ( request . LANGUAGE_CODE not in QuestionLocale . objects . locales_list ( ) and <EOL> request . LANGUAGE_CODE != settings . WIKI_DEFAULT_LANGUAGE ) : <EOL> locale , path = split_path ( request . path ) <EOL> path = '<STR_LIT:/>' + settings . WIKI_DEFAULT_LANGUAGE + '<STR_LIT:/>' + path <EOL> old_lang = settings . LANGUAGES_DICT [ request . LANGUAGE_CODE . lower ( ) ] <EOL> new_lang = settings . LANGUAGES_DICT [ settings . WIKI_DEFAULT_LANGUAGE <EOL> . lower ( ) ] <EOL> msg = ( _ ( u"<STR_LIT>" <EOL> u"<STR_LIT>" ) <EOL> . format ( old_lang = old_lang , new_lang = new_lang ) ) <EOL> messages . add_message ( request , messages . WARNING , msg ) <EOL> return HttpResponseRedirect ( path ) <EOL> if product_key is None : <EOL> product_key = request . GET . get ( '<STR_LIT>' ) <EOL> if request . MOBILE and product_key is None : <EOL> ua = request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) <EOL> if '<STR_LIT>' in ua and '<STR_LIT>' not in ua : <EOL> product_key = '<STR_LIT>' <EOL> elif '<STR_LIT>' in ua : <EOL> product_key = '<STR_LIT>' <EOL> else : <EOL> product_key = '<STR_LIT>' <EOL> product_config = config . products . get ( product_key ) <EOL> if product_key and not product_config : <EOL> raise Http404 <EOL> if product_config and '<STR_LIT>' in product_config : <EOL> try : <EOL> product = Product . objects . get ( slug = product_config [ '<STR_LIT>' ] ) <EOL> except Product . DoesNotExist : <EOL> pass <EOL> else : <EOL> if not product . questions_locales . filter ( locale = request . LANGUAGE_CODE ) . count ( ) : <EOL> locale , path = split_path ( request . path ) <EOL> path = '<STR_LIT:/>' + settings . WIKI_DEFAULT_LANGUAGE + '<STR_LIT:/>' + path <EOL> old_lang = settings . LANGUAGES_DICT [ request . LANGUAGE_CODE . lower ( ) ] <EOL> new_lang = settings . LANGUAGES_DICT [ settings . WIKI_DEFAULT_LANGUAGE . lower ( ) ] <EOL> msg = ( _ ( u"<STR_LIT>" <EOL> u"<STR_LIT>" ) <EOL> . format ( product = product . title , old_lang = old_lang , new_lang = new_lang ) ) <EOL> messages . add_message ( request , messages . WARNING , msg ) <EOL> return HttpResponseRedirect ( path ) <EOL> if category_key is None : <EOL> category_key = request . GET . get ( '<STR_LIT>' ) <EOL> if product_config and category_key : <EOL> product_obj = Product . objects . get ( slug = product_config . get ( '<STR_LIT>' ) ) <EOL> category_config = product_config [ '<STR_LIT>' ] . get ( category_key ) <EOL> if not category_config : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = [ product_key ] ) ) <EOL> deadend = category_config . get ( '<STR_LIT>' , False ) <EOL> topic = category_config . get ( '<STR_LIT>' ) <EOL> if topic : <EOL> html = None <EOL> articles , fallback = documents_for ( <EOL> locale = request . LANGUAGE_CODE , <EOL> products = [ product_obj ] , <EOL> topics = [ Topic . objects . get ( slug = topic , product = product_obj ) ] ) <EOL> else : <EOL> html = category_config . get ( '<STR_LIT:html>' ) <EOL> articles = category_config . get ( '<STR_LIT>' ) <EOL> else : <EOL> category_config = None <EOL> deadend = product_config . get ( '<STR_LIT>' , False ) if product_config else False <EOL> html = product_config . get ( '<STR_LIT:html>' ) if product_config else None <EOL> articles = None <EOL> if product_config : <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> else : <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> if request . MOBILE : <EOL> login_t = '<STR_LIT>' <EOL> else : <EOL> login_t = '<STR_LIT>' <EOL> if request . method == '<STR_LIT:GET>' : <EOL> search = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if search : <EOL> results = _search_suggestions ( <EOL> request , <EOL> search , <EOL> locale_or_default ( request . LANGUAGE_CODE ) , <EOL> [ product_config . get ( '<STR_LIT>' ) ] ) <EOL> tried_search = True <EOL> else : <EOL> results = [ ] <EOL> tried_search = False <EOL> if category_config : <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> if showform or request . GET . get ( '<STR_LIT>' ) : <EOL> if not request . user . is_authenticated ( ) : <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> login_form = AuthenticationForm ( ) <EOL> register_form = RegisterForm ( ) <EOL> return render ( request , login_t , { <EOL> '<STR_LIT>' : product_config , <EOL> '<STR_LIT>' : category_config , <EOL> '<STR_LIT:title>' : search , <EOL> '<STR_LIT>' : register_form , <EOL> '<STR_LIT>' : login_form } ) <EOL> form = NewQuestionForm ( <EOL> product = product_config , <EOL> category = category_config , <EOL> initial = { '<STR_LIT:title>' : search } ) <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> else : <EOL> form = None <EOL> if search : <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> return render ( request , template , { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : results , <EOL> '<STR_LIT>' : tried_search , <EOL> '<STR_LIT>' : config . products , <EOL> '<STR_LIT>' : product_config , <EOL> '<STR_LIT>' : category_config , <EOL> '<STR_LIT>' : html , <EOL> '<STR_LIT>' : articles , <EOL> '<STR_LIT>' : step , <EOL> '<STR_LIT>' : deadend , <EOL> '<STR_LIT:host>' : Site . objects . get_current ( ) . domain } ) <EOL> if not request . user . is_authenticated ( ) : <EOL> if request . POST . get ( '<STR_LIT>' ) : <EOL> login_form = handle_login ( request , only_active = False ) <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> register_form = RegisterForm ( ) <EOL> if login_form . is_valid ( ) : <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> else : <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> elif request . POST . get ( '<STR_LIT>' ) : <EOL> login_form = AuthenticationForm ( ) <EOL> register_form = handle_register ( <EOL> request = request , <EOL> text_template = '<STR_LIT>' , <EOL> html_template = '<STR_LIT>' , <EOL> subject = _ ( '<STR_LIT>' ) , <EOL> email_data = request . GET . get ( '<STR_LIT>' ) , <EOL> reg = '<STR_LIT>' ) <EOL> if register_form . is_valid ( ) : <EOL> user = auth . authenticate ( <EOL> username = request . POST . get ( '<STR_LIT:username>' ) , <EOL> password = request . POST . get ( '<STR_LIT:password>' ) ) <EOL> auth . login ( request , user ) <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> else : <EOL> message = _lazy ( '<STR_LIT>' ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT:message>' : message } , status = <NUM_LIT> ) <EOL> if request . user . is_authenticated ( ) : <EOL> url = urlparams ( request . get_full_path ( ) , step = '<STR_LIT>' ) <EOL> return HttpResponseRedirect ( url ) <EOL> else : <EOL> return render ( request , login_t , { <EOL> '<STR_LIT>' : product_config , <EOL> '<STR_LIT>' : category_config , <EOL> '<STR_LIT:title>' : request . POST . get ( '<STR_LIT:title>' ) , <EOL> '<STR_LIT>' : register_form , <EOL> '<STR_LIT>' : login_form } ) <EOL> form = NewQuestionForm ( product = product_config , category = category_config , data = request . POST ) <EOL> if '<STR_LIT>' in request . POST : <EOL> upload_imageattachment ( request , request . user ) <EOL> user_ct = ContentType . objects . get_for_model ( request . user ) <EOL> if form . is_valid ( ) and not is_ratelimited ( request , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> question = Question ( <EOL> creator = request . user , <EOL> title = form . cleaned_data [ '<STR_LIT:title>' ] , <EOL> content = form . cleaned_data [ '<STR_LIT:content>' ] , <EOL> locale = request . LANGUAGE_CODE ) <EOL> if product_obj : <EOL> question . product = product_obj <EOL> if category_config : <EOL> t = category_config . get ( '<STR_LIT>' ) <EOL> if t : <EOL> question . topic = Topic . objects . get ( slug = t , product = product_obj ) <EOL> question . save ( ) <EOL> qst_ct = ContentType . objects . get_for_model ( question ) <EOL> up_images = ImageAttachment . objects . filter ( creator = request . user , content_type = user_ct ) <EOL> up_images . update ( content_type = qst_ct , object_id = question . id ) <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> question . add_metadata ( ** form . cleaned_metadata ) <EOL> if product_config : <EOL> question . add_metadata ( product = product_config [ '<STR_LIT:key>' ] ) <EOL> if category_config : <EOL> question . add_metadata ( category = category_config [ '<STR_LIT:key>' ] ) <EOL> question . auto_tag ( ) <EOL> question_vote ( request , question . id ) <EOL> if request . user . is_active : <EOL> messages . add_message ( <EOL> request , messages . SUCCESS , <EOL> _ ( '<STR_LIT>' ) ) <EOL> request . session [ '<STR_LIT>' ] = False <EOL> url = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : question . id } ) <EOL> return HttpResponseRedirect ( url ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> if getattr ( request , '<STR_LIT>' , False ) : <EOL> raise PermissionDenied <EOL> images = ImageAttachment . objects . filter ( creator = request . user , <EOL> content_type = user_ct ) <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> return render ( request , template , { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : images , <EOL> '<STR_LIT>' : config . products , <EOL> '<STR_LIT>' : product_config , <EOL> '<STR_LIT>' : category_config , <EOL> '<STR_LIT>' : articles } ) <EOL> @ ssl_required <EOL> def aaq_step2 ( request , product_key ) : <EOL> """<STR_LIT>""" <EOL> return aaq ( request , product_key = product_key , step = <NUM_LIT:1> ) <EOL> @ ssl_required <EOL> def aaq_step3 ( request , product_key , category_key ) : <EOL> """<STR_LIT>""" <EOL> return aaq ( request , product_key = product_key , category_key = category_key , <EOL> step = <NUM_LIT:1> ) <EOL> @ ssl_required <EOL> def aaq_step4 ( request , product_key , category_key ) : <EOL> """<STR_LIT>""" <EOL> return aaq ( request , product_key = product_key , category_key = category_key , <EOL> step = <NUM_LIT:1> ) <EOL> @ ssl_required <EOL> def aaq_step5 ( request , product_key , category_key ) : <EOL> """<STR_LIT>""" <EOL> return aaq ( request , product_key = product_key , category_key = category_key , <EOL> showform = True , step = <NUM_LIT:3> ) <EOL> def aaq_confirm ( request ) : <EOL> """<STR_LIT>""" <EOL> if request . user . is_authenticated ( ) : <EOL> email = request . user . email <EOL> auth . logout ( request ) <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> else : <EOL> email = None <EOL> request . session [ '<STR_LIT>' ] = False <EOL> confirm_t = ( '<STR_LIT>' if request . MOBILE <EOL> else '<STR_LIT>' ) <EOL> return render ( request , confirm_t , { '<STR_LIT:email>' : email } ) <EOL> @ require_http_methods ( [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def edit_question ( request , question_id ) : <EOL> """<STR_LIT>""" <EOL> question = get_object_or_404 ( Question , pk = question_id ) <EOL> user = request . user <EOL> if not question . allows_edit ( user ) : <EOL> raise PermissionDenied <EOL> ct = ContentType . objects . get_for_model ( question ) <EOL> images = ImageAttachment . objects . filter ( content_type = ct , object_id = question . pk ) <EOL> if request . method == '<STR_LIT:GET>' : <EOL> initial = question . metadata . copy ( ) <EOL> initial . update ( title = question . title , content = question . content ) <EOL> form = EditQuestionForm ( product = question . product_config , <EOL> category = question . category_config , <EOL> initial = initial ) <EOL> else : <EOL> form = EditQuestionForm ( data = request . POST , <EOL> product = question . product_config , <EOL> category = question . category_config ) <EOL> upload_imageattachment ( request , question ) <EOL> if form . is_valid ( ) : <EOL> question . title = form . cleaned_data [ '<STR_LIT:title>' ] <EOL> question . content = form . cleaned_data [ '<STR_LIT:content>' ] <EOL> question . updated_by = user <EOL> question . save ( ) <EOL> question . clear_mutable_metadata ( ) <EOL> question . add_metadata ( ** form . cleaned_metadata ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : question . id } ) ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : question , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : images , <EOL> '<STR_LIT>' : question . product_config , <EOL> '<STR_LIT>' : question . category_config } ) <EOL> def _skip_answer_ratelimit ( request ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' in request . POST or '<STR_LIT>' in request . POST <EOL> @ require_POST <EOL> @ login_required <EOL> @ ratelimit ( '<STR_LIT>' , '<STR_LIT>' , skip_if = _skip_answer_ratelimit ) <EOL> @ ratelimit ( '<STR_LIT>' , '<STR_LIT>' , skip_if = _skip_answer_ratelimit ) <EOL> def reply ( request , question_id ) : <EOL> """<STR_LIT>""" <EOL> question = get_object_or_404 ( Question , pk = question_id , is_spam = False ) <EOL> answer_preview = None <EOL> if not question . allows_new_answer ( request . user ) : <EOL> raise PermissionDenied <EOL> form = AnswerForm ( request . POST ) <EOL> if '<STR_LIT>' in request . POST : <EOL> for image_id in request . POST . getlist ( '<STR_LIT>' ) : <EOL> ImageAttachment . objects . get ( pk = image_id ) . delete ( ) <EOL> return question_details ( request , question_id = question_id , form = form ) <EOL> if '<STR_LIT>' in request . POST : <EOL> upload_imageattachment ( request , request . user ) <EOL> return question_details ( request , question_id = question_id , form = form ) <EOL> if form . is_valid ( ) and not request . limited : <EOL> answer = Answer ( question = question , creator = request . user , <EOL> content = form . cleaned_data [ '<STR_LIT:content>' ] ) <EOL> if '<STR_LIT>' in request . POST : <EOL> answer_preview = answer <EOL> else : <EOL> answer . save ( ) <EOL> ans_ct = ContentType . objects . get_for_model ( answer ) <EOL> user_ct = ContentType . objects . get_for_model ( request . user ) <EOL> up_images = ImageAttachment . objects . filter ( creator = request . user , <EOL> content_type = user_ct ) <EOL> up_images . update ( content_type = ans_ct , object_id = answer . id ) <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in request . POST : <EOL> question . set_needs_info ( ) <EOL> elif '<STR_LIT>' in request . POST : <EOL> question . unset_needs_info ( ) <EOL> if Setting . get_for_user ( request . user , <EOL> '<STR_LIT>' ) : <EOL> QuestionReplyEvent . notify ( request . user , question ) <EOL> return HttpResponseRedirect ( answer . get_absolute_url ( ) ) <EOL> return question_details ( <EOL> request , question_id = question_id , form = form , <EOL> answer_preview = answer_preview ) <EOL> def solve ( request , question_id , answer_id ) : <EOL> """<STR_LIT>""" <EOL> question = get_object_or_404 ( Question , pk = question_id , is_spam = False ) <EOL> if not request . user . is_authenticated ( ) : <EOL> watch_secret = request . GET . get ( '<STR_LIT>' , None ) <EOL> try : <EOL> watch = Watch . objects . get ( secret = watch_secret , <EOL> event_type = '<STR_LIT>' , <EOL> user = question . creator ) <EOL> distinguishable_letters = '<STR_LIT>' <EOL> new_secret = '<STR_LIT>' . join ( random . choice ( distinguishable_letters ) <EOL> for x in xrange ( <NUM_LIT:10> ) ) <EOL> watch . update ( secret = new_secret ) <EOL> request . user = question . creator <EOL> except Watch . DoesNotExist : <EOL> return HttpResponseForbidden ( ) <EOL> answer = get_object_or_404 ( Answer , pk = answer_id , is_spam = False ) <EOL> if not question . allows_solve ( request . user ) : <EOL> raise PermissionDenied <EOL> if ( question . creator != request . user and <EOL> not request . user . has_perm ( '<STR_LIT>' ) ) : <EOL> return HttpResponseForbidden ( ) <EOL> if not question . solution : <EOL> question . set_solution ( answer , request . user ) <EOL> messages . add_message ( request , messages . SUCCESS , <EOL> _ ( '<STR_LIT>' ) ) <EOL> else : <EOL> messages . add_message ( request , messages . ERROR , <EOL> _ ( '<STR_LIT>' ) ) <EOL> return HttpResponseRedirect ( question . get_absolute_url ( ) ) <EOL> @ require_POST <EOL> @ login_required <EOL> def unsolve ( request , question_id , answer_id ) : <EOL> """<STR_LIT>""" <EOL> question = get_object_or_404 ( Question , pk = question_id ) <EOL> get_object_or_404 ( Answer , pk = answer_id ) <EOL> if not question . allows_unsolve ( request . user ) : <EOL> raise PermissionDenied <EOL> if ( question . creator != request . user and <EOL> not request . user . has_perm ( '<STR_LIT>' ) ) : <EOL> return HttpResponseForbidden ( ) <EOL> question . solution = None <EOL> question . save ( ) <EOL> question . remove_metadata ( '<STR_LIT>' ) <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> messages . add_message ( request , messages . SUCCESS , <EOL> _ ( "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( question . get_absolute_url ( ) ) <EOL> @ require_POST <EOL> @ csrf_exempt <EOL> @ ratelimit ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def question_vote ( request , question_id ) : <EOL> """<STR_LIT>""" <EOL> question = get_object_or_404 ( Question , pk = question_id , is_spam = False ) <EOL> if not question . editable : <EOL> raise PermissionDenied <EOL> if not question . has_voted ( request ) : <EOL> vote = QuestionVote ( question = question ) <EOL> if request . user . is_authenticated ( ) : <EOL> vote . creator = request . user <EOL> else : <EOL> vote . anonymous_id = request . anonymous . anonymous_id <EOL> if not request . limited : <EOL> vote . save ( ) <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> referrer = request . REQUEST . get ( '<STR_LIT>' ) <EOL> vote . add_metadata ( '<STR_LIT>' , referrer ) <EOL> if referrer == '<STR_LIT>' and '<STR_LIT>' in request . REQUEST : <EOL> vote . add_metadata ( '<STR_LIT>' , request . REQUEST . get ( '<STR_LIT>' ) ) <EOL> ua = request . META . get ( '<STR_LIT>' ) <EOL> if ua : <EOL> vote . add_metadata ( '<STR_LIT>' , ua ) <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> if request . is_ajax ( ) : <EOL> tmpl = '<STR_LIT>' <EOL> form = _init_watch_form ( request ) <EOL> html = render_to_string ( tmpl , { <EOL> '<STR_LIT>' : question , <EOL> '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : form , <EOL> } ) <EOL> return HttpResponse ( json . dumps ( { <EOL> '<STR_LIT:html>' : html , <EOL> '<STR_LIT>' : request . limited <EOL> } ) ) <EOL> return HttpResponseRedirect ( question . get_absolute_url ( ) ) <EOL> @ csrf_exempt <EOL> @ ratelimit ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def answer_vote ( request , question_id , answer_id ) : <EOL> """<STR_LIT>""" <EOL> answer = get_object_or_404 ( Answer , pk = answer_id , question = question_id , <EOL> is_spam = False , question__is_spam = False ) <EOL> if not answer . question . editable : <EOL> raise PermissionDenied <EOL> if request . limited : <EOL> if request . is_ajax ( ) : <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT>' : True } ) ) <EOL> else : <EOL> return HttpResponseRedirect ( answer . get_absolute_url ( ) ) <EOL> if not answer . has_voted ( request ) : <EOL> vote = AnswerVote ( answer = answer ) <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> vote . helpful = True <EOL> message = _ ( '<STR_LIT>' ) <EOL> else : <EOL> message = _ ( '<STR_LIT>' ) <EOL> if request . user . is_authenticated ( ) : <EOL> vote . creator = request . user <EOL> else : <EOL> vote . anonymous_id = request . anonymous . anonymous_id <EOL> vote . save ( ) <EOL> if '<STR_LIT>' in request . REQUEST : <EOL> referrer = request . REQUEST . get ( '<STR_LIT>' ) <EOL> vote . add_metadata ( '<STR_LIT>' , referrer ) <EOL> if referrer == '<STR_LIT>' and '<STR_LIT>' in request . REQUEST : <EOL> vote . add_metadata ( '<STR_LIT>' , request . REQUEST . get ( '<STR_LIT>' ) ) <EOL> ua = request . META . get ( '<STR_LIT>' ) <EOL> if ua : <EOL> vote . add_metadata ( '<STR_LIT>' , ua ) <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> else : <EOL> message = _ ( '<STR_LIT>' ) <EOL> if request . is_ajax ( ) : <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT:message>' : message } ) ) <EOL> return HttpResponseRedirect ( answer . get_absolute_url ( ) ) <EOL> @ permission_required ( '<STR_LIT>' ) <EOL> def add_tag ( request , question_id ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:GET>' : <EOL> return HttpResponseRedirect ( <EOL> reverse ( '<STR_LIT>' , args = [ question_id ] ) ) <EOL> try : <EOL> question , canonical_name = _add_tag ( request , question_id ) <EOL> except Tag . DoesNotExist : <EOL> template_data = _answers_data ( request , question_id ) <EOL> template_data [ '<STR_LIT>' ] = UNAPPROVED_TAG <EOL> template_data [ '<STR_LIT>' ] = request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return render ( <EOL> request , '<STR_LIT>' , template_data ) <EOL> if canonical_name : <EOL> question . clear_cached_tags ( ) <EOL> return HttpResponseRedirect ( <EOL> reverse ( '<STR_LIT>' , args = [ question_id ] ) ) <EOL> template_data = _answers_data ( request , question_id ) <EOL> template_data [ '<STR_LIT>' ] = NO_TAG <EOL> return render ( request , '<STR_LIT>' , template_data ) <EOL> @ permission_required ( '<STR_LIT>' ) <EOL> @ require_POST <EOL> def add_tag_async ( request , question_id ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> question , canonical_name = _add_tag ( request , question_id ) <EOL> except Tag . DoesNotExist : <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT:error>' : unicode ( UNAPPROVED_TAG ) } ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> status = <NUM_LIT> ) <EOL> if canonical_name : <EOL> question . clear_cached_tags ( ) <EOL> tag = Tag . objects . get ( name = canonical_name ) <EOL> tag_url = urlparams ( reverse ( <EOL> '<STR_LIT>' , args = [ question . product_slug ] ) , tagged = tag . slug ) <EOL> data = { '<STR_LIT>' : canonical_name , <EOL> '<STR_LIT>' : tag_url } <EOL> return HttpResponse ( json . dumps ( data ) , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT:error>' : unicode ( NO_TAG ) } ) , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> status = <NUM_LIT> ) <EOL> @ permission_required ( '<STR_LIT>' ) <EOL> @ require_POST <EOL> def remove_tag ( request , question_id ) : <EOL> """<STR_LIT>""" <EOL> prefix = '<STR_LIT>' <EOL> names = [ k for k in request . POST if k . startswith ( prefix ) ] <EOL> if names : <EOL> name = names [ <NUM_LIT:0> ] [ len ( prefix ) : ] <EOL> question = get_object_or_404 ( Question , pk = question_id ) <EOL> question . tags . remove ( name ) <EOL> question . clear_cached_tags ( ) <EOL> return HttpResponseRedirect ( <EOL> reverse ( '<STR_LIT>' , args = [ question_id ] ) ) <EOL> @ permission_required ( '<STR_LIT>' ) <EOL> @ require_POST <EOL> def remove_tag_async ( request , question_id ) : <EOL> """<STR_LIT>""" <EOL> name = request . POST . get ( '<STR_LIT:name>' ) <EOL> if name : <EOL> question = get_object_or_404 ( Question , pk = question_id ) <EOL> question . tags . remove ( name ) <EOL> question . clear_cached_tags ( ) <EOL> return HttpResponse ( '<STR_LIT:{}>' , content_type = '<STR_LIT:application/json>' ) <EOL> return HttpResponseBadRequest ( json . dumps ( { '<STR_LIT:error>' : unicode ( NO_TAG ) } ) , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> @ permission_required ( '<STR_LIT>' ) <EOL> @ require_POST <EOL> def mark_spam ( request ) : <EOL> """<STR_LIT>""" <EOL> if request . POST . get ( '<STR_LIT>' ) : <EOL> question_id = request . POST . get ( '<STR_LIT>' ) <EOL> obj = get_object_or_404 ( Question , pk = question_id ) <EOL> else : <EOL> answer_id = request . POST . get ( '<STR_LIT>' ) <EOL> obj = get_object_or_404 ( Answer , pk = answer_id ) <EOL> question_id = obj . question . id <EOL> obj . mark_as_spam ( request . user ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : question_id } ) ) <EOL> @ permission_required ( '<STR_LIT>' ) <EOL> @ require_POST <EOL> def unmark_spam ( request ) : <EOL> """<STR_LIT>""" <EOL> if request . POST . get ( '<STR_LIT>' ) : <EOL> question_id = request . POST . get ( '<STR_LIT>' ) <EOL> obj = get_object_or_404 ( Question , pk = question_id ) <EOL> else : <EOL> answer_id = request . POST . get ( '<STR_LIT>' ) <EOL> obj = get_object_or_404 ( Answer , pk = answer_id ) <EOL> question_id = obj . question . id <EOL> obj . is_spam = False <EOL> obj . marked_as_spam = None <EOL> obj . marked_as_spam_by = None <EOL> obj . save ( ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : question_id } ) ) <EOL> @ login_required <EOL> def delete_question ( request , question_id ) : <EOL> """<STR_LIT>""" <EOL> question = get_object_or_404 ( Question , pk = question_id ) <EOL> if not question . allows_delete ( request . user ) : <EOL> raise PermissionDenied <EOL> if request . method == '<STR_LIT:GET>' : <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : question } ) <EOL> product = question . product_slug <EOL> log . warning ( '<STR_LIT>' % <EOL> ( request . user , question . id ) ) <EOL> question . delete ( ) <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = [ product ] ) ) <EOL> @ login_required <EOL> def delete_answer ( request , question_id , answer_id ) : <EOL> """<STR_LIT>""" <EOL> answer = get_object_or_404 ( Answer , pk = answer_id , question = question_id ) <EOL> if not answer . allows_delete ( request . user ) : <EOL> raise PermissionDenied <EOL> if request . method == '<STR_LIT:GET>' : <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : answer } ) <EOL> log . warning ( '<STR_LIT>' % <EOL> ( request . user , answer . id ) ) <EOL> answer . delete ( ) <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , <EOL> args = [ question_id ] ) ) <EOL> @ require_POST <EOL> @ login_required <EOL> def lock_question ( request , question_id ) : <EOL> """<STR_LIT>""" <EOL> question = get_object_or_404 ( Question , pk = question_id ) <EOL> if not question . allows_lock ( request . user ) : <EOL> raise PermissionDenied <EOL> question . is_locked = not question . is_locked <EOL> log . info ( "<STR_LIT>" % <EOL> ( request . user , question . is_locked , question . id ) ) <EOL> question . save ( ) <EOL> if question . is_locked : <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> else : <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> return HttpResponseRedirect ( question . get_absolute_url ( ) ) <EOL> @ require_POST <EOL> @ login_required <EOL> def archive_question ( request , question_id ) : <EOL> """<STR_LIT>""" <EOL> question = get_object_or_404 ( Question , pk = question_id ) <EOL> if not question . allows_archive ( request . user ) : <EOL> raise PermissionDenied <EOL> question . is_archived = not question . is_archived <EOL> log . info ( "<STR_LIT>" % <EOL> ( request . user , question . is_archived , question . id ) ) <EOL> question . save ( ) <EOL> return HttpResponseRedirect ( question . get_absolute_url ( ) ) <EOL> @ login_required <EOL> def edit_answer ( request , question_id , answer_id ) : <EOL> """<STR_LIT>""" <EOL> answer = get_object_or_404 ( Answer , pk = answer_id , question = question_id ) <EOL> answer_preview = None <EOL> if not answer . allows_edit ( request . user ) : <EOL> raise PermissionDenied <EOL> upload_imageattachment ( request , answer ) <EOL> if request . method == '<STR_LIT:GET>' : <EOL> form = AnswerForm ( { '<STR_LIT:content>' : answer . content } ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , '<STR_LIT>' : answer } ) <EOL> form = AnswerForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> answer . content = form . cleaned_data [ '<STR_LIT:content>' ] <EOL> answer . updated_by = request . user <EOL> if '<STR_LIT>' in request . POST : <EOL> answer . updated = datetime . now ( ) <EOL> answer_preview = answer <EOL> else : <EOL> log . warning ( '<STR_LIT>' % <EOL> ( request . user , answer . id ) ) <EOL> answer . save ( ) <EOL> return HttpResponseRedirect ( answer . get_absolute_url ( ) ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , '<STR_LIT>' : answer , <EOL> '<STR_LIT>' : answer_preview } ) <EOL> @ require_POST <EOL> @ anonymous_csrf <EOL> def watch_question ( request , question_id ) : <EOL> """<STR_LIT>""" <EOL> question = get_object_or_404 ( Question , pk = question_id , is_spam = False ) <EOL> form = WatchQuestionForm ( request . user , request . POST ) <EOL> msg = None <EOL> if form . is_valid ( ) : <EOL> user_or_email = ( request . user if request . user . is_authenticated ( ) <EOL> else form . cleaned_data [ '<STR_LIT:email>' ] ) <EOL> try : <EOL> if form . cleaned_data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> QuestionReplyEvent . notify ( user_or_email , question ) <EOL> else : <EOL> QuestionSolvedEvent . notify ( user_or_email , question ) <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> except ActivationRequestFailed : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> if request . is_ajax ( ) : <EOL> if form . is_valid ( ) : <EOL> msg = msg or ( _ ( '<STR_LIT>' ) if <EOL> request . user . is_authenticated ( ) else <EOL> _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT:message>' : msg } ) ) <EOL> if request . POST . get ( '<STR_LIT>' ) : <EOL> tmpl = '<STR_LIT>' <EOL> else : <EOL> tmpl = '<STR_LIT>' <EOL> html = render_to_string ( tmpl , <EOL> context = { '<STR_LIT>' : question , '<STR_LIT>' : form } , <EOL> request = request ) <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT:html>' : html } ) ) <EOL> if msg : <EOL> messages . add_message ( request , messages . ERROR , msg ) <EOL> return HttpResponseRedirect ( question . get_absolute_url ( ) ) <EOL> @ require_POST <EOL> @ login_required <EOL> def unwatch_question ( request , question_id ) : <EOL> """<STR_LIT>""" <EOL> question = get_object_or_404 ( Question , pk = question_id ) <EOL> QuestionReplyEvent . stop_notifying ( request . user , question ) <EOL> QuestionSolvedEvent . stop_notifying ( request . user , question ) <EOL> return HttpResponseRedirect ( question . get_absolute_url ( ) ) <EOL> @ require_GET <EOL> def unsubscribe_watch ( request , watch_id , secret ) : <EOL> """<STR_LIT>""" <EOL> watch = get_object_or_404 ( Watch , pk = watch_id ) <EOL> question = watch . content_object <EOL> success = False <EOL> if watch . secret == secret and isinstance ( question , Question ) : <EOL> user_or_email = watch . user or watch . email <EOL> QuestionReplyEvent . stop_notifying ( user_or_email , question ) <EOL> QuestionSolvedEvent . stop_notifying ( user_or_email , question ) <EOL> success = True <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : question , '<STR_LIT:success>' : success } ) <EOL> @ require_GET <EOL> def activate_watch ( request , watch_id , secret ) : <EOL> """<STR_LIT>""" <EOL> watch = get_object_or_404 ( Watch , pk = watch_id ) <EOL> question = watch . content_object <EOL> if watch . secret == secret and isinstance ( question , Question ) : <EOL> watch . activate ( ) . save ( ) <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : question , <EOL> '<STR_LIT>' : reverse ( '<STR_LIT>' , <EOL> args = [ watch_id , secret ] ) , <EOL> '<STR_LIT>' : watch . is_active } ) <EOL> @ login_required <EOL> @ require_POST <EOL> def answer_preview_async ( request ) : <EOL> """<STR_LIT>""" <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> answer = Answer ( creator = request . user , <EOL> content = request . POST . get ( '<STR_LIT:content>' , '<STR_LIT>' ) ) <EOL> template = '<STR_LIT>' <EOL> return render ( request , template , { '<STR_LIT>' : answer } ) <EOL> @ mobile_template ( '<STR_LIT>' ) <EOL> def marketplace ( request , template = None ) : <EOL> """<STR_LIT>""" <EOL> return render ( request , template , { <EOL> '<STR_LIT>' : MARKETPLACE_CATEGORIES } ) <EOL> ZENDESK_ERROR_MESSAGE = _lazy ( <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' ) <EOL> @ anonymous_csrf <EOL> @ mobile_template ( '<STR_LIT>' ) <EOL> def marketplace_category ( request , category_slug , template = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> category_name = MARKETPLACE_CATEGORIES [ category_slug ] <EOL> except KeyError : <EOL> raise Http404 <EOL> error_message = None <EOL> if request . method == '<STR_LIT:GET>' : <EOL> form = MarketplaceAaqForm ( request . user ) <EOL> else : <EOL> form = MarketplaceAaqForm ( request . user , request . POST ) <EOL> if form . is_valid ( ) : <EOL> try : <EOL> form . submit_ticket ( ) <EOL> return HttpResponseRedirect ( <EOL> reverse ( '<STR_LIT>' ) ) <EOL> except ZendeskError : <EOL> error_message = ZENDESK_ERROR_MESSAGE <EOL> return render ( request , template , { <EOL> '<STR_LIT>' : category_name , <EOL> '<STR_LIT>' : category_slug , <EOL> '<STR_LIT>' : MARKETPLACE_CATEGORIES , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : error_message } ) <EOL> @ anonymous_csrf <EOL> @ mobile_template ( '<STR_LIT>' ) <EOL> def marketplace_refund ( request , template ) : <EOL> """<STR_LIT>""" <EOL> error_message = None <EOL> if request . method == '<STR_LIT:GET>' : <EOL> form = MarketplaceRefundForm ( request . user ) <EOL> else : <EOL> form = MarketplaceRefundForm ( request . user , request . POST ) <EOL> if form . is_valid ( ) : <EOL> try : <EOL> form . submit_ticket ( ) <EOL> return HttpResponseRedirect ( <EOL> reverse ( '<STR_LIT>' ) ) <EOL> except ZendeskError : <EOL> error_message = ZENDESK_ERROR_MESSAGE <EOL> return render ( request , template , { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : error_message } ) <EOL> @ anonymous_csrf <EOL> @ mobile_template ( '<STR_LIT>' ) <EOL> def marketplace_developer_request ( request , template ) : <EOL> """<STR_LIT>""" <EOL> error_message = None <EOL> if request . method == '<STR_LIT:GET>' : <EOL> form = MarketplaceDeveloperRequestForm ( request . user ) <EOL> else : <EOL> form = MarketplaceDeveloperRequestForm ( request . user , request . POST ) <EOL> if form . is_valid ( ) : <EOL> try : <EOL> form . submit_ticket ( ) <EOL> return HttpResponseRedirect ( <EOL> reverse ( '<STR_LIT>' ) ) <EOL> except ZendeskError : <EOL> error_message = ZENDESK_ERROR_MESSAGE <EOL> return render ( request , template , { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : error_message } ) <EOL> @ mobile_template ( '<STR_LIT>' ) <EOL> def marketplace_success ( request , template = None ) : <EOL> """<STR_LIT>""" <EOL> return render ( request , template ) <EOL> def stats_topic_data ( bucket_days , start , end , locale = None , product = None ) : <EOL> """<STR_LIT>""" <EOL> search = Sphilastic ( QuestionMappingType ) <EOL> bucket = <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * bucket_days <EOL> if isinstance ( start , date ) : <EOL> start = int ( time . mktime ( start . timetuple ( ) ) ) <EOL> if isinstance ( end , date ) : <EOL> end = int ( time . mktime ( end . timetuple ( ) ) ) <EOL> f = F ( model = '<STR_LIT>' ) <EOL> f &= F ( created__gt = start ) <EOL> f &= F ( created__lt = end ) <EOL> if locale : <EOL> f &= F ( question_locale = locale ) <EOL> if product : <EOL> f &= F ( product = product . slug ) <EOL> topics = Topic . objects . values ( '<STR_LIT>' , '<STR_LIT:title>' ) <EOL> facets = { } <EOL> for topic in topics : <EOL> filters = search . _process_filters ( [ f & F ( topic = topic [ '<STR_LIT>' ] ) ] ) <EOL> facets [ topic [ '<STR_LIT:title>' ] ] = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : bucket , '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : filters <EOL> } <EOL> search = search . facet_raw ( ** facets ) . values_dict ( ) <EOL> try : <EOL> histograms_data = search . facet_counts ( ) <EOL> except ES_EXCEPTIONS : <EOL> return [ ] <EOL> for series in histograms_data . itervalues ( ) : <EOL> if series [ '<STR_LIT>' ] : <EOL> earliest_point = series [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:key>' ] <EOL> break <EOL> else : <EOL> return [ ] <EOL> latest_point = earliest_point <EOL> interim_data = { } <EOL> for key , data in histograms_data . iteritems ( ) : <EOL> if not data : <EOL> continue <EOL> for point in data : <EOL> timestamp = point [ '<STR_LIT:key>' ] <EOL> value = point [ '<STR_LIT:count>' ] <EOL> earliest_point = min ( earliest_point , timestamp ) <EOL> latest_point = max ( latest_point , timestamp ) <EOL> datum = interim_data . get ( timestamp , { '<STR_LIT:date>' : timestamp } ) <EOL> datum [ key ] = value <EOL> interim_data [ timestamp ] = datum <EOL> timestamp = earliest_point <EOL> while timestamp <= latest_point : <EOL> datum = interim_data . get ( timestamp , { '<STR_LIT:date>' : timestamp } ) <EOL> for key in histograms_data . iterkeys ( ) : <EOL> if key not in datum : <EOL> datum [ key ] = <NUM_LIT:0> <EOL> timestamp += bucket <EOL> return interim_data . values ( ) <EOL> def metrics ( request , locale_code = None ) : <EOL> """<STR_LIT>""" <EOL> template = '<STR_LIT>' <EOL> product = request . GET . get ( '<STR_LIT>' ) <EOL> if product : <EOL> product = get_object_or_404 ( Product , slug = product ) <EOL> form = StatsForm ( request . GET ) <EOL> if form . is_valid ( ) : <EOL> bucket_days = form . cleaned_data [ '<STR_LIT>' ] <EOL> start = form . cleaned_data [ '<STR_LIT:start>' ] <EOL> end = form . cleaned_data [ '<STR_LIT:end>' ] <EOL> else : <EOL> bucket_days = <NUM_LIT:1> <EOL> start = date . today ( ) - timedelta ( days = <NUM_LIT:30> ) <EOL> end = date . today ( ) <EOL> graph_data = stats_topic_data ( bucket_days , start , end , locale_code , product ) <EOL> for group in graph_data : <EOL> for name , count in group . items ( ) : <EOL> if count == <NUM_LIT:0> : <EOL> del group [ name ] <EOL> data = { <EOL> '<STR_LIT>' : graph_data , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : locale_code , <EOL> '<STR_LIT>' : product , <EOL> '<STR_LIT>' : Product . objects . filter ( visible = True ) , <EOL> } <EOL> return render ( request , template , data ) <EOL> @ require_POST <EOL> @ permission_required ( '<STR_LIT>' ) <EOL> def screen_share ( request , question_id ) : <EOL> question = get_object_or_404 ( Question , pk = question_id , is_spam = False ) <EOL> if not question . allows_new_answer ( request . user ) : <EOL> raise PermissionDenied <EOL> content = _ ( u"<STR_LIT>" <EOL> u"<STR_LIT>" ) <EOL> answer = Answer ( question = question , creator = request . user , <EOL> content = content . format ( user = display_name ( question . creator ) ) ) <EOL> answer . save ( ) <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> question . add_metadata ( screen_sharing = '<STR_LIT:true>' ) <EOL> if Setting . get_for_user ( request . user , '<STR_LIT>' ) : <EOL> QuestionReplyEvent . notify ( request . user , question ) <EOL> message = render_to_string ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : display_name ( question . creator ) , '<STR_LIT>' : display_name ( request . user ) } ) <EOL> return HttpResponseRedirect ( '<STR_LIT>' % ( reverse ( '<STR_LIT>' ) , <EOL> question . creator . username , <EOL> message ) ) <EOL> def _search_suggestions ( request , text , locale , product_slugs ) : <EOL> """<STR_LIT>""" <EOL> question_s = QuestionMappingType . search ( ) <EOL> wiki_s = DocumentMappingType . search ( ) <EOL> WIKI_RESULTS = QUESTIONS_RESULTS = <NUM_LIT:3> <EOL> default_categories = settings . SEARCH_DEFAULT_CATEGORIES <EOL> if product_slugs : <EOL> wiki_s = wiki_s . filter ( product__in = product_slugs ) <EOL> question_s = question_s . filter ( product__in = product_slugs ) <EOL> results = [ ] <EOL> try : <EOL> query = dict ( ( '<STR_LIT>' % field , text ) <EOL> for field in DocumentMappingType . get_query_fields ( ) ) <EOL> query . update ( dict ( ( '<STR_LIT>' % field , text ) <EOL> for field in DocumentMappingType . get_query_fields ( ) ) ) <EOL> query = es_query_with_analyzer ( query , locale ) <EOL> filter = F ( ) <EOL> filter |= F ( document_locale = locale ) <EOL> filter |= F ( document_locale = settings . WIKI_DEFAULT_LANGUAGE ) <EOL> filter &= F ( document_category__in = default_categories ) <EOL> filter &= F ( document_is_archived = False ) <EOL> raw_results = ( <EOL> wiki_s . filter ( filter ) <EOL> . query ( or_ = query ) <EOL> . values_list ( '<STR_LIT:id>' ) [ : WIKI_RESULTS ] ) <EOL> raw_results = [ result [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] for result in raw_results ] <EOL> for id_ in raw_results : <EOL> try : <EOL> doc = ( Document . objects . select_related ( '<STR_LIT>' ) <EOL> . get ( pk = id_ ) ) <EOL> results . append ( { <EOL> '<STR_LIT>' : clean_excerpt ( <EOL> doc . current_revision . summary ) , <EOL> '<STR_LIT:url>' : doc . get_absolute_url ( ) , <EOL> '<STR_LIT:title>' : doc . title , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:object>' : doc , <EOL> } ) <EOL> except Document . DoesNotExist : <EOL> pass <EOL> query = dict ( ( '<STR_LIT>' % field , text ) <EOL> for field in QuestionMappingType . get_query_fields ( ) ) <EOL> query . update ( dict ( ( '<STR_LIT>' % field , text ) <EOL> for field in QuestionMappingType . get_query_fields ( ) ) ) <EOL> question_filter = F ( question_locale = locale ) <EOL> question_filter |= F ( question_locale = settings . WIKI_DEFAULT_LANGUAGE ) <EOL> question_filter &= F ( question_is_archived = False ) <EOL> raw_results = ( question_s . query ( or_ = query ) <EOL> . filter ( question_filter ) <EOL> . values_list ( '<STR_LIT:id>' ) [ : QUESTIONS_RESULTS ] ) <EOL> raw_results = [ result [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] for result in raw_results ] <EOL> for id_ in raw_results : <EOL> try : <EOL> q = Question . objects . get ( pk = id_ ) <EOL> results . append ( { <EOL> '<STR_LIT>' : clean_excerpt ( q . content [ <NUM_LIT:0> : <NUM_LIT> ] ) , <EOL> '<STR_LIT:url>' : q . get_absolute_url ( ) , <EOL> '<STR_LIT:title>' : q . title , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:object>' : q , <EOL> '<STR_LIT>' : q . updated , <EOL> '<STR_LIT>' : q . is_solved , <EOL> '<STR_LIT>' : q . num_answers , <EOL> '<STR_LIT>' : q . num_votes , <EOL> '<STR_LIT>' : q . num_votes_past_week <EOL> } ) <EOL> except Question . DoesNotExist : <EOL> pass <EOL> except ES_EXCEPTIONS as exc : <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> log . debug ( exc ) <EOL> return results <EOL> def _answers_data ( request , question_id , form = None , watch_form = None , <EOL> answer_preview = None ) : <EOL> """<STR_LIT>""" <EOL> question = get_object_or_404 ( Question , pk = question_id ) <EOL> answers_ = question . answers . all ( ) <EOL> if not request . user . has_perm ( '<STR_LIT>' ) : <EOL> answers_ = answers_ . filter ( is_spam = False ) <EOL> if not request . MOBILE : <EOL> answers_ = paginate ( request , answers_ , <EOL> per_page = config . ANSWERS_PER_PAGE ) <EOL> feed_urls = ( ( reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : question_id } ) , <EOL> AnswersFeed ( ) . title ( question ) ) , ) <EOL> frequencies = dict ( FREQUENCY_CHOICES ) <EOL> is_watching_question = ( <EOL> request . user . is_authenticated ( ) and ( <EOL> QuestionReplyEvent . is_notifying ( request . user , question ) or <EOL> QuestionSolvedEvent . is_notifying ( request . user , question ) ) ) <EOL> return { '<STR_LIT>' : question , <EOL> '<STR_LIT>' : answers_ , <EOL> '<STR_LIT>' : form or AnswerForm ( ) , <EOL> '<STR_LIT>' : answer_preview , <EOL> '<STR_LIT>' : watch_form or _init_watch_form ( request , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : feed_urls , <EOL> '<STR_LIT>' : frequencies , <EOL> '<STR_LIT>' : is_watching_question , <EOL> '<STR_LIT>' : request . user . has_perm ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . user . has_perm ( '<STR_LIT>' ) } <EOL> def _add_tag ( request , question_id ) : <EOL> """<STR_LIT>""" <EOL> tag_name = request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) . strip ( ) <EOL> if tag_name : <EOL> question = get_object_or_404 ( Question , pk = question_id ) <EOL> try : <EOL> canonical_name = add_existing_tag ( tag_name , question . tags ) <EOL> except Tag . DoesNotExist : <EOL> if request . user . has_perm ( '<STR_LIT>' ) : <EOL> question . tags . add ( tag_name ) <EOL> canonical_name = tag_name <EOL> else : <EOL> raise <EOL> tag_added . send ( sender = Question , question_id = question . id , <EOL> tag_name = canonical_name ) <EOL> return question , canonical_name <EOL> return None , None <EOL> def _init_watch_form ( request , event_type = '<STR_LIT>' ) : <EOL> initial = { '<STR_LIT>' : event_type } <EOL> return WatchQuestionForm ( request . user , initial = initial ) </s>
