Пример #1
0
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))
Пример #2
0
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'))
Пример #3
0
def edit_event(id):
    """Event editing."""
    event = current_user.event_or_404(id)
    form = EventForm(obj=event)

    if form.validate_on_submit():
        with db.transaction as session:
            form.populate_obj(event)
            event.starts_at = times.to_universal(form.starts_at.data, current_user.timezone)
        send_email_invites(event)
        return redirect(url_for('facebook_event', id=event.id))

    return render_template('edit_event.html', event=event, action='edit', form=form)
Пример #4
0
def google_event(id):
    event = current_user.event_or_404(id)
    if event.is_google_involved():
        try:
            api = google.create_api(google.CalendarClient)

            if event.google_id:
                entry = api.GetEventEntry(event.google_id)
            else:
                entry = CalendarEventEntry()

            entry.title = Title(text=event.name)
            if event.description:
                entry.content = Content(text=event.description)
            if event.venue:
                entry.where.append(CalendarWhere(value=event.venue))
            if event.starts_at:
                entry.when.append(When(
                    start=event.starts_at.strftime('%Y-%m-%dT%H:%M:%SZ'),
                    end=event.ends_at.strftime('%Y-%m-%dT%H:%M:%SZ')
                ))

            contacts_to_invite = list(event.contacts_google_to_invite)
            if contacts_to_invite:
                for contact in contacts_to_invite:
                    entry.who.append(EventWho(email=contact.email, rel='http://schemas.google.com/g/2005#event.attendee'))
                    # event.who.append(EventWho(email=contact.email, rel='http://schemas.google.com/g/2005#event.organizer'))
                entry.send_event_notifications = SendEventNotificationsProperty(value='true')

            if event.google_id:
                entry = api.Update(entry)
            else:
                entry = api.InsertEvent(entry)
                with db.transaction:
                    event.google_id = entry.GetSelfLink().href # entry.id.text

            with db.transaction:
                for contact in contacts_to_invite:
                    event.set_invitation_sent(contact)

        except (google.ConnectionError, google.UnauthorizedError) as e:
            return redirect(google.create_authorize_url(
                action_url=url_for('google_event', id=event.id),
                error_url=url_for('edit_event', id=event.id),
                scope='https://www.google.com/calendar/feeds/ https://www.google.com/m8/feeds/'
            ))
    return redirect(url_for('event', id=event.id))
Пример #5
0
def revive_event(id):
    """Canceles event by ID."""
    event = current_user.event_or_404(id)
    with db.transaction as session:
        event.cancelled_at = None
    return redirect(url_for('event', id=id))