From f85ae72cc5b320c17d548d9bf1b2269dbfde2962 Mon Sep 17 00:00:00 2001 From: rizwanisready Date: Thu, 11 Apr 2024 22:39:34 +0530 Subject: [PATCH] corrected event filtered query --- goodtimes/services.py | 37 ++++++++-- manage_events/api/serializers.py | 23 +++++++ manage_events/api/views.py | 2 +- manage_wallets/forms.py | 12 +++- manage_wallets/urls.py | 1 + manage_wallets/views.py | 67 ++++++++++++++++++- templates/manage_events/event_details.html | 25 +++++++ templates/manage_events/event_list.html | 7 ++ templates/manage_wallets/account_list.html | 24 ++++++- .../manage_wallets/bank_account_edit.html | 60 +++++++++++++++++ 10 files changed, 249 insertions(+), 9 deletions(-) create mode 100644 templates/manage_wallets/bank_account_edit.html diff --git a/goodtimes/services.py b/goodtimes/services.py index c960d50..edc485f 100644 --- a/goodtimes/services.py +++ b/goodtimes/services.py @@ -403,28 +403,55 @@ class EventFilterService: def filter_events_by_search(search_query=None): today = timezone.now().date() filtered_events = Event.objects.filter( - key_guest__isnull=False, deleted=False, active=True, draft=False + deleted=False, active=True, draft=False ) # Optional search filtering on title and key_guest (modify as needed) if search_query: + print("search_query: ", search_query) filtered_events = filtered_events.filter( Q(title__icontains=search_query) | Q(key_guest__icontains=search_query) - | Q(tags__name__in=[search_query]) | Q(venue__address__icontains=search_query) ) + print("filtered_events: ", filtered_events) # Filter for current, future, or ongoing events current_and_future_events_query = Q( start_date__lte=today, end_date__gte=today ) | Q(start_date__gt=today) - filtered_events = filtered_events.filter( - current_and_future_events_query - ).distinct() + filtered_events = filtered_events.filter(current_and_future_events_query) return filtered_events + @staticmethod + def filter_events_by_tags(search_query=None): + today = timezone.now().date() + if search_query: + print("search_query: ", search_query) + filtered_events = Event.objects.filter( + tags__isnull=False, deleted=False, active=True, draft=False + ) + + filtered_events = ( + filtered_events.annotate( + matched_tags=Count( + "tags", filter=Q(tags__name__icontains=search_query), distinct=True + ) + ) + .filter(matched_tags__gt=0) + .distinct() + ) + print("filtered_events: ", filtered_events) + + # Filter for current, future, or ongoing events + current_and_future_events_query = Q( + start_date__lte=today, end_date__gte=today + ) | Q(start_date__gt=today) + filtered_events = filtered_events.filter(current_and_future_events_query) + + return filtered_events + @staticmethod def filter_events(filter_type, principal=None): today = timezone.now().date() diff --git a/manage_events/api/serializers.py b/manage_events/api/serializers.py index 80bbd9e..6d9ffc3 100644 --- a/manage_events/api/serializers.py +++ b/manage_events/api/serializers.py @@ -157,6 +157,29 @@ class CreateEventSerializer(serializers.ModelSerializer): return event + def update(self, instance, validated_data): + tags = validated_data.pop("tags", None) + images_data = validated_data.pop("images", None) + + # Update fields if there is any change. + if tags is not None: + # Clear existing tags and add the new ones + # instance.tags.clear() + instance.tags.add(*tags) + + if images_data is not None: + # Assuming you want to add new images without deleting the old ones + # If you want to replace them, you should delete the old images first + for image_data in images_data: + EventImage.objects.create(event=instance, image=image_data) + + # Update other fields + for attr, value in validated_data.items(): + setattr(instance, attr, value) + instance.save() + + return instance + class CreateVenueSerializer(serializers.ModelSerializer): class Meta: diff --git a/manage_events/api/views.py b/manage_events/api/views.py index 81d392f..5acc4ad 100644 --- a/manage_events/api/views.py +++ b/manage_events/api/views.py @@ -177,7 +177,7 @@ class EventsAPIView(APIView): search_query=query ) elif filter == "tags": - events = services.EventFilterService.filter_events_by_search( + events = services.EventFilterService.filter_events_by_tags( search_query=query ) elif filter == "category" and category_id is not None: diff --git a/manage_wallets/forms.py b/manage_wallets/forms.py index 1971ace..47ca658 100644 --- a/manage_wallets/forms.py +++ b/manage_wallets/forms.py @@ -1,5 +1,5 @@ from django import forms -from .models import Wallet, Transaction +from .models import Wallet, Transaction, PrincipalBankAccount class WalletForm(forms.ModelForm): @@ -50,3 +50,13 @@ class TransactionForm(forms.ModelForm): "product_id": forms.TextInput(attrs={"class": "form-control"}), "reference_id": forms.TextInput(attrs={"class": "form-control"}), } + + +class PrincipalBankAccountVerificationForm(forms.ModelForm): + class Meta: + model = PrincipalBankAccount + fields = ["is_verified"] # Specify only the 'is_verified' field + + def __init__(self, *args, **kwargs): + super(PrincipalBankAccountVerificationForm, self).__init__(*args, **kwargs) + # You can add any additional customization to the 'is_verified' field here if needed diff --git a/manage_wallets/urls.py b/manage_wallets/urls.py index 465a0d5..5922a3f 100644 --- a/manage_wallets/urls.py +++ b/manage_wallets/urls.py @@ -10,6 +10,7 @@ urlpatterns = [ path("payment/", views.PaymentListView.as_view(), name="payment_list"), # for manage account related url path("bank-accounts/", views.BankAccountListView.as_view(), name="account_list"), + path("edit-bank-account//", views.BankAccountCreateOrUpdateView.as_view(), name="edit_bank_account"), # for manage withdraw related url path("withdrawals/", views.WithdrawalListView.as_view(), name="withdrawal_list"), path("update-status/", views.StatusUpdateView.as_view(), name="update_status"), diff --git a/manage_wallets/views.py b/manage_wallets/views.py index 9746284..2f7a48e 100644 --- a/manage_wallets/views.py +++ b/manage_wallets/views.py @@ -1,10 +1,11 @@ -from django.shortcuts import render, redirect +from django.shortcuts import get_object_or_404, render, redirect from django.contrib.auth.mixins import LoginRequiredMixin from django.urls import reverse_lazy from accounts import resource_action from django.views import generic from django.contrib import messages from goodtimes import constants +from manage_wallets.forms import PrincipalBankAccountVerificationForm from .models import PrincipalBankAccount, Wallet, Transaction, WithdrawalRequest """Wallet Related VIew""" @@ -94,3 +95,67 @@ class StatusUpdateView(LoginRequiredMixin, generic.View): # Redirect to the desired URL after form submission return redirect(self.success_url) + + +class BankAccountCreateOrUpdateView(LoginRequiredMixin, generic.View): + # Set the page_name and resource + page_name = resource_action.RESOURCE_MANAGE_BANK_ACCOUNTS + resource = resource_action.RESOURCE_MANAGE_BANK_ACCOUNTS + + # Initialize the action as ACTION_CREATE (can change based on logic) + action = resource_action.ACTION_CREATE # Default action + + template_name = "manage_wallets/bank_account_edit.html" + model = PrincipalBankAccount + form_class = PrincipalBankAccountVerificationForm + success_url = reverse_lazy("manage_wallets:account_list") + error_message = "An error occurred while saving the data." + + # Determine the success message dynamically based on whether it's an update or create + def get_success_message(self): + self.success_message = ( + constants.RECORD_CREATED if not self.object else constants.RECORD_UPDATED + ) + return self.success_message + + # Get the object (if exists) based on URL parameter 'pk' + def get_object(self): + pk = self.kwargs.get("pk") + return get_object_or_404(self.model, pk=pk) if pk else None + + # Add page_name and operation to the context + def get_context_data(self, **kwargs): + context = { + "page_name": self.page_name, + "operation": "Add" if not self.object else "Edit", + } + context.update(kwargs) # Include any additional context data passed to the view + return context + + def get(self, request, *args, **kwargs): + self.object = self.get_object() + + # If an object is found, change action to ACTION_UPDATE + if self.object is not None: + self.action = resource_action.ACTION_UPDATE + + form = self.form_class(instance=self.object) + context = self.get_context_data(form=form) + return render(request, self.template_name, context=context) + + def post(self, request, *args, **kwargs): + self.object = self.get_object() + + # If an object is found, change action to ACTION_UPDATE + if self.object is not None: + self.action = resource_action.ACTION_UPDATE + + form = self.form_class(request.POST, instance=self.object) + if not form.is_valid(): + print(form.errors) + context = self.get_context_data(form=form) + return render(request, self.template_name, context=context) + + form.save() + messages.success(self.request, self.get_success_message()) + return redirect(self.success_url) \ No newline at end of file diff --git a/templates/manage_events/event_details.html b/templates/manage_events/event_details.html index 1774348..b96a7b5 100644 --- a/templates/manage_events/event_details.html +++ b/templates/manage_events/event_details.html @@ -50,6 +50,14 @@

Status: {{ event.status }}

Entry: {{ event.entry_type }} - ${{ event.entry_fee }}

+ + {% if event.key_guest %} +

+ Key Guests: {{ event.key_guest }} +

+ {% else %} +

No key guests announced.

+ {% endif %} @@ -89,6 +97,23 @@ + +
+
+
Tags
+ {% if event.tags.all %} +
+ {% for tag in event.tags.all %} + {{ tag.name }} + {% endfor %} +
+ {% else %} +

No tags for this event.

+ {% endif %} +
+
+ +
diff --git a/templates/manage_events/event_list.html b/templates/manage_events/event_list.html index c951d19..1cf37e4 100644 --- a/templates/manage_events/event_list.html +++ b/templates/manage_events/event_list.html @@ -101,6 +101,13 @@ d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"> +
  • + + + visibility + + +
  • diff --git a/templates/manage_wallets/account_list.html b/templates/manage_wallets/account_list.html index d0e75f0..c3a89b7 100644 --- a/templates/manage_wallets/account_list.html +++ b/templates/manage_wallets/account_list.html @@ -54,6 +54,8 @@ Submitted + Action @@ -67,8 +69,28 @@ {{account_obj.account_no}} {{account_obj.first_name}} {{account_obj.last_name}} - {{account_obj.is_verified}} + + + {{account_obj.created_on}} + + + {% endfor %} diff --git a/templates/manage_wallets/bank_account_edit.html b/templates/manage_wallets/bank_account_edit.html new file mode 100644 index 0000000..661db77 --- /dev/null +++ b/templates/manage_wallets/bank_account_edit.html @@ -0,0 +1,60 @@ +{% extends 'layout/base_template.html' %} +{% load static %} +{% block stylesheet %} + + + {% include "cdn_through_html/quill_cdn_css.html" %} + {{form.media}} + +{% endblock %} + +{% block content %} + +
    +
    +
    +
    +

    {{operation}} Bank Account

    +
    +
    + +
    +
    +
    +
    +
    +
    + +
    + {% csrf_token %} + {% include 'includes/dynamic_template_form.html' with form=form %} +
    +
    +
    +
    + +
    +
    +
    +
    +
    +
    + + +{% endblock content %} + +{% block javascript %} + + + {% include "cdn_through_html/quill_cdn_js.html" %} + + + +{% endblock %} \ No newline at end of file