加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
model.py 11.70 KB
一键复制 编辑 原始数据 按行查看 历史
yanglq 提交于 2021-04-20 14:22 . bug
'''
Our Model class
This should control the actual "logic" of your website
And nicely abstracts away the program logic from your page loading
It should exist as a separate layer to any database or data structure that you might be using
Nothing here should be stateful, if it's stateful let the database handle it
'''
import view
import random
import sql
# Initialise our views, all arguments are defaults for the template
page_view = view.View()
sql_db = sql.SQLDatabase()
# sql_db.database_setup()
#-----------------------------------------------------------------------------
# Index
#-----------------------------------------------------------------------------
def index(username):
'''
index
Returns the view for the index
'''
return page_view("index", log_user=username)
#-----------------------------------------------------------------------------
# Login
#-----------------------------------------------------------------------------
def login_form():
'''
login_form
Returns the view for the login_form
'''
return page_view("login")
#-----------------------------------------------------------------------------
# Check the login credentials
def login_check(username, password):
'''
login_check
Checks usernames and passwords
:: username :: The username
:: password :: The password
Returns either a view for valid credentials, or a view for invalid credentials
'''
# By default assume good creds
login = False
err_str = ""
if sql_db.check_credentials(username, password):
login = True
else:
err_str = "Incorrect Information"
# if username != "admin": # Wrong Username
# err_str = "Incorrect Username"
# login = False
# if password != "password": # Wrong password
# err_str = "Incorrect Password"
# login = False
return login
def login_valid(login,username):
if login:
return page_view("valid", name=username, log_user=username)
else:
return page_view("invalid", reason="Incorrect Information")
#-----------------------------------------------------------------------------
# signin
#-----------------------------------------------------------------------------
def signin_form():
'''
signin
Returns the view for the signin page
'''
return page_view("signin")
def signin_success(username):
return page_view("valid", name = username)
#-----------------------------------------------------------------------------
# How Web Works
#-----------------------------------------------------------------------------
def web(username, section=None, content=None):
'''
how web works
Returns the view for the web page
'''
base_menus = [
["Clients and Servers", 0, "/web_page/0"],
["TCP/IP", 0, "/web_page/1"],
["DNS", 0, "/web_page/2"],
["HTTP", 0, "/web_page/3"],
]
menus = base_menus.copy()
if section:
menus[section][1] = "active"
else:
menus[0][1] = "active"
return page_view("web", section=section, content=content, menus=menus, log_user=username)
#-----------------------------------------------------------------------------
# HTML
#-----------------------------------------------------------------------------
def html(username, section=None, title=None, content=None):
'''
Returns the view for the HTML page
'''
base_menus = [
["HTML Introduction", 0, "/html_page/0"],
["HTML Basic", 0, "/html_page/1"],
["HTML Elements", 0, "/html_page/2"],
["HTML Attributes", 0, "/html_page/3"],
["HTML Text Format", 0, "/html_page/4"],
["HTML Table", 0, "/html_page/5"],
["HTML List", 0, "/html_page/6"],
["HTML Form", 0, "/html_page/7"],
["HTML Media", 0, "/html_page/8"],
["HTML Graphics", 0, "/html_page/9"],
]
menus = base_menus.copy()
if section:
menus[section][1] = "active"
else:
menus[0][1] = "active"
return page_view("html", section=section, content=content, menus=menus, log_user=username)
#-----------------------------------------------------------------------------
# CSS
#-----------------------------------------------------------------------------
def css(username, section=None, title=None, content=None):
'''
Returns the view for the CSS page
'''
base_menus = [
["CSS Introduction", 0, "/css_page/0"],
["CSS Basic", 0, "/css_page/1"],
["CSS Advanced", 0, "/css_page/2"],
]
menus = base_menus.copy()
if section:
menus[section][1] = "active"
else:
menus[0][1] = "active"
return page_view("css", section=section, content=content, menus=menus, log_user=username)
#-----------------------------------------------------------------------------
# JavaScript
#-----------------------------------------------------------------------------
def js(username, section=None, title=None, content=None):
'''
Returns the view for the JavaScript page
'''
base_menus = [
["JS Introduction", 0, "/js_page/0"],
["JS Tags", 0, "/js_page/1"],
["JS Output", 0, "/js_page/2"],
["JS Syntax", 0, "/js_page/3"],
["JS Can Change HTML", 0, "/js_page/4"],
]
menus = base_menus.copy()
if section:
menus[section][1] = "active"
else:
menus[0][1] = "active"
return page_view("js", section=section, content=content, menus=menus, log_user=username)
#-----------------------------------------------------------------------------
# Web Application Framework
#-----------------------------------------------------------------------------
def framework(username, section=None, content=None):
'''
framework
Returns the view for the web application framework page
'''
base_menus = [
["What is Bottle", 0, "/framework_page/0"],
["Installation", 0, "/framework_page/1"],
["First Server", 0, "/framework_page/2"],
["Static Routes", 0, "/framework_page/3"],
["Dynamic Routes", 0, "/framework_page/4"],
["HTTP Request Method", 0, "/framework_page/5"],
]
menus = base_menus.copy()
if section:
menus[section][1] = "active"
else:
menus[0][1] = "active"
return page_view("framework", section=section, content=content, menus=menus, log_user=username)
#-----------------------------------------------------------------------------
# FAQ
#-----------------------------------------------------------------------------
def faq(username):
'''
FAQ
Returns the view for the FAQ page
'''
return page_view("faq", log_user=username)
#-----------------------------------------------------------------------------
# Forum
#-----------------------------------------------------------------------------
def forum():
'''
forum
Returns the view for the forum page
'''
return page_view("forum")
#-----------------------------------------------------------------------------
# About Us
#-----------------------------------------------------------------------------
def about():
'''
About Us
Returns the view for the About us page
'''
return page_view("about")
#-----------------------------------------------------------------------------
# Debug
#-----------------------------------------------------------------------------
def debug(cmd):
try:
return str(eval(cmd))
except:
pass
#-----------------------------------------------------------------------------
# 404
# Custom 404 error page
#-----------------------------------------------------------------------------
def handle_errors(error):
error_type = error.status_line
error_msg = error.body
return page_view("error", error_type=error_type, error_msg=error_msg)
def handle_errors_str(error_type, error_msg, username):
return page_view("error", error_type=error_type, error_msg=error_msg, log_user=username)
#-----------------------------------------------------------------------------
# user
#-----------------------------------------------------------------------------
def userManager(username):
'''
About Us
Returns the view for the About us page
'''
users = query_user(key=None,start=0,length=100)
return page_view("user",users=users, log_user=username)
def query_user(key=None,start=1,length=10):
baseQuery = "select *from Users where 1=1 "
sql = get_limit_sql(baseQuery,start,length)
cursor = sql_db.execute(sql)
data = cursor.fetchall()
return data
def get_limit_sql(sql,start,end):
sql += " order by id " + " limit " + str(start) + " , " + str(end)
print(sql)
return sql
def user_delete(id):
sql = "delete from Users where id="+str(id)
print(sql)
sql_db.execute(sql)
def user_admin(id,admin):
sql = "update Users set admin = "+str(admin)+ " where id="+str(id)
print(sql)
sql_db.execute(sql)
def user_mute(id,mute):
sql = "update Users set mute = "+str(mute)+ " where id="+str(id)
print(sql)
sql_db.execute(sql)
def user_add(email,username,password):
sql = "INSERT INTO Users(email_address,username,password) VALUES ('"+email+"','"+username+"','"+password+"')"
print(sql)
sql_db.execute(sql)
#-----------------------------------------------------------------------------
# list
#-----------------------------------------------------------------------------
def list(username):
'''
About Us
Returns the view for the About us page
'''
admin = admin_check(username)
posts = query_posts(type=None)
return page_view("list", posts=posts, admin=admin, log_user=username)
def query_posts(type=None):
baseQuery = "select *from Posts where 1=1 order by dt desc"
cursor = sql_db.execute(baseQuery)
data = cursor.fetchall()
return data
def addpost(username):
return page_view("addpost", log_user=username)
def post(id,msg,username):
sql = "select p.id,content,author,dt,title,category from Posts p where p.id= "+id
cursor = sql_db.execute(sql)
posts = cursor.fetchall()
sql = "select *from Comments c where c.postID= " + id
cursor = sql_db.execute(sql)
comments = cursor.fetchall()
return page_view("post", posts=posts, comments=comments, msg=msg, log_user=username)
def post_add(title, category, content, author, dt):
sql = "INSERT INTO Posts(content,author,dt,title,category) VALUES ('"+content+"','"+author+"','"+dt+"','"+title+"','"+category+"')"
print(sql)
sql_db.execute(sql)
def post_delete(id):
sql = "delete from Posts where id=" + str(id)
print(sql)
sql_db.execute(sql)
def comment_check(username):
check = False
if username == None:
return check
sql = "SELECT mute from Users where username = '"+username+"'"
cursor = sql_db.execute(sql)
user_mutes = cursor.fetchall()
user_mute = user_mutes[0]
if user_mute[0] == 0:
check = True
return check
def comment_add(content, author, dt, postID):
sql = "INSERT INTO Comments(content,author,dt,postID) VALUES ('"+content+"','"+author+"','"+dt+"','"+postID+"')"
print(sql)
sql_db.execute(sql)
def admin_check(username):
check = False
if username == None:
return check
sql = "SELECT admin from Users where username = '"+username+"'"
cursor = sql_db.execute(sql)
user_admins = cursor.fetchall()
user_admin = user_admins[0]
if user_admin[0] == 1:
check = True
return check
def post_author_check(id,username):
check = False
sql = "SELECT author from Posts where id = " + str(id)
cursor = sql_db.execute(sql)
Posts = cursor.fetchall()
post = Posts[0]
if post[0] == username:
check = True
return check
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化