Skip to content

Commit

Permalink
Amend code post rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
HettieS committed Dec 11, 2024
1 parent e13c813 commit 555f194
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 65 deletions.
1 change: 0 additions & 1 deletion fala/apps/adviser/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def perform_search(form, postcode, categories, page):
)
if "error" in data:
form.add_error("postcode", data["error"])
return {}
return data
except laalaa.LaaLaaError:
form.add_error("postcode", "%s %s" % (_("Error looking up legal advisers."), _("Please try again later.")))
Expand Down
130 changes: 67 additions & 63 deletions fala/apps/adviser/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db import models
from django.utils import timezone
from django.http import Http404
from .regions import Region
from .laa_laa_paginator import LaaLaaPaginator
import urllib
Expand Down Expand Up @@ -29,69 +30,72 @@ def get_queryset(self):
return self._data.get("results", None)

def get_context_data(self):
pages = LaaLaaPaginator(self._data["count"], 10, 3, self._form.current_page)
current_page = pages.current_page()
params = {
"postcode": self._form.cleaned_data["postcode"],
"name": self._form.cleaned_data["name"],
}
categories = self._form.cleaned_data["categories"]

# create list of tuples which can be passed to urlencode for pagination links
category_tuples = [("categories", c) for c in categories]

def item_for(page_num):
if len(categories) > 0:
page_params = {"page": page_num}
href = (
"/search?"
+ urllib.parse.urlencode({**page_params, **params})
+ "&"
+ urllib.parse.urlencode(category_tuples)
)
else:
page_params = {"page": page_num}
href = "/search?" + urllib.parse.urlencode({**page_params, **params})

return {"number": page_num, "current": self._form.current_page == page_num, "href": href}

pagination = {"items": [item_for(page_num) for page_num in pages.page_range]}

if current_page.has_previous():
if len(categories) > 0:
page_params = {"page": current_page.previous_page_number()}
prev_link = (
"/search?"
+ urllib.parse.urlencode({**page_params, **params})
+ "&"
+ urllib.parse.urlencode(category_tuples)
)
else:
page_params = {"page": current_page.previous_page_number()}
prev_link = "/search?" + urllib.parse.urlencode({**page_params, **params})
pagination["previous"] = {"href": prev_link}

if current_page.has_next():
if len(categories) > 0:
page_params = {"page": current_page.next_page_number()}
href = (
"/search?"
+ urllib.parse.urlencode({**page_params, **params})
+ "&"
+ urllib.parse.urlencode(category_tuples)
)
else:
page_params = {"page": current_page.next_page_number()}
href = "/search?" + urllib.parse.urlencode({**page_params, **params})
pagination["next"] = {"href": href}

return {
"form": self._form,
"data": self._data,
"params": params,
"FEATURE_FLAG_SURVEY_MONKEY": settings.FEATURE_FLAG_SURVEY_MONKEY,
"pagination": pagination,
}
if "Invalid page" in self._data.get("error", []):
raise Http404("Page not found")
else:
pages = LaaLaaPaginator(self._data["count"], 10, 3, self._form.current_page)
current_page = pages.current_page()
params = {
"postcode": self._form.cleaned_data["postcode"],
"name": self._form.cleaned_data["name"],
}
categories = self._form.cleaned_data["categories"]

# create list of tuples which can be passed to urlencode for pagination links
category_tuples = [("categories", c) for c in categories]

def item_for(page_num):
if len(categories) > 0:
page_params = {"page": page_num}
href = (
"/search?"
+ urllib.parse.urlencode({**page_params, **params})
+ "&"
+ urllib.parse.urlencode(category_tuples)
)
else:
page_params = {"page": page_num}
href = "/search?" + urllib.parse.urlencode({**page_params, **params})

return {"number": page_num, "current": self._form.current_page == page_num, "href": href}

pagination = {"items": [item_for(page_num) for page_num in pages.page_range]}

if current_page.has_previous():
if len(categories) > 0:
page_params = {"page": current_page.previous_page_number()}
prev_link = (
"/search?"
+ urllib.parse.urlencode({**page_params, **params})
+ "&"
+ urllib.parse.urlencode(category_tuples)
)
else:
page_params = {"page": current_page.previous_page_number()}
prev_link = "/search?" + urllib.parse.urlencode({**page_params, **params})
pagination["previous"] = {"href": prev_link}

if current_page.has_next():
if len(categories) > 0:
page_params = {"page": current_page.next_page_number()}
href = (
"/search?"
+ urllib.parse.urlencode({**page_params, **params})
+ "&"
+ urllib.parse.urlencode(category_tuples)
)
else:
page_params = {"page": current_page.next_page_number()}
href = "/search?" + urllib.parse.urlencode({**page_params, **params})
pagination["next"] = {"href": href}

return {
"form": self._form,
"data": self._data,
"params": params,
"FEATURE_FLAG_SURVEY_MONKEY": settings.FEATURE_FLAG_SURVEY_MONKEY,
"pagination": pagination if len(pagination["items"]) > 1 else {},
}


class OtherJurisdictionState(object):
Expand Down
2 changes: 1 addition & 1 deletion fala/apps/adviser/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.conf import settings
from django.urls import resolve, reverse
from django.views.generic import TemplateView, ListView, View
from django.http import HttpResponse, Http404
from django.http import HttpResponse
import os
from .forms import AdviserSearchForm, AdviserRootForm, SingleCategorySearchForm
from .regions import Region
Expand Down

0 comments on commit 555f194

Please sign in to comment.