Created
December 23, 2010 14:50
-
-
Save lobodin/753070 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
from bottle import route, run, request | |
from datetime import datetime | |
import re | |
from cgi import escape | |
def articles_json(articles_tuple): | |
articles = list(({'date': escape(date), | |
'title': escape(title), | |
'content': escape(content)} | |
for date, title, content in articles_tuple)) | |
return {'articles': articles} | |
def format_date(d): | |
return '%s-%s-%s' % \ | |
re.compile('(\d{2})/(\d{2})/(\d{4})').match(d).group(3,1,2) | |
@route('/new', method='POST') | |
def new(): | |
con = sqlite3.connect('blog.sqlite3') | |
c = con.cursor() | |
date = format_date(request.POST.get('date')) | |
title = request.POST.get('title') | |
content = request.POST.get('content') | |
c.execute('INSERT INTO ARTICLE (DATE, TITLE, CONTENT) VALUES (?,?,?)', | |
(date, title, content)) | |
con.commit() | |
c.close() | |
@route('/all-articles') | |
def get_all(): | |
con = sqlite3.connect('blog.sqlite3') | |
c = con.cursor() | |
c.execute('SELECT DATE, TITLE, CONTENT FROM ARTICLE ORDER BY DATE DESC LIMIT 10') | |
articles_tuple = c.fetchall() | |
c.close() | |
return articles_json(articles_tuple) | |
@route('/articles-between-dates') | |
def get_range(): | |
con = sqlite3.connect('blog.sqlite3') | |
c = con.cursor() | |
from_date = format_date(request.GET.get('from')) | |
to_date = format_date(request.GET.get('to')) | |
c.execute('SELECT DATE, TITLE, CONTENT FROM ARTICLE WHERE DATE >= ? and DATE <= ? ORDER BY DATE DESC', | |
(from_date, to_date)) | |
articles_tuple = c.fetchall() | |
c.close | |
return articles_json(articles_tuple) | |
run(host='localhost', port=8080) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
И в 62 строке скобки пропущены.