def taskeditform(self, id=None, *args, **kwargs): if ormdb == "sqlite": # 為了避免跨執行緒使用 SQLite3, 重新 connect 資料庫 Database.connect() user = self.printuser() password, adsense, anonymous, mail_suffix, site_closed, read_only = self.parse_config(filename="pygroup_config") if read_only == "yes" and user != "admin": return "<a href='/'>Go to main page</a><br /><br />error, site is read only!" if user == "anonymous" and anonymous != "yes": raise cherrypy.HTTPRedirect("login") else: try: query = Task.at(int(id)).select() result = query.execute() data = result.one() output = "user:"******", owner:"+data.owner+"<br /><br />" if user != data.owner: if user != "admin": return output + "error! Not authorized!" else: template_lookup = TemplateLookup(directories=[template_root_dir+"/templates"]) mytemplate = template_lookup.get_template("taskeditform.html") return mytemplate.render(user=user, id=id, data=data) else: template_lookup = TemplateLookup(directories=[template_root_dir+"/templates"]) mytemplate = template_lookup.get_template("taskeditform.html") return mytemplate.render(user=user, id=id, data=data) except: return "error! Not authorized!"
def taskdelete(self, id=None, type=None, name=None, content=None, *args, **kwargs): if ormdb == "sqlite": # 為了避免跨執行緒使用 SQLite3, 重新 connect 資料庫 Database.connect() # check user and data owner user = self.printuser() password, adsense, anonymous, mail_suffix, site_closed, read_only = self.parse_config(filename="pygroup_config") if read_only == "yes" and user != "admin": return "<a href='/'>Go to main page</a><br /><br />error, site is read only!" if user == "anonymous" and anonymous != "yes": raise cherrypy.HTTPRedirect("login") query = Task.at(int(id)).select() result = query.execute() data = result.one() now = strftime("%Y-%m-%d %H:%M:%S", localtime()) output = "user:"******", owner:"+data.owner+"<br /><br />" if user != data.owner: if user != "admin": return "error! Not authorized!" else: # 若資料為主緒則一併刪除子緒, 若為子緒, 則只刪除該子緒 if data.follow == 0: # 表示資料為主緒 # 先刪除主緒 query = Task.at(int(id)).delete() query.execute() # 再刪除所有對應子緒 query = Task.where(follow=int(id)).delete() query.execute() output += "所有序列資料已經刪除!<br />" else: # 表示資料為子緒 query = Task.at(int(id)).delete() query.execute() output += "資料已經刪除!<br />" else: # 若資料為主緒則一併刪除子緒, 若為子緒, 則只刪除該子緒 if data.follow == 0: # 表示資料為主緒 # 先刪除主緒 query = Task.at(int(id)).delete() query.execute() # 再刪除所有對應子緒 query = Task.where(follow=int(id)).delete() query.execute() output += '''所有序列資料已經刪除!<br /><br /> <a href='/'>Go to main page</a><br /> ''' else: # 表示資料為子緒 query = Task.at(int(id)).delete() query.execute() output += '''資料已經刪除!<br /><br /> <a href='/'>Go to main page</a><br /> ''' return output
def channel_set(request, param): if request.method == "GET": query_filter = {} try: query_filter['channel'] = request.GET.get('channel', '') query_filter['start_time'] = request.GET.get('start_time', '') query_filter['end_time'] = request.GET.get('end_time', '') except Exception as e: raise res = A_channel_set.get_list(query_filter) print(type(res)) option = {'is_public': global_conf.is_public, } res_1 = utils.prepare_table_data(res, option) return JsonResponse(res_1 if res_1 else res, safe = False) elif request.method == "POST": try: keys = ['channel', 'parent_id', 'weight', 'remark', 'channel_type', 'is_public'] par = utils.get_post_parameter(request, keys) par['admin'] = request.user.username par['status'] = 1 except Exception as e: print(traceback.format_exc()) return JsonResponse({"status": 1, "message":"参数错误"}) a_channel_set = A_channel_set() a_channel_set = utils.model_set(a_channel_set, par) try: a_channel_set.save() except Exception as e: return JsonResponse({"status": 1, "message":"渠道名相同"}) return JsonResponse({"status": 0, "message":"添加成功"}) elif request.method == "PUT": id = int(param) try: keys = ['channel', 'parent_id', 'weight', 'remark', 'channel_type', 'is_public'] par = utils.get_post_parameter(request, keys) par['admin'] = request.user.username except Exception as e: print(traceback.format_exc()) return JsonResponse({"status": 1, "message":"参数错误"}) a_channel_set = A_channel_set.where(id=id).select().execute.one() a_channel_set = utils.model_set(a_channel_set, par) try: a_channel_set.save() except Exception as e: return JsonResponse({"status": 1, "message":"渠道名相同"}) return JsonResponse({"status": 0, "message":"编辑成功"}) elif request.method == "DELETE": id = int(param) sql = "UPDATE a_channel_set SET status = 0 WHERE id = %s" % (id) Database.execute(sql) return JsonResponse({"status": 0, "message":"删除成功"})
def test_config(self): Database.config(db=mysql_db, user=mysql_user, passwd=mysql_passwd, charset='utf8', autocommit=True) assert Database.configs == { 'db': mysql_db, 'user': mysql_user, 'passwd': mysql_passwd, 'host': 'localhost', 'port': 3306, 'charset': 'utf8' } assert Database.autocommit is True
def _init_db(self, dbname): if not os.path.exists(dbname): cx = sqlite3.connect(dbname) cursor = cx.cursor() res = cursor.execute(""" create table proxy ( ip_port varchar primary key, anonymous varchar, proxy_type varchar, location varchar, response_time varchar, last_confirm varchar, last_used float, is_dead boolean ) """) print 'create a new proxy database in: %s' % os.path.abspath(dbname) Database.set_dbapi(sqlite3) Database.config(db=dbname)
def taskaction(self, type=None, name=None, follow=0, content=None, ip=None, *args, **kwargs): if content == None or name == "": return "標題與內容都不可空白!<br /><a href='/'>Go to main page</a><br />" else: start_time = time.time() owner = self.printuser() if self.allow_pass(owner) == "no": raise cherrypy.HTTPRedirect("login") ip = self.client_ip() now = datetime.datetime.now(pytz.timezone('Asia/Taipei')).strftime('%Y-%m-%d %H:%M:%S') # user 若帶有 @ 則用 at 代替 if "@" in owner: owner = owner.replace('@', 'at') content = content.replace('\n', '') #invalid_tags = ['table', 'th', 'tr', 'td', 'html', 'body', 'head', 'javascript', 'script', 'tbody', 'thead', 'tfoot', 'div', 'span'] #content = self.clean_html(content, invalid_tags) valid_tags = ['a', 'br', 'h1', 'h2', 'h3', 'p', 'div', 'hr', 'img', 'iframe', 'li', 'ul', 'b', 'ol', 'pre'] tags = '' for tag in valid_tags: tags += tag content = self.strip_tags(content, tags) # 這裡要除掉 </br> 關閉 break 的標註, 否則在部分瀏覽器會產生額外的跳行 content = str(content).replace('</br>', '') time_elapsed = round(time.time() - start_time, 5) if ormdb == "sqlite": # 為了避免跨執行緒使用 SQLite3, 重新 connect 資料庫 Database.connect() # last insert id 為 data.id data = Task.create(owner=owner, name=str(name), type=type, time=str(now), follow=follow, content=content, ip=str(ip)) # 這裡要與 taskedit 相同, 提供回到首頁或繼續編輯按鈕 output = "<a href='/'>Go to main page</a><br />" output +="<a href='/taskeditform?id="+str(data.id)+"'>繼續編輯</a><br /><br />" output += '''以下資料已經更新:<br /><br /> owner:'''+owner+'''<br /> name:'''+name+'''<br /> type:'''+type+'''<br /> time:'''+str(now)+'''<br /> content:'''+str(content)+'''<br /><br /> <a href='/'>Go to main page</a><br /> ''' output +="<a href='/taskeditform?id="+str(data.id)+"'>繼續編輯</a><br /><br />" return output
def test_change(self): conn = Database.conn Database.change(mysql_db) assert Database.conn is conn Database.config(db=mysql_db) Database.execute('insert into user set user.name="test"') assert Database.conn_is_up() assert Database.conn is not conn
import MySQLdb from skylark import Database, Model, Field from django.conf import settings as _options Database.set_dbapi(MySQLdb) Database.config(host=_options.DATABASES['default']['HOST'], user=_options.DATABASES['default']['USER'], passwd=_options.DATABASES['default']['PASSWORD'], port=int(_options.DATABASES['default']['PORT']), db=_options.DATABASES['default']['NAME'], charset='utf8')
import MySQLdb from skylark import Database, Model, Field, PrimaryKey, ForeignKey class User(Model): name = Field() email = Field() class Post(Model): name = Field() post_id = PrimaryKey() user_id = ForeignKey(User.id) Database.set_dbapi(MySQLdb) Database.config(db='mydb', user='******', passwd='')
def index(self, page=1, item_per_page=5, id=0, flat=0, desc=0, keyword=None, *args, **kwargs): if ormdb == "sqlite": # 為了避免跨執行緒使用 SQLite3, 重新 connect 資料庫 Database.connect() user = self.printuser() # 這裡不用 self.allow_pass 原因在於需要 adsense 變數 saved_password, adsense, anonymous, mail_suffix, site_closed, read_only = self.parse_config(filename="pygroup_config") if user == "anonymous" and anonymous != "yes": if id != 0: raise cherrypy.HTTPRedirect("login?id="+id) else: raise cherrypy.HTTPRedirect("login") if adsense == "yes": filename = data_dir+"adsense_content" with open(filename, encoding="utf-8") as file: adsense_content = file.read() else: adsense_content = "" #ip = cherrypy.request.remote.ip ip = self.client_ip() template_lookup = TemplateLookup(directories=[template_root_dir+"/templates"]) # 必須要從 templates 目錄取出 tasklist2.html mytemplate = template_lookup.get_template("tasklist.html") # 這裡要加入單獨根據 id 號, 列出某一特定資料緒的分支 # 若 id 為 0 表示非指定列出各別主緒資料, 而是列出全部資料 # 這時可再根據各筆資料列印時找出各主緒資料的附屬資料筆數 # 加入 flat = 1 時, 列出所有資料 # 請注意這裡直接從 tasksearchform.html 中的關鍵字查詢, 指定以 tasklist 執行, 但是無法單獨列出具有關鍵字的 task 資料, 而是子緒有關鍵字時, 也是列出主緒資料 if keyword == None: if id == 0: if flat == 0: # 只列出主資料緒 # desc 為 0 表示要 id 由小到大排序列出資料 if desc == 0: method = "?" query = Task.where(Task.follow==0).select() else: # desc 為 1 表示 id 反向排序 method = "?desc=1" query = Task.where(Task.follow==0).orderby(Task.id, desc=True).select() result = query.execute() data = result.all() else: # flat 為 1 表示要列出所有資料 # 原先沒有反向排序, 內建使用正向排序 if desc == 0: method = "?flat=1" query = Task.select() else: method = "?flat=1&desc=1" query = Task.orderby(Task.id, desc=True).select() result = query.execute() data = result.all() else: method = "?id="+str(id) # 設法列出主資料與其下屬資料緒, 這裡是否可以改為 recursive 追蹤多緒資料 # 只列出主緒與下一層子緒資料 query = Task.where((Task.id == id) | (Task.follow == id)).select() result = query.execute() data = result.all() else: # 有關鍵字查詢時(只查 owner, content, type 與 name), 只列出主資料緒 #flat = 1 method = "?keyword="+keyword+"&flat="+str(flat) query = Task.where((Task.content.like('%%%s%%' % (keyword))) | (Task.name.like('%%%s%%' % (keyword))) | \ (Task.owner.like('%%%s%%' % (keyword))) | \ (Task.type.like('%%%s%%' % (keyword))) \ ).select() result = query.execute() data = result.all() follow = [] for task in data: # 改為 mysql query = Task.where((Task.follow == task.id)).select() result = query.execute() follow_data = result.all() follow.append(len(follow_data)) # # 送出 user, id, flat, method 與 data # # 增加傳送 read_only, 若 read_only = yes 則不列出 taskform, 而且所有新增編輯刪除功能均失效 # return mytemplate.render(user=user, id=id, flat=flat, method=method, data=data, \ page=page, item_per_page=item_per_page, ip=ip, follow=follow, keyword=keyword, \ adsense_content=adsense_content, adsense=adsense, anonymous=anonymous, \ site_closed=site_closed, read_only=read_only)
# coding=utf8 from skylark import Database, Model, Field Database.config(user='******', passwd='', db='mytest') class User(Model): name = Field() email = Field() user = User(name='Join', email='*****@*****.**') user.save()
def test_execute(self): cursor = Database.execute('insert into user set user.name="test"') assert cursor
from zhiz import app from skylark import Database # appfog mysql configuration import json, os services_json = json.loads(os.getenv("VCAP_SERVICES")) mysql_config = services_json["mysql-5.1"][0]["credentials"] DB_CFG = { "user": mysql_config["username"], "passwd": mysql_config["password"], "host": mysql_config["hostname"], "port": mysql_config["port"], "db": mysql_config["name"], } # update database configuration app.config["DB_CFG"].update(DB_CFG) app.name = "wsgi" application = app # !important # Configure database again Database.config(**app.config["DB_CFG"]) if __name__ == "__main__": app.run()
return md5(self.email).hexdigest() return '' class Blog(Model): name = Field() description = Field() disqus = Field() class Post(Model): datetime = Field() title = Field() title_pic = Field() body = Field() published = Field() @property def summary(self): return markdown.render(self.body[:150]) class Admin(Model): passwd = Field() Database.config(**app.config['DB_CFG'])
def taskdeleteform(self, id=None, *args, **kwargs): if ormdb == "sqlite": # 為了避免跨執行緒使用 SQLite3, 重新 connect 資料庫 Database.connect() user = self.printuser() password, adsense, anonymous, mail_suffix, site_closed, read_only = self.parse_config(filename="pygroup_config") if read_only == "yes" and user != "admin": return "<a href='/'>Go to main page</a><br /><br />error, site is read only!" if user == "anonymous" and anonymous != "yes": raise cherrypy.HTTPRedirect("login") else: try: # 這裡要區分刪除子緒或主緒資料 # 若刪除子緒, 則 data 只包含子緒資料, 若為主緒, 則 data 必須包含所有資料 # 先找出資料, 判定是否為主緒 query = Task.at(int(id)).select() result = query.execute() data = result.one() owner = data.owner if user != data.owner: if user != "admin": return output + "error! 非資料擁有者, Not authorized!" else: if data.follow == 0: # 表示該資料為主緒資料 # 資料要重新搜尋, 納入子資料 query = Task.where((Task.id == id) | (Task.follow == id)).select() result = query.execute() data = result.all() output = "資料為主緒資料<br />" # 增加一個資料類型判斷, main 表資料為主緒 type = "main" else: # 表示該資料為子緒資料 # 直接採用 data 資料送到 taskdeleteform.html output = "資料為子緒資料<br />" # 增加一個資料類型判斷, alone 表資料為子緒 type = "alone" output += "user:"******", owner:"+owner+"<br /><br />" template_lookup = TemplateLookup(directories=[template_root_dir+"/templates"]) mytemplate = template_lookup.get_template("taskdeleteform.html") # 這裡的 type 為所要刪除資料的類型, 為 main 或為 alone return mytemplate.render(user=user, id=id, data=data, type=type) else: if data.follow == 0: # 表示該資料為主緒資料 # 資料要重新搜尋, 納入子資料 query = Task.where((Task.id == id) | (Task.follow == id)).select() result = query.execute() data = result.all() output = "資料為主緒資料<br />" # 增加一個資料類型判斷, main 表資料為主緒 type = "main" else: # 表示該資料為子緒資料 # 直接採用 data 資料送到 taskdeleteform.html output = "資料為子緒資料<br />" # 增加一個資料類型判斷, alone 表資料為子緒 type = "alone" output += "user:"******", owner:"+owner+"<br /><br />" template_lookup = TemplateLookup(directories=[template_root_dir+"/templates"]) mytemplate = template_lookup.get_template("taskdeleteform.html") # 這裡的 type 為所要刪除資料的類型, 為 main 或為 alone return mytemplate.render(user=user, id=id, data=data, type=type) except: return "error! 無法正確查詢資料, Not authorized!"
def taskedit(self, id=None, type=None, name=None, content=None, *args, **kwargs): if ormdb == "sqlite": # 為了避免跨執行緒使用 SQLite3, 重新 connect 資料庫 Database.connect() # check user and data owner if id == None: return "error<br /><br /><a href='/'>Go to main page</a><br />" user = self.printuser() password, adsense, anonymous, mail_suffix, site_closed, read_only = self.parse_config(filename="pygroup_config") if read_only == "yes" and user != "admin": return "<a href='/'>Go to main page</a><br /><br />error, site is read only!" if user == "anonymous" and anonymous != "yes": raise cherrypy.HTTPRedirect("login") query = Task.at(int(id)).select() result = query.execute() data = result.one() now = strftime("%Y-%m-%d %H:%M:%S", localtime()) # Python 3.x: #import html.parser #html_parser = html.parser.HTMLParser() #content = html_parser.unescape(content) # 過濾資料 content = content.replace('\n', '') #invalid_tags = ['table', 'th', 'tr', 'td', 'html', 'body', 'head', 'javascript', 'script', 'tbody', 'thead', 'tfoot', 'div', 'span'] #content = self.clean_html(content, invalid_tags) valid_tags = ['a', 'br', 'h1', 'h2', 'h3', 'p', 'div', 'hr', 'img', 'iframe', 'li', 'ul', 'b', 'ol', 'pre'] tags = '' for tag in valid_tags: tags += tag content = self.strip_tags(content, tags) #content = self.html_filter(content, valid_tags) # 這裡要除掉 </br> 關閉 break 的標註, 否則在部分瀏覽器會產生額外的跳行 content = str(content).replace('</br>', '') output = "user:"******", owner:"+data.owner+"<br /><br />" if user != data.owner: if user != "admin": return "error! Not authorized!" else: query = Task.at(int(id)).update(type=type, name=name, content=content, time=str(now)) query.execute() output += "<a href='/'>Go to main page</a><br />" output +="<a href='/taskeditform?id="+str(id)+"'>繼續編輯</a><br /><br />" output += '''以下資料已經更新:<br /><br /> owner:'''+data.owner+'''<br /> name:'''+name+'''<br /> type:'''+type+'''<br /> time:'''+str(now)+'''<br /> content:'''+str(content)+'''<br /><br /> <a href='/'>Go to main page</a><br /> ''' output +="<a href='/taskeditform?id="+str(id)+"'>繼續編輯</a><br />" else: query = Task.at(int(id)).update(type=type, name=name, content=str(content), time=str(now)) query.execute() output += "<a href='/'>Go to main page</a><br />" output +="<a href='/taskeditform?id="+str(id)+"'>繼續編輯</a><br /><br />" output += '''以下資料已經更新:<br /><br /> owner:'''+data.owner+'''<br /> name:'''+name+'''<br /> type:'''+type+'''<br /> time:'''+str(now)+'''<br /> content:'''+str(content)+'''<br /><br /> <a href='/'>Go to main page</a><br /> ''' output +="<a href='/taskeditform?id="+str(id)+"'>繼續編輯</a><br />" return output
from lxml import html import requests from skylark import Database, Model, Field #~ database connection Database.config(db='valdzia', user='******', passwd='123456') class employees(Model): name = Field() position = Field() #~ get contacts page contactspage = requests.get('http://president.lt/lt/dbs_kontaktai/printerlist.html') contactstree = html.fromstring(contactspage.text) #~ Get staff names and positions from contacts page staffname = [el.text or ' ' for el in contactstree.xpath('//p[@class="vv"]')] staffposition = [el.text or ' ' for el in contactstree.xpath('//p[@class="vvv"]')] #~ Write staff information to database employees.create(name=staffname[i],position=staffposition[i])
class Groups(Model): id = PrimaryKey() uid = Field() # 用户id name = Field() # 分类名 class Ngroups(Model): uid = Field() # 用户id name = Field() # 分组名 count = Field() # 分组好友数量 if __name__ == "__main__": import sqlite3 import os from skylark import Database Database.set_dbapi(sqlite3) Database.set_autocommit(True) Database.config(db = os.path.abspath("../../data/data.db")) r = Groups.where(Groups.id == "1", Groups.uid == 1).update(name = "bbbdfdfdfb").execute() print(r) r = Ngroups.select() for i in r: print(i.data) groups = Ngroups.where(Ngroups.uid == 1).select() for g in groups: print(g) print([group.data for group in groups])
def setUp(self): create_tables() Database.config(db=mysql_db, user=mysql_user, passwd=mysql_passwd)
return '' class Blog(Model): name = Field() description = Field() disqus = Field() class Post(Model): datetime = Field() title = Field() title_pic = Field() body = Field() published = Field() @property def summary(self): return markdown.render(self.body[:150]) class Admin(Model): passwd = Field() import pymysql Database.set_dbapi(pymysql) Database.config(**app.config['DB_CFG'])
# coding=utf8 import pymysql from skylark import Database, Model, Field from messageboard import app class Message(Model): title = Field() content = Field() create_at = Field() Database.set_dbapi(pymysql) Database.config(**app.config['DB_CONN_CFG'])
def __init__(self, db_name): Database.set_dbapi(sqlite3) Database.config(db=db_name)
import sqlite3 from skylark import Database, Model, Field, PrimaryKey, ForeignKey class User(Model): name = Field() email = Field() class Post(Model): name = Field() post_id = PrimaryKey() user_id = ForeignKey(User.id) Database.set_dbapi(sqlite3) Database.config(db='mydb')
def test_connect(self): Database.connect() assert Database.conn_is_up()
from skylark import Database, Model, Field, PrimaryKey, ForeignKey class User(Model): name = Field() email = Field() class Post(Model): name = Field() post_id = PrimaryKey() user_id = ForeignKey(User.id) Database.config(db='mydb', user='******', passwd='')
def initdb(path = None): Database.set_dbapi(sqlite3) Database.set_autocommit(True) Database.config(db = path if path else lib.DBPATH, check_same_thread = False)
# 表示程式在雲端執行 download_root_dir = os.environ['OPENSHIFT_DATA_DIR'] data_dir = os.environ['OPENSHIFT_DATA_DIR'] template_root_dir = os.environ['OPENSHIFT_REPO_DIR']+"/wsgi/static" # template_root_dir = _curdir + "/static" else: # 表示程式在近端執行 download_root_dir = _curdir + "/local_data/" data_dir = _curdir + "/local_data/" template_root_dir = _curdir + "/static" # 資料庫選用 # 內建使用 sqlite3 ormdb = "sqlite" #ormdb = "mysql" if ormdb == "sqlite": Database.set_dbapi(sqlite3) Database.config(db=data_dir+"task.db") else: # 選用 mysql # 注意 port 必須為整數, 而非字串 if 'OPENSHIFT_REPO_DIR' in os.environ.keys(): Database.set_dbapi(pymysql) Database.config(host=os.environ[str('OPENSHIFT_MYSQL_DB_HOST')], \ port=int(os.environ['OPENSHIFT_MYSQL_DB_PORT']), db='cadp', \ user=os.environ['OPENSHIFT_MYSQL_DB_USERNAME'], \ passwd=os.environ['OPENSHIFT_MYSQL_DB_PASSWORD'], charset='utf8') else: Database.set_dbapi(pymysql) Database.config(host='localhost', port=3306, db='cadp', user='******', passwd='root', charset='utf8') #@-<<declarations>> #@+others
# -*- coding: utf-8 -*- import MySQLdb from skylark import Database, Model, Field from config import DB_HOST, DB_PORT, DB_USER, DB_PWD, DB class Userdevice(Model): id = Field() deviceId = Field() deviceType = Field() openId = Field() fromUserName = Field() Database.set_dbapi(MySQLdb) Database.config(host=DB_HOST, port=DB_PORT, db=DB, user=DB_USER, passwd=DB_PWD) if __name__ == '__main__': user = Userdevice(deviceId='koovox_01', deviceType='wechat', openId='123456', fromUserName='******') user.save()