def import_facebook_friends(): try: api = facebook.create_api() me = api.get('me') friends = api.get('me/friends')['data'] friends.append(me) for friend in friends: with db.transaction as session: contact = current_user.find_facebook_contact(friend['id']) if not contact: contact = FacebookContact() contact.name = friend['name'] contact.facebook_id = friend['id'] contact.user = current_user contact.belongs_to_user = friend['id'] == me['id'] session.add(contact) else: contact.name = friend['name'] return redirect(url_for('contacts')) except (facebook.ConnectionError, facebook.OAuthError): return redirect(facebook.create_authorize_url( action_url=url_for('import_facebook_friends'), error_url=url_for('contacts') ))
def import_facebook_friends(): try: api = facebook.create_api() me = api.get('me') friends = api.get('me/friends')['data'] friends.append(me) for friend in friends: with db.transaction as session: contact = current_user.find_facebook_contact(friend['id']) if not contact: contact = FacebookContact() contact.name = friend['name'] contact.facebook_id = friend['id'] contact.user = current_user contact.belongs_to_user = friend['id'] == me['id'] session.add(contact) else: contact.name = friend['name'] return redirect(url_for('contacts')) except (facebook.ConnectionError, facebook.OAuthError): return redirect( facebook.create_authorize_url( action_url=url_for('import_facebook_friends'), error_url=url_for('contacts')))
def facebook_event(id): event = current_user.event_or_404(id) if event.is_facebook_involved(): try: api = facebook.create_api() payload = { 'name': event.name, 'description': event.description or '', 'location': event.venue or '', 'start_time': times.format(event.starts_at, current_user.timezone, '%Y-%m-%dT%H:%M:%S'), } if event.facebook_id: api.post(path='/' + event.facebook_id, **payload) else: data = api.post(path='/events', **payload) with db.transaction: event.facebook_id = data['id'] contacts_to_invite = list(event.contacts_facebook_to_invite) if contacts_to_invite: ids = ','.join([c.facebook_id for c in contacts_to_invite]) api.post(path='/' + event.facebook_id + '/invited?users=' + ids) with db.transaction: for contact in contacts_to_invite: event.set_invitation_sent(contact) except (facebook.ConnectionError, facebook.OAuthError): return redirect(facebook.create_authorize_url( action_url=url_for('facebook_event', id=event.id), error_url=url_for('edit_event', id=event.id), scope='create_event' )) return redirect(url_for('google_event', id=event.id))
def cancel_event(id): """Canceles event by ID.""" event = current_user.event_or_404(id) this_url = url_for('cancel_event', id=event.id) if event.facebook_id: try: api = facebook.create_api() api.delete(path='/' + event.facebook_id) except (facebook.ConnectionError, facebook.OAuthError): return redirect(facebook.create_authorize_url( action_url=this_url, error_url=this_url )) if event.google_id: try: api = google.create_api(google.CalendarClient) entry = api.GetEventEntry(event.google_id) api.Delete(entry) except (google.ConnectionError, google.UnauthorizedError) as e: return redirect(google.create_authorize_url( action_url=this_url, error_url=this_url, scope='https://www.google.com/calendar/feeds/ https://www.google.com/m8/feeds/' )) with db.transaction as session: session.delete(event) return redirect(url_for('events'))
def event(id): """Public event page.""" event = Event.fetch_or_404(id) this_url = url_for('event', id=event.id) if event.facebook_id and current_user.is_authenticated(): try: api = facebook.create_api() data = api.get(path='/' + event.facebook_id + '/invited')['data'] for friend in data: contact = current_user.find_facebook_contact(friend['id']) if not contact: with db.transaction as session: contact = FacebookContact() contact.user = current_user contact.facebook_id = friend['id'] contact.name = friend['name'] session.add(contact) with db.transaction: event.set_attendance(contact, Attendance.types_mapping[friend['rsvp_status']]) except (facebook.ConnectionError, facebook.OAuthError): return redirect(facebook.create_authorize_url( action_url=this_url, error_url=this_url )) if event.google_id and current_user.is_authenticated(): try: api = google.create_api(google.CalendarClient) entry = api.GetEventEntry(event.google_id) for participant in entry.who: contact = current_user.find_email_contact(participant.email) if not contact: continue with db.transaction: if not participant.attendee_status and participant.rel == 'http://schemas.google.com/g/2005#event.organizer': event.set_attendance(contact, 'going') else: event.set_attendance(contact, Attendance.types_mapping[participant.attendee_status.value]) except (google.ConnectionError, google.UnauthorizedError) as e: return redirect(google.create_authorize_url( action_url=this_url, error_url=this_url, scope='https://www.google.com/calendar/feeds/ https://www.google.com/m8/feeds/' )) return render_template('event.html', event=event)