Skip to content

Commit

Permalink
bookings global export, extended filters (#219)
Browse files Browse the repository at this point in the history
* Intermediate commit

* Doc

* created control

* Readme

* Readme

* Readme

* Additional try/except

* Additional try/except

* Locales

* Fix grammar

* Locales
  • Loading branch information
folix-01 authored Nov 27, 2024
1 parent dc229a6 commit c52f53a
Show file tree
Hide file tree
Showing 6 changed files with 393 additions and 156 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Changelog
2.8.1 (unreleased)
------------------

- Nothing changed yet.
- Extend bookings-export filters.
[folix-01]


2.8.0 (2024-11-26)
Expand Down
10 changes: 8 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -798,10 +798,16 @@ The script is located at src/redturtle/prenotazioni/scripts/notify_upcoming_book
@@bookings-export
-----------------

Export bookings by passed **date** in ISO format or today by default.
All the time parameters below are ISO formatted datetime strings

- **booking_start_from** - booking start from range.
- **booking_start_to** - booking start to range.
- **booking_creation_from** - booking created from range.
- **booking_creation_to** - bookking createtd to range
- **path** - booking folder path (es: "/Plone/booking_folder")

Example::
curl -i http://localhost:8080/Plone/@@bookings-export?date=2024-06-21
curl -i http://localhost:8080/Plone/@@bookings-export?booking_start_from=2023-10-22T12:27:18

Response::
Binary csv file
Expand Down
131 changes: 109 additions & 22 deletions src/redturtle/prenotazioni/browser/bookings_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

class BookingsExport(BrowserView):
filename = "Bookings_export"
date = None
booking_start_from = None
booking_start_to = None
booking_creation_from = None
booking_creation_to = None
path = None

@property
def csv_fields(self):
Expand Down Expand Up @@ -49,25 +53,62 @@ def csv_fields(self):
def csv_filename(self):
"""Return a filename for this csv"""

return "%s-%s.csv" % (self.filename, self.date.date().isoformat())
return "%s_%s.csv" % (
self.filename,
self.booking_start_from.date().isoformat()
+ "-"
+ self.booking_start_to.date().isoformat(),
)

@property
def brains(self):
created = (self.booking_creation_from or self.booking_creation_to) and {
"query": (self.booking_creation_from and self.booking_creation_to)
and (
get_default_timezone(True).localize(self.booking_creation_from),
get_default_timezone(True).localize(self.booking_creation_to),
)
or self.booking_creation_from
and get_default_timezone(True).localize(self.booking_creation_from)
or self.booking_creation_to
and get_default_timezone(True).localize(self.booking_creation_to),
"range": (self.booking_creation_from and self.booking_creation_to)
and "min:max"
or self.booking_creation_from
and "min"
or self.booking_creation_to
and "max",
}

return api.portal.get_tool("portal_catalog").unrestrictedSearchResults(
portal_type="Prenotazione",
Date={
"query": (
get_default_timezone(True).localize(self.date),
get_default_timezone(True).localize(
self.date.replace(hour=23, minute=59)
if created:
return api.portal.get_tool("portal_catalog").unrestrictedSearchResults(
portal_type="Prenotazione",
Date={
"query": (
get_default_timezone(True).localize(self.booking_start_from),
get_default_timezone(True).localize(self.booking_start_to),
),
),
"range": "min:max",
},
review_state="confirmed",
sort_on="Date",
)
"range": "min:max",
},
review_state="confirmed",
sort_on="Date",
path=self.path and {"query": self.path} or "",
created=created,
)
else:
return api.portal.get_tool("portal_catalog").unrestrictedSearchResults(
portal_type="Prenotazione",
Date={
"query": (
get_default_timezone(True).localize(self.booking_start_from),
get_default_timezone(True).localize(self.booking_start_to),
),
"range": "min:max",
},
review_state="confirmed",
sort_on="Date",
path=self.path and {"query": self.path} or "",
)

def setHeader(self, *args):
"""
Expand Down Expand Up @@ -145,19 +186,65 @@ def get_csv(self):
return buffer.getvalue().encode("utf-8")

def __call__(self):
date = self.request.get("date")
booking_start_from = self.request.get("booking_start_from")
booking_start_to = self.request.get("booking_start_to")
booking_creation_from = self.request.get("booking_creation_from")
booking_creation_to = self.request.get("booking_creation_to")
self.path = self.request.get("booking_folder_path")

if not booking_start_from:
self.booking_start_from = datetime.datetime.now().replace(
hour=0, minute=0, second=0, microsecond=0
)

else:
try:
self.booking_start_from = datetime.datetime.fromisoformat(
booking_start_from
)
except ValueError:
raise BadRequest(
api.portal_translate(_("Badly composed `booking_start_from` value"))
)

if not date:
self.date = datetime.datetime.combine(
datetime.date.today(), datetime.datetime.min.time()
if not booking_start_to:
self.booking_start_to = datetime.datetime.now().replace(
hour=23, minute=59, second=0, microsecond=0
)
else:
try:
self.date = datetime.datetime.combine(
datetime.date.fromisoformat(date), datetime.datetime.min.time()
self.booking_start_to = datetime.datetime.fromisoformat(
booking_start_to
)
except ValueError:
raise BadRequest(
api.portal_translate(_("Badly composed `booking_start_to` value"))
)

if booking_creation_from:
try:
self.booking_creation_from = datetime.datetime.fromisoformat(
booking_creation_from
)
except ValueError:
raise BadRequest(_("Bad date format passed"))
raise BadRequest(
api.portal_translate(
_("Badly composed `booking_creation_from` value")
)
)

if booking_creation_to:
try:
self.booking_creation_to = datetime.datetime.fromisoformat(
booking_creation_to
)
except ValueError:
raise BadRequest(
api.portal_translate(
_("Badly composed `booking_creation_to` value")
)
)

self.setHeader(
"Content-Disposition", "attachment;filename=%s" % self.csv_filename
)
Expand Down
Loading

0 comments on commit c52f53a

Please sign in to comment.