def generate_dates(self, start, end, start_time=None, end_time=None, weekdays=None): """ Takes the given dates and generates the date tuples using rrule. The `except_for` field will be considered if present. """ if not (start and end): return [] start = start and sedate.as_datetime(start) end = end and sedate.as_datetime(end) if start == end: dates = (start, ) else: dates = rrule(DAILY, dtstart=start, until=end, byweekday=weekdays) if start_time is None or end_time is None: return [(d, d) for d in dates] else: if end_time < start_time: end_offset = timedelta(days=1) else: end_offset = timedelta() return [ ( datetime.combine(d, start_time), datetime.combine(d + end_offset, end_time) ) for d in dates ]
def dates(self): return ( sedate.as_datetime(self.date.data), sedate.as_datetime(self.date.data) + timedelta( days=1, microseconds=-1 ) )
def dates(self): if self.whole_day: return ( sedate.as_datetime(self.date.data), sedate.as_datetime(self.date.data) + timedelta( days=1, microseconds=-1 ) ) else: return ( self.combine_datetime('date', 'start_time'), self.combine_datetime('date', 'end_time'), )
def apply_meta(self, session): """ Updates the category, organization and issue date from the meta values. """ self.organization = None query = session.query(Organization.title) query = query.filter(Organization.name == self.organization_id).first() if query: self.organization = query[0] self.category = None query = session.query(Category.title) query = query.filter(Category.name == self.category_id).first() if query: self.category = query[0] self.first_issue = None if self._issues: query = session.query(Issue.date) query = query.filter(Issue.name.in_(self._issues.keys())) query = query.order_by(Issue.date).first() if query: self.first_issue = standardize_date(as_datetime(query[0]), 'UTC')
def add_events(session): start = as_datetime(datetime.today().date()) while start.weekday() != 6: start = start + timedelta(days=1) events = EventCollection(session) event = events.add( title="150 Jahre Govikon", start=start + timedelta(hours=11, minutes=0), end=start + timedelta(hours=22, minutes=0), timezone="Europe/Zurich", tags=["Party"], location="Sportanlage", content={"description": "Lorem ipsum."}, meta={"submitter_email": "*****@*****.**"}, ) event.submit() event.publish() event = events.add( title="Gemeindeversammlung", start=start + timedelta(days=2, hours=20, minutes=0), end=start + timedelta(days=2, hours=22, minutes=30), timezone="Europe/Zurich", tags=["Politics"], location="Gemeindesaal", content={"description": "Lorem ipsum."}, meta={"submitter_email": "*****@*****.**"}, ) event.submit() event.publish() event = events.add( title="MuKi Turnen", start=start + timedelta(days=2, hours=10, minutes=0), end=start + timedelta(days=2, hours=11, minutes=0), recurrence=( "RRULE:FREQ=WEEKLY;WKST=MO;BYDAY=TU,TH;UNTIL={0}".format( (start + timedelta(days=31)).strftime('%Y%m%dT%H%M%SZ') ) ), timezone="Europe/Zurich", tags=["Sports"], location="Turnhalle", content={"description": "Lorem ipsum."}, meta={"submitter_email": "*****@*****.**"}, ) event.submit() event.publish() event = events.add( title="Grümpelturnier", start=start + timedelta(days=7, hours=10, minutes=0), end=start + timedelta(days=7, hours=18, minutes=0), timezone="Europe/Zurich", tags=["Sports"], location="Sportanlage", content={"description": "Lorem ipsum."}, meta={"submitter_email": "*****@*****.**"}, ) event.submit() event.publish()
def combine_datetime(self, field, time_field): """ Takes the given date field and combines it with the given time field. """ d, t = getattr(self, field).data, getattr(self, time_field).data if d and not t: return sedate.as_datetime(d) if not t: return None return datetime.combine(d, t)
def view_get_occurrence(self, request): """ View a single occurrence of an event. """ layout = OccurrenceLayout(self, request) today = replace_timezone(as_datetime(date.today()), self.timezone) occurrences = self.event.occurrence_dates(localize=True) occurrences = list(filter(lambda x: x >= today, occurrences)) description = linkify(self.event.description).replace('\n', '<br>') session = request.app.session() ticket = TicketCollection(session).by_handler_id(self.event.id.hex) return { 'description': description, 'layout': layout, 'occurrence': self, 'occurrences': occurrences, 'ticket': ticket, 'title': self.title, }
def date_observer(self, date_): """ Changes the issue date of the notices when updating the date of the issue. At this moment, the transaction is not yet commited: Querying the current issue returns the old date. """ issues = object_session(self).query(Issue.name, Issue.date) issues = dict(issues.order_by(Issue.date)) issues[self.name] = date_ issues = { key: standardize_date(as_datetime(value), 'UTC') for key, value in issues.items() } for notice in self.notices(): dates = [issues.get(issue, None) for issue in notice._issues] dates = [date for date in dates if date] notice.first_issue = min(dates)
def query(self, start=None, end=None, tags=None, outdated=False): """ Queries occurrences with the given parameters. Finds all occurrences with any of the given tags and within the given start and end date. Start and end date are assumed to be dates only and therefore without a timezone - we search for the given date in the timezone of the occurrence!. If no start date is given and ``outdated`` is not set, only current occurrences are returned. """ query = self.session.query(Occurrence) if start is not None: assert type(start) is date start = as_datetime(start) expressions = [] for timezone in self.used_timezones: localized_start = replace_timezone(start, timezone) localized_start = standardize_date(localized_start, timezone) expressions.append( and_( Occurrence.timezone == timezone, Occurrence.start >= localized_start ) ) query = query.filter(or_(*expressions)) elif not outdated: start = as_datetime(date.today()) expressions = [] for timezone in self.used_timezones: localized_start = replace_timezone(start, timezone) localized_start = standardize_date(localized_start, timezone) expressions.append( and_( Occurrence.timezone == timezone, Occurrence.start >= localized_start ) ) query = query.filter(or_(*expressions)) if end is not None: assert type(end) is date end = as_datetime(end) end = end + timedelta(days=1) expressions = [] for timezone in self.used_timezones: localized_end = replace_timezone(end, timezone) localized_end = standardize_date(localized_end, timezone) expressions.append( and_( Occurrence.timezone == timezone, Occurrence.end < localized_end ) ) query = query.filter(or_(*expressions)) if tags: query = query.filter(Occurrence._tags.has_any(array(tags))) query = query.order_by(Occurrence.start, Occurrence.title) return query
def author_date_utc(self): if self.author_date.data: return standardize_date(as_datetime(self.author_date.data), 'UTC') return None