Browse Source

Added management system so that adding duplicate workdays is avoided

master
Raphael Roberts 6 years ago
parent
commit
c7ff22f8cf
  1. 20
      panera_sync/models.py
  2. 1
      panera_sync/scraper.py
  3. 11
      panera_sync/utils.py
  4. 55
      panera_sync/workday_creator.py
  5. 3
      test.py

20
panera_sync/models.py

@ -2,6 +2,7 @@ import datetime
from gapi.apis.calendar_api.models import Event
from .constants import WEEKDAYS, WEEK
from .utils import get_previous_wednesday
class Workday:
@ -17,19 +18,30 @@ class Workday:
e.add_reminder(60)
return e
@classmethod
def from_event(cls, event: Event):
return cls(event.start, event.end, event.location)
def get_week_start(self):
return get_previous_wednesday(self.start_time)
class PaneraWeek:
def __init__(self, start_date: datetime.date, work_location):
def __init__(self, start_date: datetime.date):
if start_date.weekday() != WEEKDAYS.W:
raise ValueError(
('"start_date is" on a "{:%A}" instead of Wednesday'.format(start_date))
)
self.start_date = start_date
self.workdays = []
self._workdays = []
def get_week_end(self):
def get_week_end(self) -> datetime.date:
return self.start_date + datetime.timedelta(days=7 - 1)
def add_workday(self, workday: Workday):
self.workdays.append(workday)
self._workdays.append(workday)
def workdays(self):
for workday in self._workdays:
yield workday

1
panera_sync/scraper.py

@ -0,0 +1 @@
from rlbr_browser.browser import start_browser

11
panera_sync/utils.py

@ -0,0 +1,11 @@
from typing import Union
import datetime
from .constants import WEEKDAYS
def get_previous_wednesday(date: Union[datetime.date, datetime.datetime]):
delta = (date.weekday() - WEEKDAYS.W) % 7
if isinstance(date, datetime.datetime):
date = datetime.date(date.year, date.month, date.day)
return date - datetime.timedelta(days=delta)

55
panera_sync/workday_creator.py

@ -0,0 +1,55 @@
import datetime
from gapi.apis.calendar_api import Calendar, calendar_api, Event
from .models import PaneraWeek, Workday
class WorkdayCreator:
def __init__(self):
self.created_workdays = {}
def create(
self, start_time: datetime.datetime, end_time: datetime.datetime, location
):
try:
return self.created_workdays[(start_time, end_time, location)]
except KeyError:
workday = Workday(start_time, end_time, location)
self.created_workdays[(start_time, end_time, location)] = workday
return workday
def add(self, workday: Workday):
self.created_workdays[
(workday.start_time, workday.end_time, workday.location)
] = workday
def from_event(self, event: Event):
return self.create(event.start, event.end, event.location)
def grab_workdays_from_calendar(
self, calendar: Calendar, start: datetime.date, end: datetime.date
):
workday: Workday
return sorted(
map(self.from_event, calendar.get_events(start, end)),
key=lambda workday: workday.start_time,
)
def is_present(self, workday: Workday):
return (
workday.start_time,
workday.end_time,
workday.location,
) in self.created_workdays.keys()
def upload_workweek(self, calendar: Calendar, week: PaneraWeek):
grab_workdays_from_calendar(
datetime.datetime.combine(week.start, datetime.time(0, 0, 0)),
datetime.datetime.combine(week.get_week_end(), datetime.time(23, 59, 59)),
calendar,
)
workday: Workday
for workday in week.workdays:
if not self.is_present(workday):
calendar.update_or_add_event(workday.convert_to_Event())

3
test.py

@ -1,6 +1,9 @@
import datetime
from panera_sync.constants import WEEKDAYS
from panera_sync.models import PaneraWeek
from panera_sync.utils import get_previous_wednesday
if __name__ == "__main__":
today = datetime.datetime.today()
P = PaneraWeek(datetime.date(2020, 1, 22))
pw = get_previous_wednesday(today)