def get_events(self): events = [] for x_event in self._schedule: c_event = Event() c_event.from_mongo(x_event) events.append(c_event.json()) return events
def update_event(self, _event_uuid=None, new_event: Event = None): if _event_uuid is None or new_event is None: return [False, "No uuid or new_event set"] for idx, l_event in enumerate(self._schedule): if 'event_uuid' in l_event: c_event = Event() c_event.from_mongo(l_event) if c_event.event_uuid == _event_uuid: self._schedule[idx] = new_event.json() return [True, "event found and updated"] return [False, "event not found and updated"]
def get_schedule(): request_json = request.json if request_json["arduino_uuid"] is None: return return_json(succes=False, error="Arduino UUID is not set") arduino_uuid = request_json["arduino_uuid"] if not data_validation.validate_uuid(arduino_uuid): return return_json(success=False, error="Arduino UUID is not valid") # The maximum amount of events that can be returned due to limited memory try: max_events_amount = int(request_json["max_events_amount"]) except TypeError: return return_json(success=False, error="Max_event_amounts must be a number") except KeyError: max_events_amount = 20 if not data_validation.verify_arduino(arduino_uuid): return return_json(success=False, error="Arduino is not linked") c_user_uuid = arduino_db.get_user_uuid_by_arduino_uuid(arduino_uuid) res = user_db.get_user(uuid=c_user_uuid) if not res[0]: return return_json(success=False, error="User not found") c_user_uuid = res[1].uuid c_schedule = schedule_db.get_user_schedule(c_user_uuid).get_events() # User schedule if len(c_schedule) > max_events_amount: c_schedule = c_schedule[-max_events_amount:] for idx, l_item in enumerate(c_schedule): c_item = Event() c_item.from_mongo(l_item) c_schedule[idx] = { 'start': c_item.start, 'end': c_item.end, 'title': c_item.title, 'content': c_item.content, 'location': c_item.location } return return_json(success=True, data={'schedule': c_schedule})
def delete_event(self, _event_uuid=None, delete_all: bool = False): if delete_all: self._schedule = [] return return_json(success=True, data={'message': 'Deleted all events'}) for idx, l_event in enumerate(self._schedule): if 'event_uuid' in l_event: c_event = Event() c_event.from_mongo(l_event) if c_event.event_uuid == _event_uuid: del self._schedule[idx] return return_json(success=True, data={"message": "Deleted one event"}) return return_json(success=False, error="Event not found")
def check_for_upcoming_events(self): current_time = datetime.datetime.today() upcoming_events = [] for idx, l_event in enumerate(self._schedule): c_event = Event() c_event.from_mongo(l_event) if c_event.remind_minutes_before is not None: start_time = datetime.datetime.strptime( c_event.start, "%d/%m/%Y %H:%M:%S") remaining_seconds = (start_time - current_time).total_seconds() # Convert seconds to days remaining_minutes = divmod(remaining_seconds, 60)[0] if remaining_minutes <= c_event.remind_minutes_before and remaining_minutes > 0: # So that the notifications won't be send twice c_event.remind_minutes_before = None self._schedule[idx] = c_event.json() upcoming_events.append(c_event) return upcoming_events