Skip to content

Commit

Permalink
- Simplified settings
Browse files Browse the repository at this point in the history
- Better naming
- General code readability improvements
Note: Untested
  • Loading branch information
shon committed Dec 2, 2019
1 parent 2b7a20f commit ebec5dd
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 44 deletions.
22 changes: 5 additions & 17 deletions app/libs/pending_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,17 @@ def update(id, actor: user_id, **mod_data):


def approve(id, actor: user_id):
pending_comment = get(id)
comment = get(id)
delete(id)
commentactionloglib.create(
comment=id,
action=comment_actions.approved.value,
actor=actor
)
if settings.EMAIL_NOTIFICATION:
commenter = memberlib.get(pending_comment['commenter_id'])

comment = pending_comment['content']
display_comment = comment[:215] + '...' if len(comment) > 215 else comment

email_info = dict(
mail_subject='Comment Approved',
template_name='comment_approved',
template_data=dict(
comment=display_comment,
comment_id=id
)
)
signals.send_notification.send((commenter['email'],), **email_info)
return commentlib.create(**pending_comment)
commenter = memberlib.get(comment['commenter_id'])
ret = commentlib.create(**comment)
signals.comment_approved.send('approve', comment=comment, commenter=commenter)
return ret
approve.groups_required = [groups.moderator.value]


Expand Down
5 changes: 5 additions & 0 deletions app/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from blinker import signal


comment_approved = signal('comment_approved')
comment_featured = signal('comment_featured')
41 changes: 22 additions & 19 deletions app/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import app.signals as signals
import settings

RETRY_DELAY = 60 # every 30 sec
RETRY_DELAY = 60 # every 60 sec
RETRIES = (60 * 60) / RETRY_DELAY # 1 hour of retries

queue = Celery(
Expand All @@ -17,25 +17,28 @@
logging.basicConfig(filename='logs/tasks.log', level=logging.DEBUG)


@queue.task(autoretry_for=(Exception,), max_retries=RETRIES, default_retry_delay=RETRY_DELAY)
def notify_commentor(email_id, **email_info):
html = jinja2.Environment(
loader=jinja2.FileSystemLoader(
settings.ARTICLE_EMAIL_TEMPLATE_DIR + '/')
).get_template(email_info['template_name'] + '.html'
).render(data=email_info['template_data'])
if settings.EMAIL_NOTIFICATIONS.ENABLED:

sender = settings.EMAIL_NOTIFICATION_SENDER
subject = settings.ARTICLE_EMAIL_SUBJECT_PREFIX + email_info['mail_subject']
template_env = jinja2.Environment(
loader=jinja2.FileSystemLoader(settings.EMAIL_NOTIFICATIONS.TEMPLATE_DIR))

try:
send_email(sender, recipient=email_id, subject=subject, html=html)
logging.info('{} mail sent to {}'.format(
email_info['template_name'], email_id))
except Exception as e:
logging.info(e)
def send_comment_notification(commenter_email, subject, template, comment):
html = template_env.get_template(template + '.html').render(comment=comment)

sender = settings.EMAIL_NOTIFICATIONS.SENDER
subject = settings.EMAIL_NOTIFICATIONS.PREFIX + email_info['mail_subject']

@signals.send_notification.connect
def on_send_notification(email_id, **email_info):
notify_commentor.apply_async(email_id, email_info)
send_email(sender, recipient=commenter_email, subject=subject, html=html)
logging.info('{} mail sent to {}'.format( email_info['template_name'], email_id))


@queue.task(autoretry_for=(Exception,), max_retries=5, default_retry_delay=RETRY_DELAY)
@signals.comment_approved.connect
@signals.comment_featured.connect
def on_comment_action(action, comment, commenter):
email = commenter['email']
comment_preview = comment[:215] + '...' if len(comment) > 215 else comment
subject = f'Comment {action.title()}'
template = f'comment_{action}.html'
send_comment_notification(email, subject=subject, template=template,
comment=comment_preview)
4 changes: 2 additions & 2 deletions app/templates/email/scroll/comment_approved.html
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ <h1>Your comment has been approved<br>
<tr>
<td class="comment-block">
<h3>You wrote:</h3>
<p>{{data['comment']}}</p>
<p>{{comment}}</p>
<div class="share-links">
<a href="{{'https://www.facebook.com/sharer/sharer.php?u=https://scroll.in/article/944961/#comments:' + data['comment_id'] | string }}" target="_blank"><img alt="facebook" src="https://s01.sgp1.cdn.digitaloceanspaces.com/images/facebook_share.png"></a> <a href="{{'https://twitter.com/share?text=' + data['comment'] + '&amp;url=https://scroll.in/article/944961/#comments:' + data['comment_id'] | string + '&amp;via=scroll_in'}}" target="_blank"><img alt="twitter" src="https://s01.sgp1.cdn.digitaloceanspaces.com/images/twitter_share_icon.png"></a>
</div>
Expand All @@ -393,4 +393,4 @@ <h3>You wrote:</h3>
</tr>
</table>
</body>
</html>
</html>
13 changes: 7 additions & 6 deletions settings/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
TASKQ_PORT = 6379
TASKQ_DB = 1

EMAIL_NOTIFICATION = False
class EMAIL_NOTIFICATIONS:

ENABLED = False
TEMPLATE_DIR = "app/templates"
SENDER = "[email protected]"
PREFIX = ""


# SMTP
MD_HOST = ''
MD_PORT = 587
MD_USERNAME = ''
MD_KEY = ''

EMAIL_NOTIFICATION_SENDER = "[email protected]"
TEMPLATE_DIR = "app/templates"
ARTICLE_EMAIL_TEMPLATE_DIR = TEMPLATE_DIR + ""
ARTICLE_EMAIL_SUBJECT_PREFIX = ""

0 comments on commit ebec5dd

Please sign in to comment.