forked from miguelgrinberg/microblog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bfea690
commit 2e38845
Showing
16 changed files
with
224 additions
and
15 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
web: flask db upgrade; flask translate compile; gunicorn microblog:app | ||
worker: rq worker microblog-tasks |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import json | ||
import sys | ||
import time | ||
from flask import render_template | ||
from rq import get_current_job | ||
from app import create_app, db | ||
from app.models import User, Post, Task | ||
from app.email import send_email | ||
|
||
app = create_app() | ||
app.app_context().push() | ||
|
||
|
||
def _set_task_progress(progress): | ||
job = get_current_job() | ||
if job: | ||
job.meta['progress'] = progress | ||
job.save_meta() | ||
task = Task.query.get(job.get_id()) | ||
task.user.add_notification('task_progress', {'task_id': job.get_id(), | ||
'progress': progress}) | ||
if progress >= 100: | ||
task.complete = True | ||
db.session.commit() | ||
|
||
|
||
def export_posts(user_id): | ||
try: | ||
user = User.query.get(user_id) | ||
_set_task_progress(0) | ||
data = [] | ||
i = 0 | ||
total_posts = user.posts.count() | ||
for post in user.posts.order_by(Post.timestamp.asc()): | ||
data.append({'body': post.body, | ||
'timestamp': post.timestamp.isoformat() + 'Z'}) | ||
time.sleep(5) | ||
i += 1 | ||
_set_task_progress(100 * i // total_posts) | ||
|
||
send_email('[Microblog] Your blog posts', | ||
sender=app.config['ADMINS'][0], recipients=[user.email], | ||
text_body=render_template('email/export_posts.txt', user=user), | ||
html_body=render_template('email/export_posts.html', | ||
user=user), | ||
attachments=[('posts.json', 'application/json', | ||
json.dumps({'posts': data}, indent=4))], | ||
sync=True) | ||
except: | ||
_set_task_progress(100) | ||
app.logger.error('Unhandled exception', exc_info=sys.exc_info()) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<p>Dear {{ user.username }},</p> | ||
<p>Please find attached the archive of your posts that you requested.</p> | ||
<p>Sincerely,</p> | ||
<p>The Microblog Team</p> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Dear {{ user.username }}, | ||
|
||
Please find attached the archive of your posts that you requested. | ||
|
||
Sincerely, | ||
|
||
The Microblog Team |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[program:microblog-tasks] | ||
command=/home/ubuntu/microblog/venv/bin/rq worker microblog-tasks | ||
numprocs=1 | ||
directory=/home/ubuntu/microblog | ||
user=ubuntu | ||
autostart=true | ||
autorestart=true | ||
stopasgroup=true | ||
killasgroup=true |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"""tasks | ||
Revision ID: c81bac34faab | ||
Revises: f7ac3d27bb1d | ||
Create Date: 2017-11-23 10:56:49.599779 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'c81bac34faab' | ||
down_revision = 'f7ac3d27bb1d' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('task', | ||
sa.Column('id', sa.String(length=36), nullable=False), | ||
sa.Column('name', sa.String(length=128), nullable=True), | ||
sa.Column('description', sa.String(length=128), nullable=True), | ||
sa.Column('user_id', sa.Integer(), nullable=True), | ||
sa.Column('complete', sa.Boolean(), nullable=True), | ||
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ), | ||
sa.PrimaryKeyConstraint('id') | ||
) | ||
op.create_index(op.f('ix_task_name'), 'task', ['name'], unique=False) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index(op.f('ix_task_name'), table_name='task') | ||
op.drop_table('task') | ||
# ### end Alembic commands ### |
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