Skip to content

Commit

Permalink
1.update model.py;
Browse files Browse the repository at this point in the history
2.add form.py;
3.add setup.py
4.add manager.py
  • Loading branch information
sixu05202004 committed Apr 3, 2013
1 parent dede0f8 commit 5bcdeaf
Show file tree
Hide file tree
Showing 74 changed files with 12,348 additions and 10,404 deletions.
35 changes: 12 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
flaskblog
=========

person blog powered by flask

修改配置:modify the config.py

创建数据库和表:create table: modify model.py

运行:run myapp:python myapp.py

Admin
==========
http://127.0.0.1:8080/login


Blog以及源码解析:
==========
http://www.pythonpub.com
http://www.pythonpub.com/article/1474
http://www.pythonpub.com/article/1475
待添加:
http://www.pythonpub.com/article/1476
Intro
This is a person blog, powered by flask.

Steps:

1. python setup.py install
install all extensions:for example:flask,flask-wtf and so on

2. python manager.py createall
create all tables in mysql, but MUST modify config.py:SQLALCHEMY_DATABASE_URI

3. python manager.py runserver and Visit 127.0.0.1:5000/login, put your posts.
23 changes: 19 additions & 4 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
# -*- coding: utf-8 -*-

# configuration page num
"""
config.py
~~~~~~~~~~~
basic configuration
:copyright: (c) 2013.
:license: BSD, see LICENSE for more details.
"""


DEBUG = True

# configuration page num
PER_PAGE = 10

# configuration mysql

SQLALCHEMY_DATABASE_URI = "mysql://%s:%s@%s/%s"%('admin','admin','127.0.0.1','test')
SQLALCHEMY_DATABASE_URI = "mysql://%s:%s@%s/%s" % ('root', 'root', '127.0.0.1', 'test')

SECRET_KEY = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
USERNAME = 'admin'
PASSWORD = 'admin'

UPLOAD_FOLDER = '/static/upload/'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

RECAPTCHA_PUBLIC_KEY = '6LeJTt8SAAAAACuSjRrt3a2jgGX-xQBREEAXw9Rs'
RECAPTCHA_PRIVATE_KEY = '6LeJTt8SAAAAACjz_N65vlf9yuscktZZjOIEISFA'
16 changes: 0 additions & 16 deletions date.py

This file was deleted.

24 changes: 24 additions & 0 deletions form.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
"""
form.py
~~~~~~~~~~~
comment form
:copyright: (c) 2013.
:license: BSD, see LICENSE for more details.
"""

from flask.ext.wtf import Form, SubmitField, TextField, required, length, TextAreaField, email, RecaptchaField, HiddenField


class CommentForm(Form):
author_name = TextField(u'Name', validators=[required(message=u"Need an name"), length(max=50)])
author_email = TextField(u"Email", validators=[
required(message=u"Need an email address"),
email(message=u"Need a valid email address")])
author_url = TextField(u"Url")
content = TextAreaField(u"Content")
post_id = TextField()
recaptcha = RecaptchaField(u"Copy the words appearing below")
submit = SubmitField(u"Save")
33 changes: 33 additions & 0 deletions manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
"""
manager.py
~~~~~~~~~~~
flask manager script
:copyright: (c) 2013.
:license: BSD, see LICENSE for more details.
"""
from flask.ext.script import Server, Manager, prompt_bool
from myapp import app
from model import db

manager = Manager(app)
manager.add_command("runserver", Server('0.0.0.0', port=5000))


@manager.command
def createall():
"Creates database tables"
db.create_all()


@manager.command
def dropall():
"Drops all database tables"

if prompt_bool("Are you sure ? You will lose all your data !"):
db.drop_all()

if __name__ == "__main__":
manager.run()
85 changes: 13 additions & 72 deletions model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from flask import url_for,g
from flask import url_for
from flask.ext.sqlalchemy import SQLAlchemy, BaseQuery
from werkzeug import cached_property
from datetime import datetime
Expand All @@ -23,8 +23,8 @@ class Category(db.Model):
id = db.Column(db.Integer, primary_key=True)
category_name = db.Column(db.String(200), unique=True)

def __init__(self, category_name):
self.category_name = category_name
def __init__(self, *args, **kwargs):
super(Category, self).__init__(*args, **kwargs)

def __repr__(self):
return '<category name %r>' % self.category_name
Expand Down Expand Up @@ -55,8 +55,8 @@ class Tag(db.Model):

#post = db.relationship('Post', secondary=article_tags)

def __init__(self, name):
self.name = name
def __init__(self, *args, **kwargs):
super(Tag, self).__init__(*args, **kwargs)

def __repr__(self):
return '<tag name %r>' % self.name
Expand All @@ -80,7 +80,7 @@ def hottest(self):
return self.order_by(Post.comment_count.desc(), Post.view_num.desc())

def newpost(self):
return self.order_by(Post.post_create_time.desc())
return self.order_by(Post.post_modified_time.desc())

def search(self, keywords):
criteria = []
Expand Down Expand Up @@ -124,24 +124,9 @@ class Post(db.Model):
tags = db.relationship('Tag', secondary=article_tags,
backref=db.backref('posts', lazy='dynamic'))
tags_name = db.Column(db.Text)
def __init__(self, tags, post_content, post_title, category_id, post_name,
tags_name, post_create_time=None, view_num=0, comment_count=0, status=1,
author_id=1, post_modified_time=None):
self.post_content = post_content
self.post_title = post_title
self.category_id = category_id
self.post_name = post_name
if post_create_time is None:
self.post_create_time = datetime.utcnow()
self.view_num = view_num
self.comment_count = comment_count
self.status = status
self.author_id = author_id
if post_modified_time is None:
self.post_modified_time = post_modified_time
#self.categorys = category
self.tags = tags
self.tags_name = tags_name

def __init__(self, *args, **kwargs):
super(Post, self).__init__(*args, **kwargs)

def __repr__(self):
return '<post %r>' % self.post_title
Expand Down Expand Up @@ -177,64 +162,20 @@ class Comment(db.Model):
id = db.Column(db.Integer, primary_key=True)
post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
posts = db.relationship('Post', backref=db.backref('comments', lazy='dynamic'))
author = db.Column(db.String(50))
author_name = db.Column(db.String(50))
author_email = db.Column(db.String(100))
author_url = db.Column(db.String(1024))
author_ip = db.Column(db.String(20))
comment_create_time = db.Column(db.DateTime, default=datetime.utcnow)
content = db.Column(db.Text)
isvisible = db.Column(db.Integer, default=1)

def __init__(self, postid, author, author_email, author_url, author_ip,
content, isvisible=1, comment_create_time=None):
self.post_id = postid
self.author = author
self.author_email = author_email
self.author_url = author_url
self.author_ip = author_ip
self.content = content
self.isvisible = isvisible
if comment_create_time is None:
self.comment_create_time = datetime.utcnow()
def __init__(self, *args, **kwargs):
super(Comment, self).__init__(*args, **kwargs)

def __repr__(self):
return '<comment %r>' % self.content


def pageby(obj, pageid, per_page, orderby):
return obj.order_by(orderby).paginate(pageid, per_page)

if __name__ == '__main__':

#print Post.query.search('7998797').order_by(Post.post_create_time.desc()).paginate(1, 10).items
#print len(Tag.query.getall())
#print p.total
#db.create_ll()
#a=Category("test")
#db.session.add(a)
#print app.config['PER_PAGE']
#print Tag.query.gettag_id(2).posts.all()
#print Post.query.getpost_id(54).comments.all()
#print Category.query.getcategory_id(3).posts.order_by(Post.post_create_time.desc()).paginate(1, 10).items


#p=Post.query.getpost_id(110)
#print Post.query.search_tag(Tag.query.gettag_id(1).name).all()
print Tag.query.gettag_id(1).name
"""
a=Category("test2")
b1=Tag('3')
b2=Tag('4')
c=Post(a,[b1,b2],'000','0','a')
db.session.add(a)
db.session.add(b1)
db.session.add(b2)
db.session.add(c)
db.session.commit()
"""
#c = Comment(1458,'dan','[email protected]','www.baidu.com','10.0.11.111','不错')
#db.session.add(c)
#db.session.commit()

#print db.engine.execute('update post set comment_count = comment_count + 1 where id = 1458')
#print c.tags[0].name
#print db
27 changes: 27 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-

"""
setup.py
~~~~~~~~~~~
set up extensions
:copyright: (c) 2013.
:license: BSD, see LICENSE for more details.
"""

from setuptools import setup

setup(
install_requires=[
'Flask',
'Flask-Cache',
'Flask-SQLAlchemy',
'Flask-WTF',
'Flask-Testing',
'Flask-Script',
'Flask-Uploads',
'sqlalchemy'
]

)
Loading

0 comments on commit 5bcdeaf

Please sign in to comment.