Exemple #1
0
def bottom(request, page=None, records=None):
    """The worst
    """

    # Get prkls
    if request.user.id:
        prkls = prkl_sql_ob.PrklQuery(vote_userid=request.user.id, like_userid=request.user.id)
    else:
        prkls = prkl_sql_ob.PrklQuery(vote_trueid=request.true_id)

    # order
    prkls.bottom()

    # Pagination
    if not page:
        page = 1
    page = int(page)

    if not records:
        records = 10
    records = int(records)
    pag_ctx = get_paginator_context(prkls, page, records)

    context = {"title": "Huonoimmat", "prkls": prkls, "base_url": reverse("bottom")}
    context.update(pag_ctx)
    req_ctx = RequestContext(request, context)

    return render_to_response("index.html", req_ctx)
Exemple #2
0
def members(request, page=None, records=None):
    data = request.GET.copy() or None

    find_friend = None
    find_friend_form = forms.FindFriendForm(data)
    if find_friend_form.is_bound and find_friend_form.is_valid():
        find_friend = find_friend_form.cleaned_data["find_friend"]
        if find_friend:
            members = models.User.objects.search(find_friend, order_by=("username",))
        else:
            members = models.User.objects.all().order_by("-date_joined")
    else:
        members = models.User.objects.all().order_by("-date_joined")

    # Pagination
    if not page:
        page = 1
    page = int(page)

    if not records:
        records = 5
    records = int(records)

    # Affect css
    css_ctx = {"css_paginator": "members_paginator"}

    pag_ctx = get_paginator_context(members, page, records, def_ctx=css_ctx)

    # Prkl counts
    shown = pag_ctx["page_objects"].object_list
    prkl_counts = models.Prkl.objects.filter(user__id__in=[u.id for u in shown])
    prkl_counts = prkl_counts.extra(
        tables=("auth_user",), where=("web_prkl.user_id=auth_user.id",), select={"username": "******"}
    )
    prkl_counts = prkl_counts.order_by("user")
    prkl_count_dict = {}
    for group, prkls in itertools.groupby(prkl_counts, lambda x: x.username):
        prkl_count_dict[group] = len(list(prkls))

    context = {
        "title": "Jäsenlista",
        "members": members,
        "find_friend": find_friend,
        "find_friend_form": find_friend_form,
        "prkl_count_dict": prkl_count_dict,
        "members_page": True,
    }
    context.update(pag_ctx)
    req_ctx = RequestContext(request, context)

    return render_to_response("members.html", req_ctx)
Exemple #3
0
def search(request, page=None, records=None):
    """Detailed search
    """

    data = request.GET.copy() or None

    initial = {"sex": "4"}

    result = None
    user_search_form = forms.UserSearchForm(data, initial=initial)
    if user_search_form.is_bound:
        if user_search_form.is_valid():
            result = user_search_form.search()

    # Pagination
    if not page:
        page = 1
    page = int(page)

    if not records:
        records = 5
    records = int(records)

    # Affect css
    css_ctx = {"css_paginator": "members_paginator"}

    prkl_count_dict = {}
    if result:
        pag_ctx = get_paginator_context(result, page, records, def_ctx=css_ctx)

        # Prkl counts
        shown = pag_ctx["page_objects"].object_list

        prkl_counts = models.Prkl.objects.filter(user__id__in=[u.id for u in shown])
        prkl_counts = prkl_counts.extra(
            tables=("auth_user",), where=("web_prkl.user_id=auth_user.id",), select={"username": "******"}
        )
        prkl_counts = prkl_counts.order_by("user")
        for group, prkls in itertools.groupby(prkl_counts, lambda x: x.username):
            prkl_count_dict[group] = len(list(prkls))

    context = {"title": "Haku", "user_search_form": user_search_form, "prkl_count_dict": prkl_count_dict}
    if result:
        context.update(pag_ctx)
    req_ctx = RequestContext(request, context)

    return render_to_response("search.html", req_ctx)
Exemple #4
0
def index(request, page=None, records=None, tag=None):
    """Our index page
    """

    mark_intro = request.GET.get("mark_intro", None)
    if mark_intro:
        request.true_id.mark_seen_intro()
        return HttpResponseRedirect(request.META["PATH_INFO"])

    data = request.POST.copy() or None
    if data:
        if data["submit"] != "Lisää":
            data = None
        else:
            data["user"] = request.user

    submit_prkl_form = forms.SubmitPrklForm(data)

    tag_obs = models.Tag.objects.filter(is_default=True).values("id", "name").order_by("id")
    tags = [(t["id"], t["name"]) for t in tag_obs]
    submit_prkl_form.fields["tags"].choices = tags

    if tag:
        try:
            tag_ob = tag_obs.filter(name__iexact=tag)[0]
        except IndexError:
            return notfound(request)

    if request.user.id:
        checkbox = django_forms.BooleanField(label="Anonyymisti?", required=False)
        checkbox.widget.attrs["style"] = "margin-bottom: 10px"

        submit_prkl_form.fields["anonymous"] = checkbox

    if submit_prkl_form.is_bound:
        if submit_prkl_form.is_valid():
            request.true_id.visible_prklform = False
            request.true_id.save()

            submit_prkl_form.save()

            return HttpResponseRedirect(request.META["PATH_INFO"])

    ### Was 27 queries 70.39ms for index
    ### Now 7 queries in 41ms for index
    if request.user.id:
        prkls = prkl_sql_ob.PrklQuery(vote_userid=request.user.id, like_userid=request.user.id)
    else:
        prkls = prkl_sql_ob.PrklQuery(vote_trueid=request.true_id)

    ### Tag affects base url too!
    base_url = "http://%s" % request.META["HTTP_HOST"]
    if tag:
        prkls.tag(tag)
        base_url = "%s%s" % (base_url, reverse("tag", args=(tag,)))

    # Pagination
    if not page:
        page = 1
    page = int(page)

    if not records:
        records = 10
    records = int(records)

    pag_ctx = get_paginator_context(prkls, page, records)

    context = {"title": "Etusivu", "submit_prkl_form": submit_prkl_form, "prkls": prkls, "base_url": base_url}
    context.update(pag_ctx)
    req_ctx = RequestContext(request, context)

    return render_to_response("index.html", req_ctx)