<Python, flask> 13b

みげるっちのFlask Web Development を勉強中。

github.com

手っ取り早く、だいたいブログが出来上がった、
13bからスタートするために、いくつかメモ。

その1 git

チェックアウトする。

>git clone https://github.com/migelgrinberg/flasky
>cd flasky
>git checkout 13b

その2 データベース作る

>python manage.py shell
In  [1]: db.create_all()

その3 ユーザロールを登録する

In  [2]: Role.insert_roles()

その4 ちとバグを直す。

どうも、Flask-loginUserMixin
is_authenticated()is_anonymous()()を忘れてる。
(まあ、何故これが関数なのか、、と思うんだけど、、)

下記ファイルで当該箇所をちくたく直す。

flasky/app/auth/views.py
flasky/templates/base.html
flasky/templates/index.html
flasky/templates/user.html
flasky/app/main/views.py

16/05/06
ぶぶー。 Flask-Login==0.3.0以降の場合は、こいつらは、propertyとのこと。
なので、0.3.0インストールすれば、直す必要なし。

その5 SMTPサーバオンする

デバック用のサーバをオンする。

python -m smtpd -n -c DebuggingServer localhost:1025

その6 config.py を少しいじる

ローカルホスト 1025のSMTPにアクセスするために、ちといじる。

 5 class Config:
 6   
 8     # MAIN_SERVER = 'smtp.googlemail.com'   # comment out
 9     MAIL_PORT = 1025                        # 1025
10     # MAIL_USE_TLS = True                   # comment out

その7 走らせる。

以上でOKのはず。

>python manage.py runserver -p 8000

その他、、、

その8 アドミン追加

ユーザロールを書き換える。
登録済みのユーザをまずはゲット。

>python manage.py shell
In  [1]: aaa = User.query.filter_by(username='aaa').first()

In  [2]: aaa.role

In  [3]: Role.query.all()
Out [3]: [<Role 'Moderator'>, <Role 'Administrator'>, <Role 'User'>]

In  [4]: aaa.role = Role.query.all()[1]

In  [5]: aaa.role
Out [5]: <Role 'Administrator'>

In  [6]: db.session.add(aaa)

In  [7]: db.session.commit()

いちおう、sqlite3で確認する。

>sqlite3 data-dev.sqlite
SQLite version 3.9.2 2015-11-02 22:21:45
Enter ".help" for usage hints.
sqlite>.explain on
sqlite>.tables
comments  follows  posts  roles  users
sqlite>select * from users;
id    email         user  role
----  ------------  ----  ----
1     aaa@hage.com  aaa   2

オッケー! 書き換わってる。ナイス。

その9 Python/シンタックスハイライトを追加 16/05/10

みげるっちのコードを少し直して、
Syntax highlightを追加する。

markdownbleachのところが修正箇所。(286-292)

260 class Post(db.Model):
261     __tablename__ = 'posts'
262     id = db.Column(db.Integer, primary_key=True)
263     body = db.Column(db.Text)
264     body_html = db.Column(db.Text)
265     timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
266     author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
267     comments = db.relationship('Comment', backref='post', lazy='dynamic')
268
269     @staticmethod
270     def generate_fake(count=100):
271         from random import seed, randint
272         import forgery_py
273
274         seed()
275         user_count = User.query.count()
276         for i in range(count):
277             u = User.query.offset(randint(0, user_count - 1)).first()
278             p = Post(body=forgery_py.lorem_ipsum.sentences(randint(1, 5)),
279                      timestamp=forgery_py.date.date(True),
280                      author=u)
281             db.session.add(p)
282             db.session.commit()
283
284     @staticmethod
285     def on_changed_body(target, value, oldvalue, initiator):
286         allowed_tags = ['a', 'abbr', 'acronym', 'b', 'blockquote', 'code',
287                         'em', 'i', 'li', 'ol', 'pre', 'strong', 'ul',
288                         'h1', 'h2', 'h3', 'p', 'span', 'div']
289         attrs = {'div': ['class'], 'span': ['class']}
290         target.body_html = bleach.linkify(bleach.clean(
291             markdown(value, output_format='html', extensions=['codehilite', 'extra']),
292             tags=allowed_tags, attributes=attrs, strip=True))