def handle_calendar_updates(namespace_id, calendars, log, db_session): """Persists new or updated Calendar objects to the database.""" ids_ = [] added_count = 0 updated_count = 0 for calendar in calendars: assert calendar.uid is not None, 'Got remote item with null uid' local_calendar = db_session.query(Calendar).filter( Calendar.namespace_id == namespace_id, Calendar.uid == calendar.uid).first() if local_calendar is not None: local_calendar.update(calendar) updated_count += 1 else: local_calendar = Calendar(namespace_id=namespace_id) local_calendar.update(calendar) db_session.add(local_calendar) db_session.flush() added_count += 1 ids_.append((local_calendar.uid, local_calendar.id)) log.info('synced added and updated calendars', added=added_count, updated=updated_count) return ids_
def parse_calendar_response(calendar): """ Constructs a Calendar object from a Google calendarList resource (a dictionary). See http://developers.google.com/google-apps/calendar/v3/reference/calendarList Parameters ---------- calendar: dict Returns ------- A corresponding Calendar instance. """ uid = calendar['id'] name = calendar['summary'] role = calendar['accessRole'] read_only = True if role == "owner" or role == "writer": read_only = False description = calendar.get('description', None) return Calendar(uid=uid, name=name, read_only=read_only, description=description)
def test_transactions_created_for_calendars(db, default_namespace): calendar = Calendar( namespace_id=default_namespace.id, name='New Calendar', uid='uid') db.session.add(calendar) db.session.commit() transaction = get_latest_transaction(db.session, 'calendar', calendar.id, default_namespace.id) assert transaction.record_id == calendar.id assert transaction.object_type == 'calendar' assert transaction.command == 'insert' calendar.name = 'Updated Calendar' db.session.commit() transaction = get_latest_transaction(db.session, 'calendar', calendar.id, default_namespace.id) assert transaction.record_id == calendar.id assert transaction.object_type == 'calendar' assert transaction.command == 'update' db.session.delete(calendar) db.session.commit() transaction = get_latest_transaction(db.session, 'calendar', calendar.id, default_namespace.id) assert transaction.record_id == calendar.id assert transaction.object_type == 'calendar' assert transaction.command == 'delete'
def calendar_long_name(): return CalendarSyncResponse([], [ Calendar( name= 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris_!', uid='long_calendar_uid', read_only=True) ])
def add_fake_calendar(db_session, namespace_id, name="Cal", description="A Calendar", uid="UID", read_only=False): from inbox.models import Calendar calendar = Calendar(namespace_id=namespace_id, name=name, description=description, uid=uid, read_only=read_only) db_session.add(calendar) db_session.commit() return calendar
def calendar_response_with_update(): return CalendarSyncResponse( [], [ Calendar( name="Super Important Meetings", uid="first_calendar_uid", read_only=False, ) ], )
def create_calendar(namespace, db_session, name, description): calendar = Calendar(namespace=namespace, name=name, provider_name=INBOX_PROVIDER_NAME, description=description, uid=uuid.uuid4().hex, read_only=False) db_session.add(calendar) db_session.commit() return calendar
def get_calendar_id(self, name): calendar_id = None with session_scope() as db_session: cal_name = "{}-{}".format(self.PROVIDER_NAME, name) cal = db_session.query(Calendar). \ filter_by(account_id=self.account_id, name=cal_name).first() if not cal: cal = Calendar(account_id=self.account_id, name=cal_name) db_session.add(cal) db_session.commit() calendar_id = cal.id return calendar_id
def test_get_calendar(db, default_namespace, api_client): cal = Calendar(namespace_id=default_namespace.id, uid='uid', provider_name='WTF', name='Holidays') db.session.add(cal) db.session.commit() cal_id = cal.public_id calendar_item = api_client.get_data('/calendars/{}'.format(cal_id)) assert calendar_item['account_id'] == default_namespace.public_id assert calendar_item['name'] == 'Holidays' assert calendar_item['description'] is None assert calendar_item['read_only'] is False assert calendar_item['object'] == 'calendar'
def test_get_calendar(db, default_namespace, api_client): cal = Calendar( namespace_id=default_namespace.id, uid="uid", provider_name="WTF", name="Holidays", ) db.session.add(cal) db.session.commit() cal_id = cal.public_id calendar_item = api_client.get_data("/calendars/{}".format(cal_id)) assert calendar_item["account_id"] == default_namespace.public_id assert calendar_item["name"] == "Holidays" assert calendar_item["description"] is None assert calendar_item["read_only"] is False assert calendar_item["object"] == "calendar"
def test_get_calendar(db, api_client): acct = db.session.query(Account).filter_by(id=ACCOUNT_ID).one() ns_id = acct.namespace.public_id cal = Calendar(namespace_id=acct.namespace.id, uid='uid', provider_name='WTF', name='Holidays') db.session.add(cal) db.session.commit() cal_id = cal.public_id resp_data = api_client.get_data('/calendars/' + cal_id, ns_id) assert resp_data['namespace_id'] == ns_id assert resp_data['name'] == 'Holidays' assert resp_data['description'] is None assert resp_data['read_only'] is False assert resp_data['object'] == 'calendar'
def test_add_to_specific_calendar(db, default_namespace, api_client): cal = Calendar( namespace_id=default_namespace.id, uid="uid", provider_name="WTF", name="Custom" ) db.session.add(cal) db.session.commit() cal_id = cal.public_id e_data = { "calendar_id": cal_id, "title": "subj", "description": "body1", "when": {"time": 1}, "location": "NylasHQ", } r = api_client.post_data("/events", e_data) assert r.status_code == 200 events = api_client.get_data("/events?calendar_id={}".format(cal_id)) assert len(events) == 1
def get_calendar_id(self, name, description=None): calendar_id = None with session_scope() as db_session: cal = db_session.query(Calendar). \ filter_by(namespace_id=self.namespace_id, provider_name=self.PROVIDER_NAME, name=name).first() if not cal: cal = Calendar(namespace_id=self.namespace_id, provider_name=self.PROVIDER_NAME, name=name) db_session.add(cal) db_session.commit() calendar_id = cal.id # update the description if appropriate if cal.description != description: cal.description = description db_session.commit() return calendar_id
def test_add_to_specific_calendar(db, default_namespace, api_client): cal = Calendar(namespace_id=default_namespace.id, uid='uid', provider_name='WTF', name='Custom') db.session.add(cal) db.session.commit() cal_id = cal.public_id e_data = { 'calendar_id': cal_id, 'title': 'subj', 'description': 'body1', 'when': { 'time': 1 }, 'location': 'InboxHQ' } r = api_client.post_data('/events', e_data) assert r.status_code == 200 events = api_client.get_data('/events?calendar_id={}'.format(cal_id)) assert len(events) == 1
def test_calendar_parsing(): raw_response = [{ 'accessRole': 'owner', 'backgroundColor': '#9a9cff', 'colorId': '17', 'defaultReminders': [{ 'method': 'popup', 'minutes': 30 }], 'etag': '"1425508164135000"', 'foregroundColor': '#000000', 'id': '*****@*****.**', 'kind': 'calendar#calendarListEntry', 'notificationSettings': { 'notifications': [{ 'method': 'email', 'type': 'eventCreation' }, { 'method': 'email', 'type': 'eventChange' }, { 'method': 'email', 'type': 'eventCancellation' }, { 'method': 'email', 'type': 'eventResponse' }] }, 'primary': True, 'selected': True, 'summary': '*****@*****.**', 'timeZone': 'America/Los_Angeles' }, { 'accessRole': 'reader', 'backgroundColor': '#f83a22', 'colorId': '3', 'defaultReminders': [], 'description': 'Holidays and Observances in United States', 'etag': '"1399416119263000"', 'foregroundColor': '#000000', 'id': 'en.usa#[email protected]', 'kind': 'calendar#calendarListEntry', 'selected': True, 'summary': 'Holidays in United States', 'timeZone': 'America/Los_Angeles' }, { 'defaultReminders': [], 'deleted': True, 'etag': '"1425952878772000"', 'id': '*****@*****.**', 'kind': 'calendar#calendarListEntry' }] expected_deletes = ['*****@*****.**'] expected_updates = [ Calendar(uid='*****@*****.**', name='*****@*****.**', description=None, read_only=False), Calendar(uid='en.usa#[email protected]', name='Holidays in United States', description='Holidays and Observances in United States', read_only=True) ] provider = GoogleEventsProvider(1, 1) provider._get_raw_calendars = mock.MagicMock(return_value=raw_response) deletes, updates = provider.sync_calendars() assert deletes == expected_deletes for obtained, expected in zip(updates, expected_updates): assert cmp_cal_attrs(obtained, expected)
def calendar_response_with_update(): return CalendarSyncResponse([], [ Calendar(name='Super Important Meetings', uid='first_calendar_uid', read_only=False) ])
def test_calendar_parsing(): raw_response = [ { "accessRole": "owner", "backgroundColor": "#9a9cff", "colorId": "17", "defaultReminders": [{"method": "popup", "minutes": 30}], "etag": '"1425508164135000"', "foregroundColor": "#000000", "id": "*****@*****.**", "kind": "calendar#calendarListEntry", "notificationSettings": { "notifications": [ {"method": "email", "type": "eventCreation"}, {"method": "email", "type": "eventChange"}, {"method": "email", "type": "eventCancellation"}, {"method": "email", "type": "eventResponse"}, ] }, "primary": True, "selected": True, "summary": "*****@*****.**", "timeZone": "America/Los_Angeles", }, { "accessRole": "reader", "backgroundColor": "#f83a22", "colorId": "3", "defaultReminders": [], "description": "Holidays and Observances in United States", "etag": '"1399416119263000"', "foregroundColor": "#000000", "id": "en.usa#[email protected]", "kind": "calendar#calendarListEntry", "selected": True, "summary": "Holidays in United States", "timeZone": "America/Los_Angeles", }, { "defaultReminders": [], "deleted": True, "etag": '"1425952878772000"', "id": "*****@*****.**", "kind": "calendar#calendarListEntry", }, ] expected_deletes = ["*****@*****.**"] expected_updates = [ Calendar( uid="*****@*****.**", name="*****@*****.**", description=None, read_only=False, ), Calendar( uid="en.usa#[email protected]", name="Holidays in United States", description="Holidays and Observances in United States", read_only=True, ), ] provider = GoogleEventsProvider(1, 1) provider._get_raw_calendars = mock.MagicMock(return_value=raw_response) deletes, updates = provider.sync_calendars() assert deletes == expected_deletes for obtained, expected in zip(updates, expected_updates): assert cmp_cal_attrs(obtained, expected)
def test_api_filter(db, api_client, calendar, default_namespace): cal = Calendar(namespace_id=default_namespace.id, uid='uid', provider_name='Nylas', name='Climbing Schedule') db.session.add(cal) db.session.commit() cal_id = cal.public_id e1_data = { 'calendar_id': cal_id, 'title': 'Normal Party', 'description': 'Everyone Eats Cake', 'when': { 'time': 1 }, 'location': 'Normal Town' } post_1 = api_client.post_data('/events', e1_data) assert post_1.status_code == 200 e2_data = { 'calendar_id': cal_id, 'title': 'Hipster Party', 'description': 'Everyone Eats Kale', 'when': { 'time': 3 }, 'location': 'Hipster Town' } post_2 = api_client.post_data('/events', e2_data) assert post_2.status_code == 200 # This event exists to test for unicode handling. e3_data = { 'calendar_id': cal_id, 'title': u'Unicode Party \U0001F389', 'description': u'Everyone Eats Unicode Tests \u2713', 'when': { 'start_time': 2678401, 'end_time': 5097601 }, 'location': u'Unicode Castle \U0001F3F0' } event_3 = api_client.post_data('/events', e3_data) assert event_3.status_code == 200 e3_id = json.loads(event_3.data)['id'] events = api_client.get_data('/events?offset=%s' % '1') assert len(events) == 2 events = api_client.get_data('/events?limit=%s' % '1') assert len(events) == 1 # Test description queries: all, some, unicode, none events = api_client.get_data('/events?description=%s' % 'Everyone Eats') assert len(events) == 3 events = api_client.get_data('/events?description=%s' % 'Cake') assert len(events) == 1 events = api_client.get_data('/events?description=%s' % u'\u2713') assert len(events) == 1 events = api_client.get_data('/events?description=%s' % 'bad') assert len(events) == 0 # Test title queries: all, some, unicode, none events = api_client.get_data('/events?title=%s' % 'Party') assert len(events) == 3 events = api_client.get_data('/events?title=%s' % 'Hipster') assert len(events) == 1 events = api_client.get_data('/events?title=%s' % u'\U0001F389') assert len(events) == 1 events = api_client.get_data('/events?title=%s' % 'bad') assert len(events) == 0 # Test location queries: all, some, unicode, none events = api_client.get_data('/events?location=%s' % 'o') assert len(events) == 3 events = api_client.get_data('/events?location=%s' % 'Town') assert len(events) == 2 events = api_client.get_data('/events?location=%s' % u'\U0001F3F0') assert len(events) == 1 events = api_client.get_data('/events?location=%s' % 'bad') assert len(events) == 0 # Test ID queries _filter = 'event_id={}'.format(e3_id) events = api_client.get_data('/events?' + _filter) assert len(events) == 1 # Test time queries _filter = 'starts_before=2' events = api_client.get_data('/events?' + _filter) assert len(events) == 1 _filter = 'starts_after=2' events = api_client.get_data('/events?' + _filter) assert len(events) == 2 _filter = 'ends_before=2700000' events = api_client.get_data('/events?' + _filter) assert len(events) == 2 _filter = 'ends_after=2700000' events = api_client.get_data('/events?' + _filter) assert len(events) == 1 # Test calendar queries _filter = 'calendar_id={}'.format(cal_id) events = api_client.get_data('/events?' + _filter) assert len(events) == 3 _filter = 'calendar_id=0000000000000000000000000' events = api_client.get_data('/events?' + _filter) assert len(events) == 0
def test_api_filter(db, api_client, calendar, default_namespace): cal = Calendar( namespace_id=default_namespace.id, uid="uid", provider_name="Nylas", name="Climbing Schedule", ) db.session.add(cal) db.session.commit() cal_id = cal.public_id e1_data = { "calendar_id": cal_id, "title": "Normal Party", "description": "Everyone Eats Cake", "when": {"time": 1}, "location": "Normal Town", } post_1 = api_client.post_data("/events", e1_data) assert post_1.status_code == 200 e2_data = { "calendar_id": cal_id, "title": "Hipster Party", "description": "Everyone Eats Kale", "when": {"time": 3}, "location": "Hipster Town", } post_2 = api_client.post_data("/events", e2_data) assert post_2.status_code == 200 # This event exists to test for unicode handling. e3_data = { "calendar_id": cal_id, "title": u"Unicode Party \U0001F389", "description": u"Everyone Eats Unicode Tests \u2713", "when": {"start_time": 2678401, "end_time": 5097601}, "location": u"Unicode Castle \U0001F3F0", } event_3 = api_client.post_data("/events", e3_data) assert event_3.status_code == 200 e3_id = json.loads(event_3.data)["id"] events = api_client.get_data("/events?offset=1") assert len(events) == 2 events = api_client.get_data("/events?limit=1") assert len(events) == 1 # Test description queries: all, some, unicode, none events = api_client.get_data("/events?description=Everyone Eats") assert len(events) == 3 events = api_client.get_data("/events?description=Cake") assert len(events) == 1 events = api_client.get_data(u"/events?description=\u2713") assert len(events) == 1 events = api_client.get_data("/events?description=bad") assert len(events) == 0 # Test title queries: all, some, unicode, none events = api_client.get_data("/events?title=Party") assert len(events) == 3 events = api_client.get_data("/events?title=Hipster") assert len(events) == 1 events = api_client.get_data(u"/events?title=\U0001F389") assert len(events) == 1 events = api_client.get_data("/events?title=bad") assert len(events) == 0 # Test location queries: all, some, unicode, none events = api_client.get_data("/events?location=o") assert len(events) == 3 events = api_client.get_data("/events?location=Town") assert len(events) == 2 events = api_client.get_data(u"/events?location=\U0001F3F0") assert len(events) == 1 events = api_client.get_data("/events?location=bad") assert len(events) == 0 # Test ID queries _filter = "event_id={}".format(e3_id) events = api_client.get_data("/events?" + _filter) assert len(events) == 1 # Test time queries _filter = "starts_before=2" events = api_client.get_data("/events?" + _filter) assert len(events) == 1 _filter = "starts_after=2" events = api_client.get_data("/events?" + _filter) assert len(events) == 2 _filter = "ends_before=2700000" events = api_client.get_data("/events?" + _filter) assert len(events) == 2 _filter = "ends_after=2700000" events = api_client.get_data("/events?" + _filter) assert len(events) == 1 # Test calendar queries _filter = "calendar_id={}".format(cal_id) events = api_client.get_data("/events?" + _filter) assert len(events) == 3 _filter = "calendar_id=0000000000000000000000000" events = api_client.get_data("/events?" + _filter) assert len(events) == 0