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 exec(self) -> str: # Get browser browser = _api.get_browser(self.arg('model')) # Set page title metatag.t_set('title', browser.title) # Render admin template if self.arg('_pytsite_router_rule_name') == 'odm_ui@admin_browse': return admin.render( tpl.render('odm_ui@browse', {'browser': browser})) # Render user template elif self.arg('_pytsite_router_rule_name') == 'odm_ui@browse': try: # Call a controller provided by application self.args['browser'] = browser return router.call('odm_ui_browse', self.args) except routing.error.RuleNotFound: # Render a template provided by application return tpl.render('odm_ui/browse', {'browser': browser}) # Unknown rule else: raise self.not_found()
def exec(self) -> str: theme_v = reg.get('theme.version', '1') self.args.update(content.paginate(self.arg('finder'))) assetman.preload('v{}/css/content-entity-index.css'.format(theme_v)) return tpl.render('v{}/content-entity-index'.format(theme_v), self.args)
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) -> str: theme_v = reg.get('theme.version', '1') assetman.preload('v1/css/content-entity-modify.css'.format(theme_v)) return tpl.render( 'v1/content-entity-modify'.format(reg.get('theme.version', '1')), self.args)
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 exec(self): form = _frm.SetNewPassword(self.request, token=self.arg('token')) metatag.t_set('title', form.title) tpl_args = { 'driver': 'password', 'form_type': 'reset-password', 'form': form, } # Try to render tpl provided by application try: return tpl.render('auth_ui/form', tpl_args) # Render auth_ui plugin's built-in tpl except tpl.error.TemplateNotFound: return tpl.render('auth_ui@form', tpl_args)
def router_dispatch(): """'pytsite.router.dispatch' handler. """ counter_id = _reg.get('yandex_metrika.counter_id') if not counter_id and _auth.get_current_user().has_role('dev'): _router.session().add_warning_message( _lang.t('yandex_metrika@plugin_setup_required_warning')) else: _assetman.add_inline_js( _tpl.render('yandex_metrika@counter', {'counter_id': counter_id}))
def router_dispatch(): """'pytsite.router.dispatch' handler. """ t_id = _reg.get('google_analytics.tracking_id') if not t_id and _auth.get_current_user().has_role('dev'): _router.session().add_warning_message( _lang.t('google_analytics@plugin_setup_required_warning')) else: _assetman.inline_js( _tpl.render('google_analytics@counter', {'tracking_id': t_id}))
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 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 exec(self) -> str: self.args.update(content.paginate(self.arg('finder'))) exclude_ids = [e.id for e in self.arg('entities')] self.args.update({ 'sidebar': _get_sidebar(exclude_ids), }) author = self.arg('author') if author: self.args['author_widget'] = auth_profile.widget.Profile('user-profile', user=author) return tpl.render('$theme@content/index', self.args)
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 __init__(self, uid: str, **kwargs): """Init. """ super().__init__(uid, **kwargs) self._app_id = _facebook.get_app_id() self._href = kwargs.get('href', _router.current_url()) js_sdk_args = { 'app_id': self._app_id, 'language': _lang.ietf_tag(sep='_') } _assetman.add_inline_js(_tpl.render('facebook@fb-js-sdk', js_sdk_args))
def exec(self) -> str: try: self.args['form'] = _api.user_form( self.request, auth.get_user(nickname=self.arg('nickname')).uid) except auth.error.UserNotFound: raise self.not_found() metatag.t_set('title', lang.t('auth_ui@profile_edit_title')) try: return router.call('auth_ui_user_profile_modify', self.args) except routing.error.RuleNotFound: return tpl.render('auth_ui/user-profile-modify', self.args)
def exec(self) -> str: theme_v = reg.get('theme.version', '1') exclude_ids = [] starred = _get_articles(exclude_ids, 1, starred=True) tpl_args = { 'page_header_article': starred[0] if starred else None, 'page_header_article_tags': tag.widget.EntityTagCloud('header-article-tags', entity=starred[0], term_css='') if starred else None, 'fullscreen_page_header': bool(starred), } if theme_v in ('1', '2'): sections = list(section.get()) latest_by_section = {} for sec in sections: latest_by_section[sec.alias] = _get_articles(exclude_ids, 6, sec=sec) tpl_args.update({ 'sections': sections, 'latest_articles_by_section': latest_by_section, }) elif theme_v == '3': tpl_args.update({ 'latest_articles_first_left': _get_articles(exclude_ids, 4), 'latest_articles_first_right': _get_articles(exclude_ids, 7, sort_field='views_count', days=7), 'latest_articles_second_left': _get_articles(exclude_ids, 4), 'latest_articles_second_right': _get_articles(exclude_ids, 7, sort_field='views_count', days=14), }) assetman.preload('v{}/css/home.css'.format(theme_v)) return tpl.render('v{}/home'.format(theme_v), tpl_args)
def exec(self): if not auth.get_current_user().is_admin: raise self.forbidden() e_type = self.arg('e_type') if e_type == 'role': metatag.t_set('title', lang.t('auth_admin@roles')) form = _frm.BrowseRoles(self.request) elif e_type == 'user': metatag.t_set('title', lang.t('auth_admin@users')) form = _frm.BrowseUsers(self.request) else: raise self.server_error('Unknown auth entity type') return admin.render(tpl.render('auth_admin@form', {'form': form}))
def exec(self) -> str: rule_name = self.arg('_pytsite_router_rule_name') # type: str model = self.arg('model') # Get form if rule_name.endswith('m_form'): try: eid = self.arg('eid') form = _api.get_m_form(model, eid if eid != '0' else None, hide_title=True) metatag.t_set('title', form.title) except errors.NotFound as e: raise self.not_found(e) elif rule_name.endswith('d_form'): eids = self.arg('ids', []) or self.arg('eids', []) form = _api.get_d_form(model, eids, hide_title=True) else: raise self.not_found() metatag.t_set('title', form.title) # Render admin template if 'admin' in rule_name: return admin.render(tpl.render('odm_ui@form', {'form': form})) # Render user template else: try: # Call a controller provided by application self.args['form'] = form return router.call('odm_ui_form', self.args) except routing.error.RuleNotFound: # Render a template provided by application return tpl.render('odm_ui/form', {'form': form})
def exec(self): if not auth.get_current_user().is_admin: raise self.forbidden() eids = self.arg('eids') if isinstance(eids, str): eids = util.cleanup_list(eids.split(',')) form = _frm.DeleteEntities(self.request, e_type=self.arg('e_type'), eids=eids) form.hide_title = True metatag.t_set('title', form.title) return admin.render(tpl.render('auth_admin@form', {'form': form}))
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 exec(self) -> str: theme_v = reg.get('theme.version', '1') e = self.arg('entity') exclude_ids = [e.id] self.args.update({ 'entity_tags': tag.widget.EntityTagCloud('entity-tag-cloud', entity=e, term_css=''), 'related_1': _get_articles(exclude_ids, 3, e.section, 'views_count') if e.model == 'article' else [], 'related_2': _get_articles(exclude_ids, 2, e.section, 'views_count') if e.model == 'article' else [], 'related_3': _get_articles(exclude_ids, 2, e.section) if e.model == 'article' else [], }) if e.images: self.args.update({ 'page_header_article': e, 'page_header_article_tags': tag.widget.EntityTagCloud('header-article-tags', entity=e, term_css=''), 'fullscreen_page_header': True, }) if plugman.is_installed('addthis'): from plugins import addthis self.args.update({ 'share_widget': addthis.widget.AddThis('add-this-share') if reg.get('addthis.pub_id') else '', }) if plugman.is_installed('disqus'): self.args.update( {'comments_widget': comments.get_widget(driver_name='disqus')}) assetman.preload('v{}/css/content-entity-view.css'.format(theme_v)) return tpl.render('v{}/content-entity-view'.format(theme_v), self.args)
def exec(self) -> Union[str, http.RedirectResponse]: # Redirect to the base URL if user is already authenticated if not auth.get_current_user().is_anonymous: return self.redirect(self.arg('__redirect', router.base_url())) # Determine driver's name from argument or get default try: driver_name = self.arg('driver', _api.get_driver().name) except auth.error.DriverNotRegistered: raise self.not_found() rule_name = self.arg('_pytsite_router_rule_name') if 'sign_in' in rule_name: form_type = 'sign-in' form = _api.sign_in_form(self.request, driver_name) elif 'sign_up' in rule_name: # Check if sign up is enabled if not auth.is_sign_up_enabled(): raise self.not_found() form_type = 'sign-up' form = _api.sign_up_form(self.request, driver_name) elif 'restore_account' in rule_name: form_type = 'restore-account' form = _api.restore_account_form(self.request, driver_name) form.redirect = router.base_url() else: raise ValueError('Unsupported form type') if not form.redirect: form.redirect = router.base_url() metatag.t_set('title', form.title) tpl_args = { 'driver': driver_name, 'form_type': form_type, 'form': form, } try: return router.call('auth_ui_form', tpl_args) except routing.error.RuleNotFound: return tpl.render('auth_ui/form', tpl_args)
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 _get_element(self, **kwargs) -> htmler.Element: self._css += ' layout-{}'.format(self._layout) self._data.update({ 'url': http_api.url('file_ui@post'), 'max_files': self._max_files, 'max_file_size': self._max_file_size, 'accept_files': self._accept_files, 'slot_css': self._slot_css, 'show_numbers': self._show_numbers, 'dnd': self._dnd, 'preview_images': self._preview_images, 'thumb_width': self._thumb_width, 'thumb_height': self._thumb_height, }) return htmler.TagLessElement( tpl.render('file_ui@file_upload_widget', {'widget': self}))
def exec(self) -> str: uid = self.arg('uid') # Load setting definition setting_def = _api.get_definition(uid) # Update page's title metatag.t_set('title', lang.t(setting_def['title'])) content = setting_def['content'] if issubclass(content, _frm.Form): return admin.render( tpl.render('settings@form', {'form': content(self.request, setting_uid=uid)})) elif callable(content): return admin.render(content()) else: return content
def exec(self) -> str: exclude_ids = [] latest = _get_articles(exclude_ids, 3) sections = list(section.get()) latest_by_section = {} for sec in sections: latest_by_section[sec.alias] = _get_articles(exclude_ids, 4, sec=sec) tpl_args = { 'sections': sections, 'latest_articles': latest, 'latest_by_section': latest_by_section, 'sidebar': _get_sidebar(exclude_ids), } return tpl.render('home', tpl_args)
def render(self, navbar: admin.NavBar, sidebar: admin.SideBar, content: Union[str, htmler.Element]): return tpl.render( 'admin_theme_lte@html', { 'admin_sidebar': self._render_sidebar(sidebar), 'admin_language_nav': widget.select.LanguageNav( 'admin-language-nav', dropdown=True, bs_version=3), 'content': content, 'core_name': package_info.name('pytsite'), 'core_url': package_info.url('pytsite'), 'core_version': package_info.version('pytsite'), 'sidebar_collapsed': router.request().cookies.get('adminSidebarCollapsed') is not None, })
def exec(self): if not auth.get_current_user().is_admin: raise self.forbidden() e_type = self.arg('e_type') uid = self.arg('uid') if e_type == 'role': metatag.t_set( 'title', lang.t('auth_admin@' + ('create_role' if uid == '0' else 'modify_role'))) form = auth_ui.role_form(self.request, role_uid=uid) elif e_type == 'user': metatag.t_set( 'title', lang.t('auth_admin@' + ('create_user' if uid == '0' else 'modify_user'))) form = auth_ui.user_form(self.request, user_uid=uid) else: raise self.server_error('Unknown entity type') return admin.render(tpl.render('auth_admin@form', {'form': form}))
def exec(self) -> str: try: user = auth.get_user(nickname=self.arg('nickname')) except auth.error.UserNotFound: raise self.not_found() if not user.is_active: raise self.not_found() c_user = auth.get_current_user() if not user.is_public and not (c_user == user or c_user.is_admin): raise self.not_found() self.args['user'] = user metatag.t_set( 'title', lang.t('auth_ui@profile_view_title', {'name': user.first_last_name})) try: return router.call('auth_ui_user_profile_view', self.args) except routing.error.RuleNotFound: return tpl.render('auth_ui/user-profile-view', self.args)