Offset in event dates generated by update_recurring_dates #195 (#196)

Offset in event dates generated by update_recurring_dates #195
This commit is contained in:
Daniel Grams 2021-06-18 10:40:33 +02:00 committed by GitHub
parent 93d1f0611b
commit 086ee211ce
9 changed files with 40 additions and 11 deletions

View File

@ -170,8 +170,8 @@ class EventRefSchema(EventIdSchema):
class EventSearchItemSchema(EventRefSchema):
description = marshmallow.auto_field()
start = marshmallow.auto_field()
end = marshmallow.auto_field()
start = CustomDateTimeField()
end = CustomDateTimeField()
recurrence_rule = marshmallow.auto_field()
photo = fields.Nested(ImageSchema)
place = fields.Nested(PlaceSearchItemSchema, attribute="event_place")

View File

@ -6,6 +6,7 @@ from project.api.event.schemas import (
EventSearchItemSchema,
EventSearchRequestSchema,
)
from project.api.fields import CustomDateTimeField
from project.api.schemas import PaginationRequestSchema, PaginationResponseSchema
from project.models import EventDate
@ -16,8 +17,8 @@ class EventDateSchema(marshmallow.SQLAlchemySchema):
load_instance = True
id = marshmallow.auto_field()
start = marshmallow.auto_field()
end = marshmallow.auto_field()
start = CustomDateTimeField()
end = CustomDateTimeField()
event = fields.Nested(EventRefSchema)
@ -27,7 +28,7 @@ class EventDateRefSchema(marshmallow.SQLAlchemySchema):
load_instance = True
id = marshmallow.auto_field()
start = marshmallow.auto_field()
start = CustomDateTimeField()
class EventDateListRequestSchema(PaginationRequestSchema):
@ -49,7 +50,7 @@ class EventDateSearchItemSchema(EventDateRefSchema):
model = EventDate
load_instance = True
end = marshmallow.auto_field()
end = CustomDateTimeField()
event = fields.Nested(EventSearchItemSchema)

View File

@ -18,6 +18,12 @@ class NumericStr(fields.String):
class CustomDateTimeField(fields.DateTime):
def _serialize(self, value, attr, obj, **kwargs):
if value:
value = value.astimezone(berlin_tz)
return super()._serialize(value, attr, obj, **kwargs)
def deserialize(self, value, attr, data, **kwargs):
result = super().deserialize(value, attr, data, **kwargs)

View File

@ -2,6 +2,7 @@ import click
from flask.cli import AppGroup
from project import app, db
from project.dateutils import berlin_tz
from project.services.event import (
get_recurring_events,
update_event_dates_with_recurrence_rule,
@ -12,13 +13,16 @@ event_cli = AppGroup("event")
@event_cli.command("update-recurring-dates")
def update_recurring_dates():
# Setting the timezone is neccessary for cli command
db.session.execute("SET timezone TO :val;", {"val": berlin_tz.zone})
events = get_recurring_events()
for event in events:
update_event_dates_with_recurrence_rule(event)
db.session.commit()
click.echo(f"{len(events)} event(s) where updated.")
click.echo(f"{len(events)} event(s) were updated.")
app.cli.add_command(event_cli)

View File

@ -273,7 +273,7 @@ def get_events_query(params):
def get_recurring_events():
return Event.query.filter(Event.recurrence_rule is not None).all()
return Event.query.filter(func.coalesce(Event.recurrence_rule, "") != "").all()
def update_event_dates_with_recurrence_rule(event):

View File

@ -42,7 +42,7 @@
<link rel="apple-touch-icon" sizes="180x180" href="{{ url_for('static', filename='apple-touch-icon.png')}}">
<link rel="icon" type="image/png" sizes="32x32" href="{{ url_for('static', filename='favicon-32x32.png')}}">
<link rel="icon" type="image/png" sizes="16x16" href="{{ url_for('static', filename='apple-touch-icon.png')}}">
<link rel="manifest" href="/site.webmanifest">
<link rel="manifest" href="{{ url_for('static', filename='site.webmanifest')}}">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

View File

@ -19,4 +19,5 @@ def test_search(client, seeder, utils):
seeder.create_event(admin_unit_id)
url = utils.get_url("api_v1_event_date_search", sort="-rating")
utils.get_ok(url)
response = utils.get_ok(url)
assert response.json["items"][0]["start"].endswith("+02:00")

View File

@ -4,4 +4,4 @@ def test_update_recurring_dates(client, seeder, app):
runner = app.test_cli_runner()
result = runner.invoke(args=["event", "update-recurring-dates"])
assert "1 event(s) where updated." in result.output
assert "1 event(s) were updated." in result.output

View File

@ -152,3 +152,20 @@ def test_get_meta_data(seeder, app):
with app.test_request_context():
meta = get_meta_data(event)
assert meta is not None
def test_get_recurring_events(client, seeder, app):
user_id, admin_unit_id = seeder.setup_base()
event_id = seeder.create_event(
admin_unit_id, recurrence_rule="RRULE:FREQ=DAILY;COUNT=7"
)
seeder.create_event(admin_unit_id, recurrence_rule=None)
seeder.create_event(admin_unit_id, recurrence_rule="")
with app.app_context():
from project.services.event import get_recurring_events
recurring_events = get_recurring_events()
assert len(recurring_events) == 1
assert recurring_events[0].id == event_id