#python 3.6
#蔡军生
#http://blog.csdn.net/caimouse/article/details/51749579
#
import web
import settings
import model
import util
import os
import json
import hashlib
urls = (
'/', 'Index', #首页
'/register', 'Register', #注册
'/user/(\d+)', 'Profile', #用户简介
'/login', 'Login', #登录
'/logout', 'Logout', #退出登录
'/password', 'Password', #找回密码
'/add', 'Add', #发贴
'/view/(\d+)', 'View', #看贴/回贴
'/account/(\w+)', 'Account', #查看一个帐号所有文章
'/edit/(\d+)', 'Edit', #编辑和修改以前的文章
'/del/(\d+)', 'Del', #删除文章
'/about', 'About' #关于
)
#
app = web.application(urls, globals(), autoreload = True)
##### BEG: 模板渲染 #####
curdir = os.path.abspath(os.path.dirname(__file__))
templates = curdir + '/templates/'
def render(params={}, partial=False):
global_vars = {**settings.GLOBAL_PARAMS, **params}
if partial:
return web.template.render(templates, globals=global_vars)
else:
return web.template.render(templates, base='layout', globals=global_vars)
def titled_render(subtitle=''):
subtitle = subtitle + ' - ' if subtitle else ''
return render({'title': subtitle + settings.SITE_NAME, 'make_html': util.make_html,
'trim_utf8': util.trim_utf8, 'menu': util.menu(model.User())})
##### END: 模板渲染 #####
class Index:
def GET(self):
i = web.input(page='1')
page = int(i.page)
page_posts, page_count = model.Post().list(page)
#计算分页
grace = 5
range = grace * 2
start = page - grace if page - grace > 0 else 1
end = start + range
if end > page_count:
end = page_count
start = end - range if end - range > 0 else 1
return titled_render().list(page_posts, page_count, page, start, end)
class Register:
def GET(self):
return titled_render('注册').register()
def POST(self):
try:
i = web.input()
user_id = model.User().new(i.email, i.username, i.password)
except Exception as e:
print(e)
return titled_render().failed('邮箱或帐号已存在,请重新<a href="/register">注册</a>')
else:
if user_id:
# 设置cookie
web.setcookie('user_id', str(user_id), settings.COOKIE_EXPIRES)
raise web.seeother('/user/%d' % user_id)
class Profile:
def GET(self, user_id):
user_id = int(user_id)
status = model.User().status(user_id)
if status['username']:
if user_id == model.User().current_id():
return titled_render(status['username']).master_profile(status['username'], status['picture'], status['description'])
else:
return titled_render(status['username']).user_profile(status['username'], status['picture'], status['description'])
else:
raise web.notfound()
def POST(self, user_id):
# 获取当前登录用户的状态
user_id = int(user_id)
user = model.User()
status = user.status(user_id)
# 将头像和简介
i = web.input(mypic={}, description='')
if 'mypic' in i:
filepath = i.mypic.filename.replace('\\','/') # 将Windows风格的斜杠转换为Linux风格
filename = filepath.split('/')[-1] # 文件名(带后缀)
ext = filename.split('.')[-1] # 扩展名
# 扩展名不为空时才更新头像
if ext:
# 网站主页的相对路径
rel_filename = settings.IMG_DIR + '/' + str(user_id) + '_head.' + ext
# 服务器上的绝对路径
abs_filename = curdir + '/' + rel_filename
# 1.更新头像路径和简介
if user.update(user_id, picture = rel_filename):
# 2.更新数据库成功后,保存头像
with open(abs_filename, 'wb') as f:
f.write(i.mypic.file.read())
# 对简介不做检查,直接更新
user.update(user_id, description=i.description)
raise web.seeother('/user/%d' % user_id)
class Login:
def GET(self):
return titled_render('登录').login()
def POST(self):
i = web.input(username='', password='')
user_id = model.User().login(i.username, i.password)
if user_id:
# 设置cookie
web.setcookie('user_id', str(user_id), settings.COOKIE_EXPIRES)
raise web.seeother('/user/%d' % user_id)
else:
return titled_render().failed('登录验证失败,请检查帐号和密码是否正确')
class Logout:
def GET(self):
if model.User().current_id(): # 用户已登录
# 取消cookie
web.setcookie('user_id', '', -1)
raise web.seeother('/')
class Add:
def GET(self):
if model.User().current_id(): # 用户已登录
return titled_render('发帖').add()
else:
return titled_render().failed('操作受限,请先<a href="/login">登录</a>')
def POST(self):
i = web.input(title='', content='')
post_id = model.Post().new(i.title, i.content, model.User().current_id())
if post_id:
raise web.seeother("/view/%d" % post_id)
else:
return titled_render().failed('你不应该到达这里')
class View:
def GET(self, post_id):
post_id = int(post_id)
post = model.Post().view(post_id)
if post:
comment = model.Comment(int(post_id))
comments = comment.quote(comment.list())
comment_lis = util.comments_to_lis(comments)
return titled_render(post.title).view(post, comment_lis)
else:
raise web.seeother('/')
def POST(self, post_id):
# jQuery+Ajax实现无刷新回帖
i = web.input()
cur_user_id = model.User().current_id()
web.header('Content-Type', 'application/json')
if cur_user_id:
comment = model.Comment(int(post_id))
# 回帖成功:返回"回帖"+"引用贴"信息
if comment.new(i.content, cur_user_id, i.quote_id):
comments = comment.quote([comment.last()])
return json.dumps(util.comments_to_lis(comments))
# 无权限:返回空
return json.dumps([])
class Account:
def GET(self, part):
# 获取当前登录用户的状态
cur_user_id = model.User().current_id()
status = model.User().status(cur_user_id)
# 已登录用户才能进行账户管理
if cur_user_id:
dispatch = {'posts': titled_render('文章').account_posts(model.Post().digest_list(cur_user_id)),
'settings': titled_render('设置').account_settings(status['username'],
status['picture'], status['description'], status['email'])}
if part in dispatch:
return dispatch[part]
else: # 无法访问
raise web.notfound()
# 用户未登录
return titled_render().failed('操作受限,请先<a href="/login">登录</a>')
def POST(self, part):
# 获取当前登录用户的状态
user = model.User()
cur_user_id = user.current_id()
status = user.status(cur_user_id)
i = web.input(email='', password='')
web.header('Content-Type', 'application/json')
if cur_user_id:
# 修改成功
if user.update(cur_user_id, email=i.email, password=
没有合适的资源?快使用搜索试试~ 我知道了~
python web py入门配套源代码
共41个文件
js:14个
html:14个
py:4个
4星 · 超过85%的资源 需积分: 36 93 下载量 89 浏览量
2018-02-23
09:29:02
上传
评论 5
收藏 88KB RAR 举报
温馨提示
python web py入门- 实现论坛,开发技术:后端采用Python+web.py+MySQL,前端采用HTML+CSS+JavaScript+jQuery+RequireJS。
资源推荐
资源详情
资源评论
收起资源包目录
caiforum2.rar (41个子文件)
caiforum
settings.py 817B
util.py 2KB
schema.sql 1KB
model.py 9KB
forum.py 11KB
templates
master_profile.html 758B
edit.html 912B
view.html 1KB
user_profile.html 202B
about.html 213B
account_settings.html 1KB
register.html 906B
login.html 639B
failed.html 34B
list.html 889B
password.html 417B
layout.html 1KB
add.html 737B
account_posts.html 539B
static
img
user_normal.jpg 1KB
buttons.gif 3KB
2_head.png 8KB
girl.gif 4KB
toolbar.gif 68B
boy.gif 4KB
js
main.js 1007B
lib
jquery-1.10.1.min.js 91KB
jquery.cleditor.min.js 12KB
require.min.js 15KB
app
setting.js 5KB
login.js 1KB
validator.js 1KB
password.js 2KB
register.js 2KB
profile.js 2KB
cleditor_use.js 307B
common.js 1016B
comment.js 3KB
new_post.js 1KB
css
jquery.cleditor.css 1KB
style.css 7KB
共 41 条
- 1
资源评论
- iHappy522018-09-05不错,学习一下!
caimouse
- 粉丝: 1w+
- 资源: 46
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功