Skip to content

Commit

Permalink
sorry for delay, user post added, post edit added
Browse files Browse the repository at this point in the history
  • Loading branch information
lesliebinbin committed Aug 27, 2018
1 parent bb44e28 commit 9f264a3
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
from flask_sqlalchemy import SQLAlchemy
from config import config
from flask_login import LoginManager
from flask_pagedown import PageDown

bootstrap = Bootstrap()
mail = Mail()
moment = Moment()
db = SQLAlchemy()
login_manager = LoginManager()
pagedown = PageDown()
login_manager.login_view = "auth.login"


Expand All @@ -23,6 +25,7 @@ def create_app(config_name):
moment.init_app(app)
db.init_app(app)
login_manager.init_app(app)
pagedown.init_app(app)
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
from .auth import auth as auth_blueprint
Expand Down
Binary file modified app/main/__pycache__/forms.cpython-36.pyc
Binary file not shown.
Binary file modified app/main/__pycache__/views.cpython-36.pyc
Binary file not shown.
3 changes: 2 additions & 1 deletion app/main/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from wtforms import StringField, SubmitField, TextAreaField, SelectField, BooleanField
from wtforms.validators import DataRequired, Length, Email, Regexp, ValidationError
from ..models import Role, User
from flask_pagedown.fields import PageDownField


class NameForm(FlaskForm):
Expand Down Expand Up @@ -58,5 +59,5 @@ def validate_username(self, field):


class PostForm(FlaskForm):
body = TextAreaField("What's on your mind?", validators=[DataRequired()])
body = PageDownField("What's on your mind?", validators=[DataRequired()])
submit = SubmitField("Submit")
23 changes: 23 additions & 0 deletions app/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,26 @@ def edit_profile_admin(id):
form.location.data = user.location
form.about_me.data = user.about_me
return render_template('edit_profile.html', form=form, user=user)


@main.route('/post/<int:id>')
def post(id):
post = Post.query.get_or_404(id)
return render_template('post.html', posts=[post])


@main.route('/edit/<int:id>', methods=['GET', 'POST'])
@login_required
def edit(id):
post = Post.query.get_or_404(id)
if current_user != post.author and not current_user.can(Permission.ADMIN):
abort(403)
form = PostForm()
if form.validate_on_submit():
post.body = form.body.data
db.session.add(post)
db.session.commit()
flash('The post has been updated.')
return redirect(url_for('.post', id=post.id))
form.body.data = post.body
return render_template('edit_post.html', form=form)
17 changes: 17 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from datetime import datetime
import hashlib
from markdown import markdown
import bleach


class Permission:
Expand Down Expand Up @@ -204,7 +206,22 @@ class Post(db.Model):
body = db.Column(db.Text)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
body_html = db.Column(db.Text)

@staticmethod
def on_changed_body(target, value, oldvalue, initiator):
allowed_tags = [
'a', 'abbr', 'acronym', 'b', 'blockquote', 'code', 'em', 'i', 'li',
'ol', 'pre', 'strong', 'ul', 'h1', 'h2', 'h3', 'p'
]
target.body_html = bleach.linkify(
bleach.clean(
markdown(value, output_format='html'),
tags=allowed_tags,
strip=True))


db.event.listen(Post.body, 'set', Post.on_changed_body)

login_manager.anonymous_user = AnonymousUser

Expand Down
20 changes: 19 additions & 1 deletion app/templates/_posts.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,25 @@
</a>
</div>
<div class="post-body">
{{post.body}}
{% if post.body_html %}
{{post.body_html | safe}}
{% else %}
{{post.body}}
{% endif %}
</div>
<div class="post-footer">
<a href="{{url_for('.post', id=post.id)}}">
<span class="label label-default">Permalink</span>
</a>
{% if current_user == post.author %}
<a href="{{url_for('.edit', id=post.id)}}">
<span class="label label-primary">Edit</span>
</a>
{% elif current_user.is_administrator()%}
<a href="{{url_for('.edit', id=post.id)}}">
<span class="label label-danger">Edit [Admin]</span>
</a>
{% endif %}
</div>
</div>
</li>
Expand Down
17 changes: 17 additions & 0 deletions app/templates/edit_post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf%}
{% block title %}
Flasky - Edit Post
{% endblock title %}
{% block page_content %}
<div class="page-header">
<h1>Edit Post</h1>
</div>
<div>
{{wtf.quick_form(form)}}
</div>
{% endblock page_content %}
{% block scripts %}
{{super()}}
{{pagedown.include_pagedown()}}
{% endblock scripts %}
6 changes: 5 additions & 1 deletion app/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ <h1>Hello, {% if current_user.is_authenticated %}{{ current_user.username }}{% e
{{ macros.pagination_widget(pagination, '.index') }}
</div>
{% endif %}
{% endblock %}
{% endblock %}
{% block scripts %}
{{super()}}
{{pagedown.include_pagedown()}}
{% endblock scripts %}
7 changes: 7 additions & 0 deletions app/templates/post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends "base.html" %}
{% block title %}
Flasky - Post
{% endblock title %}
{% block page_content %}
{% include "_posts.html" %}
{% endblock page_content %}
28 changes: 28 additions & 0 deletions migrations/versions/6f248965421e_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""empty message
Revision ID: 6f248965421e
Revises: 3b2834457dae
Create Date: 2018-08-27 22:33:47.612213
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '6f248965421e'
down_revision = '3b2834457dae'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('posts', sa.Column('body_html', sa.Text(), nullable=True))
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('posts', 'body_html')
# ### end Alembic commands ###

0 comments on commit 9f264a3

Please sign in to comment.