コード例 #1
0
def validate(reactor_id):
    db = client[DB_NAME]
    reactors = db[BIOREACTOR_COLLECTION]
    id_search = [res for res in reactors.find({'_id': reactor_id})]
    if len(id_search) == 0:
        return(render_template('SorryReactor.html', search_id=reactor_id))
    else:
        # if the bioreactor has already been validated, just direct to its page
        if id_search[0]['validated']:
            return(redirect(url_for('reactor', reactor_id=reactor_id)))
        key = request.args.get('key')
        # if the key is incorrect
        if key != id_search[0]['validation_key']:
            return(render_template('WrongKeyReactor.html', search_id=reactor_id))
        else:
            reactors.find_and_modify({'_id': reactor_id}, \
                                     {'$set': {'validated': True}})

            info_email = MIMEText(render_template('FurtherInformationEmail', \
                                                  experiment_validation_code = id_search[0]['experiment_validation_code'], \
                                                  upload_code = id_search[0]['upload_code'], \
                                                  reactor_id = reactor_id))
            info_email['Subject'] = 'Big Algae Open Experiment Important Information'

            recipient = id_search[0]['email']

            try:
                bigalgae.send_email(info_email, recipient)
                error_with_email = False
            except smtplib.SMTPAuthenticationError:
                error_with_email = True

            return(render_template('SuccessfulValidation.html', reactor_id = reactor_id, error_with_email = error_with_email))
コード例 #2
0
def register():
    if request.method == 'GET':
        return(render_template('RegisterBioreactor.html'));
    elif request.method == 'POST':
        db = client[DB_NAME]
        reactors = db[BIOREACTOR_COLLECTION]

        global_var_collection = db[GLOBAL_VARIABLE_COLLECTION]

        user_data = request.get_json()
        upload_code = bigalgae.generate_digit_code(4)
        experiment_validation_code = bigalgae.generate_digit_code(6)
        validation_key = bigalgae.generate_validation_key(32)
        utc = datetime.datetime.utcnow().isoformat()
        success = False

        while not success:
            try:
                id_str = returnNewID(global_var_collection)

                reactors.insert({'_id': id_str, \
                                'name': user_data['name'], \
                                'location': user_data['location'], \
                                'email': user_data['email'], \
                                'latitude': user_data['latitude'], \
                                'longitude': user_data['longitude'], \
                                'upload_code': upload_code, \
                                'experiment_validation_code': experiment_validation_code, \
                                'validation_key': validation_key, \
                                'validated': False, \
                                'experiments': [], \
                                'created_datetime': utc})
                success = True
            except errors.DuplicateKeyError:
                success = False

        validation_link = url_for('validate', reactor_id=id_str, _external=True) + '?key=' + validation_key
        confirmation_email = MIMEText(render_template('ConfirmationEmail', \
                                                        validation_link = validation_link, \
                                                        reactor_id = id_str))
        confirmation_email['Subject'] = 'Big Algae Open Experiment Validation'

        recipient = user_data['email']

        try:
            bigalgae.send_email(confirmation_email, recipient)
            error_with_email = False
        except smtplib.SMTPAuthenticationError:
            error_with_email = True

        return(url_for('thanks'))