<commit_before>from django.conf import settings
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.forms.models import model_to_dict
from django.http import FileResponse
from django.shortcuts import get_object_or_404

from shared.request import make_response, ErrorResponse

from .models import Proposal, Attribute, Document, Event
from .query import build_proposal_query

default_attributes = [
    "applicant_name",
    "legal_notice",
    "dates_of_public_hearing"
]


def proposal_json(proposal, include_images=True,
                  include_attributes=default_attributes,
                  include_events=False, include_documents=True,
                  include_projects=True):
    pdict = model_to_dict(proposal, exclude=["location", "fulltext"])
    pdict["location"] = {"lat": proposal.location.y,
                         "lng": proposal.location.x}

    if include_documents:
        pdict["documents"] = [d.to_dict() for d in proposal.document_set.all()]

    if include_images:
        images = proposal.images.order_by("-priority")
        # Booleans are considered integers
        if isinstance(include_images, int) and include_images is not True:
            images = images[0:include_images]

        pdict["images"] = [img.to_dict() for img in images]

    if include_attributes:
        attributes = Attribute.objects.filter(proposal=proposal)
        if include_attributes is not True:
            attributes = attributes.filter(handle__in=include_attributes)
        pdict["attributes"] = [a.to_dict() for a in attributes]

    if include_events:
        pdict["events"] = [e.to_dict for e in proposal.events.all()]

    if include_projects and proposal.project:
        pdict["project"] = proposal.project.to_dict()

    # TODO: fulltext query

    return pdict

# Views:


@make_response("list.djhtml")
def active_proposals(req):
    proposal_query = Proposal.objects.filter(build_proposal_query(req.GET))
    if "include_projects" in req.GET:
        proposal_query = proposal_query.select_related("project")
    paginator = Paginator(proposal_query, per_page=100)

    page = req.GET.get("page")
    try:
        proposals = paginator.page(page)
    except PageNotAnInteger:
        proposals = paginator.page(1)
    except EmptyPage as err:
        raise ErrorResponse("No such page", {"page": page}, err=err)

    pjson = [proposal_json(proposal, include_images=1)
             for proposal in proposals]

    return {"proposals": pjson,<commit_msg>Don't paginate the proposal results<commit_after>from django.conf import settings
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.forms.models import model_to_dict
from django.http import FileResponse
from django.shortcuts import get_object_or_404

from shared.request import make_response, ErrorResponse

from .models import Proposal, Attribute, Document, Event
from .query import build_proposal_query

default_attributes = [
    "applicant_name",
    "legal_notice",
    "dates_of_public_hearing"
]


def proposal_json(proposal, include_images=True,
                  include_attributes=default_attributes,
                  include_events=False, include_documents=True,
                  include_projects=True):
    pdict = model_to_dict(proposal, exclude=["location", "fulltext"])
    pdict["location"] = {"lat": proposal.location.y,
                         "lng": proposal.location.x}

    if include_documents:
        pdict["documents"] = [d.to_dict() for d in proposal.document_set.all()]

    if include_images:
        images = proposal.images.order_by("-priority")
        # Booleans are considered integers
        if isinstance(include_images, int) and include_images is not True:
            images = images[0:include_images]

        pdict["images"] = [img.to_dict() for img in images]

    if include_attributes:
        attributes = Attribute.objects.filter(proposal=proposal)
        if include_attributes is not True:
            attributes = attributes.filter(handle__in=include_attributes)
        pdict["attributes"] = [a.to_dict() for a in attributes]

    if include_events:
        pdict["events"] = [e.to_dict for e in proposal.events.all()]

    if include_projects and proposal.project:
        pdict["project"] = proposal.project.to_dict()

    # TODO: fulltext query

    return pdict

# Views:

@make_response("list.djhtml")
def active_proposals(req):
    proposals = Proposal.objects.filter(build_proposal_query(req.GET))
    if "include_projects" in req.GET:
        proposals = proposals.select_related("project")

    pjson = [proposal_json(proposal, include_images=1)
             for proposal in proposals]

    return {"proposals": pjson}


@make_response("list.djhtml")
def paginated_active_proposals(req):
    proposal_query = Proposal.objects.filter(build_proposal_query(req.GET))
    if "include_projects" in req.GET:
        proposal_query = proposal_query.select_related("project")
    paginator = Paginator(proposal_query, per_page=100)

    page = req.GET.get("page")
    try:
        proposals = paginator.page(page)
    except PageNotAnInteger:
        proposals = paginator.page(1)
    except EmptyPage as err:
        raise ErrorResponse("No such page", {"page": page}, err=err)

    pjson = [proposal_json(proposal, include_images=1)
             for proposal in proposals]

    return {"proposals": pjson,
{'fraction_non_alphanumeric': 0.09655937846836848, 'fraction_numerical': 0.003514613392526822, 'mean_word_length': 3.47970173985087, 'pattern_counts': {'":': 9, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6404273', 'n_tokens_mistral': 1597, 'n_tokens_neox': 1546, 'n_words': 366}