"""api of get user default image""" def __init__(self): super(AvatarAction, self).__init__() def doget(self, http_request, http_response): """http get function""" request_data = http_request.parameters email_digest = request_data.getvalue("uid") if email_digest is None or email_digest == "": http_response.header["Content-Type"] = "text/plain" http_response.send_binary("链接错误") else: current_user = helper.User() current_user.email_digest = email_digest if not current_user.load_by_email_digest(): http_response.header["Content-Type"] = "text/plain" http_response.send_binary("用户不存在!") return image_path = "".join(["..", current_user.get_image_path()]) http_response.header["Content-Dispostion"] = \ 'attachment; filename="{0}"'.\ format(image_path[image_path.rindex("/")+1:]) http_response.header["Content-Type"] = "image/jpeg" with open(image_path, "rb") as image_fp: buff = image_fp.read() http_response.send_binary(buff) helper.Application(AvatarAction()).execute()
image_list = "" #image html template default_template = '<div class="image default"><img src="{0}"></div>' normal_template = '<div class="image"><img src="{0}"></div>' try: images = os.listdir("../userimage/{0}".format(current_user.id_)) for image in images: path = "/userimage/{0}/{1}".format(current_user.id_, image) if current_user.default_image == image: image_list = "".join( [image_list, default_template.format(path)]) else: image_list = "".join( [image_list, normal_template.format(path)]) except OSError: pass http_request.close_session_file() http_response.send_html("index.html", image_path=image_path, email=current_user.email, email_digest=current_user.email_digest, image_list=image_list) def dopost(self, http_request, http_response): """ http post function """ self.doget(http_request, http_response) helper.Application(IndexAction()).execute()
image_fp = image.file #get image suffix suffix = image.filename[image.filename.rindex("."):] accept_images = [".jpg", ".gif", ".png", "bmp"] if suffix not in accept_images: http_response.send_html( "changeimg.html", error_message="非法文件格式!只支持jpg, gif, png, bmp!", email=current_user.email, image_path=current_user.get_image_path()) return http_request.close_session_file() buff = image_fp.read() image_fp.close() #get the unique filename by md5 digest file_name = "".join([md5.new(buff).hexdigest(), suffix]) #save file if not os.path.exists("../userimage/{0}".format(current_user.id_)): os.mkdir("../userimage/{0}".format(current_user.id_)) with open("../userimage/{0}/{1}".\ format(current_user.id_, file_name), "wb") as output_fp: output_fp.write(buff) current_user.default_image = file_name current_user.save_default_image() http_request.close_session_file() http_response.send_redirect("index") helper.Application(ChangeImgAction()).execute()
repeat_changed = form.getvalue('repeat_changed') if repeat_changed is None: http_response.send_html("passwd.html", error_message="重复密码不能为空!", email=current_user.email, image_path=current_user.get_image_path()) return http_request.close_session_file() if changed != repeat_changed: http_response.send_html("passwd.html", error_message="两次输入的密码不一致!", email=current_user.email, image_path=current_user.get_image_path()) return http_request.close_session_file() if helper.Utils.encryption_password(current) != current_user.password: http_response.send_html("passwd.html", error_message="当前密码不正确!", email=current_user.email, image_path=current_user.get_image_path()) return http_request.close_session_file() else: current_user.password = helper.Utils.encryption_password(changed) current_user.save_password() http_request.delete_session() http_request.close_session_file() http_response.send_redirect("login") helper.Application(PasswdAction()).execute()
error_message="数据库主机不能为空!") port = form.getfirst('port') if port is None: return http_response.send_html( "install.html", error_message="端口不能为空!") user = form.getfirst('user') if user is None: return http_response.send_html( "install.html", error_message="用户名不能为空!") password = form.getvalue('password') if password is None: return http_response.send_html( "install.html", error_message="密码不能为空!") database = form.getfirst('database') if database is None: return http_response.send_html( "install.html", error_message="数据库名不能为空!") if helper.Utils.config_database(host, port, user, password, database): http_response.send_redirect("login") else: http_response.send_html( "install.html", error_message="数据库连接错误,请检查参数是否正确!") helper.Application(InstallAction()).execute()
#!/usr/bin/env python #_*_coding:utf-8_*_ import cgitb import helper cgitb.enable() class LogoutAction(helper.Action): """logout action""" def __init__(self): """ """ super(LogoutAction, self).__init__() def doget(self, http_request, http_response): """ http get function """ http_request.delete_session() http_request.close_session_file() http_response.send_redirect("login") def dopost(self, http_request, http_response): self.doget(http_request, http_response) helper.Application(LogoutAction()).execute()
error_message="输入的邮箱地址格式不正确!") password = form.getvalue('password') if password is None: return http_response.send_html( "regist.html", error_message="密码不能为空!") repeat_password = form.getvalue('repeat_password') if repeat_password is None: return http_response.send_html( "regist.html", error_message="重复密码不能为空!") if password != repeat_password: return http_response.send_html( "regist.html", error_message="两次输入的密码不一致!") newuser = helper.User() newuser.email = email newuser.password = helper.Utils.encryption_password(password) if newuser.exists(): return http_response.send_html( "regist.html", error_message="该邮箱已被注册!") newuser.add_to_db() http_response.send_redirect("login") helper.Application(RegistAction()).execute()
""" http post function """ form = http_request.parameters email = form.getfirst('email') password = form.getfirst('password') if email is None or password is None: return http_response.send_html( "login.html", error_message="邮箱和密码不能为空!") if not helper.Utils.verify_email(email): return http_response.send_html( "login.html", error_message="输入的邮箱地址格式不正确!") newuser = helper.User() newuser.email = email newuser.password = helper.Utils.encryption_password(password) if not newuser.auth(): return http_response.send_html( "login.html", error_message="用户名密码不匹配,或者该用户不存在!") session = http_request.get_session() session["user"] = newuser http_request.close_session_file() http_response.send_redirect("index") helper.Application(LoginAction()).execute()