Skip to content

Commit

Permalink
Small refactoring of recuring event
Browse files Browse the repository at this point in the history
  • Loading branch information
anufrievroman committed Jul 21, 2024
1 parent d162db2 commit a6d4a2c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
10 changes: 5 additions & 5 deletions calcure/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from calcure.translations.en import *


__version__ = "3.0.2"
__version__ = "3.0.3"


def read_items_from_user_arguments(screen, user_tasks, user_events, task_saver_csv, event_saver_csv):
Expand Down Expand Up @@ -750,8 +750,8 @@ def render(self):
header_view.render()

# Display the events from current day to as many as possible days:
repeated_user_events = RepeatedEvents(self.user_events, cf.USE_PERSIAN_CALENDAR, self.screen.year, self.screen.month)
repeated_ics_events = RepeatedEvents(self.user_ics_events, cf.USE_PERSIAN_CALENDAR, self.screen.year, self.screen.month)
repeated_user_events = RepeatedEvents(self.user_events, cf.USE_PERSIAN_CALENDAR, self.screen.year)
repeated_ics_events = RepeatedEvents(self.user_ics_events, cf.USE_PERSIAN_CALENDAR, self.screen.year)
max_num_days = (self.screen.y_max - 5)//2
vertical_shift = 0

Expand Down Expand Up @@ -813,8 +813,8 @@ def render(self):
days_name_view.render()

# Displaying the dates and events:
repeated_user_events = RepeatedEvents(self.user_events, cf.USE_PERSIAN_CALENDAR, self.screen.year, self.screen.month)
repeated_ics_events = RepeatedEvents(self.user_ics_events, cf.USE_PERSIAN_CALENDAR, self.screen.year, self.screen.month)
repeated_user_events = RepeatedEvents(self.user_events, cf.USE_PERSIAN_CALENDAR, self.screen.year)
repeated_ics_events = RepeatedEvents(self.user_ics_events, cf.USE_PERSIAN_CALENDAR, self.screen.year)
num_events_this_month = 0
for row, week in enumerate(dates):
for col, day in enumerate(week):
Expand Down
15 changes: 10 additions & 5 deletions calcure/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def filter_events_that_day(self, screen):
class RepeatedEvents(Events):
"""List of events that are repetitions of main events"""

def __init__(self, user_events, use_persian_calendar, until_year, until_month):
def __init__(self, user_events, use_persian_calendar, current_year):
super().__init__()
self.user_events = user_events
self.use_persian_calendar = use_persian_calendar
Expand All @@ -375,10 +375,14 @@ def __init__(self, user_events, use_persian_calendar, until_year, until_month):

elif event.rrule:
dtstart = event.getDatetime()


# For infinitely repeated events, we limit them by end of the next year:
if 'COUNT' not in event.rrule and 'UNTIL' not in event.rrule:
event.rrule += ';UNTIL=' + datetime.datetime(until_year + 1, until_month, 1).strftime('%Y%m%dT%H%M%SZ')

until_year = current_year + 1
until_month = 12
event.rrule += ';UNTIL=' + datetime.datetime(until_year, until_month, 1).strftime('%Y%m%dT%H%M%SZ')

# Create a list of dates of repeated events:
rule = rrulestr(event.rrule, dtstart=dtstart)
rset = rruleset()
rset.rrule(rule)
Expand All @@ -390,7 +394,8 @@ def __init__(self, user_events, use_persian_calendar, until_year, until_month):
for exdate in exdates.dts:
exdate_dt = datetime.datetime.combine(exdate.dt, datetime.time.min, tzinfo=dtstart.tzinfo) if not isinstance(exdate.dt, datetime.datetime) else exdate.dt
rset.exdate(exdate_dt)


# Create an event for each repetition and add to the list:
for date in list(rset)[1:]:
self.add_item(UserRepeatedEvent(event.item_id, date.year, date.month, date.day, event.name,
event.status, event.privacy, event.calendar_number))
Expand Down
11 changes: 6 additions & 5 deletions calcure/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,16 +438,17 @@ def parse_event(self, component, index, calendar_number):
if dt_difference.days > 0:
repetition = dt_difference.days
frequency = Frequency.DAILY

# Parsing recuring rules:
if 'rrule' in component:
rrule = component.get('rrule').to_ical().decode('utf-8')
exdate = component.get('exdate')
repetition = 0

except AttributeError:
logging.error("Failed to parse event %s on %s.", name, dt)
pass

if 'rrule' in component:
rrule = component.get('rrule').to_ical().decode('utf-8')
exdate = component.get('exdate')
repetition = 0

# Add start time to non-all-day events:
all_day = component.get('dtstart').params.get('VALUE') == 'DATE' if component.get('dtstart') else False
if not all_day:
Expand Down

0 comments on commit a6d4a2c

Please sign in to comment.