Beispiel #1
0
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 test_event_collection_remove_stale_events(session):
    timezone = 'UTC'

    events = EventCollection(session)
    events.add(
        title='Event', timezone=timezone,
        start=datetime(2010, 6, 1, 10),
        end=datetime(2010, 6, 1, 10) + timedelta(hours=1),
    )
    assert events.query().count() == 1

    max_stale = standardize_date(datetime.today() + timedelta(days=2), 'UTC')
    events.remove_stale_events(max_stale=max_stale)
    assert events.query().count() == 0
def test_unique_names(session):
    events = EventCollection(session)
    added = [
        events.add(
            title='Squirrel Park Visit',
            start=datetime(2015, 6, 16, 9, 30),
            end=datetime(2015, 6, 16, 18, 00),
            timezone='US/Eastern'
        ) for x in range(11)
    ]
    assert added[0].name == 'squirrel-park-visit'
    assert added[1].name == 'squirrel-park-visit-1'
    assert added[2].name == 'squirrel-park-visit-2'
    assert added[3].name == 'squirrel-park-visit-3'
    assert added[4].name == 'squirrel-park-visit-4'
    assert added[5].name == 'squirrel-park-visit-5'
    assert added[6].name == 'squirrel-park-visit-6'
    assert added[7].name == 'squirrel-park-visit-7'
    assert added[8].name == 'squirrel-park-visit-8'
    assert added[9].name == 'squirrel-park-visit-9'
    assert added[10].name == 'squirrel-park-visit-10'

    events.delete(added[6])
    event = events.add(
        title='Squirrel Park Visit',
        start=datetime(2015, 6, 16, 9, 30),
        end=datetime(2015, 6, 16, 18, 00),
        timezone='US/Eastern',
        recurrence='RRULE:FREQ=DAILY;INTERVAL=1;COUNT=5'
    )
    assert event.name == 'squirrel-park-visit-6'

    event.submit()
    event.publish()
    assert event.occurrences[0].name == 'squirrel-park-visit-6-2015-06-16'
    assert event.occurrences[1].name == 'squirrel-park-visit-6-2015-06-17'
    assert event.occurrences[2].name == 'squirrel-park-visit-6-2015-06-18'
    assert event.occurrences[3].name == 'squirrel-park-visit-6-2015-06-19'
    assert event.occurrences[4].name == 'squirrel-park-visit-6-2015-06-20'

    assert events.by_name('test') is None
    assert events.by_name('squirrel-park-visit-6') == event

    occurrences = OccurrenceCollection(session)
    assert occurrences.by_name('test') is None

    occurrence = occurrences.by_name('squirrel-park-visit-6-2015-06-20')
    assert occurrence == event.occurrences[4]
def test_event_collection_pagination(session):
    events = EventCollection(session)

    assert events.page_index == 0
    assert events.pages_count == 0
    assert events.batch == []

    for year in range(2008, 2011):
        for month in range(1, 13):
            event = events.add(
                title='Event {0}-{1}'.format(year, month),
                start=datetime(year, month, 18, 14, 00),
                end=datetime(year, month, 18, 16, 00),
                timezone='US/Eastern',
                tags=['month-{0}'.format(month)],
                recurrence='RRULE:FREQ=DAILY;INTERVAL=1;COUNT=4'
            )
            event.submit()
            if year > 2008:
                event.publish()
            if year > 2009:
                event.withdraw()
    assert events.query().count() == 3 * 12

    events = EventCollection(session, state='initiated')
    assert events.subset_count == 0

    events = EventCollection(session, state='submitted')
    assert events.subset_count == 12
    assert all([e.start.year == 2008 for e in events.batch])
    assert all([e.start.month < 11 for e in events.batch])
    assert len(events.next.batch) == 12 - events.batch_size
    assert all([e.start.year == 2008 for e in events.next.batch])
    assert all([e.start.month > 10 for e in events.next.batch])

    events = EventCollection(session, state='published')
    assert events.subset_count == 12
    assert all([e.start.year == 2009 for e in events.batch])
    assert all([e.start.month < 11 for e in events.batch])
    assert len(events.next.batch) == 12 - events.batch_size
    assert all([e.start.year == 2009 for e in events.next.batch])
    assert all([e.start.month > 10 for e in events.next.batch])

    events = EventCollection(session, state='withdrawn')
    assert events.subset_count == 12
    assert all([e.start.year == 2010 for e in events.batch])
    assert all([e.start.month < 11 for e in events.batch])
    assert len(events.next.batch) == 12 - events.batch_size
    assert all([e.start.year == 2010 for e in events.next.batch])
    assert all([e.start.month > 10 for e in events.next.batch])
def test_event_collection(session):
    events = EventCollection(session)
    assert events.query().count() == 0

    event_1 = events.add(
        title='Squirrel Park Visit',
        start=datetime(2015, 6, 16, 9, 30),
        end=datetime(2015, 6, 16, 18, 00),
        timezone='US/Eastern',
        content={
            'description': '<em>Furry</em> things will happen!'
        },
        location='Squirrel Park',
        tags=['fun', 'animals'],
        recurrence='RRULE:FREQ=DAILY;INTERVAL=1;COUNT=5'
    )
    event_1.submit()
    event_1.publish()
    event_2 = events.add(
        title='History of the Squirrel Park',
        start=datetime(2015, 6, 18, 14, 00),
        end=datetime(2015, 6, 18, 16, 00),
        timezone='US/Eastern',
        content={
            'description': 'Learn how the Park got so <em>furry</em>!'
        },
        location='Squirrel Park',
        tags=['history']
    )
    event_2.submit()
    event_2.publish()
    assert events.query().count() == 2

    events.delete(event_1)
    events.delete(event_2)
    assert events.query().count() == 0
    assert session.query(Occurrence).count() == 0
Beispiel #6
0
def test_export_import(session):

    collection = EventCollection(session)

    events = []
    for year in range(2008, 2011):
        for month in range(1, 13):
            tags = ['month-{0}'.format(month)]
            recurrence = 'RRULE:FREQ=DAILY;INTERVAL=1;COUNT=4'
            location = 'location'
            meta = {'1': '3', '2': '2'}
            content = {'a': 'x', 'b': 'z', 'description': 'description'}

            if month == 1:
                content = None
                meta = {'1': '1'}

            if month == 2:
                recurrence = ''
                content = {'a': 'y'}

            if month == 3:
                tags = []
                content = {'d': 'n'}

            if month == 4:
                recurrence = None
                location = ''

            if month == 5:
                meta = {}
                location = None

            if month == 6:
                meta = {'4': '7'}
                content = {}

            if month == 7:
                meta = None

            event = collection.add(
                title='Event {0}-{1}'.format(year, month),
                start=datetime(year, month, 18, 14, 00),
                end=datetime(year, month, 18, 16, 00),
                timezone='US/Eastern',
                tags=tags,
                recurrence=recurrence,
                location=location,
                meta=meta,
                content=content
            )
            event.submit()
            if year > 2008:
                event.publish()
            if year > 2009:
                event.withdraw()

            events.append({
                'state': event.state,
                'title': event.title,
                'localized_start': event.localized_start,
                'localized_end': event.localized_end,
                'timezone': event.timezone,
                'recurrence': event.recurrence or '',
                'tags': event.tags,
                'location': event.location or '',
                'description': event.description,
                'occurrence_dates': event.occurrence_dates(),
                'meta': event.meta,
                'content': event.content,
            })

    transaction.commit()

    output = StringIO()
    assert export_csv(collection, output) == 36
    output.seek(0)

    for event in collection.query():
        collection.delete(event)
    assert collection.query().count() == 0

    import_csv(collection, output)
    assert collection.query().count() == 36

    events = sorted(events, key=lambda x: x['localized_start'])
    for index, event in enumerate(collection.query().order_by(Event.start)):
        assert events[index]['state'] == event.state
        assert events[index]['title'] == event.title
        assert events[index]['localized_start'] == event.localized_start
        assert events[index]['localized_end'] == event.localized_end
        assert events[index]['timezone'] == event.timezone
        assert events[index]['recurrence'] == event.recurrence or ''
        assert events[index]['tags'] == event.tags
        assert events[index]['location'] == event.location or ''
        assert events[index]['description'] == event.description
        assert events[index]['occurrence_dates'] == event.occurrence_dates()
        assert events[index]['meta'] == event.meta
        assert events[index]['content'] == event.content