Skip to content
This repository has been archived by the owner on May 5, 2020. It is now read-only.

Commit

Permalink
First step
Browse files Browse the repository at this point in the history
Yes, almost can be use
I am so lazy ...
  • Loading branch information
SIvaCoHan committed Nov 1, 2014
1 parent f2e09d1 commit dfbec0b
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
src/test.db

# C extensions
*.so
Expand Down
12 changes: 12 additions & 0 deletions config/requirement.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Flask==0.10.1
Flask-Login==0.2.11
Flask-SQLAlchemy==2.0
Jinja2==2.7.3
MarkupSafe==0.23
SQLAlchemy==0.9.8
Werkzeug==0.9.6
argparse==1.2.1
docutils==0.12
itsdangerous==0.24
psycopg2==2.5.4
wsgiref==0.1.2
20 changes: 20 additions & 0 deletions init_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from src import db
from src import models

db.drop_all()
db.create_all()
u = models.User('lee','123456')
a = '''
========================================
Every beauty start with ugly
========================================
Zero
----
Here, we start
'''
b = models.Article(a)
db.session.add(b)
db.session.add(u)
db.session.commit()
4 changes: 4 additions & 0 deletions run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from src import app

if __name__ == '__main__':
app.run(debug=True)
16 changes: 16 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#! /usr/bin/env python2.7
# -*- coding: UTF-8 -*-

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.login import LoginManager

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
app.config['SECRET_KEY'] = 'guess'
db = SQLAlchemy(app)
lm = LoginManager()
lm.login_view='/'
lm.init_app(app)

from src import views, models
48 changes: 48 additions & 0 deletions src/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#! /usr/bin/env python2.7
# -*- coding: UTF-8 -*-

from datetime import datetime
from docutils.core import publish_parts
from flask.ext.sqlalchemy import SQLAlchemy
from src import db


class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
passwd = db.Column(db.String(120), unique=True)

def __init__(self, username, passwd):
self.username = username
self.passwd = passwd

def is_authenticated(self):
return True

def is_active(self):
return True

def is_anonymous(self):
return False

def get_id(self):
return unicode(self.id)

def __repr__(self):
return '<User %r>' % self.username


class Article(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), unique=True)
article = db.Column(db.Text(), unique=True)
rst = db.Column(db.Text(), unique=True)
create_time = db.Column(db.DateTime())

def __init__(self, rst, create_time=None):
self.rst = rst
_article = publish_parts(rst, writer_name="html")
self.name = _article['title']
self.article = _article['whole']
if create_time is None:
self.create_time = datetime.utcnow()
60 changes: 60 additions & 0 deletions src/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#! /usr/bin/env python2.7
# -*- coding: UTF-8 -*-

from flask import request, redirect
from src import app, db, models, lm
from flask.ext.login import login_user, logout_user, current_user, login_required

@lm.user_loader
def load_user(id):
return models.User.query.get(int(id))

@app.route('/')
def index():
article_list = models.Article.query.order_by(models.Article.create_time.desc()).all()
s = '<ul>'
for article in article_list:
s += '<li><a href="/article/'+str(article.id)+'">'+article.name+'</a></li>'
s += '</ul>'
return s

@app.route('/article/<int:article_id>')
def article(article_id):
article = models.Article.query.filter_by(id=article_id).one()
return article.article

@app.route('/raw_article/<int:article_id>')
def get_rst(article_id):
article = models.Article.query.filter_by(id=article_id).one()
return article.rst

@app.route('/login', methods=['POST', 'GET'])
def login():
if request.method == 'GET':
return '<form action="#" method="post"><input type="text" name="username" placeholder="username"><input type="password" name="passwd" placeholder="password"><input type="submit"/></form>'
if request.method == 'POST':
username = request.form.get('username')
passwd = request.form.get('passwd')
u = models.User.query.filter_by(username=username).filter_by(passwd=passwd).first()
if u is None:
return redirect('/')
login_user(u)
return redirect('/new')

@app.route('/logout', methods=['POST', 'GET'])
@login_required
def logout():
logout_user()
return redirect('/')

@app.route('/new', methods=['POST', 'GET'])
@login_required
def new_article():
if request.method == 'GET':
return '<form action="#" method="post"><textarea name="p"></textarea><input type="submit"/></form>'
if request.method == 'POST':
p = request.form.get('p')
article = models.Article(p)
db.session.add(article)
db.session.commit()
return redirect('/')

0 comments on commit dfbec0b

Please sign in to comment.