Ejemplo n.º 1
0
def poll_create_event(poll):
    user = DB.find_one(collection="Profile",
                       query={"email": current_user.email})
    if user is None:
        flash('Please create your profile first!')
        return redirect(url_for('edit_profile'))

    my_poll = DB.find_one(collection="Poll", query={"_id": ObjectId(poll)})
    if my_poll is None:
        flash('Please contact admin, DB issues!')
        return redirect(url_for('polls'))

    date1 = None
    largest = 0
    for option in my_poll['options']:
        if 'voters' in option:
            if len(list(option['voters'])) > largest:
                largest = len(list(option['voters']))
                date1 = option['date']

    event_obj = Event(name=my_poll['name'],
                      description=my_poll['description'],
                      start=date1,
                      end=None,
                      host=user['_id'],
                      invitees=[],
                      pictureDir='events.jpg',
                      private=True).json()

    form = EventForm()
    if form.validate_on_submit():
        form.starttime.data = form.starttime.data.split(' ')
        time1 = form.starttime.data[0].split(':')
        if form.starttime.data[1] == 'PM':
            time1[0] = int(time1[0]) + 12
            if time1[0] == 24:
                time1[0] = 0
        time1 = time(int(time1[0]), int(time1[1]))

        form.endtime.data = form.endtime.data.split(' ')
        time2 = form.endtime.data[0].split(':')
        if form.endtime.data[1] == 'PM':
            time2[0] = int(time2[0]) + 12
            if time2[0] == 24:
                time2[0] = 0
        time2 = time(int(time2[0]), int(time2[1]))

        date1 = datetime((form.start.data).year, (form.start.data).month,
                         (form.start.data).day, time1.hour, time1.minute)
        date2 = datetime((form.end.data).year, (form.end.data).month,
                         (form.end.data).day, time2.hour, time2.minute)
        # check date and time
        if date1 < datetime.now():
            flash('Start has to be today or later!')
            return render_template('poll-create-event.html',
                                   title="Create Your Event",
                                   form=form,
                                   poll=poll,
                                   event=event_obj)
        elif date2 < date1:
            flash('End cannot be earlier than Start!')
            return render_template('poll-create-event.html',
                                   title="Create Your Event",
                                   form=form,
                                   poll=poll,
                                   event=event_obj)
        elif date1 == date2:
            flash('Start and End cannot be the same!')
            return render_template('poll-create-event.html',
                                   title="Create Your Event",
                                   form=form,
                                   poll=poll,
                                   event=event_obj)
        if form.pictureDir.data is None:
            filename = "event.jpg"
        else:
            filename = photos.save(form.pictureDir.data,
                                   name='event/' + str(user['_id']) + '.')
            filename = filename.split('/')[1]

        form.name.data = form.name.data.strip()
        form.description.data = form.description.data.strip()
        if form.eventType.data == 'private':
            event_type = True
        else:
            event_type = False

        inviteesList = []
        for voter in my_poll['voters']:
            voter_obj = DB.find_one(collection='Profile', query={'_id': voter})
            voter_dict = {'email': voter_obj['email'], 'status': 'going'}
            inviteesList.append(voter_dict)

        updated_event = Event(name=form.name.data,
                              description=form.description.data,
                              start=date1,
                              end=date2,
                              host=user['_id'],
                              invitees=inviteesList,
                              pictureDir=filename,
                              private=event_type)
        eventid = updated_event.insert(user['email'], user['_id'])

        for invitees in inviteesList:
            DB.update_one(collection="Profile",
                          filter={"email": invitees['email']},
                          data={"$push": {
                              "events": ObjectId(eventid)
                          }})

        return redirect(url_for('delete_poll', poll=poll))
    return render_template('poll-create-event.html',
                           title="Create Your Event",
                           form=form,
                           poll=poll,
                           event=event_obj)
Ejemplo n.º 2
0
def create_events():
    user = DB.find_one(collection="Profile",
                       query={"email": current_user.email})
    if user is None:
        flash('Please create your profile first!')
        return redirect(url_for('edit_profile'))

    form = EventForm()
    # if form.is_submitted():
    if form.validate_on_submit():

        form.starttime.data = form.starttime.data.split(' ')
        time1 = form.starttime.data[0].split(':')
        if form.starttime.data[1] == 'PM':
            time1[0] = int(time1[0]) + 12
            if time1[0] == 24:
                time1[0] = 0
        time1 = time(int(time1[0]), int(time1[1]))

        form.endtime.data = form.endtime.data.split(' ')
        time2 = form.endtime.data[0].split(':')
        if form.endtime.data[1] == 'PM':
            time2[0] = int(time2[0]) + 12
            if time2[0] == 24:
                time2[0] = 0
        time2 = time(int(time2[0]), int(time2[1]))

        date1 = datetime((form.start.data).year, (form.start.data).month,
                         (form.start.data).day, time1.hour, time1.minute)
        date2 = datetime((form.end.data).year, (form.end.data).month,
                         (form.end.data).day, time2.hour, time2.minute)
        # check date and time
        if date1 < datetime.now():
            flash('Start has to be today or later!')
            return render_template('create-event.html',
                                   title="Create Your Event",
                                   form=form)
        elif date2 < date1:
            flash('End cannot be earlier than Start!')
            return render_template('create-event.html',
                                   title="Create Your Event",
                                   form=form)
        elif date1 == date2:
            flash('Start and End cannot be the same!')
            return render_template('create-event.html',
                                   title="Create Your Event",
                                   form=form)
        if form.pictureDir.data is None:
            filename = "event.jpg"
        else:
            filename = photos.save(form.pictureDir.data,
                                   name='event/' + str(user['_id']) + '.')
            filename = filename.split('/')[1]

        form.name.data = form.name.data.strip()
        form.description.data = form.description.data.strip()
        if form.eventType.data == 'private':
            event = Event(name=form.name.data,
                          description=form.description.data,
                          start=date1,
                          end=date2,
                          host=user['_id'],
                          invitees=[],
                          pictureDir=filename,
                          private=True)
        else:
            event = Event(name=form.name.data,
                          description=form.description.data,
                          start=date1,
                          end=date2,
                          host=user['_id'],
                          invitees=[],
                          pictureDir=filename,
                          private=False)
        event.insert(current_user.email, user['_id'])
        flash('Create event successful')
        return redirect(url_for('view_events'))
    return render_template('create-event.html',
                           title="Create Your Event",
                           form=form)