Skip to content

Instantly share code, notes, and snippets.

@lobodin
Created December 23, 2010 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lobodin/753070 to your computer and use it in GitHub Desktop.
Save lobodin/753070 to your computer and use it in GitHub Desktop.
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)
@refaim
Copy link

refaim commented Dec 23, 2010

import time

def format_date(d):
    return time.strftime('%Y-%m-%d', time.strptime(d, '%m/%d/%Y'))

И в 62 строке скобки пропущены.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment