Created
August 8, 2010 18:20
-
-
Save dahlia/514367 to your computer and use it in GitHub Desktop.
Questions
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
questions.pyc | |
questions.sqlite3 |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>질문들</title> | |
<style type="text/css"> | |
body { background-color: #333; color: white; } | |
fieldset { border: none; } | |
a { color: white; } | |
</style> | |
</head> | |
<body> | |
<h1>질문들</h1> | |
<form action="{{ url_for('ask') }}" method="post"> | |
<fieldset> | |
<legend>새 질문 추가</legend> | |
<label>질문 <input type="text" name="question" /></label> | |
<button type="submit">추가</button> | |
</fieldset> | |
</form> | |
<table> | |
<thead> | |
<tr> | |
<th>질문</th> | |
<th colspan="3">점수</th> | |
</tr> | |
</thead> | |
<tbody> | |
{% for question in questions %} | |
<tr> | |
<th>{{ question.body }}</th> | |
<td>{{ question.score }}</td> | |
<td><form action="{{ url_for('upvote', id=question.id) }}" | |
method="post"> | |
<input type="hidden" name="mode" value="upvote" /> | |
<button type="submit">나도궁금</button> | |
</form></td> | |
<td><form action="{{ url_for('downvote', id=question.id) }}" | |
method="post"> | |
<input type="hidden" name="mode" value="downvote" /> | |
<button type="submit">안궁금</button> | |
</form></td> | |
</tr> | |
{% endfor %} | |
</tbody> | |
</table> | |
<p><a href="http://gist.github.com/514367">소스 코드</a></p> | |
</body> | |
</html> |
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 collections | |
import sqlite3 | |
import flask | |
class Question(collections.namedtuple("Question", "id,body,score")): | |
"""Questions.""" | |
DATABASE_PATH = "questions.sqlite3" | |
database = None | |
@classmethod | |
def source(cls): | |
"""Opens the database connection.""" | |
if not cls.database: | |
cls.database = sqlite3.connect(cls.DATABASE_PATH) | |
return cls.database | |
@classmethod | |
def initialize(cls): | |
"""Initializes a database schema.""" | |
db = cls.source() | |
c = db.cursor() | |
c.execute("""CREATE TABLE questions ( | |
id integer PRIMARY KEY AUTOINCREMENT, | |
body text NOT NULL | |
)""") | |
c.execute("""CREATE TABLE votes ( | |
question_id int, | |
score integer NOT NULL | |
)""") | |
db.commit() | |
c.close() | |
@classmethod | |
def questions(cls, id=None): | |
"""Reads questions.""" | |
db = cls.source() | |
c = db.cursor() | |
cond = "id = {0}".format(id) if id else "1 = 1" | |
c.execute("""SELECT id, body, | |
(SELECT sum(score) FROM votes | |
WHERE question_id = id) AS score | |
FROM questions | |
WHERE {condition} | |
ORDER BY id""".format(condition=cond)) | |
for id, body, score in c: | |
if id is not None: | |
yield cls(id, body, score or 0) | |
c.close() | |
def vote(self, score): | |
"""Votes the question.""" | |
db = self.source() | |
c = db.cursor() | |
c.execute("""INSERT INTO votes (question_id, score) VALUES (?, ?)""", | |
(self.id, int(score))) | |
db.commit() | |
c.close() | |
def upvote(self): | |
"""Upvotes the question.""" | |
self.vote(1) | |
def downvote(self): | |
"""Upvotes the question.""" | |
self.vote(-1) | |
def put(self): | |
"""Puts the question.""" | |
db = self.source() | |
c = db.cursor() | |
c.execute("""INSERT INTO questions (body) VALUES (?)""", (self.body,)) | |
db.commit() | |
c.close() | |
app = flask.Flask(__name__) | |
@app.route("/", methods=["GET"]) | |
def questions(): | |
questions = sorted(Question.questions(), key=lambda q: -q.score) | |
return flask.render_template("list.html", questions=questions) | |
@app.route("/", methods=["POST"]) | |
def ask(): | |
if flask.request.form.get("question", "").strip(): | |
question = Question(id=None, | |
body=flask.request.form["question"], | |
score=0) | |
question.put() | |
return flask.redirect(flask.url_for("questions")) | |
@app.route("/<int:id>/upvotes", methods=["POST"]) | |
def upvote(id): | |
question, = Question.questions(id=id) | |
question.upvote() | |
return flask.redirect(flask.url_for("questions")) | |
@app.route("/<int:id>/downvotes", methods=["POST"]) | |
def downvote(id): | |
question, = Question.questions(id=id) | |
question.downvote() | |
return flask.redirect(flask.url_for("questions")) | |
if __name__ == "__main__": | |
app.run(debug=True, host="0.0.0.0", port=3333) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment