Beispiel #1
0
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)
Beispiel #2
0
def import_google_contacts():
    try:
        api = google.create_api(google.ContactsClient)

        group_id = None
        feed = api.GetGroups()
        for entry in feed.entry:
            if entry.title.text == 'System Group: My Contacts':
                group_id = entry.id.text

        query = ContactsQuery()
        query.max_results = 10000
        if group_id:
            query.group = group_id
        feed = api.GetContacts(q=query)

        my_emails = current_user.emails

        for entry in feed.entry:
            with db.transaction as session:
                for email in entry.email:
                    if not entry.name or not entry.name.full_name:
                        continue
                    contact = current_user.find_email_contact(email.address)
                    if not contact:
                        contact = create_email_contact(email.address)
                        contact.name = entry.name.full_name.text
                        contact.email = email.address
                        contact.user = current_user
                        contact.belongs_to_user = email.address in my_emails
                        session.add(contact)
                    else:
                        contact.name = entry.name.full_name.text

        return redirect(url_for('contacts'))

    except (google.ConnectionError, google.UnauthorizedError):
        return redirect(
            google.create_authorize_url(
                action_url=url_for('import_google_contacts'),
                error_url=url_for('contacts'),
                scope=
                'https://www.google.com/calendar/feeds/ https://www.google.com/m8/feeds/'
            ))
Beispiel #3
0
def import_google_contacts():
    try:
        api = google.create_api(google.ContactsClient)

        group_id = None
        feed = api.GetGroups()
        for entry in feed.entry:
            if entry.title.text == 'System Group: My Contacts':
                group_id = entry.id.text

        query = ContactsQuery()
        query.max_results = 10000
        if group_id:
            query.group = group_id
        feed = api.GetContacts(q=query)

        my_emails = current_user.emails

        for entry in feed.entry:
            with db.transaction as session:
                for email in entry.email:
                    if not entry.name or not entry.name.full_name:
                        continue
                    contact = current_user.find_email_contact(email.address)
                    if not contact:
                        contact = create_email_contact(email.address)
                        contact.name = entry.name.full_name.text
                        contact.email = email.address
                        contact.user = current_user
                        contact.belongs_to_user = email.address in my_emails
                        session.add(contact)
                    else:
                        contact.name = entry.name.full_name.text

        return redirect(url_for('contacts'))

    except (google.ConnectionError, google.UnauthorizedError):
        return redirect(google.create_authorize_url(
            action_url=url_for('import_google_contacts'),
            error_url=url_for('contacts'),
            scope='https://www.google.com/calendar/feeds/ https://www.google.com/m8/feeds/'
        ))