|
| 1 | +from odoo import http |
| 2 | +from odoo.http import request |
| 3 | +from odoo.addons.website_appointment.controllers.appointment import WebsiteAppointment |
| 4 | + |
| 5 | +class WebsiteAppointmentFiltered(WebsiteAppointment): |
| 6 | + |
| 7 | + @http.route() |
| 8 | + def appointment_type_index(self, page=1, **kwargs): |
| 9 | + available_domain = self._appointment_website_domain() |
| 10 | + extra_domain = [] |
| 11 | + |
| 12 | + if kwargs.get("appointment_type"): |
| 13 | + if kwargs["appointment_type"] == "online": |
| 14 | + extra_domain.append(("location_id", "=", False)) |
| 15 | + elif kwargs["appointment_type"] == "offline": |
| 16 | + extra_domain.append(("location_id", "!=", False)) |
| 17 | + |
| 18 | + if kwargs.get("payment_step") == "required": |
| 19 | + extra_domain.append(("has_payment_step", "=", True)) |
| 20 | + elif kwargs.get("payment_step") == "not_required": |
| 21 | + extra_domain.append(("has_payment_step", "=", False)) |
| 22 | + |
| 23 | + if kwargs.get("schedule_based_on"): |
| 24 | + if kwargs["schedule_based_on"] == "users": |
| 25 | + extra_domain.append(("schedule_based_on", "=", "users")) |
| 26 | + elif kwargs["schedule_based_on"] == "resources": |
| 27 | + extra_domain.append(("schedule_based_on", "=", "resources")) |
| 28 | + |
| 29 | + if kwargs.get("search"): |
| 30 | + extra_domain.append(("name", "ilike", kwargs["search"])) |
| 31 | + |
| 32 | + final_domain = available_domain + extra_domain |
| 33 | + appointments = request.env["appointment.type"].sudo().search(final_domain) |
| 34 | + |
| 35 | + # Pass kwargs to persist filters in pagination |
| 36 | + values = self._prepare_appointments_cards_data( |
| 37 | + appointment_types=appointments, |
| 38 | + page=page, |
| 39 | + **kwargs # Ensures all filters persist in pagination |
| 40 | + ) |
| 41 | + |
| 42 | + return request.render("website_appointment.appointments_cards_layout", values) |
| 43 | + |
| 44 | + def _prepare_appointments_cards_data(self, page, appointment_types, **kwargs): |
| 45 | + """ |
| 46 | + Compute specific data for the cards layout, ensuring filters persist in pagination. |
| 47 | + """ |
| 48 | + APPOINTMENTS_PER_PAGE = 12 |
| 49 | + website = request.website |
| 50 | + appointment_count = len(appointment_types) |
| 51 | + |
| 52 | + pager = website.pager( |
| 53 | + url='/appointment', |
| 54 | + url_args=kwargs, # Keeps filters in pagination |
| 55 | + total=appointment_count, |
| 56 | + page=page, |
| 57 | + step=APPOINTMENTS_PER_PAGE, |
| 58 | + scope=5, |
| 59 | + ) |
| 60 | + |
| 61 | + appointment_types = appointment_types.sorted('is_published', reverse=True)[pager['offset']:pager['offset'] + APPOINTMENTS_PER_PAGE] |
| 62 | + |
| 63 | + return { |
| 64 | + 'appointment_types': appointment_types, |
| 65 | + 'current_search': kwargs.get('search'), |
| 66 | + 'pager': pager, |
| 67 | + 'filter_appointment_type_ids': kwargs.get('filter_appointment_type_ids'), |
| 68 | + 'filter_staff_user_ids': kwargs.get('filter_staff_user_ids'), |
| 69 | + 'invite_token': kwargs.get('invite_token'), |
| 70 | + 'search_count': appointment_count, |
| 71 | + } |
0 commit comments