Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix rules in week table overrides #114

Merged
merged 9 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ jobs:
pip install --upgrade coveralls
coveralls --service=github --finish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
18 changes: 17 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Changelog
=========


2.1.4 (unreleased)
2.2.0 (unreleased)
------------------

- Extend the booking duration limit to 180 min.
Expand All @@ -21,6 +21,22 @@ Changelog
- Add booking_refuse_message to Prenotazione stringinterp variables.
[folix-01]

2.1.5 (2023-11-10)
------------------

- Fix release (2.1.4 was already made).
[cekk]


2.1.4 (2023-11-10)
------------------

- Fix week overrides when booking next year.
[cekk]

- Bypass limit for out-of-office bookings.
[cekk]


2.1.3 (2023-10-13)
------------------
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

setup(
name="redturtle.prenotazioni",
version="2.1.4.dev0",
version="2.2.0.dev0",
description="An add-on for Plone",
long_description=long_description,
# Get more from https://pypi.python.org/pypi?%3Aaction=list_classifiers
Expand Down Expand Up @@ -78,6 +78,7 @@
"plone.app.contenttypes",
"plone.app.robotframework[debug]",
"collective.MockMailHost",
"freezegun",
],
"app_io": [
"bravado",
Expand Down
25 changes: 16 additions & 9 deletions src/redturtle/prenotazioni/browser/prenotazioni_context_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,27 @@ def get_week_overrides(self, day):
from_day = int(override.get("from_day", ""))
to_month = int(override.get("to_month", ""))
to_day = int(override.get("to_day", ""))
toYear = day.year
to_year = day.year

if from_month > to_month:
# next year
toYear += 1

fromDate = date(day.year, from_month, from_day)
toDate = date(toYear, to_month, to_day)
if from_month <= to_month:
# same year (i.e from "10 aug" to "4 sep")
from_date = date(to_year, from_month, from_day)
to_date = date(to_year, to_month, to_day)
else:
# ends next year (i.e. from "20 dec" to "7 jan")
today_year = date.today().year
if today_year < to_year:
from_date = date(today_year, from_month, from_day)
to_date = date(to_year, to_month, to_day)
else:
from_date = date(to_year, from_month, from_day)
to_date = date(to_year + 1, to_month, to_day)

if isinstance(day, datetime):
if fromDate <= day.date() <= toDate:
if from_date <= day.date() <= to_date:
return override
else:
if fromDate <= day <= toDate:
if from_date <= day <= to_date:
return override
return {}

Expand Down
12 changes: 7 additions & 5 deletions src/redturtle/prenotazioni/tests/test_available_slots.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytz
import transaction
from freezegun import freeze_time
from plone import api
from plone.app.testing import (
SITE_OWNER_NAME,
Expand All @@ -18,6 +19,8 @@
from redturtle.prenotazioni.adapters.booker import IBooker
from redturtle.prenotazioni.testing import REDTURTLE_PRENOTAZIONI_API_FUNCTIONAL_TESTING

DATE_STR = "2023-05-14"


class TestAvailableSlots(unittest.TestCase):
layer = REDTURTLE_PRENOTAZIONI_API_FUNCTIONAL_TESTING
Expand Down Expand Up @@ -182,10 +185,7 @@ def test_month_slots_called_without_params_return_available_slots_of_current_mon
),
)

@unittest.skipIf(
date.today().day >= 20 or date.today().day <= 8,
"issue testing in the last days of a month",
)
@freeze_time(DATE_STR)
def test_if_start_and_not_end_return_all_available_slots_for_that_month(
self,
):
Expand All @@ -194,14 +194,16 @@ def test_if_start_and_not_end_return_all_available_slots_for_that_month(
current_month = now.month
next_month = current_month + 1

self.folder_prenotazioni.daData = now
transaction.commit()

# all mondays in next month
response = self.api_session.get(
"{}/@available-slots?start={}".format(
self.folder_prenotazioni.absolute_url(),
json_compatible(date(current_year, next_month, 1)),
)
)

# get next mondays in current month
expected = []
for week in calendar.monthcalendar(current_year, next_month):
Expand Down
37 changes: 21 additions & 16 deletions src/redturtle/prenotazioni/tests/test_day.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: UTF-8 -*-
import pytz
import unittest
from datetime import date, datetime, timedelta
from datetime import date, timedelta

from plone import api
from plone.app.testing import (
Expand All @@ -11,6 +12,7 @@
)
from plone.restapi.interfaces import ISerializeToJson
from plone.restapi.testing import RelativeSession
from redturtle.prenotazioni import tznow
from transaction import commit
from zope.component import getMultiAdapter
from zope.globalrequest import getRequest
Expand Down Expand Up @@ -65,13 +67,13 @@ def setUp(self):
row["morning_end"] = "1000"
self.folder_prenotazioni.week_table = week_table

self.today = datetime.now().replace(hour=8)
self.tomorrow = self.today + timedelta(1)

# fix timezone
api.portal.set_registry_record(
"plone.portal_timezone",
"Europe/Rome",
)
self.today = tznow().replace(hour=8)
self.tomorrow = self.today + timedelta(1)

commit()

Expand Down Expand Up @@ -106,10 +108,6 @@ def test_bookings_returned_by_gate(self):
results,
)

@unittest.skipIf(
date.today().day >= 20 or date.today().day <= 8,
"issue testing in the last days of a month",
)
def test_pauses_returned(self):
# le pause sono in localtime
self.folder_prenotazioni.pause_table = [
Expand All @@ -127,10 +125,14 @@ def test_pauses_returned(self):

results = response.json()["pauses"]
# la risposta è in UTC
tomorrow_start_utc = self.tomorrow.replace(hour=7, minute=15).astimezone(
pytz.utc
)
tomorrow_end_utc = self.tomorrow.replace(hour=8, minute=30).astimezone(pytz.utc)
self.assertIn(
{
"start": self.tomorrow.strftime("%Y-%m-%d") + "T05:15:00+00:00",
"end": self.tomorrow.strftime("%Y-%m-%d") + "T06:30:00+00:00",
"start": tomorrow_start_utc.strftime("%Y-%m-%dT%H:%M:00+00:00"),
"end": tomorrow_end_utc.strftime("%Y-%m-%dT%H:%M:00+00:00"),
},
results,
)
Expand All @@ -142,10 +144,6 @@ def test_bad_request_date(self):
self.assertEqual(res.json()["type"], "BadRequest")
self.assertEqual(res.status_code, 400)

@unittest.skipIf(
date.today().day >= 20 or date.today().day <= 8,
"issue testing in the last days of a month",
)
def test_daily_schedule(self):
# TODO: testare con timezone differenti
response = self.api_session.get(
Expand All @@ -161,12 +159,19 @@ def test_daily_schedule(self):
)
self.assertEqual(self.folder_prenotazioni.week_table[0]["morning_end"], "1000")
# la risposta è in UTC
tomorrow_start_utc = self.tomorrow.replace(hour=7, minute=00).astimezone(
pytz.utc
)
tomorrow_end_utc = self.tomorrow.replace(hour=10, minute=00).astimezone(
pytz.utc
)

self.assertEqual(
{
"afternoon": {"start": None, "end": None},
"morning": {
"start": self.tomorrow.strftime("%Y-%m-%d") + "T05:00:00+00:00",
"end": self.tomorrow.strftime("%Y-%m-%d") + "T08:00:00+00:00",
"start": tomorrow_start_utc.strftime("%Y-%m-%dT%H:%M:00+00:00"),
"end": tomorrow_end_utc.strftime("%Y-%m-%dT%H:%M:00+00:00"),
},
},
results["daily_schedule"],
Expand Down
11 changes: 8 additions & 3 deletions src/redturtle/prenotazioni/tests/test_gates_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ def test_if_gates_override_not_set_use_default(self):
type="PrenotazioniFolder",
title="Prenota foo",
daData=date.today(),
booking_types=[
{"name": "Type A", "duration": "30"},
],
gates=["Gate A"],
week_table=WEEK_TABLE_SCHEMA,
week_table_overrides=json.dumps(
Expand All @@ -110,6 +107,14 @@ def test_if_gates_override_not_set_use_default(self):
]
),
)
api.content.create(
type="PrenotazioneType",
title="Type A",
duration=30,
container=self.folder_prenotazioni,
gates=["all"],
)

view = api.content.get_view(
name="prenotazioni_context_state",
context=folder,
Expand Down
5 changes: 1 addition & 4 deletions src/redturtle/prenotazioni/tests/test_vacation_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,14 @@ def test_add_vacation(self):
res = self.api_session_anon.post(
self.folder_prenotazioni.absolute_url() + "/@booking",
json={
"booking_date": self.next_monday.replace(
hour=10, minute=10
).isoformat(),
"booking_date": start.isoformat(),
"booking_type": "Type A",
"fields": [
{"name": "title", "value": "Mario Rossi"},
{"name": "email", "value": "mario.rossi@example"},
],
},
)

self.assertEqual(res.status_code, 400)
self.assertEqual(
res.json()["message"],
Expand Down
17 changes: 14 additions & 3 deletions src/redturtle/prenotazioni/tests/test_week_table_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import unittest
from datetime import date
from freezegun import freeze_time

from plone import api
from plone.app.testing import (
Expand All @@ -19,7 +20,7 @@
)


class TestContextState(unittest.TestCase):
class TestWeekTableOverridesContextState(unittest.TestCase):
layer = REDTURTLE_PRENOTAZIONI_FUNCTIONAL_TESTING

def setUp(self):
Expand Down Expand Up @@ -93,6 +94,7 @@ def test_if_day_is_in_overrides_use_override_week_table(self):
json.loads(self.folder_prenotazioni.week_table_overrides)[0]["week_table"],
)

@freeze_time("2023-05-14")
def test_override_between_year(self):
self.folder_prenotazioni.week_table_overrides = json.dumps(
[
Expand All @@ -114,18 +116,27 @@ def test_override_between_year(self):
]
)
now = date.today()

# if in range, return table overrides
self.assertEqual(
self.view.get_week_table(date(now.year, 12, 25)),
json.loads(self.folder_prenotazioni.week_table_overrides)[0]["week_table"],
)

# if in range and next year, return table overrides
self.assertEqual(
self.view.get_week_table(date(now.year + 1, 1, 25)),
json.loads(self.folder_prenotazioni.week_table_overrides)[0]["week_table"],
)

# if out of range, return base table
self.assertEqual(
self.view.get_week_table(date(now.year, 5, 10)),
self.view.get_week_table(date(now.year, 10, 10)),
self.folder_prenotazioni.week_table,
)


class TestApiValidateDataOnPost(unittest.TestCase):
class TestWeekTableOverridesApiValidateDataOnPost(unittest.TestCase):
layer = REDTURTLE_PRENOTAZIONI_API_FUNCTIONAL_TESTING

def setUp(self):
Expand Down
Loading