def PUT(self, uri): LogClient.info('PUT - ' + uri) attrs = Controller.__getAttrs(uri) if (len(attrs) > 0) & (attrs[0] == 'api'): attrs[0] = 'put' return self._cmd['api'](attrs) return ''
def updateCard(cardId, cardJsonStr): if (cardId is None) | (cardJsonStr is None): return BuildClient.EMPTY_INPUT try: cardJsonObj = json.loads(cardJsonStr) except: return BuildClient.NOT_JSON_FORMAT LogClient.info('UPDATE CARD') LogClient.info(cardJsonObj) db = MysqlConn().getMysqlConn('wp') cur = db.cursor() description = '%s - %s' % (cardJsonObj['introduction'], cardJsonObj['name']) now = time.time() try: if cardId[0] == 'p': ids = cardId.split('-') cardId = ids[1] privateToken = ids[2] sql = "UPDATE `wp_card` SET `data` = '%s', `description` = '%s', `last_update` = '%s' WHERE `id` = '%s' AND `private_token` = '%s'" % ( cardJsonStr, description, now, cardId, privateToken) else: sql = "UPDATE `wp_card` SET `data` = '%s', `description` = '%s', `last_update` = '%s' WHERE `id` = '%s' AND `is_private` = '0'" % ( cardJsonStr, description, now, cardId) LogClient.info(sql) cur.execute(sql) db.commit() except: LogClient.error(sys.exc_info()) finally: cur.close() BuildClient.addCardHistory(cardId, cardJsonStr) LogClient.info('UPDATE CARD SUCCEED') return {"status": "succeed"}
def DELETE(self, uri): LogClient.info('DELETE - ' + uri) attrs = Controller.__getAttrs(uri) if (len(attrs) > 0) & (attrs[0] == 'api'): attrs[0] = 'delete' return self._cmd['api'](attrs) return ''
def getCard(cardId): db = MysqlConn().getMysqlConn('wp') cur = db.cursor() item = None try: if cardId[0] == 'p': ids = cardId.split('-') cardId = ids[1] privateToken = ids[2] sql = "SELECT * FROM `wp_card` WHERE `id` = '%s' AND `is_delete` = '0' AND `private_token` = '%s' LIMIT 1" % ( cardId, privateToken) else: sql = "SELECT * FROM `wp_card` WHERE `id` = '%s' AND `is_delete` = '0' AND `is_private` = '0' LIMIT 1" % cardId cur.execute(sql) item = cur.fetchone() except: LogClient.error(sys.exc_info()) finally: cur.close() if item is not None: return { "status": "succeed", "data": { "creator": item['creator'], "imgpath": item['img_path'], "imgpath_large": item['img_path_large'], "ctime": item['ctime'], "last_update": item['last_update'], "data": json.loads(item['data']) } } else: return BuildClient.ERROR
def convertPage(pageName, creator, imgPath, pageData): LogClient.info("Convert Page Start: %s" % pageName) filePath = r'%s/page/%s.html' % (ABS_PATH, pageName) if os.path.isfile(filePath): os.remove(filePath) htmlData = WikiEngine.strToHtml(pageData) file = open(filePath,'wa') file.write('$def with (DATA)\n') file.write('<!DOCTYPE html>\n') file.write('<html>\n') file.write('<head>\n') file.write(' <meta http-equiv="content-type" content="text/html; charset=UTF-8">\n') file.write(' <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">\n') file.write(' <title>%s - Will Project Wiki</title>\n' % pageName) file.write(' <link rel="icon" type="image/png" href="$DATA[\'HOST\']/favicon.ico"/>\n') file.write(' <script type="text/javascript">const DATA = $:DATA; </script>\n') file.write(' <script type="text/javascript" src="http://cdn.bootcss.com/jquery/2.2.0/jquery.min.js"></script>\n') file.write(' <script type="text/javascript" src="http://cdn.bootcss.com/react/15.2.0/react-with-addons.min.js"></script>\n') file.write(' <script type="text/javascript" src="http://cdn.bootcss.com/react/15.2.0/react-dom.min.js"></script>\n') file.write(' <script type="text/javascript" src="http://cdn.bootcss.com/antd/1.6.4/antd.min.js"></script>\n') file.write(' <link type="text/css" rel="stylesheet" href="http://cdn.bootcss.com/antd/1.6.4/antd.min.css"/>\n') file.write(' <script type="text/javascript" src="$DATA[\'HOST\']/static/js/base.bundle.js"></script>\n') file.write('</head>\n') file.write('<body>\n') file.write('<div class="stage">\n') file.write(' <script>\n') file.write(' if (wp.base.BROWSER_TYPE) { $$(".stage").addClass("mobile"); }\n') file.write(' else { $$(".stage").addClass("pc"); }\n') file.write(' </script>\n') file.write(' <div class="main wrap" id="main">\n') file.write(' <div class="nav" id="nav" style="min-height:139px;"></div>\n') file.write(' <div class="content" id="content">\n') file.write(' <div style="margin:10px 0;float:right;font-size:small;"><a href="/editpage/%s">编辑本页</a></div>\n' % pageName) file.write(' <p style="text-align:left;font-weight:bold;">%s</p>\n' % pageName) file.write(' <div style="margin:10px;"><hr/></div>\n') file.write(' <div class="page-content" style="text-align:left;">\n') file.write(' <script> $$(".page-content").css("minHeight", wp.base.WINDOW_HEIGHT-300); </script>\n') if imgPath != "": file.write(' <div style="margin:10px;text-align:center;"><img style="max-width:100%;" src="' + imgPath + '"/></div>\n') file.write(htmlData) file.write('\n </div>\n') file.write(' <div style="margin:10px;"><hr/></div>\n') file.write(' <p style="font-size:small;">最后编辑:%s</p>\n' % creator) file.write(' <p style="font-size:small;">最后更新时间:%s</p>\n' % (time.strftime('%Y-%m-%d %H:%M', time.gmtime(time.time() + 3600 * 8)))) file.write(' </div>\n') file.write(' </div>\n') file.write('</div>\n') file.write('<script>wp.base.calcWindowHeight();</script>\n') file.write('</body>\n') file.write('<script type="text/javascript" src="$DATA[\'HOST\']/static/js/nav.bundle.js"></script>\n') file.write('</html>\n') LogClient.info("Convert Page Finished: %s" % pageName)
def updatePage(pageName, creator, imgPath, pageData): if (pageName is None) | (creator is None) | (imgPath is None) | (pageData is None): return PageClient.EMPTY_INPUT if len(pageName) > PageClient.PAGE_NAME_MAX_LENGTH: return PageClient.NAME_TOO_LONG pageItem = PageClient.getPage(pageName) if pageItem['status'] != 'succeed': return PageClient.NOT_EXIST if pageItem['is_lock'] != 0: return { "status": "locked" } LogClient.info('UPDATE PAGE') db = MysqlConn().getMysqlConn('wp') cur = db.cursor() now = time.time() try: sql = "UPDATE `wp_page` SET `creator` = '%s', `img_path` = '%s', `data` = '%s', `last_update` = '%s' WHERE `name` = '%s'" % (creator, imgPath, pageData, now, pageName) LogClient.info(sql) cur.execute(sql) db.commit() except: LogClient.error(sys.exc_info()) finally: cur.close() PageClient.addPageHistory(pageName, creator, pageData) PageClient.convertPage(pageName, creator, imgPath, pageData) LogClient.info('UPDATE PAGE SUCCEED') return { "status": "succeed" }
def addPage(pageName, creator, imgPath, pageData): if (pageName is None) | (creator is None) | (imgPath is None) | (pageData is None): return PageClient.EMPTY_INPUT if len(pageName) > PageClient.PAGE_NAME_MAX_LENGTH: return PageClient.NAME_TOO_LONG if (PageClient.checkPageByName(pageName)): return PageClient.REDUNDANT LogClient.info('INSERT PAGE') db = MysqlConn().getMysqlConn('wp') cur = db.cursor() now = time.time() try: sql = "INSERT INTO `wp_page` (`name`, `creator`, `img_path`, `data`, `ctime`, `last_update`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')" % (pageName, creator, imgPath, pageData, now, now) LogClient.info(sql) cur.execute(sql) db.commit() except: LogClient.error(sys.exc_info()) finally: cur.close() PageClient.addPageHistory(pageName, creator, pageData) PageClient.convertPage(pageName, creator, imgPath, pageData) LogClient.info('INSERT PAGE SUCCEED') return { "status": "succeed" }
def updateCardImgLarge(cardId, imgPath): if (cardId is None) | (imgPath is None): return BuildClient.EMPTY_INPUT LogClient.info('UPDATE CARD IMG LARGE') db = MysqlConn().getMysqlConn('wp') cur = db.cursor() now = time.time() try: if cardId[0] == 'p': ids = cardId.split('-') cardId = ids[1] privateToken = ids[2] sql = "UPDATE `wp_card` SET `img_path_large` = '%s', `last_update` = '%s' WHERE `id` = '%s' AND `private_token` = '%s'" % ( imgPath, now, cardId, privateToken) else: sql = "UPDATE `wp_card` SET `img_path_large` = '%s', `last_update` = '%s' WHERE `id` = '%s' AND `is_private` = '0'" % ( imgPath, now, cardId) LogClient.info(sql) cur.execute(sql) db.commit() except: LogClient.error(sys.exc_info()) finally: cur.close() LogClient.info('UPDATE CARD IMG LARGE SUCCEED') return {"status": "succeed"}
def refreshAllPages(): db = MysqlConn().getMysqlConn('wp') cur = db.cursor() items = None try: sql = "SELECT `name`, `creator`, `img_path`, `data` FROM `wp_page` WHERE 1=1" cur.execute(sql) items = cur.fetchall() except: LogClient.error(sys.exc_info()) finally: cur.close() if items is not None: for item in items: PageClient.convertPage(item['name'], item['creator'], item['img_path'], item['data'])
def GET(self, uri): LogClient.info('GET - ' + uri) attrs = Controller.__getAttrs(uri) if uri == '': return self._cmd['index'](attrs) if uri == 'favicon.ico': web.redirect('/static/img/favicon.ico') if (len(attrs) > 0) & (attrs[0] is not ''): if attrs[0] in self._cmd.keys(): key = attrs[0] attrs[0] = 'get' return self._cmd[key](attrs) else: return self._cmd['index'](attrs) return self._cmd['404'](attrs)
def checkPageByName(name): db = MysqlConn().getMysqlConn('wp') cur = db.cursor() item = None try: sql = "SELECT * FROM `wp_page` WHERE `name` = '%s' LIMIT 1" % name cur.execute(sql) item = cur.fetchone() except: LogClient.error(sys.exc_info()) finally: cur.close() if item is not None: return True else: return False
def countCards(): item = None db = MysqlConn().getMysqlConn('wp') cur = db.cursor() try: sql = "SELECT COUNT(1) AS num FROM `wp_card` WHERE `is_delete` = '0' AND `is_private` = '0'" cur.execute(sql) item = cur.fetchone() except: LogClient.error(sys.exc_info()) finally: cur.close() if item is not None: return item['num'] else: return 0
def getCards(page, pageSize): total = BuildClient.countCards() start = pageSize * (page - 1) db = MysqlConn().getMysqlConn('wp') cur = db.cursor() items = None try: sql = "SELECT `id`, `description`, `img_path` FROM `wp_card` WHERE `is_delete` = '0' AND `is_private` = '0' ORDER BY `id` DESC LIMIT %s, %s" % ( start, pageSize) cur.execute(sql) items = cur.fetchall() except: LogClient.error(sys.exc_info()) finally: cur.close() if items is not None: return {'status': 'succeed', 'total': total, 'data': items} else: return BuildClient.ERROR
def run(self, argvList): if len(argvList) == 1: argvList.append('help') cmd = argvList[1] if self._CMDLIST.has_key(cmd): method = self._CMDLIST[cmd][0] avgNum = self._CMDLIST[cmd][1] if len(argvList) <= avgNum: LogClient.error( 'Invalid Command. Needs %d Arguments, but only %d provided.' % (avgNum, len(argvList) - 2)) exit(0) if avgNum == 0: method() elif avgNum == 1: method(argvList[2]) elif avgNum == 2: method(argvList[2], argvList[3]) elif avgNum == 3: method(argvList[2], argvList[3], argvList[4]) else: LogClient.error('Impossible Error') exit(1) else: LogClient.error( "Invalid Command. Run 'python main.py help' for help") exit(0)
def upload(fileName, data): if (fileName is None) | (data is None): return ImgClient.EMPTY_INPUT LogClient.info(fileName) ext = fileName.replace('\\', '/').split('/')[-1].split('.', 1)[1] if (not ((ext == 'jpeg') | (ext == 'gif') | (ext == 'png') | (ext == 'bmp') | (ext == 'jpg') | (ext == 'webp'))): return ImgClient.NOT_SUPPORTED_FORMAT randFileName = randomStr(10) fout = open(ImgClient.IMG_STORAGE_PATH + randFileName + '.' + ext, 'wb') fout.write(data) fout.close() db = MysqlConn().getMysqlConn('wp') cur = db.cursor() now = time.time() try: sql = "INSERT INTO `wp_img` (`filename`, `ctime`) VALUES ('%s', '%s')" % (randFileName + '.' + ext, now) LogClient.info(sql) cur.execute(sql) db.commit() except: LogClient.error(sys.exc_info()) finally: cur.close() return { "status": "succeed", "filepath": WEB_CONF['host'] + '/static/img/custom/' + randFileName + '.' + ext }
def getPage(pageName): db = MysqlConn().getMysqlConn('wp') cur = db.cursor() item = None try: sql = "SELECT `data`, `creator`, `img_path`, `is_lock` FROM `wp_page` WHERE `name` = '%s' AND `is_delete` = '0' LIMIT 1" % pageName cur.execute(sql) item = cur.fetchone() except: LogClient.error(sys.exc_info()) finally: cur.close() if item is not None: return { "status": "succeed", "pagedata": item['data'], "creator": item['creator'], "imgpath": item['img_path'], "is_lock": item['is_lock'] } else: return PageClient.NOT_EXIST
def addPageHistory(pageName, creator, pageData): LogClient.info('INSERT PAGE HISTORY') db = MysqlConn().getMysqlConn('wp') cur = db.cursor() now = time.time() try: sql = "INSERT INTO `wp_page_history` (`name`, `creator`, `data`, `ctime`) VALUES ('%s', '%s', '%s', '%s')" % (pageName, creator, pageData, now) LogClient.info(sql) cur.execute(sql) db.commit() except: LogClient.error(sys.exc_info()) finally: cur.close() LogClient.info('INSERT PAGE HISTORY SUCCEED')
def makePublic(cardId): if cardId is None: return BuildClient.EMPTY_INPUT LogClient.info('MAKE PUBLIC CARD') db = MysqlConn().getMysqlConn('wp') cur = db.cursor() try: ids = cardId.split('-') cardId = ids[1] privateToken = ids[2] sql = "UPDATE `wp_card` SET `is_private` = '0', `private_token` = '' WHERE `id` = '%s' AND `private_token` = '%s'" % ( cardId, privateToken) LogClient.info(sql) cur.execute(sql) db.commit() except: LogClient.error(sys.exc_info()) finally: cur.close() LogClient.info('MAKE PUBLIC CARD SUCCEED') return {"status": "succeed"}
def addCardHistory(cardId, cardJsonStr): db = MysqlConn().getMysqlConn('wp') cur = db.cursor() now = time.time() try: sql = "INSERT INTO `wp_card_history` (`card_id`, `data`, `ctime`) VALUES ('%s', '%s', '%s')" % ( cardId, cardJsonStr, now) LogClient.info(sql) cur.execute(sql) db.commit() except: LogClient.error(sys.exc_info()) finally: cur.close() LogClient.info('INSERT CARD HISTORY SUCCEED')
def deleteCard(cardId): if cardId is None: return BuildClient.EMPTY_INPUT LogClient.info('DELETE CARD') db = MysqlConn().getMysqlConn('wp') cur = db.cursor() try: if cardId[0] == 'p': ids = cardId.split('-') cardId = ids[1] privateToken = ids[2] sql = "UPDATE `wp_card` SET `is_delete` = '1' WHERE `id` = '%s' AND `private_token` = '%s'" % ( cardId, privateToken) else: sql = "UPDATE `wp_card` SET `is_delete` = '1' WHERE `id` = '%s' AND `is_private` = '0'" % cardId LogClient.info(sql) cur.execute(sql) db.commit() except: LogClient.error(sys.exc_info()) finally: cur.close() LogClient.info('DELETE CARD SUCCEED') return {"status": "succeed"}
def __autoClear(self): LogClient.autoClear()
def addCard(creator, imgPath, cardJsonStr, isPrivate=False): if (creator is None) | (imgPath is None) | (cardJsonStr is None): return BuildClient.EMPTY_INPUT try: cardJsonObj = json.loads(cardJsonStr) except: return BuildClient.NOT_JSON_FORMAT LogClient.info('INSERT CARD') LogClient.info(creator) LogClient.info(cardJsonObj) db = MysqlConn().getMysqlConn('wp') cur = db.cursor() description = '%s - %s' % (cardJsonObj['introduction'], cardJsonObj['name']) private = isPrivate and 1 or 0 privateToken = isPrivate and randomStr(10) or '' lastId = 0 now = time.time() try: sql = "INSERT INTO `wp_card` (`creator`, `data`, `description`, `img_path`, `is_private`, `private_token`, `ctime`, `last_update`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')" % ( creator, cardJsonStr, description, imgPath, private, privateToken, now, now) LogClient.info(sql) cur.execute(sql) db.commit() lastId = cur.lastrowid except: LogClient.error(sys.exc_info()) finally: cur.close() BuildClient.addCardHistory(lastId, cardJsonStr) LogClient.info('INSERT CARD SUCCEED') return { "status": "succeed", "id": isPrivate and ('p-%s-%s' % (lastId, privateToken)) or lastId }
def __test(self): from common.dao import MemcacheConn mc = MemcacheConn() # mc.getMcConn('wp').set('test', 'helloworld!') LogClient.info(mc.getMcConn('wp').get('test'))