Esempio n. 1
0
def product_landing(request, slug):
    """The product landing page."""
    product = get_object_or_404(Product, slug=slug)
    user = request.user
    template = "products/product.html"

    if request.is_ajax():
        # Return a list of topics/subtopics for the product
        topic_list = list()
        for t in Topic.objects.filter(product=product, visible=True):
            topic_list.append({"id": t.id, "title": t.title})
        return HttpResponse(
            json.dumps({"topics": topic_list}), content_type="application/json"
        )

    if slug == "firefox":
        latest_version = product_details.firefox_versions["LATEST_FIREFOX_VERSION"]
    else:
        versions = product.versions.filter(default=True)
        if versions:
            latest_version = versions[0].min_version
        else:
            latest_version = 0

    return render(
        request,
        template,
        {
            "product": product,
            "products": Product.objects.filter(visible=True),
            "topics": topics_for(product=product, parent=None),
            "search_params": {"product": slug},
            "latest_version": latest_version,
            "subscribed_products_ids": (
                user.profile.products.all().values_list("id", flat=True)
                if user.is_authenticated
                else []
            ),
            "featured": get_featured_articles(product, locale=request.LANGUAGE_CODE),
        },
    )
Esempio n. 2
0
def aaq(request, product_key=None, category_key=None, step=1):
    """Ask a new question."""

    template = "questions/new_question.html"

    # Check if any product forum has a locale in the user's current locale
    if (request.LANGUAGE_CODE not in QuestionLocale.objects.locales_list()
            and request.LANGUAGE_CODE != settings.WIKI_DEFAULT_LANGUAGE):

        locale, path = split_path(request.path)
        path = "/" + settings.WIKI_DEFAULT_LANGUAGE + "/" + path

        old_lang = settings.LANGUAGES_DICT[request.LANGUAGE_CODE.lower()]
        new_lang = settings.LANGUAGES_DICT[
            settings.WIKI_DEFAULT_LANGUAGE.lower()]
        msg = _(
            "The questions forum isn't available in {old_lang}, we "
            "have redirected you to the {new_lang} questions forum.").format(
                old_lang=old_lang, new_lang=new_lang)
        messages.add_message(request, messages.WARNING, msg)

        return HttpResponseRedirect(path)

    # Check if the user is using a mobile device,
    # render step 2 if they are
    product_key = product_key or request.GET.get("product")
    if product_key is None:

        change_product = False
        if request.GET.get("q") == "change_product":
            change_product = True

        is_mobile_device = get_user_agent(request).is_mobile

        if is_mobile_device and not change_product:
            user_agent = request.META.get("HTTP_USER_AGENT", "")
            product_key = get_mobile_product_from_ua(user_agent)
            if product_key:
                # redirect needed for InAAQMiddleware
                step_2 = reverse("questions.aaq_step2",
                                 kwargs={"product_key": product_key})
                return HttpResponseRedirect(step_2)

    # Return 404 if the product doesn't exist in config
    product_config = config.products.get(product_key)
    if product_key and not product_config:
        raise Http404

    # If the selected product doesn't exist in DB, render a 404
    if step > 1:
        try:
            product = Product.objects.get(slug=product_config["product"])
        except Product.DoesNotExist:
            raise Http404
        else:
            # Check if the selected product has a forum in the user's locale
            if not product.questions_locales.filter(
                    locale=request.LANGUAGE_CODE).count():
                locale, path = split_path(request.path)
                path = "/" + settings.WIKI_DEFAULT_LANGUAGE + "/" + path

                old_lang = settings.LANGUAGES_DICT[
                    request.LANGUAGE_CODE.lower()]
                new_lang = settings.LANGUAGES_DICT[
                    settings.WIKI_DEFAULT_LANGUAGE.lower()]
                msg = _(
                    "The questions forum isn't available for {product} in {old_lang}, we "
                    "have redirected you to the {new_lang} questions forum."
                ).format(product=product.title,
                         old_lang=old_lang,
                         new_lang=new_lang)
                messages.add_message(request, messages.WARNING, msg)

                return HttpResponseRedirect(path)

    context = {
        "products": config.products,
        "current_product": product_config,
        "current_step": step,
        "host": Site.objects.get_current().domain,
    }

    if step == 2:
        context["featured"] = get_featured_articles(
            product, locale=request.LANGUAGE_CODE)
        context["topics"] = topics_for(product, parent=None)
    elif step == 3:
        form = NewQuestionForm(
            product=product_config,
            data=request.POST or None,
            initial={"category": category_key},
        )
        context["form"] = form

        # NOJS: upload image
        if "upload_image" in request.POST:
            upload_imageattachment(request, request.user)

        if form.is_valid() and not is_ratelimited(request, "aaq-day", "5/d"):

            question = form.save(
                user=request.user,
                locale=request.LANGUAGE_CODE,
                product=product,
                product_config=product_config,
            )

            if form.cleaned_data.get("is_spam"):
                _add_to_moderation_queue(request, question)

            # Submitting the question counts as a vote
            question_vote(request, question.id)

            my_questions_url = reverse("users.questions",
                                       args=[request.user.username])
            messages.add_message(
                request,
                messages.SUCCESS,
                _("Done! Your question is now posted on the Mozilla community support forum. "
                  +
                  "You can see your post anytime by visiting the {a_open}My Questions"
                  + "{a_close} page in your profile.").format(
                      a_open="<a href='" + my_questions_url + "'>",
                      a_close="</a>"),
                extra_tags="safe",
            )

            request.session["aaq-final-step"] = True

            url = reverse("questions.details",
                          kwargs={"question_id": question.id})
            return HttpResponseRedirect(url)

        if getattr(request, "limited", False):
            raise PermissionDenied

        user_ct = ContentType.objects.get_for_model(request.user)
        context["images"] = ImageAttachment.objects.filter(
            creator=request.user,
            content_type=user_ct,
        ).order_by("-id")[:IMG_LIMIT]

    return render(request, template, context)