Esempio n. 1
0
def completed():
    training_name = "Test Training Page"
    current_date, current_time = get_current_time()

    form = TrainingCompleteForm()
    if form.validate_on_submit():
        flash('Congratulations on finishing the training!')
        msg = Message(training_name, sender="Safety Website", recipients=['*****@*****.**', form.email.data])
        msg.body = """
        ***Training Completed***
        Employee name: %s
        Subject: %s
        Date: %s
        Time: %s
        ************************
        """ % (session['username'], training_name, current_date, current_time)
        mail.send(msg)
        return redirect(url_for('home'))
    return render_template('completed.html', training_name=training_name, 
                           current_date=current_date, current_time=current_time, form=form)
def completed():
    # Query database to get user from user_id
    user = User.query.get(session['user_id'])
    user_name = user.firstname + ' ' + user.lastname

    # If guest is logged in, user_email is set top session['email']
    # otherwise, it is set from database user
    if session['user_id'] == GUEST_ID:
        user_email = session['email']
    else:
        user_email = user.email

    # Assign name of training subject to training_name
    # print "Session :", session['training_subject']
    training_name = session['training_subject']

    # Get current date and current time from function
    current_date, current_time, current_month, current_year = get_current_time()

    form = TrainingCompleteForm()
    if form.validate_on_submit():

        # Commented out for debugging
        send_mail(training_name, form.email.data, user_name, user_email, current_date, current_time, form.message.data)

        # Checks database to see if the user has already performed the training course and assigns it to user_training.
        user_training = Record.query.filter_by(user_id=session['user_id'], training_name=session['training_subject']).first()
        print user_training, type(user_training)

        date_time = datetime.now()

        print 'Completed: ', date_time

        # If the user has already performed the training, the record will be stored in user_training and will be True.
        # The database will then be updated to record the current month, year, and time that the training session was completed.
        if user_training:
            dbtraining = Record.query.get(user_training.id)
            flash("You originally completed the training '%s' on %s/%s.  Thank you! Your record will be updated." % (session['training_subject'], dbtraining.date_time.month, dbtraining.date_time.year))
            # dbtraining.month = current_month
            # dbtraining.year = current_year
            # dbtraining.time = current_time
            dbtraining.date_time = date_time
            db.session.commit()

        # If user has not completed training previously, user_training is Nonetype and will create a new record in the following
        # else statement.
        else:
            # Creates a model of the current training session to record to the database
            dbtraining = Record(session['user_id'],
                                session['training_subject'],
                                # current_month,
                                # current_year,
                                # current_time,
                                date_time)
            db.session.add(dbtraining)
            db.session.commit()
        session.pop('training_subject', None)
        return redirect(url_for('profile'))

        flash("Congratulations %s! You've completed the training!" % (user_name))
    return render_template('completed.html', training_name=training_name,
                           user_name=user_name, user_email=user_email,
                           current_date=current_date, current_time=current_time,
                           form=form)