def revision(request, document_slug, revision_id): """View a wiki document revision.""" rev = get_object_or_404(Revision, pk=revision_id, document__slug=document_slug) data = {'document': rev.document, 'revision': rev} data.update(showfor_data()) return render(request, 'wiki/revision.html', data)
def preview_revision(request): """Create an HTML fragment preview of the posted wiki syntax.""" wiki_content = request.POST.get('content', '') statsd.incr('wiki.preview') # TODO: Get doc ID from JSON. data = {'content': wiki_to_html(wiki_content, request.LANGUAGE_CODE)} data.update(showfor_data()) return render(request, 'wiki/preview.html', data)
def test_all_versions(self): """Test that products with visible=False are in the showfor data.""" prod = ProductFactory() VersionFactory(visible=True, product=prod) VersionFactory(visible=False, product=prod) data = showfor_data([prod]) eq_(len(data['versions'][prod.slug]), 2)
def test_all_versions(self): """Test that products with visible=False are in the showfor data.""" prod = ProductFactory() VersionFactory(visible=True, product=prod) VersionFactory(visible=False, product=prod) data = showfor_data([prod]) eq_(len(data["versions"][prod.slug]), 2)
def test_all_versions(self): """Test that products with visible=False are in the showfor data.""" prod = product(save=True) v1 = version(visible=True, product=prod, save=True) v2 = version(visible=False, product=prod, save=True) data = showfor_data([prod]) eq_(len(data["versions"][prod.slug]), 2)
def test_all_versions(self): """Test that products with visible=False are in the showfor data.""" prod = product(save=True) version(visible=True, product=prod, save=True) version(visible=False, product=prod, save=True) data = showfor_data([prod]) eq_(len(data['versions'][prod.slug]), 2)
def document(request, document_slug, template=None): """View a wiki document.""" fallback_reason = None # If a slug isn't available in the requested locale, fall back to en-US: try: doc = Document.objects.get(locale=request.LANGUAGE_CODE, slug=document_slug) if (not doc.current_revision and doc.parent and doc.parent.current_revision): # This is a translation but its current_revision is None # and OK to fall back to parent (parent is approved). fallback_reason = 'translation_not_approved' elif not doc.current_revision: # No current_revision, no parent with current revision, so # nothing to show. fallback_reason = 'no_content' except Document.DoesNotExist: # Look in default language: doc = get_object_or_404(Document, locale=settings.WIKI_DEFAULT_LANGUAGE, slug=document_slug) # If there's a translation to the requested locale, take it: translation = doc.translated_to(request.LANGUAGE_CODE) if translation: url = translation.get_absolute_url() url = urlparams(url, query_dict=request.GET) return HttpResponseRedirect(url) elif doc.current_revision: # There is no translation # and OK to fall back to parent (parent is approved). fallback_reason = 'no_translation' # Obey explicit redirect pages: # Don't redirect on redirect=no (like Wikipedia), so we can link from a # redirected-to-page back to a "Redirected from..." link, so you can edit # the redirect. redirect_url = (None if request.GET.get('redirect') == 'no' else doc.redirect_url(request.LANGUAGE_CODE)) if redirect_url: url = urlparams(redirect_url, query_dict=request.GET, redirectslug=doc.slug, redirectlocale=doc.locale) return HttpResponseRedirect(url) # Get "redirected from" doc if we were redirected: redirect_slug = request.GET.get('redirectslug') redirect_locale = request.GET.get('redirectlocale') redirected_from = None if redirect_slug and redirect_locale: try: redirected_from = Document.objects.get(locale=redirect_locale, slug=redirect_slug) except Document.DoesNotExist: pass related_documents = doc.related_documents related_questions = doc.related_questions contributors = doc.contributors.all() products = doc.get_products() if len(products) < 1: product = Product.objects.filter(visible=True)[0] else: product = products[0] topics = Topic.objects.filter(product=product, visible=True, parent=None) ga_push = [] if fallback_reason is not None: ga_push.append([ '_trackEvent', 'Incomplete L10n', 'Not Localized', '%s/%s' % (doc.slug, request.LANGUAGE_CODE) ]) elif doc.is_outdated(): ga_push.append([ '_trackEvent', 'Incomplete L10n', 'Not Updated', '%s/%s' % (doc.parent.slug, request.LANGUAGE_CODE) ]) hide_voting = False if (doc.category == TEMPLATES_CATEGORY or waffle.switch_is_active('hide-voting')): hide_voting = True data = { 'document': doc, 'redirected_from': redirected_from, 'related_documents': related_documents, 'related_questions': related_questions, 'contributors': contributors, 'fallback_reason': fallback_reason, 'is_aoa_referral': request.GET.get('ref') == 'aoa', 'topics': topics, 'product': product, 'products': products, 'hide_voting': hide_voting, 'ga_push': ga_push } data.update(showfor_data(products)) return render(request, template, data)
def review_revision(request, document_slug, revision_id): """Review a revision of a wiki document.""" rev = get_object_or_404(Revision, pk=revision_id, document__slug=document_slug) doc = rev.document if not doc.allows(request.user, 'review_revision'): raise PermissionDenied form = ReviewForm( initial={ 'needs_change': doc.needs_change, 'needs_change_comment': doc.needs_change_comment }) # Don't ask significance if this doc is a translation or if it has no # former approved versions: should_ask_significance = not doc.parent and doc.current_revision based_on_revs = doc.revisions.all() last_approved_date = getattr(doc.current_revision, 'created', datetime.fromordinal(1)) based_on_revs = based_on_revs.filter(created__gt=last_approved_date) revision_contributors = list( set(based_on_revs.values_list('creator__username', flat=True))) # Don't include the reviewer in the recent contributors list. if request.user.username in revision_contributors: revision_contributors.remove(request.user.username) if request.method == 'POST': form = ReviewForm(request.POST) if form.is_valid() and not rev.reviewed: # Don't allow revisions to be reviewed twice rev.is_approved = 'approve' in request.POST rev.reviewer = request.user rev.reviewed = datetime.now() if should_ask_significance and form.cleaned_data['significance']: rev.significance = form.cleaned_data['significance'] # If document is localizable and revision was approved and # user has permission, set the is_ready_for_localization value. if (doc.allows(request.user, 'mark_ready_for_l10n') and rev.is_approved and rev.can_be_readied_for_localization()): rev.is_ready_for_localization = form.cleaned_data[ 'is_ready_for_localization'] # If the revision is ready for l10n, store the date # and the user. if rev.is_ready_for_localization: rev.readied_for_localization = rev.reviewed rev.readied_for_localization_by = rev.reviewer rev.save() # Update the needs change bit (if approved, default language and # user has permission). if (doc.locale == settings.WIKI_DEFAULT_LANGUAGE and doc.allows(request.user, 'edit_needs_change') and rev.is_approved): doc.needs_change = form.cleaned_data['needs_change'] doc.needs_change_comment = \ form.cleaned_data['needs_change_comment'] doc.save() # Send notifications of approvedness and readiness: if rev.is_ready_for_localization or rev.is_approved: events = [ApproveRevisionInLocaleEvent(rev)] if rev.is_ready_for_localization: events.append(ReadyRevisionEvent(rev)) ApprovedOrReadyUnion(*events).fire( exclude=[rev.creator, request.user]) # Send an email (not really a "notification" in the sense that # there's a Watch table entry) to revision creator. msg = form.cleaned_data['comment'] send_reviewed_notification.delay(rev, doc, msg) send_contributor_notification(based_on_revs, rev, doc, msg) # Schedule KB rebuild? statsd.incr('wiki.review') schedule_rebuild_kb() return HttpResponseRedirect( reverse('wiki.document_revisions', args=[document_slug])) if doc.parent: # A translation # For diffing the based_on revision against, to help the user see if he # translated all the recent changes: parent_revision = (rev.based_on or doc.parent.localizable_or_latest_revision()) template = 'wiki/review_translation.html' else: parent_revision = None template = 'wiki/review_revision.html' data = { 'revision': rev, 'document': doc, 'form': form, 'parent_revision': parent_revision, 'revision_contributors': list(revision_contributors), 'should_ask_significance': should_ask_significance } data.update(showfor_data()) return render(request, template, data)
def document(request, document_slug, template=None): """View a wiki document.""" fallback_reason = None # If a slug isn't available in the requested locale, fall back to en-US: try: doc = Document.objects.get(locale=request.LANGUAGE_CODE, slug=document_slug) if (not doc.current_revision and doc.parent and doc.parent.current_revision): # This is a translation but its current_revision is None # and OK to fall back to parent (parent is approved). fallback_reason = 'translation_not_approved' elif not doc.current_revision: # No current_revision, no parent with current revision, so # nothing to show. fallback_reason = 'no_content' except Document.DoesNotExist: # Look in default language: doc = get_object_or_404(Document, locale=settings.WIKI_DEFAULT_LANGUAGE, slug=document_slug) # If there's a translation to the requested locale, take it: translation = doc.translated_to(request.LANGUAGE_CODE) if translation: url = translation.get_absolute_url() url = urlparams(url, query_dict=request.GET) return HttpResponseRedirect(url) elif doc.current_revision: # There is no translation # and OK to fall back to parent (parent is approved). fallback_reason = 'no_translation' # Obey explicit redirect pages: # Don't redirect on redirect=no (like Wikipedia), so we can link from a # redirected-to-page back to a "Redirected from..." link, so you can edit # the redirect. redirect_url = (None if request.GET.get('redirect') == 'no' else doc.redirect_url(request.LANGUAGE_CODE)) if redirect_url: url = urlparams(redirect_url, query_dict=request.GET, redirectslug=doc.slug, redirectlocale=doc.locale) return HttpResponseRedirect(url) # Get "redirected from" doc if we were redirected: redirect_slug = request.GET.get('redirectslug') redirect_locale = request.GET.get('redirectlocale') redirected_from = None if redirect_slug and redirect_locale: try: redirected_from = Document.objects.get(locale=redirect_locale, slug=redirect_slug) except Document.DoesNotExist: pass related_documents = doc.related_documents related_questions = doc.related_questions contributors = doc.contributors.all() products = doc.get_products() if len(products) < 1: product = Product.objects.filter(visible=True)[0] else: product = products[0] topics = Topic.objects.filter(product=product, visible=True, parent=None) ga_push = [] if fallback_reason is not None: ga_push.append( ['_trackEvent', 'Incomplete L10n', 'Not Localized', '%s/%s' % (doc.slug, request.LANGUAGE_CODE)]) elif doc.is_outdated(): ga_push.append( ['_trackEvent', 'Incomplete L10n', 'Not Updated', '%s/%s' % (doc.parent.slug, request.LANGUAGE_CODE)]) hide_voting = False if (doc.category == TEMPLATES_CATEGORY or waffle.switch_is_active('hide-voting')): hide_voting = True data = {'document': doc, 'redirected_from': redirected_from, 'related_documents': related_documents, 'related_questions': related_questions, 'contributors': contributors, 'fallback_reason': fallback_reason, 'is_aoa_referral': request.GET.get('ref') == 'aoa', 'topics': topics, 'product': product, 'products': products, 'hide_voting': hide_voting, 'ga_push': ga_push} data.update(showfor_data(products)) return render(request, template, data)
def review_revision(request, document_slug, revision_id): """Review a revision of a wiki document.""" rev = get_object_or_404(Revision, pk=revision_id, document__slug=document_slug) doc = rev.document if not doc.allows(request.user, 'review_revision'): raise PermissionDenied form = ReviewForm( initial={'needs_change': doc.needs_change, 'needs_change_comment': doc.needs_change_comment}) # Don't ask significance if this doc is a translation or if it has no # former approved versions: should_ask_significance = not doc.parent and doc.current_revision based_on_revs = doc.revisions.all() last_approved_date = getattr(doc.current_revision, 'created', datetime.fromordinal(1)) based_on_revs = based_on_revs.filter(created__gt=last_approved_date) revision_contributors = list(set( based_on_revs.values_list('creator__username', flat=True))) # Don't include the reviewer in the recent contributors list. if request.user.username in revision_contributors: revision_contributors.remove(request.user.username) if request.method == 'POST': form = ReviewForm(request.POST) if form.is_valid() and not rev.reviewed: # Don't allow revisions to be reviewed twice rev.is_approved = 'approve' in request.POST rev.reviewer = request.user rev.reviewed = datetime.now() if should_ask_significance and form.cleaned_data['significance']: rev.significance = form.cleaned_data['significance'] # If document is localizable and revision was approved and # user has permission, set the is_ready_for_localization value. if (doc.allows(request.user, 'mark_ready_for_l10n') and rev.is_approved and rev.can_be_readied_for_localization()): rev.is_ready_for_localization = form.cleaned_data[ 'is_ready_for_localization'] # If the revision is ready for l10n, store the date # and the user. if rev.is_ready_for_localization: rev.readied_for_localization = rev.reviewed rev.readied_for_localization_by = rev.reviewer rev.save() # Update the needs change bit (if approved, default language and # user has permission). if (doc.locale == settings.WIKI_DEFAULT_LANGUAGE and doc.allows(request.user, 'edit_needs_change') and rev.is_approved): doc.needs_change = form.cleaned_data['needs_change'] doc.needs_change_comment = \ form.cleaned_data['needs_change_comment'] doc.save() # Send notifications of approvedness and readiness: if rev.is_ready_for_localization or rev.is_approved: events = [ApproveRevisionInLocaleEvent(rev)] if rev.is_ready_for_localization: events.append(ReadyRevisionEvent(rev)) ApprovedOrReadyUnion(*events).fire(exclude=[rev.creator, request.user]) # Send an email (not really a "notification" in the sense that # there's a Watch table entry) to revision creator. msg = form.cleaned_data['comment'] send_reviewed_notification.delay(rev, doc, msg) send_contributor_notification(based_on_revs, rev, doc, msg) # Schedule KB rebuild? statsd.incr('wiki.review') schedule_rebuild_kb() return HttpResponseRedirect(reverse('wiki.document_revisions', args=[document_slug])) if doc.parent: # A translation # For diffing the based_on revision against, to help the user see if he # translated all the recent changes: parent_revision = (rev.based_on or doc.parent.localizable_or_latest_revision()) template = 'wiki/review_translation.html' else: parent_revision = None template = 'wiki/review_revision.html' data = {'revision': rev, 'document': doc, 'form': form, 'parent_revision': parent_revision, 'revision_contributors': list(revision_contributors), 'should_ask_significance': should_ask_significance} data.update(showfor_data()) return render(request, template, data)