app.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:arowf 作者: priyankamandikal 项目源码 文件源码
def register():
    with open('users.pkl', 'r') as f:
        userdict = load(f)
    if request.method == 'GET':
        logdate = datetime.strftime(date.today(), '%Y-%m-%d')
        logfn = './logs/activity-'+logdate
        user = 'Anonymous'                      # Username is Anonymous by default
        if 'token' in session:
            token = session['token']
            tokenfilename = 'registered/'+token
            with open(tokenfilename, 'r') as f: # for getting username associated with the set token
                user = f.readline()[:-1]
        with open(logfn, 'a') as f:     # logging username, IP addr, end-point, request type
            log = user+' '+request.environ['REMOTE_ADDR']+' register'+' GET\n'
            f.write(log)
        return render_template('register.html', userdict=userdict) # inputs for name, email, timezone, phone, aboutme
    elif request.method == 'POST':
        uname = str(request.form['uname'])      # mandatory
        email = str(request.form['email'])      # mandatory
        timezone = request.form['timezone']     # optional
        phone = request.form['phone']           # optional
        aboutme = request.form['aboutme']       # optional
        token = pbkdf2_sha512.encrypt(email)    # setting the token as a salted and hashed email
        token = token.replace('/', '+')         # filenames can't contain '/'
        session['token'] = token                # set token in session key on successful registration
        fn = 'registered' + path.sep + token
        with open(fn, 'w') as f:                # write reviewer info into a file named by the token
            f.write(uname+'\n'+email+'\n'+timezone+'\n'+phone+'\n'+aboutme+'\n')
            f.write('--files--\n')
        userdict[uname] = email
        with open('users.pkl', 'w') as f:       # dictionary with keys as usernames and values as emails
            dump(userdict, f)
        send_email(email, 'Welcome to AROWF!', 'registration_mail', name=uname, token=token)    # send welcome email with token
        logdate = datetime.strftime(date.today(), '%Y-%m-%d')
        logfn = './logs/activity-'+logdate
        with open(logfn, 'a') as f:     # logging username, IP addr, end-point, request type
            log = uname+' '+request.environ['REMOTE_ADDR']+' register'+' POST\n'
            f.write(log)
        return redirect(url_for('index'))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号