def post(self): filename = unquote(self.request.query[7:]) if filename == "": return self.write(json.dump({"error": u"文件名错误"})) prefix = prefix_rand() cur = self.db.cursor() for i in range(6): cur.execute('SELECT count(0) as count FROM files WHERE filename = %s', ('%s/%s' % (prefix, filename),)) if fetchone(cur).count > 0: prefix = prefix_rand() if i >= 5: return self.write(json.dump({"error": u"文件名重复次数过多"})) else: break md5text = md5(self.request.body).hexdigest()#计算文件md5值 try: open('static/upload/%s' % md5text, 'wb').write(self.request.body)#保存到文件系统中。 cur.execute('INSERT INTO files(filename,filestore) VALUES(%s,%s)', ('%s/%s' % (prefix, filename), md5text)) except Exception, e: try: cur.execute('DELETE FROM files WHERE filestore=%s', (md5text,))#从数据库中删除错误的记录 os.unlink('static/upload/%s' % md5text)#从文件系统中删除错误的文件 except: self.db.rollback() return self.write(json.dump({'error': u'数据存取错误'}))
def get(self, month, page=1): page = int(page) if page <= 0: page = 1 pagesize = 10 start_date = datetime.strptime(month.encode('utf-8'), '%Y年%m月') if start_date.month < 12: stop_date = datetime(year=start_date.year, month=start_date.month + 1, day=1) else: stop_date = datetime(year=start_date.year + 1, month=1, day=1) cur = self.db.cursor() cur.execute('SELECT count(0) as count ' 'FROM articles WHERE posttime>=%s and posttime<%s', (start_date, stop_date)) totalrows = fetchone(cur).count self.d['month_name'] = month cur.execute('SELECT id,title,render,posttime,' 'ARRAY(' 'SELECT tag FROM link_tags_articles as lta WHERE lta.article_id = articles.id' ') as tags FROM articles WHERE posttime>=%s and posttime<%s ' 'ORDER BY posttime desc OFFSET %s LIMIT %s', (start_date, stop_date, (page - 1) * pagesize, pagesize)) self.d['articles'] = fetchall(cur) self.d = Pagination(self.d, page, pagesize, totalrows) self.render('month.html', **self.d)
def post(self): loginname = self.get_argument('loginname', '') # 用户名 loginpass = self.get_argument('loginpass', '') # 登陆密码 cur = self.db.cursor() cur.execute('SELECT password as password FROM users WHERE username = %s', (loginname,)) user = fetchone(cur) if user and user.password == loginpass: self.set_session('islogin', 'yes') self.redirect('/')
def get(self, filename): mimetype = mimetypes.guess_type(filename)[0] if not mimetype: mimetype = 'application/octet-stream' self.set_header('Content-Type', mimetype) cur = self.db.cursor() cur.execute('SELECT filestore FROM files WHERE filename = %s', (filename,)) filestore = fetchone(cur) if filestore: self.set_header('X-Accel-Redirect', os.path.join('/uploadfiles', filestore.filestore)) else: self.set_status(404)
def get(self, key): key = unquote(key) cur = self.db.cursor() try: cur.execute('SELECT * FROM files WHERE filename = %s', (key,)) f = fetchone(cur) try: os.unlink('static/upload/%s' % f.filestore) except: pass cur.execute('DELETE FROM files WHERE filename = %s', (key,)) self.db.commit() except Exception, e: print e
def get(self, id): id = int(id) cur = self.db.cursor() cur.execute( 'SELECT id,title,posttime,render,' 'ARRAY(' 'SELECT tag FROM link_tags_articles WHERE link_tags_articles.article_id = articles.id' ') as tags ' 'FROM articles WHERE id = %s' , (id,)) self.d['article'] = fetchone(cur) if not self.d['article']: return self.send_error(status_code=404) self.render('article.html', **self.d)
def get(self, id): id = int(id) cur = self.db.cursor() cur.execute('SELECT id, title, content, posttime, ' 'ARRAY(' 'SELECT tag FROM link_tags_articles WHERE link_tags_articles.article_id = articles.id' ') as tags ' 'FROM articles WHERE id = %s', (id,)) self.d['article'] = fetchone(cur) cur.execute('SELECT filename FROM files ' 'WHERE filename NOT IN (SELECT filename FROM link_files_articles)') self.d['tmpfile'] = fetchall(cur) self.d['tags_cloud'] = self.get_tags(sort='desc') # 添加文章所用到的按倒序排列的tags列表 self.render('modify.html', **self.d)
def get(self, id): id = int(id) # 转换成数字 cur = self.db.cursor() cur.execute('SELECT posttime FROM articles WHERE id = %s', (id,)) posttime = fetchone(cur).posttime cur.execute('UPDATE tags SET count = count - 1 ' 'WHERE tag in (SELECT tag FROM link_tags_articles WHERE article_id = %s)', (id,)) cur.execute('DELETE FROM link_tags_articles WHERE article_id = %s', (id,)) cur.execute('DELETE FROM tags WHERE count = 0') cur.execute('DELETE FROM link_files_articles WHERE article_id = %s', (id,)) cur.execute('DELETE FROM articles WHERE id = %s', (id,))#删除文章 date_add(self.db, posttime, add=-1) # 减去一个日期计数 search.delete_document(id) # 删除索引 search.flush() return self.write(u'ID:%s 的文章已删除' % id)
def get(self, key): key = unquote(key) cur = self.db.cursor() try: cur.execute('SELECT * FROM files WHERE filename = %s', (key,)) f = fetchone(cur) try: os.unlink('static/upload/%s' % f.filestore) except: pass cur.execute('DELETE FROM files WHERE filename = %s', (key,)) self.db.commit() except Exception, e: # 如果出错则回滚事务 self.db.rollback() logging.error(str(e))
def get(self, page=1): page = int(page) if page <= 0: page = 1 pagesize = 10 cur = self.db.cursor() cur.execute('SELECT count(0) as count FROM articles WHERE id != %s', (0,)) totalrows = fetchone(cur)['count'] cur.execute( 'SELECT id, title, posttime, render, ' 'ARRAY(' 'SELECT tag FROM link_tags_articles WHERE link_tags_articles.article_id = articles.id' ') as tags ' 'FROM articles WHERE id != %s ' 'ORDER BY posttime desc OFFSET %s LIMIT %s', (0, (page - 1) * pagesize, pagesize)) self.d['articles'] = fetchall(cur) self.d = Pagination(self.d, page, pagesize, totalrows) self.render('index.html', **self.d)
def get(self, tag, page=1): page = int(page) if page <= 0: page = 1 pagesize = 10 cur = self.db.cursor() cur.execute('SELECT count(0) as count ' 'FROM link_tags_articles LEFT JOIN articles ' 'ON link_tags_articles.article_id = articles.id ' 'WHERE link_tags_articles.tag=%s', (tag,)) totalrows = fetchone(cur).count self.d['tag_name'] = tag cur.execute( 'SELECT articles.id, articles.title, articles.posttime, articles.render, ' 'ARRAY(SELECT tag FROM link_tags_articles as lta WHERE lta.article_id = articles.id) as tags ' 'FROM link_tags_articles LEFT JOIN articles ' 'ON link_tags_articles.article_id = articles.id ' 'WHERE link_tags_articles.tag=%s ' 'ORDER BY posttime desc OFFSET %s LIMIT %s', (tag, (page - 1) * pagesize, pagesize)) self.d['articles'] = fetchall(cur) self.d = Pagination(self.d, page, pagesize, totalrows) self.render('tag.html', **self.d)