def create_event():
    if 'userid' not in session:
        return redirect('/')
    if request.method == 'GET':
        user = User.query.get(session['userid'])
        if len(user.user_dogs) == 0:
            flash('Must have dog to create event', 'error')
            return redirect('/')
        return render_template('create_event.html')
    event_data = event_schema.dump(request.form)
    event_data.data['size_restrictions'] = request.form.getlist(
        'size_restrictions[]')
    validated_event = Event.validate_event(event_data.data)
    if validated_event:
        event_data.data['creator_id'] = session['userid']
        new_event = Event.add_event(event_data.data)
        conflicts = get_conflicts(new_event)
        if len(conflicts) > 0:
            for conflict in conflicts:
                conflicting_event = EventAttendance.leave_event(conflict)
                flash(
                    'You have been removed from {} due to time_conflict'.
                    format(conflicting_event), 'warning')
        if new_event:
            for restriction in request.form.getlist('size_restrictions[]'):
                EventSizeRestriction.add_restriction(data={
                    'event': new_event.id,
                    'size_id': restriction
                })
        flash('Event successfully added', 'success')
        return redirect('/')
    return redirect('/events/create')
def edit_event(id):
    if 'userid' not in session:
        return redirect('/')
    event = Event.query.get(id)
    if event.creator_id != session['userid']:
        flash('You cannot edit this event', 'error')
        return redirect('/events/{}'.format(id))
    if check_days_until(event) < 1:
        flash('Cannot edit events happening within 24 hours', 'error')
        return redirect('/')
    if request.method == 'GET':
        restrictions = []
        for size in event.size_restrictions:
            restrictions.append(size.id)
        return render_template('event_details.html',
                               edit=True,
                               event=event,
                               restrictions=restrictions)
    event_info = event_schema.dump(request.form)
    event_info.data['size_restrictions'] = None
    if len(event.attendees) == 0:
        event_info.data['size_restrictions'] = request.form.getlist(
            'size_restrictions[]')
        event_info.data['capacity'] = request.form['capacity']
    event_info.data['id'] = id
    validated_event = Event.validate_existing_event(event_info.data)
    if validated_event:
        Event.edit_event(event_info.data)
        if len(event.attendees) == 0:
            EventSizeRestriction.query.filter_by(event_id=id).delete()
            for restriction in request.form.getlist('size_restrictions[]'):
                EventSizeRestriction.add_restriction(data={
                    'event_id': id,
                    'restriction': restriction
                })
        conflicts = get_conflicts(event)
        if len(conflicts) > 0:
            for conflict in conflicts:
                conflicting_event = EventAttendance.leave_event(conflict)
                flash(
                    'You have been removed from {} due to time conflict'.
                    format(conflicting_event), 'warning')
        flash('Event updated', 'success')
        return redirect('/events/{}'.format(id))
    return redirect('/events/{}/edit'.format(id))
import webapp2
Example #4
0
import webapp2