<s> import os <EOL> import hashlib <EOL> from datetime import datetime <EOL> from flask import Blueprint , render_template , current_app , request , flash <EOL> from flask . ext . login import login_required , current_user <EOL> from . . extensions import db <EOL> from . . user import User <EOL> from . . utils import allowed_file , make_dir <EOL> from . forms import ProfileForm , PasswordForm <EOL> settings = Blueprint ( '<STR_LIT>' , __name__ , url_prefix = '<STR_LIT>' ) <EOL> @ settings . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def profile ( ) : <EOL> user = User . query . filter_by ( name = current_user . name ) . first_or_404 ( ) <EOL> form = ProfileForm ( obj = user . user_detail , <EOL> email = current_user . email , <EOL> role_code = current_user . role_code , <EOL> status_code = current_user . status_code , <EOL> next = request . args . get ( '<STR_LIT>' ) ) <EOL> if form . validate_on_submit ( ) : <EOL> if form . avatar_file . data : <EOL> upload_file = request . files [ form . avatar_file . name ] <EOL> if upload_file and allowed_file ( upload_file . filename ) : <EOL> user_upload_dir = os . path . join ( current_app . config [ '<STR_LIT>' ] , "<STR_LIT>" % user . id ) <EOL> current_app . logger . debug ( user_upload_dir ) <EOL> make_dir ( user_upload_dir ) <EOL> root , ext = os . path . splitext ( upload_file . filename ) <EOL> today = datetime . now ( ) . strftime ( '<STR_LIT>' ) <EOL> hash_filename = hashlib . sha1 ( upload_file . read ( ) ) . hexdigest ( ) + "<STR_LIT:_>" + today + ext <EOL> user . avatar = hash_filename <EOL> avatar_ab_path = os . path . join ( user_upload_dir , user . avatar ) <EOL> upload_file . seek ( <NUM_LIT:0> ) <EOL> upload_file . save ( avatar_ab_path ) <EOL> form . populate_obj ( user ) <EOL> form . populate_obj ( user . user_detail ) <EOL> db . session . add ( user ) <EOL> db . session . commit ( ) <EOL> flash ( '<STR_LIT>' , '<STR_LIT:success>' ) <EOL> return render_template ( '<STR_LIT>' , user = user , <EOL> active = "<STR_LIT>" , form = form ) <EOL> @ settings . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def password ( ) : <EOL> user = User . query . filter_by ( name = current_user . name ) . first_or_404 ( ) <EOL> form = PasswordForm ( next = request . args . get ( '<STR_LIT>' ) ) <EOL> if form . validate_on_submit ( ) : <EOL> form . populate_obj ( user ) <EOL> user . password = form . new_password . data <EOL> db . session . add ( user ) <EOL> db . session . commit ( ) <EOL> flash ( '<STR_LIT>' , '<STR_LIT:success>' ) <EOL> return render_template ( '<STR_LIT>' , user = user , <EOL> active = "<STR_LIT:password>" , form = form ) </s>
