def on_auth_sign_up(user: auth.AbstractUser): # Set session notification router.session().add_success_message( lang.t('auth_ui@registration_form_success')) # Send a confirmation email to the user if auth.is_sign_up_confirmation_required(): msg = tpl.render( 'auth_ui@mail/{}/sign-up'.format(lang.get_current()), { 'user': user, 'confirm_url': router.rule_url('auth_ui@sign_up_confirm', {'code': user.confirmation_hash}) if not user.is_confirmed else None }) mail.Message(user.login, lang.t('auth_ui@confirm_registration'), msg).send() # Send a notification emails to admins if auth.is_sign_up_admins_notification_enabled(): for admin in auth.get_admin_users(): msg = tpl.render( 'auth_ui@mail/{}/sign-up-admin-notify'.format( lang.get_current()), { 'admin': admin, 'user': user, }) mail.Message(admin.login, lang.t('auth_ui@registration_admin_notify'), msg).send()
def _on_submit(self): try: user = auth.get_user(self.val('login')) if user.status != auth.USER_STATUS_ACTIVE: return token = util.random_password(64, True) _RESET_TOKENS_POOL.put(token, user.login, _RESET_TOKEN_TTL) reset_url = router.rule_url('auth_ui_password@reset', {'token': token}) msg_body = tpl.render( 'auth_ui_password@mail/{}/reset-password'.format( lang.get_current()), { 'user': user, 'reset_url': reset_url }) mail.Message( user.login, lang.t('auth_ui_password@reset_password_mail_subject'), msg_body).send() router.session().add_info_message( lang.t('auth_ui_password@check_email_for_instructions')) except auth.error.UserNotFound: pass
def exec(self): reporter = auth.get_current_user() if reporter.is_anonymous: raise self.forbidden() model = self.arg('model') try: entity = _api.dispense(model, self.arg('uid')) except odm.error.EntityNotFound: raise self.not_found() tpl_name = 'content@mail/{}/abuse'.format(lang.get_current()) subject = lang.t('content@mail_subject_abuse') for recipient in auth.find_users( query.Query(query.Eq('status', 'active'))): if not entity.odm_auth_check_entity_permissions( [PERM_MODIFY, PERM_DELETE], recipient): continue body = tpl.render(tpl_name, { 'reporter': reporter, 'recipient': recipient, 'entity': entity }) mail.Message(entity.author.login, subject, body).send() return {'message': lang.t('content@abuse_receipt_confirm')}
def on_auth_user_status_change(user: auth.AbstractUser, status: str): if auth.is_user_status_change_notification_enabled(): msg = tpl.render( 'auth_ui@mail/{}/user-status-change'.format(lang.get_current()), { 'user': user, 'status': status, }) mail.Message(user.login, lang.t('auth_ui@user_status_change_notify'), msg).send()
def _content_notify_author_status_change(self): """Notify content author about status change by another user """ if auth.get_current_user() == self.author: return m_subject = lang.t('content@content_status_change_mail_subject') m_body = tpl.render('content@mail/{}/content-status-change'.format(lang.get_current()), { 'entity': self, 'status': self.t('content_status_{}_{}'.format(self.model, self.status)), }) mail.Message(self.author.login, m_subject, m_body).send()
def _content_notify_admins_waiting_status(self): """Notify administrators about waiting content """ if auth.get_current_user().is_admin or self.status != CONTENT_STATUS_WAITING: return for u in auth.get_admin_users(): m_subject = lang.t('content@content_waiting_mail_subject') m_body = tpl.render('content@mail/{}/waiting-content'.format(lang.get_current()), { 'user': u, 'entity': self, }) mail.Message(u.login, m_subject, m_body).send()
def on_comments_create_comment(comment: comments.model.AbstractComment): """comments.create_comment """ entity = _api.find_by_url(comment.thread_uid) if comment.is_reply or not entity or comment.author == entity.author: return tpl_name = 'content@mail/{}/comment'.format(lang.get_current()) subject = lang.t('content@mail_subject_new_comment') body = tpl.render(tpl_name, {'comment': comment, 'entity': entity}) m_from = '{} <{}>'.format(comment.author.first_last_name, mail.mail_from()[1]) mail.Message(entity.author.login, subject, body, m_from).send()
def comments_report_comment(uid: str): try: comment = comments.get_comment(uid, 'pytsite') except comments.error.CommentNotExist: return tpl_name = 'comments_odm@mail/{}/report'.format(lang.get_current()) m_subject = lang.t('comments_odm@mail_subject_report_comment') for user in auth.find_users(query.Query(query.Eq('status', 'active'))): if not user.has_permission('*****@*****.**'): continue m_body = tpl.render(tpl_name, {'comment': comment, 'recipient': user}) mail.Message(user.login, m_subject, m_body).send()
def create_comment(thread_id: str, body: str, author: auth.model.AbstractUser, status: str = 'published', parent_uid: str = None, driver_name: str = None) -> _model.AbstractComment: """Create a new comment """ # Check min length if len(body) < get_comment_min_body_length(): raise _error.CommentTooShort(lang.t('comments@error_body_too_short')) # Check max length if len(body) > get_comment_max_body_length(): raise _error.CommentTooLong(lang.t('comments@error_body_too_long')) # Check status if status not in get_comment_statuses(): raise _error.InvalidCommentStatus( "'{}' is not a valid comment's status.".format(status)) # Load driver driver = get_driver(driver_name) # Create comment comment = driver.create_comment(thread_id, body, author, status, parent_uid) events.fire('comments@create_comment', comment=comment) # Send email notification about reply if reg.get('comments.email_notify', True) and comment.is_reply: parent_comment = get_comment(comment.parent_uid) if comment.author != parent_comment.author: tpl_name = 'comments@mail/{}/reply'.format(lang.get_current()) m_subject = lang.t('comments@mail_subject_new_reply') m_body = tpl.render( tpl_name, { 'reply': comment, 'comment': get_comment(comment.parent_uid, driver_name) }) m_from = '{} <{}>'.format(author.first_last_name, mail.mail_from()[1]) mail.Message(parent_comment.author.login, m_subject, m_body, m_from).send() return comment
def on_cron_every_min(): """Send weekly mail digest """ # Check if the models is specified models = _reg.get('content_digest.models') if not models: return # Check for the current day and time weekdays = _reg.get('content_digest.days_of_week', []) # type: list time_of_day = _reg.get('content_digest.day_time', '00:00') if isinstance(time_of_day, _datetime): time_of_day = time_of_day.time() else: time_of_day = _util.parse_date_time(time_of_day).time() now = _datetime.now() now_weekday = now.weekday() if now.weekday() not in weekdays or not (time_of_day.hour == now.hour and time_of_day.minute == now.minute): return # Calculate days number to query collections prev_weekday = weekdays[weekdays.index(now_weekday) - 1] if prev_weekday < now_weekday: days_diff = (now_weekday + 1) - (prev_weekday + 1) else: days_diff = 8 - (prev_weekday + 1) + now_weekday # Get entities of each model entities = [] entities_num = _reg.get('content_digest.entities_number', 10) pub_period = _datetime.now() - _timedelta(days_diff) for model in models: f = _content.find(model, language='*').gte('publish_time', pub_period).sort([ ('views_count', _odm.I_DESC) ]) entities += list(f.get(entities_num)) # Nothing to send if not entities: return # Sort all entities and cut top entities = sorted(entities, key=lambda e: e.views_count)[:entities_num] for subscriber in _odm.find('content_digest_subscriber').eq( 'enabled', True).get(): _logger.info('Preparing content digest for {}'.format( subscriber.f_get('email'))) lng = subscriber.f_get('language') default_m_subject = _lang.t('content_digest@default_mail_subject', language=lng) m_subject = _reg.get('content_digest.mail_subject_{}'.format(lng), default_m_subject) m_body = _tpl.render( _reg.get('content_digest.tpl', 'content_digest@digest'), { 'entities': entities, 'subscriber': subscriber, 'language': lng, }) _mail.Message(subscriber.f_get('email'), m_subject, m_body).send()