From bc854e8d0d89f020c7f3fce9bae98750789684a2 Mon Sep 17 00:00:00 2001 From: Martin Monzo Gancia Date: Sun, 9 Aug 2020 03:19:48 -0300 Subject: [PATCH] Refactor filter apply filter method --- search_events_app/filters/buyer_filter.py | 3 --- search_events_app/filters/category_filter.py | 5 +---- search_events_app/filters/city_filter.py | 7 ++----- search_events_app/filters/country_filter.py | 3 --- search_events_app/filters/currency_filter.py | 5 +---- search_events_app/filters/event_name_filter.py | 5 +---- .../filters/feature_filter_manager.py | 4 ++-- .../filters/features/add_ons_filter.py | 10 ++-------- .../filters/features/custom_question_filter.py | 11 ++--------- .../filters/features/donation_filter.py | 8 +------- .../filters/features/eb_studio_filter.py | 10 ++-------- .../filters/features/facebook_filter.py | 10 ++-------- .../features/group_registration_filter.py | 10 ++-------- .../filters/features/repeating_events_filter.py | 9 +-------- .../filters/features/reserved_seating_filter.py | 8 +------- .../filters/features/wait_list_filter.py | 8 +------- .../filters/features/website_widgets_filter.py | 7 ++----- search_events_app/filters/filter.py | 17 +++++++---------- search_events_app/filters/format_filter.py | 3 --- search_events_app/filters/language_filter.py | 3 --- search_events_app/filters/last_four_filter.py | 5 +---- search_events_app/filters/online_filter.py | 12 ++++-------- search_events_app/filters/organizer_filter.py | 3 --- search_events_app/filters/price_filter.py | 4 ---- search_events_app/filters/start_date_filter.py | 3 --- search_events_app/filters/venue_filter.py | 3 --- search_events_app/services/filter_manager.py | 6 ++---- templates/table_events.html | 2 +- 28 files changed, 38 insertions(+), 146 deletions(-) diff --git a/search_events_app/filters/buyer_filter.py b/search_events_app/filters/buyer_filter.py index 446bfa0..9df0a86 100644 --- a/search_events_app/filters/buyer_filter.py +++ b/search_events_app/filters/buyer_filter.py @@ -24,6 +24,3 @@ def get_join_query(self): ') AS at ON at.event = dw_event.event_id' ] return [''] - - def get_where_query(self): - return '' diff --git a/search_events_app/filters/category_filter.py b/search_events_app/filters/category_filter.py index 25e6933..9f30969 100644 --- a/search_events_app/filters/category_filter.py +++ b/search_events_app/filters/category_filter.py @@ -6,7 +6,7 @@ class CategoryFilter(Filter): def apply_filter(self, request): - category_code = request.GET.get('category', '') + category_code = request.GET.get('category') new_filter = None if category_code: try: @@ -17,9 +17,6 @@ def apply_filter(self, request): if self.has_changed: self.value = new_filter - def get_join_query(self): - return [''] - def get_where_query(self): if self.value: return f"AND dw_cat.event_category_desc = '{self.value.name}'" diff --git a/search_events_app/filters/city_filter.py b/search_events_app/filters/city_filter.py index 803e55f..dca7581 100644 --- a/search_events_app/filters/city_filter.py +++ b/search_events_app/filters/city_filter.py @@ -11,21 +11,18 @@ class CityFilter(Filter): def apply_filter(self, request): country_name = request.GET.get('country', '') - city_name = request.GET.get('city', '') + city_name = request.GET.get('city') new_filter = None if city_name: try: country = Country.objects.get(name=country_name) - new_filter = City.objects.get(name=city_name, country=country.alpha_2_code ) + new_filter = City.objects.get(name=city_name, country=country.alpha_2_code) except ObjectDoesNotExist: new_filter = None self.has_changed = new_filter != self.value if self.has_changed: self.value = new_filter - def get_join_query(self): - return [''] - def get_where_query(self): if self.value: if self.value.code: diff --git a/search_events_app/filters/country_filter.py b/search_events_app/filters/country_filter.py index 395db6c..1e83cce 100644 --- a/search_events_app/filters/country_filter.py +++ b/search_events_app/filters/country_filter.py @@ -18,9 +18,6 @@ def apply_filter(self, request): if self.has_changed: self.value = new_filter - def get_join_query(self): - return [''] - def get_where_query(self): if self.value: return f"AND country_desc='{self.value.alpha_2_code}'" diff --git a/search_events_app/filters/currency_filter.py b/search_events_app/filters/currency_filter.py index 045c050..1a0d998 100644 --- a/search_events_app/filters/currency_filter.py +++ b/search_events_app/filters/currency_filter.py @@ -6,7 +6,7 @@ class CurrencyFilter(Filter): def apply_filter(self, request): - currency_code = request.GET.get('currency', '') + currency_code = request.GET.get('currency') new_filter = None if currency_code: try: @@ -17,9 +17,6 @@ def apply_filter(self, request): if self.has_changed: self.value = new_filter - def get_join_query(self): - return [''] - def get_where_query(self): if self.value: return f"AND dw_event.event_currency = '{self.value.code}'" diff --git a/search_events_app/filters/event_name_filter.py b/search_events_app/filters/event_name_filter.py index a905667..f9c2b4c 100644 --- a/search_events_app/filters/event_name_filter.py +++ b/search_events_app/filters/event_name_filter.py @@ -3,7 +3,7 @@ class EventNameFilter(Filter): def apply_filter(self, request): - event_name = request.GET.get('event_name', '') + event_name = request.GET.get('event_name') new_filter = None if event_name: new_filter = event_name @@ -11,9 +11,6 @@ def apply_filter(self, request): if self.has_changed: self.value = new_filter - def get_join_query(self): - return [''] - def get_where_query(self): if self.value: return f"AND LOWER(dw_event.event_title) LIKE '%{self.value.lower().strip()}%'" diff --git a/search_events_app/filters/feature_filter_manager.py b/search_events_app/filters/feature_filter_manager.py index 745769f..4e2d5b1 100644 --- a/search_events_app/filters/feature_filter_manager.py +++ b/search_events_app/filters/feature_filter_manager.py @@ -30,9 +30,9 @@ def __init__(self): ] def apply_filter(self, request): - features_codes = request.GET.get('feature', '').split('-') + feature_codes = request.GET.get('feature', '').split('-') for latest_filter in self.value: - latest_filter.apply_filter(features_codes) + latest_filter.apply_filter(feature_codes) self.has_changed = len([latest_filter for latest_filter in self.value if latest_filter.has_changed]) > 0 def get_select_query(self): diff --git a/search_events_app/filters/features/add_ons_filter.py b/search_events_app/filters/features/add_ons_filter.py index 5725e1e..0f01017 100644 --- a/search_events_app/filters/features/add_ons_filter.py +++ b/search_events_app/filters/features/add_ons_filter.py @@ -3,14 +3,8 @@ class AddOnsFilter(Filter): - def apply_filter(self, features_codes): - new_filter = FeatureCodes.add_ons in features_codes - self.has_changed = new_filter != self.value - if self.has_changed: - self.value = new_filter - - def get_join_query(self): - return [''] + def apply_filter(self, feature_codes): + super().apply_filter(FeatureCodes.add_ons, feature_codes) def get_where_query(self): if self.value: diff --git a/search_events_app/filters/features/custom_question_filter.py b/search_events_app/filters/features/custom_question_filter.py index 5934379..1f7f0d6 100644 --- a/search_events_app/filters/features/custom_question_filter.py +++ b/search_events_app/filters/features/custom_question_filter.py @@ -4,16 +4,9 @@ class CustomQuestionFilter(Filter): def apply_filter(self, feature_codes): - new_filter = FeatureCodes.custom_question in feature_codes - self.has_changed = new_filter != self.value - if self.has_changed: - self.value = new_filter + super().apply_filter(FeatureCodes.custom_question, feature_codes) def get_join_query(self): if self.value: - query = ['INNER JOIN eb.questions q ON q.asset = dw_event.event_id'] - return query + return ['INNER JOIN eb.questions q ON q.asset = dw_event.event_id'] return [''] - - def get_where_query(self): - return '' diff --git a/search_events_app/filters/features/donation_filter.py b/search_events_app/filters/features/donation_filter.py index d135a9d..7e0a223 100644 --- a/search_events_app/filters/features/donation_filter.py +++ b/search_events_app/filters/features/donation_filter.py @@ -4,13 +4,7 @@ class DonationFilter(Filter): def apply_filter(self, feature_codes): - new_filter = FeatureCodes.donation in feature_codes - self.has_changed = new_filter != self.value - if self.has_changed: - self.value = new_filter - - def get_join_query(self): - return [''] + super().apply_filter(FeatureCodes.donation, feature_codes) def get_where_query(self): if self.value: diff --git a/search_events_app/filters/features/eb_studio_filter.py b/search_events_app/filters/features/eb_studio_filter.py index 575ae11..8ac98de 100644 --- a/search_events_app/filters/features/eb_studio_filter.py +++ b/search_events_app/filters/features/eb_studio_filter.py @@ -3,14 +3,8 @@ class EBStudioFilter(Filter): - def apply_filter(self, features_codes): - new_filter = FeatureCodes.eventbrite_studio in features_codes - self.has_changed = new_filter != self.value - if self.has_changed: - self.value = new_filter - - def get_join_query(self): - return [''] + def apply_filter(self, feature_codes): + super().apply_filter(FeatureCodes.eventbrite_studio, feature_codes) def get_where_query(self): if self.value: diff --git a/search_events_app/filters/features/facebook_filter.py b/search_events_app/filters/features/facebook_filter.py index 56970fd..f1a5c23 100644 --- a/search_events_app/filters/features/facebook_filter.py +++ b/search_events_app/filters/features/facebook_filter.py @@ -3,11 +3,8 @@ class FacebookFilter(Filter): - def apply_filter(self, features_codes): - new_filter = FeatureCodes.facebook in features_codes - self.has_changed = new_filter != self.value - if self.has_changed: - self.value = new_filter + def apply_filter(self, feature_codes): + super().apply_filter(FeatureCodes.facebook, feature_codes) def get_select_query(self): if self.value: @@ -24,9 +21,6 @@ def get_join_query(self): ] return [''] - def get_where_query(self): - return '' - def get_group_query(self): if self.value: return ', fb.facebook_event_id' diff --git a/search_events_app/filters/features/group_registration_filter.py b/search_events_app/filters/features/group_registration_filter.py index dbf9466..01cbe80 100644 --- a/search_events_app/filters/features/group_registration_filter.py +++ b/search_events_app/filters/features/group_registration_filter.py @@ -3,11 +3,8 @@ class GroupRegistrationFilter(Filter): - def apply_filter(self, features_codes): - new_filter = FeatureCodes.group_registration in features_codes - self.has_changed = new_filter != self.value - if self.has_changed: - self.value = new_filter + def apply_filter(self, feature_codes): + super().apply_filter(FeatureCodes.group_registration, feature_codes) def get_join_query(self): if self.value: @@ -18,6 +15,3 @@ def get_join_query(self): ') AS team ON dw_event.event_id = team.event_id' ] return [''] - - def get_where_query(self): - return '' diff --git a/search_events_app/filters/features/repeating_events_filter.py b/search_events_app/filters/features/repeating_events_filter.py index c18c48b..e007a8a 100644 --- a/search_events_app/filters/features/repeating_events_filter.py +++ b/search_events_app/filters/features/repeating_events_filter.py @@ -4,16 +4,9 @@ class RepeatingEventsFilter(Filter): def apply_filter(self, feature_codes): - new_filter = FeatureCodes.repeating_events in feature_codes - self.has_changed = new_filter != self.value - if self.has_changed: - self.value = new_filter - - def get_join_query(self): - return [''] + super().apply_filter(FeatureCodes.repeating_events, feature_codes) def get_where_query(self): if self.value: return "AND dw_event.is_repeating_event = 'Y'" return '' - diff --git a/search_events_app/filters/features/reserved_seating_filter.py b/search_events_app/filters/features/reserved_seating_filter.py index 9ba7b78..24251d1 100644 --- a/search_events_app/filters/features/reserved_seating_filter.py +++ b/search_events_app/filters/features/reserved_seating_filter.py @@ -4,13 +4,7 @@ class ReservedSeatingFilter(Filter): def apply_filter(self, feature_codes): - new_filter = FeatureCodes.reserved_seating in feature_codes - self.has_changed = new_filter != self.value - if self.has_changed: - self.value = new_filter - - def get_join_query(self): - return [''] + super().apply_filter(FeatureCodes.reserved_seating, feature_codes) def get_where_query(self): if self.value: diff --git a/search_events_app/filters/features/wait_list_filter.py b/search_events_app/filters/features/wait_list_filter.py index 20a5827..32e8e09 100644 --- a/search_events_app/filters/features/wait_list_filter.py +++ b/search_events_app/filters/features/wait_list_filter.py @@ -4,10 +4,7 @@ class WaitlistFilter(Filter): def apply_filter(self, feature_codes): - new_filter = FeatureCodes.wait_list in feature_codes - self.has_changed = new_filter != self.value - if self.has_changed: - self.value = new_filter + super().apply_filter(FeatureCodes.wait_list, feature_codes) def get_join_query(self): if self.value: @@ -30,6 +27,3 @@ def get_join_query(self): ') AS waitlist ON waitlist.event = dw_event.event_id' ] return [''] - - def get_where_query(self): - return '' diff --git a/search_events_app/filters/features/website_widgets_filter.py b/search_events_app/filters/features/website_widgets_filter.py index f1d89c8..44e02fa 100644 --- a/search_events_app/filters/features/website_widgets_filter.py +++ b/search_events_app/filters/features/website_widgets_filter.py @@ -3,11 +3,8 @@ class WebsiteWidgetsFilter(Filter): - def apply_filter(self, features_codes): - new_filter = FeatureCodes.website_widgets in features_codes - self.has_changed = new_filter != self.value - if self.has_changed: - self.value = new_filter + def apply_filter(self, feature_codes): + super().apply_filter(FeatureCodes.website_widgets, feature_codes) def get_join_query(self): if self.value: diff --git a/search_events_app/filters/filter.py b/search_events_app/filters/filter.py index abd4a7b..3cadf55 100644 --- a/search_events_app/filters/filter.py +++ b/search_events_app/filters/filter.py @@ -1,26 +1,23 @@ -from abc import abstractmethod - - class Filter: def __init__(self): self.value = None self.has_changed = False - @abstractmethod - def apply_filter(self, request): - pass + def apply_filter(self, code, feature_codes): + new_filter = code in feature_codes + self.has_changed = new_filter != self.value + if self.has_changed: + self.value = new_filter def get_select_query(self): return '' - @abstractmethod def get_join_query(self): - pass + return [''] - @abstractmethod def get_where_query(self): - pass + return '' def get_group_query(self): return '' diff --git a/search_events_app/filters/format_filter.py b/search_events_app/filters/format_filter.py index 14810d0..044afda 100644 --- a/search_events_app/filters/format_filter.py +++ b/search_events_app/filters/format_filter.py @@ -22,9 +22,6 @@ def get_value(self): if self.value: return self.value.name - def get_join_query(self): - return [''] - def get_where_query(self): if self.value: return f"AND dw_cat.event_format_desc='{self.value.name}'" diff --git a/search_events_app/filters/language_filter.py b/search_events_app/filters/language_filter.py index a1e3033..0171f71 100644 --- a/search_events_app/filters/language_filter.py +++ b/search_events_app/filters/language_filter.py @@ -18,9 +18,6 @@ def apply_filter(self, request): if self.has_changed: self.value = new_filter - def get_join_query(self): - return [''] - def get_where_query(self): if self.value: return f"AND dw_event.event_language LIKE '%{self.value.code}_%'" diff --git a/search_events_app/filters/last_four_filter.py b/search_events_app/filters/last_four_filter.py index 9e3a6b1..81cbb42 100644 --- a/search_events_app/filters/last_four_filter.py +++ b/search_events_app/filters/last_four_filter.py @@ -4,7 +4,7 @@ class LastFourFilter(Filter): def apply_filter(self, request): - last_four = request.GET.get('card', '') + last_four = request.GET.get('card') new_filter = None if last_four: new_filter = last_four @@ -27,6 +27,3 @@ def get_join_query(self): ') AS o on o.event = dw_event.event_id' ] return [''] - - def get_where_query(self): - return '' diff --git a/search_events_app/filters/online_filter.py b/search_events_app/filters/online_filter.py index eb3c329..f29a3bc 100644 --- a/search_events_app/filters/online_filter.py +++ b/search_events_app/filters/online_filter.py @@ -7,18 +7,14 @@ class OnlineFilter(Filter): def apply_filter(self, request): online = request.GET.get('online') new_filter = None - if online: - if online == 'on': - new_filter = OnlineParameters.ONLINE - elif online == 'off': - new_filter = OnlineParameters.OFFLINE + if online == 'on': + new_filter = OnlineParameters.ONLINE + elif online == 'off': + new_filter = OnlineParameters.OFFLINE self.has_changed = new_filter != self.value if self.has_changed: self.value = new_filter - def get_join_query(self): - return [''] - def get_where_query(self): if self.value: return f"AND dw_event.online_flag='{self.value.get('query_value')}' "\ diff --git a/search_events_app/filters/organizer_filter.py b/search_events_app/filters/organizer_filter.py index 274e8d9..96bfb30 100644 --- a/search_events_app/filters/organizer_filter.py +++ b/search_events_app/filters/organizer_filter.py @@ -13,9 +13,6 @@ def apply_filter(self, request): if self.has_changed: self.value = new_filter - def get_join_query(self): - return [''] - def get_where_query(self): if self.value: return 'AND ('\ diff --git a/search_events_app/filters/price_filter.py b/search_events_app/filters/price_filter.py index 193e174..f8cb516 100644 --- a/search_events_app/filters/price_filter.py +++ b/search_events_app/filters/price_filter.py @@ -8,14 +8,10 @@ def apply_filter(self, request): new_filter = None if price: new_filter = f'{price} event' - self.has_changed = new_filter != self.value if self.has_changed: self.value = new_filter - def get_join_query(self): - return [''] - def get_where_query(self): if self.value: if self.value == 'paid event': diff --git a/search_events_app/filters/start_date_filter.py b/search_events_app/filters/start_date_filter.py index 352f850..6463dad 100644 --- a/search_events_app/filters/start_date_filter.py +++ b/search_events_app/filters/start_date_filter.py @@ -16,9 +16,6 @@ def apply_filter(self, request): if self.has_changed: self.value = new_filter - def get_join_query(self): - return [''] - def get_where_query(self): if self.value: return 'AND CAST(CAST(event_start_date AS TIMESTAMP) AS DATE) ' \ diff --git a/search_events_app/filters/venue_filter.py b/search_events_app/filters/venue_filter.py index 26b3717..8f84bc0 100644 --- a/search_events_app/filters/venue_filter.py +++ b/search_events_app/filters/venue_filter.py @@ -13,9 +13,6 @@ def apply_filter(self, request): if self.has_changed: self.value = new_filter - def get_join_query(self): - return [''] - def get_where_query(self): if self.value: return 'AND (' \ diff --git a/search_events_app/services/filter_manager.py b/search_events_app/services/filter_manager.py index 5cc1068..b30ec1c 100644 --- a/search_events_app/services/filter_manager.py +++ b/search_events_app/services/filter_manager.py @@ -1,7 +1,5 @@ -from search_events_app.dto import ( - DTOFilter, - DTODBServiceFilter, -) +from search_events_app.dto import DTODBServiceFilter + from search_events_app.filters import ( BuyerFilter, CategoryFilter, diff --git a/templates/table_events.html b/templates/table_events.html index 4721785..e4343e0 100644 --- a/templates/table_events.html +++ b/templates/table_events.html @@ -10,7 +10,7 @@ - +
Admin DashboardAdmin Dashboard Name Category Format