Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

project-search: add identifer to search field #6247

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions meinberlin/apps/kiezradar/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ def get_search_profiles_for_project(project: Project) -> QuerySet[SearchProfile]
)
if project.administrative_district:
search_term += " " + project.administrative_district.name
if hasattr(project, "externalproject"):
if hasattr(project.externalproject, "bplan"):
search_term += " " + project.externalproject.bplan.identifier
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be project.indentifier, since bplan is externalproject and externaprojects are projects. And I see you pass bplans directly in your tests, which makes sense.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m4ra I tried this before and again today but it didn't work. Since ExternalProject and Projects are not abstract models but just further inherited, they don't have that identifier field on themselves, I can only access that through the Bplan model. And to get that I have to follow the OneToOne relation that is being created when a model inherits another model, which I do in the code here and in serializers.py.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I try to just return project.identifier I get nothing back. And doing type(instance) also shows that it's a Project not a Bplan, doing vars(instance) also shows no identifier.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, for this to work we need to query projects = Project.objects.select_related("externalproject__bplan").all(). This would decrease the amount of queries. And then we can squash the three checks into one with if hasattr(p, 'externalproject') and hasattr(p.externalproject, 'bplan'): Need to find where we query the projects.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I am missing something, but since get_search_profiles_for_project does not query the project, it would mean whoever is using get_search_profiles_for_project would have to always query with the select_related, otherwise it would lead to bugs. I imagine it's any easy thing to oversee and don't know if we should change that.

I could query the project again, something like

project = Project.object.select_related("externalproject__bplan").get(pk=project.filter)

but I suppose that feels weird as well. In the serializer it might be easier because we can just override get_queryset in all the views where it's used, but still I am not sure if this works that well? What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new field method identifier in the project serializers, it is introducing more lookups when we call it from other views, e.g the list of projects. That's why we are getting a test failure related to caching. So yes It is not related to the get_serch_profiles_for_project. Can you add the externalproject__bplan here? I just tested it locally and the caching test is passing.
ps. I realized am commenting on the wrong place, it should be in the serializers o_O

for topic in project.topic_names:
search_term += " " + topic
if connection.vendor == "postgresql":
Expand Down
7 changes: 7 additions & 0 deletions meinberlin/apps/projects/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class ProjectSerializer(
type = serializers.SerializerMethodField()
url = serializers.SerializerMethodField()
topics = serializers.SerializerMethodField()
identifier = serializers.SerializerMethodField()

def __init__(self, *args, **kwargs):
self.now = kwargs.pop("now")
Expand All @@ -84,6 +85,7 @@ class Meta:
"description",
"district",
"future_phase",
"identifier",
"organisation",
"participation",
"participation_active",
Expand Down Expand Up @@ -248,6 +250,11 @@ def get_point_label(self, instance):
def get_cost(self, instance):
return ""

def get_identifier(self, instance):
if hasattr(instance, "externalproject"):
if hasattr(instance.externalproject, "bplan"):
return instance.externalproject.bplan.identifier

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, just check for if hasattr(project.identifier) and the currently failing task will pass fine.

can you add the field in the bplan tests as well?


class ActiveProjectSerializer(ProjectSerializer):
def seconds_in_units(self, seconds):
Expand Down
2 changes: 1 addition & 1 deletion meinberlin/react/projects/filter-projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const statusNames = ['active', 'future', 'past']

export const filterProjects = (items, appliedFilters, kiezradars, topics, projectState) => {
const { search, topics: activeTopics, districts, organisation, participations, plansOnly, kiezradars: activeKiezradars } = appliedFilters

return items.filter((item) => {
const isWithinAnyRadius =
item.point && kiezradars.some(kiezradar =>
Expand All @@ -37,6 +36,7 @@ export const filterProjects = (items, appliedFilters, kiezradars, topics, projec
isInTitle(item.district, search) ||
isInTitle(item.organisation, search) ||
isInTitle(item.description, search) ||
isInTitle(item.identifier, search) ||
isInTopic(topics, item.topics, search)) &&
(projectState.includes(statusNames[item.status])) &&
(!plansOnly || item.type === 'plan') &&
Expand Down
15 changes: 15 additions & 0 deletions tests/kiezradar/test_search_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,21 @@ def test_searchprofile_filter_query(
assert list(result) == expected_profiles


@pytest.mark.django_db
def test_searchprofile_filter_query_bplan(
search_profile_factory,
kiezradar_query_factory,
bplan_factory,
):
bplan = bplan_factory(identifier="B30-1 A30-bplan 2024")
bplan_profile = search_profile_factory(
query=kiezradar_query_factory(text="A30-bplan")
)

result = get_search_profiles_for_project(bplan).order_by("pk")
assert list(result) == [bplan_profile]


@pytest.mark.django_db
def test_searchprofile_filter_kiezradar(
user, phase_factory, search_profile_factory, kiez_radar_factory
Expand Down
Loading