diff --git a/app/backend/wells/filters.py b/app/backend/wells/filters.py index f8236742e..147abce47 100644 --- a/app/backend/wells/filters.py +++ b/app/backend/wells/filters.py @@ -17,6 +17,7 @@ from django import forms from django.core.exceptions import FieldDoesNotExist +from django.db import connection from django.http import HttpRequest, QueryDict from django.contrib.gis.geos import GEOSException, Polygon, GEOSGeometry, Point from django.contrib.gis.gdal import GDALException @@ -42,6 +43,7 @@ BoundaryEffectCode, AnalysisMethodCode ) +from wells.constants import WELL_TAGS logger = logging.getLogger('wells_filters') @@ -346,7 +348,10 @@ class WellListFilter(AnyOrAllFilterSet): method='filter_licenced_status', label='Licence status' ) - + licence_number = filters.NumberFilter(field_name='licence_number', + method="filter_licence_number", + label="Licence Number" + ) class Meta: model = Well fields = [ @@ -416,6 +421,7 @@ class Meta: 'legal_range', 'legal_section', 'legal_township', + 'licence_number', 'licenced_status', 'liner_diameter', 'liner_from', @@ -509,11 +515,37 @@ def filter_street_address_or_city(self, queryset, name, value): return queryset.filter(Q(street_address__icontains=value) | Q(city__icontains=value)) + def filter_licence_number(self, queryset, name, value): + raw_query = """ + SELECT DISTINCT + wl.well_id + FROM well_licences wl + LEFT JOIN aquifers_waterrightslicence aw + ON aw.wrl_sysid = wl.waterrightslicence_id + WHERE aw.licence_number = %s + """ + params = [value] + try: + if int(value): + with connection.cursor() as cursor: + cursor.execute(raw_query, params) + result = cursor.fetchall() + well_ids = [row[0] for row in result] + print(well_ids) + return queryset.filter(well_tag_number__in=well_ids) + except: + pass + logger.warning(f"[FILTER SEARCH]: invalid licence_number '{value}' was inputted.") + return queryset.filter(well_tag_number=None) + def filter_by_document_type(self, queryset, name, value): - filter_condition = {f"{value.lower().replace(' ', '_')}__gt": 0} - attachments = WellAttachment.objects.filter(**filter_condition).values_list('well_tag_number', flat=True) - attachments = list(map(int, attachments)) - return queryset.filter(well_tag_number__in=attachments) + if any(value == entry["value"] for entry in WELL_TAGS): + filter_condition = {f"{value.lower().replace(' ', '_')}__gt": 0} + attachments = WellAttachment.objects.filter(**filter_condition).values_list('well_tag_number', flat=True) + attachments = list(map(int, attachments)) + return queryset.filter(well_tag_number__in=attachments) + logger.warning(f"[FILTER SEARCH]: invalid document_type '{value}' was inputted.") + return queryset.filter(well_tag_number=None) def filter_combined_legal(self, queryset, name, value): lookups = ( @@ -729,7 +761,6 @@ class WellListOrderingFilter(OrderingFilter): for field in Well._meta.get_fields() if field.many_to_many and not field.auto_created } - def get_ordering(self, request, queryset, view): ordering = super().get_ordering(request, queryset, view) updated_ordering = [] diff --git a/app/backend/wells/serializers_v2.py b/app/backend/wells/serializers_v2.py index 1237d6dbd..b43250268 100644 --- a/app/backend/wells/serializers_v2.py +++ b/app/backend/wells/serializers_v2.py @@ -13,7 +13,7 @@ """ import logging from decimal import Decimal - +from django.db import connection from rest_framework import serializers from django.contrib.gis.geos import Point @@ -142,7 +142,7 @@ def create(self, validated_data): class WellListSerializerV2(serializers.ModelSerializer): """Serializes a well record""" - + licence_number = serializers.SerializerMethodField() legal_pid = serializers.SerializerMethodField() drilling_company = serializers.ReadOnlyField( source='company_of_person_responsible.org_guid') @@ -286,9 +286,29 @@ class Meta: "static_water_level", "alternative_specs_submitted", "technical_report", - "drinking_water_protection_area_ind" + "drinking_water_protection_area_ind", + "licence_number" ) + def get_licence_number(self, obj): + with connection.cursor() as cursor: + licence_num_query = """ + SELECT ARRAY_AGG(aw.licence_number) AS licence_numbers + FROM well_licences wl + LEFT JOIN aquifers_waterrightslicence aw ON aw.wrl_sysid = wl.waterrightslicence_id + WHERE wl.well_id = %s + GROUP BY wl.well_id + """ + cursor.execute(licence_num_query, [obj.well_tag_number]) + row = cursor.fetchone() + if row: + return row[0] + else: + return None + def to_representation(self, instance): + representation = super().to_representation(instance) + representation['licence_number'] = self.get_licence_number(instance) + return representation class WellListAdminSerializerV2(WellListSerializerV2): class Meta: diff --git a/app/frontend/src/wells/components/AdvancedSearchForm.vue b/app/frontend/src/wells/components/AdvancedSearchForm.vue index 8cb2d0f6f..a200888ab 100644 --- a/app/frontend/src/wells/components/AdvancedSearchForm.vue +++ b/app/frontend/src/wells/components/AdvancedSearchForm.vue @@ -105,6 +105,9 @@ const ADDITIONAL_FILTER_SECTIONS = [ 'ownerCity', 'ownerProvince', 'ownerPostalCode' ] }, + { header: "Licence Information", + fields: + [ 'licenceNumber'] }, { header: 'Well location', fields: [ 'legalBlock', diff --git a/app/frontend/src/wells/components/SearchColumnSelect.vue b/app/frontend/src/wells/components/SearchColumnSelect.vue index 059b4a961..a7af84e8b 100644 --- a/app/frontend/src/wells/components/SearchColumnSelect.vue +++ b/app/frontend/src/wells/components/SearchColumnSelect.vue @@ -185,7 +185,8 @@ const RESULT_COLUMNS = [ 'alterationStartDate', 'alterationEndDate', 'decomissionStartDate', - 'decomissionEndDate' + 'decomissionEndDate', + 'licenceNumber', ] export default { diff --git a/app/frontend/src/wells/components/SearchResults.vue b/app/frontend/src/wells/components/SearchResults.vue index 285ed3ca0..2a15045ca 100644 --- a/app/frontend/src/wells/components/SearchResults.vue +++ b/app/frontend/src/wells/components/SearchResults.vue @@ -103,6 +103,13 @@ + @@ -154,7 +161,8 @@ export default { { value: 10, text: '10' }, { value: 25, text: '25' }, { value: 50, text: '50' } - ] + ], + LICENCE_URL: 'https://j200.gov.bc.ca/pub/ams/Default.aspx?PossePresentation=AMSPublic&PosseObjectDef=o_ATIS_DocumentSearch&PosseMenuName=WS_Main&Criteria_LicenceNumber=' } }, computed: { diff --git a/app/frontend/src/wells/components/mixins/filters.js b/app/frontend/src/wells/components/mixins/filters.js index 8132f1fb4..2fa0e02a5 100644 --- a/app/frontend/src/wells/components/mixins/filters.js +++ b/app/frontend/src/wells/components/mixins/filters.js @@ -24,6 +24,12 @@ const SEARCH_FIELDS = { ] }, well: { param: 'well', label: 'Well tag or ID plate number', type: 'text' }, + licenceNumber: { + param: "licence_number", + label: "Licence number", + type: "number", + resultLabel: "Licence number(s)" + }, wellDocumentType: { param: "well_document_type", label: "Contains document type",