def render_html(self): from apps.monster.models import MONSTER_GROUP comment = TemplateComment(self.comment.details()) top_sticker = comment.top_sticker() if top_sticker is None: sticker = knobs.DEFAULT_FOOTER_STICKER else: sticker = top_sticker['name'] if comment.category == MONSTER_GROUP and comment.parent_comment is not None: top = TemplateComment(self.comment.parent_comment.details()) url = "/monster/{0}/{1}".format(base36encode(top.id), comment.id) return render_jinja_to_string( 'comment/stitched_monster.html', { 'top': top, 'bottom': comment, 'sticker': sticker, 'knobs': knobs, 'url': url, }) else: return render_jinja_to_string('comment/footer.html', { 'comment': comment, 'sticker': sticker, 'knobs': knobs, })
def render_html(self): from apps.monster.models import MONSTER_GROUP comment = TemplateComment(self.comment.details()) top_sticker = comment.top_sticker() if top_sticker is None: sticker = knobs.DEFAULT_FOOTER_STICKER else: sticker = top_sticker['name'] if comment.category == MONSTER_GROUP and comment.parent_comment is not None: top = TemplateComment(self.comment.parent_comment.details()) url = "/monster/{0}/{1}".format(base36encode(top.id), comment.id) return render_jinja_to_string('comment/stitched_monster.html', { 'top': top, 'bottom': comment, 'sticker': sticker, 'knobs': knobs, 'url': url, }) else: return render_jinja_to_string('comment/footer.html', { 'comment': comment, 'sticker': sticker, 'knobs': knobs, })
def suggestion_widget(context, type=None): request = context['request'] browser = request.META[ 'HTTP_USER_AGENT'] if 'HTTP_USER_AGENT' in request.META else '' user = request.user tags = get_suggested_tags(user)() users = get_most_stickered_unfollowed_users(user)() has_tags = len(tags) > 0 has_users = len(users) > 0 using_chrome = chrome.search(browser) is not None def get_type(): types = ['invite'] if has_users: types += ['people'] if has_tags: types += ['tags'] if using_chrome: types += ['chrome'] return random.choice(types) try: ttype = get_type() except IndexError: return Markup('') if ttype == 'tags': ctx = {'tags': tags, 'type': ttype} return Markup( render_jinja_to_string('suggest/suggest_tags_widget.html', update_context(context, ctx))) elif ttype == 'people': ctx = {'users': users, 'type': ttype} return Markup( render_jinja_to_string('suggest/suggest_people_widget.html', update_context(context, ctx))) elif ttype == 'invite': ctx = {'type': ttype} return Markup( render_jinja_to_string('suggest/suggest_invite_widget.html', update_context(context, ctx))) elif ttype == 'chrome': ctx = {'type': ttype} return Markup( render_jinja_to_string('suggest/suggest_extension_widget.html', update_context(context, ctx)))
def thread_new_comment(context, comment, is_op=False, is_current=False, is_first_reply_with_content=False, is_last_reply_with_content=False, ignore_lazy_load=False): from canvas.templatetags.helpers import TemplateComment from canvas.models import CommentSticker request = context['request'] comment = TemplateComment(comment, request_context=context, is_op=is_op) try: sticker_name = comment.top_sticker()['name'] except TypeError: sticker_name = None try: viewer_following_thread = request.user.redis.followed_threads.sismember(comment.id) except AttributeError: viewer_following_thread = False ctx = { 'request': request, 'short_id': context.get('short_id'), 'comment': comment, 'can_edit_tags': request.user.is_staff or comment.is_author(request.user), 'lazy_content': features.lazy_content(request), 'is_current':is_current, 'ignore_lazy_load': ignore_lazy_load, 'is_first_reply_with_content': is_first_reply_with_content, 'is_last_reply_with_content': is_last_reply_with_content, 'my_post': comment.is_author(request.user), 'stickered_by_viewer': bool(CommentSticker.get_sticker_from_user(comment.id, request.user)), 'sticker_name': sticker_name, 'has_content': bool(comment.reply_content_id), 'viewer_following_thread': viewer_following_thread, } return Markup(render_jinja_to_string('threads_new/_thread_comment.html', ctx))
def jinja_thread_comment(context, comment_details, fullsize): context.update({ 'comment' : TemplateComment(comment_details, request_context=context, is_op=bool(fullsize)), 'fullsize' : fullsize, 'show_tags' : True, }) return render_jinja_to_string('comment/jinja_thread_comment.html', context)
def image_tile(context, tile, render_options, nav_category, template='comment/explore_tile.html'): request = context['request'] user = request.user if not hasattr(tile, 'comment'): raise TypeError("A tile should be an instance of TileDetails or LastReplyTileDetails; something that " "has a .comment. Received a %s" % type(tile)) comment = tile.comment new_activity = False if render_options.get('show_activity') and render_options.get('show_pins') and comment.last_reply_time: pinned_lastviewed = user.kv.pinned_lastviewed.get() or 1303426306. # When we launched pinned. new_activity = pinned_lastviewed < float(comment.last_reply_time) def nav_aware(url): default_category = Category.get_default(request.user).details() # If we don't have a nav_category (user / about page) it should be the default for this user. _nav_category = nav_category or default_category if _nav_category['name'] != default_category['name']: url += "?nav=%s" % _nav_category['name'] return url #TODO probably change TemplateComment to TemplateTile instead. # This is weird - we pass tile to the template too, but tile.comment != comment now. comment = TemplateComment(tile.comment, request_context=context) float_sticker = False sticky_text = getattr(tile, 'text', None) return render_jinja_to_string(template, locals())
def jinja_thread_preview(context, thread, admin_view=False): ctx = { 'thread': thread, 'admin_view': admin_view, 'show_curation_info': admin_view and bool(getattr(thread, 'curator', None)), } return Markup(render_jinja_to_string('logged_out_homepage/thread_preview.html', update_context(context, ctx)))
def r2r_jinja(template, context, request=None, **response_kwargs): if request: context = RequestContext(request, context) response_kwargs.setdefault('content_type', 'text/html') response_kwargs.setdefault('status', 200) return HttpResponse(render_jinja_to_string(template, context), **response_kwargs)
def post_thread_form(context, show_start_options=True): ctx = { 'show_start_options': bool(show_start_options), } return Markup( render_jinja_to_string('post_thread/_post_thread_form.html', update_context(context, ctx)))
def explore_tile(context, tile, render_options={}, nav_category={}): comment = TemplateComment(tile.comment, request_context=context) sticky_text = getattr(tile, 'text', None) viewer_sticker = tile.viewer_sticker viewer = context['request'].user remixer_count = comment.thread.author_count return Markup(render_jinja_to_string('/comment/explore_tile.html', locals()))
def activity_stream_item(activity, viewer): ctx = { 'activity': activity, 'viewer': viewer, } return Markup( render_jinja_to_string( u'activity/types/{0}.html'.format(activity.TYPE), ctx))
def invite_remixers(viewer, comment): invite_url = absolute_invite_url(viewer, comment_id=comment.id) context = { 'comment' : comment, 'uuid' : uuid.uuid4(), 'invite_url': invite_url, } return mark_safe(Markup(render_jinja_to_string('comment/invite_remixers.html', context)))
def handle_analytics(self, fact_query, *args, **kwargs): funnel_name = args[0] try: funnel = Funnels.by_name[funnel_name] except KeyError: print "Error, invalid experiment name %r." % funnel_name sys.exit(1) aggr = aggregate_facts_by_tokens(fact_query.fact_iter) logged_out_tests = [ ('total', lambda user: True), ] logged_in_tests = [ ('total', lambda user: True), ] for metric in funnel.steps: test_func = lambda user, metric=metric: user.facts[metric.name] test = ('%s' % metric, test_func) logged_out_tests.append(test) test = ('%s ever' % metric, test_func) logged_in_tests.append(test) def get_tables(new_users): return [ ('By Unique Browser Session', gather_results(aggr, logged_out_tests, aggr.browser_sessions, new_users)), ('By Unique User', gather_results(aggr, logged_in_tests, aggr.logged_in_users, new_users)), ] sections = [ ('Excluding Old Users', get_tables(True)), ('All Users', get_tables(False)), ] context = { 'start': self.start, 'stop': self.stop, 'funnel': funnel, 'sections': sections, } output_filename = "/var/canvas/analytics/reports/funnel_results_%s_%s.html" % ( funnel_name, int(time.time())) with file(output_filename, 'w') as output: output.write( render_jinja_to_string('analytics/funnel_results.html', context)) webbrowser_util.open_if_able(output_filename)
def thread_new_comment(context, comment, is_op=False, is_current=False, is_first_reply_with_content=False, is_last_reply_with_content=False, ignore_lazy_load=False): from canvas.templatetags.helpers import TemplateComment from canvas.models import CommentSticker request = context['request'] comment = TemplateComment(comment, request_context=context, is_op=is_op) try: sticker_name = comment.top_sticker()['name'] except TypeError: sticker_name = None try: viewer_following_thread = request.user.redis.followed_threads.sismember( comment.id) except AttributeError: viewer_following_thread = False ctx = { 'request': request, 'short_id': context.get('short_id'), 'comment': comment, 'can_edit_tags': request.user.is_staff or comment.is_author(request.user), 'lazy_content': features.lazy_content(request), 'is_current': is_current, 'ignore_lazy_load': ignore_lazy_load, 'is_first_reply_with_content': is_first_reply_with_content, 'is_last_reply_with_content': is_last_reply_with_content, 'my_post': comment.is_author(request.user), 'stickered_by_viewer': bool(CommentSticker.get_sticker_from_user(comment.id, request.user)), 'sticker_name': sticker_name, 'has_content': bool(comment.reply_content_id), 'viewer_following_thread': viewer_following_thread, } return Markup( render_jinja_to_string('threads_new/_thread_comment.html', ctx))
def quest_preview(context, quest_preview, admin_view=False, already_saved=False): ctx = { 'quest_preview': quest_preview, 'admin_view': admin_view, 'show_curation_info': admin_view and bool(getattr(quest_preview, 'curator', None)), 'already_saved': already_saved, } return Markup(render_jinja_to_string('quests/quest_preview.html', update_context(context, ctx)))
def jinja_thread_reply(context, comment_details, template='threads/reply.html', is_expanded=False): opts = { 'reply': TemplateComment(comment_details, request_context=context), 'is_expanded': is_expanded, } return Markup( render_jinja_to_string(template, update_context(context, opts)))
def suggestion_widget(context, type=None): request = context['request'] browser = request.META['HTTP_USER_AGENT'] if 'HTTP_USER_AGENT' in request.META else '' user = request.user tags = get_suggested_tags(user)() users = get_most_stickered_unfollowed_users(user)() has_tags = len(tags) > 0 has_users = len(users) > 0 using_chrome = chrome.search(browser) is not None def get_type(): types = ['invite'] if has_users: types += ['people'] if has_tags: types += ['tags'] if using_chrome: types += ['chrome'] return random.choice(types) try: ttype = get_type() except IndexError: return Markup('') if ttype == 'tags': ctx = { 'tags': tags, 'type': ttype } return Markup(render_jinja_to_string('suggest/suggest_tags_widget.html', update_context(context, ctx))) elif ttype == 'people': ctx = { 'users': users, 'type': ttype } return Markup(render_jinja_to_string('suggest/suggest_people_widget.html', update_context(context, ctx))) elif ttype == 'invite': ctx = { 'type': ttype } return Markup(render_jinja_to_string('suggest/suggest_invite_widget.html', update_context(context, ctx))) elif ttype == 'chrome': ctx = { 'type': ttype } return Markup(render_jinja_to_string('suggest/suggest_extension_widget.html', update_context(context, ctx)))
def invite_remixers(viewer, comment): invite_url = absolute_invite_url(viewer, comment_id=comment.id) context = { 'comment': comment, 'uuid': uuid.uuid4(), 'invite_url': invite_url, } return mark_safe( Markup(render_jinja_to_string('comment/invite_remixers.html', context)))
def render_thread_preview(request, short_id): thread_preview = ThreadPreview.get_by_short_id(short_id) ctx = { 'thread': thread_preview, 'admin_view': True, 'show_curation_info': False, } return HttpResponse(render_jinja_to_string('logged_out_homepage/thread_preview.html', ctx))
def render_quest_preview(request, short_id): quest_preview = models.QuestPreview.get_by_short_id(short_id) ctx = { 'quest_preview': quest_preview, 'admin_view': True, 'show_curation_info': False, } return HttpResponse(render_jinja_to_string('quests/quest_preview.html', ctx))
def render_quest_preview(request, short_id): quest_preview = QuestPreview.get_by_short_id(short_id) ctx = { 'quest_preview': quest_preview, 'admin_view': True, 'show_curation_info': False, } return HttpResponse( render_jinja_to_string('quests/quest_preview.html', ctx))
def feed_item(context, item): request = context['request'] ctx = { 'item': item, 'comment': item['comment'], 'request': request, 'viewer_sticker': item['viewer_sticker'], 'lazy_content': features.lazy_content(request), } return Markup(render_jinja_to_string('feed/_feed_item.html', ctx))
def jinja_site_header(context): # Group list is current category + following + top, without duplicates. request = context['request'] user = request.user if user.is_authenticated(): economy.grant_daily_free_stickers(request.user) template_name = 'header/logged_in.html' else: template_name = 'header/logged_out.html' return mark_safe(render_jinja_to_string(template_name, context))
def render_sticky_thread_preview(request, short_id): from apps.sticky_threads.models import StickyThread, ThreadPreview thread_preview = ThreadPreview.get_by_short_id(short_id) ctx = { 'thread': thread_preview, 'admin_view': True, 'show_curation_info': False, } return HttpResponse(render_jinja_to_string('sticky_threads/thread_preview.html', ctx))
def handle_analytics(self, fact_query, *args, **kwargs): funnel_name = args[0] try: funnel = Funnels.by_name[funnel_name] except KeyError: print "Error, invalid experiment name %r." % funnel_name sys.exit(1) aggr = aggregate_facts_by_tokens(fact_query.fact_iter) logged_out_tests = [ ('total', lambda user: True), ] logged_in_tests = [ ('total', lambda user: True), ] for metric in funnel.steps: test_func = lambda user, metric=metric: user.facts[metric.name] test = ('%s' % metric, test_func) logged_out_tests.append(test) test = ('%s ever' % metric, test_func) logged_in_tests.append(test) def get_tables(new_users): return [ ('By Unique Browser Session', gather_results(aggr, logged_out_tests, aggr.browser_sessions, new_users)), ('By Unique User', gather_results(aggr, logged_in_tests, aggr.logged_in_users, new_users)), ] sections = [ ('Excluding Old Users', get_tables(True)), ('All Users', get_tables(False)), ] context = { 'start': self.start, 'stop': self.stop, 'funnel': funnel, 'sections': sections, } output_filename = "/var/canvas/analytics/reports/funnel_results_%s_%s.html" % (funnel_name, int(time.time())) with file(output_filename, 'w') as output: output.write(render_jinja_to_string('analytics/funnel_results.html', context)) webbrowser_util.open_if_able(output_filename)
def monster_image_tile(context, tile, render_options={}, nav_category={}, display_monster=True): request = context['request'] user = request.user viewer_sticker = tile.viewer_sticker top = TemplateComment(tile.top, request_context=context) bottom = TemplateComment(tile.bottom, request_context=context) float_sticker = getattr(top, 'reply_content_id', None) and top.reply_text template = 'monster/monster_explore_tile.html' return Markup(render_jinja_to_string(template, locals()))
def monster_image_tile(context, tile, render_options={}, nav_category={}, display_monster=True): request = context['request'] user = request.user viewer_sticker = tile.viewer_sticker top = TemplateComment(tile.top, request_context=context) bottom = TemplateComment(tile.bottom, request_context=context) float_sticker = False template = 'monster/monster_explore_tile.html' return Markup(render_jinja_to_string(template, locals()))
def thread_new_comment( context, comment, is_op=False, is_current=False, is_first_reply_with_content=False, is_last_reply_with_content=False, ignore_lazy_load=False, ): from canvas.templatetags.helpers import TemplateComment from canvas.models import CommentSticker request = context["request"] comment = TemplateComment(comment, request_context=context, is_op=is_op) try: sticker_name = comment.top_sticker()["name"] except TypeError: sticker_name = None try: viewer_following_thread = request.user.redis.followed_threads.sismember(comment.id) except AttributeError: viewer_following_thread = False ctx = { "request": request, "comment": comment, "can_edit_tags": request.user.is_staff or comment.is_author(request.user), "lazy_content": features.lazy_content(request), "is_current": is_current, "ignore_lazy_load": ignore_lazy_load, "is_first_reply_with_content": is_first_reply_with_content, "is_last_reply_with_content": is_last_reply_with_content, "my_post": comment.is_author(request.user), "stickered_by_viewer": bool(CommentSticker.get_sticker_from_user(comment.id, request.user)), "sticker_name": sticker_name, "has_content": bool(comment.reply_content_id), "viewer_following_thread": viewer_following_thread, } return Markup(render_jinja_to_string("threads_new/_thread_comment.html", ctx))
def jinja_sidebar(context): followed_tags = context.get('followed_tags') current_tag = context['nav_tag'] def which_nav(path): if path.startswith('/feed') or path == '/': return 'feed' elif path.startswith('/monster'): return 'monster' elif path.startswith('/x/everything'): return 'explore' return '' context.update({ 'current_nav': which_nav(context['request'].path), }) if followed_tags is not None: context['tracked_tags'] = get_tracked_tags(context['request'].user, followed_tags, current_tag) return mark_safe(render_jinja_to_string('sidebar/sidebar.html', context))
def jinja_sticker_pack(context): context['store_items']= stickers.get_purchasable(context['request'].user) return mark_safe(render_jinja_to_string('sticker_pack/sticker_pack.html', context))
def mobile_tile(comment): return Markup(render_jinja_to_string("mobile/tile.html", {'comment': comment}))
def r2r_jinja(template, context, request=None): if request: context = RequestContext(request, context) return HttpResponse(render_jinja_to_string(template, context), content_type='text/html')
def make_message(self, notification, force=False): # This is a hack to get around circular imports. from canvas.templatetags.jinja_base import render_jinja_to_string action = notification.action guid = str(uuid.uuid1()) action_unsubscribe_link = self.make_action_unsubscribe_link(action, notification.recipient) # In the future, use "*_url" for URLs as a standard convention, not "_link". unsubscribe_links = {'action_unsubscribe_link': action_unsubscribe_link} comment = getattr(notification, 'comment', None) if comment: thread_unsubscribe_link = self.make_thread_unsubscribe_link(comment, notification.recipient) unsubscribe_links['thread_unsubscribe_link'] = thread_unsubscribe_link tracking_query = urlencode({ 'ct': 'email', 'ct_action': action, 'ct_guid': guid, 'ct_user_id': notification.recipient.id, }) # Get Comment or None for the post being replied/remixed recipient_comment = { 'remixed': lambda: comment.reply_content.remix_of.first_caption, 'replied': lambda: comment.replied_comment, 'thread_replied': lambda: comment.parent_comment, }.get(notification.action, lambda: None)() if recipient_comment: recipient_comment = recipient_comment.details() context = { 'notification': notification, 'tracking_query': tracking_query, 'recipient_comment': recipient_comment, 'timestamp': time.time(), 'comments_exist': notification.recipient.comments.exists(), } context.update(unsubscribe_links) context.update(getattr(self, '_{0}_extra_context'.format(action), lambda _: {})(notification)) # Grab the template text_template = (self.get_overriden_template_file(action, "text") or "email/%s.txt" % action) html_template = (self.get_overriden_template_file(action, "body") or "email/%s.html" % action) subject_template = (self.get_overriden_template_file(action, "subject") or "email/%s_subject.txt" % action) # Render it. subject = render_to_string(subject_template, context).strip() try: text = render_to_string(text_template, context) except TemplateDoesNotExist: text = "" html = render_jinja_to_string(html_template, context) # Separate Gmail threads based on post_id by altering the subject line if recipient_comment: thread_id = recipient_comment.id if thread_id: subject = u''.join([subject, ' (post #', str(thread_id), ')']) return EmailMessage(from_email=settings.UPDATES_EMAIL, to_email=notification.recipient.email, subject=subject, body_html=html, body_text=text, recipient=notification.recipient, guid=guid, unsubscribe_links=unsubscribe_links)
def post_thread_form(context, show_start_options=True): ctx = { 'show_start_options': bool(show_start_options), } return Markup(render_jinja_to_string('post_thread/_post_thread_form.html', update_context(context, ctx)))
def handle_analytics(self, fact_query, *args, **kwargs): EXPERIMENT = args[0] try: experiment = Experiments.by_name[EXPERIMENT] except KeyError: print "Error, invalid experiment name %r." % EXPERIMENT sys.exit(1) aggr = aggregate_facts_by_tokens(fact_query.fact_iter, experiment=experiment) def in_branch(experiment, branch): def test_user(user): return user.experiments.get(experiment) == branch return test_user basis = [(branch.name, in_branch(experiment.name, branch.name)) for branch in experiment.branches.values()] logged_out_tests = [ ('total', lambda user: True), ('viewed 3+ pages', lambda user: user.facts['view'] >= 3), ('5+ infinite scrolls', lambda user: user.facts['infinite_scroll'] >= 5), ('2nd day view', lambda user: user.day_bins[1]['view']), ('signed up', lambda user: user.facts['signup']), ('stickered', lambda user: user.facts['sticker']), ('posted', lambda user: user.facts['post']), ] logged_in_tests = [ ('total', lambda user: True), ('stickered ever', lambda user: user.facts['sticker']), ('posted ever', lambda user: user.facts['post']), ('2nd day after signup viewed', lambda user: user.post_signup_bins[1]['view']), ('2nd day after signup stickered', lambda user: user.post_signup_bins[1]['sticker']), ('2nd day after signup posted', lambda user: user.post_signup_bins[1]['post']), ] for metric in experiment.metrics: test = ('%s ever' % metric, lambda user, metric=metric: user.facts[metric]) logged_out_tests.append(test) logged_in_tests.append(test) def get_tables(new_users): return [ ('By Unique Browser Session', gather_results(aggr, logged_out_tests, aggr.browser_sessions, new_users, basis=basis)), ('By Unique User', gather_results(aggr, logged_in_tests, aggr.logged_in_users, new_users, basis=basis)), ] sections = [] if not experiment.logged_in_only: sections.append(('Excluding Old Users', get_tables(True))) # Even if the experiment is logged_out_only, this shows us what users who saw the logged out experiment and then signed up did. sections.append(('All Users', get_tables(False))) context = { 'start': self.start, 'stop': self.stop, 'experiment': EXPERIMENT, 'sections': sections, } output_filename = "/var/canvas/analytics/reports/ab_results_%s_%s.html" % ( EXPERIMENT, int(time.time())) with file(output_filename, 'w') as output: output.write( render_jinja_to_string('analytics/ab_results.html', context)) webbrowser_util.open_if_able(output_filename)
def submit_quest_form(context): ctx = {} return Markup(render_jinja_to_string('submit_quest/_submit_quest_form.html', update_context(context, ctx)))
def handle_analytics(self, fact_query, *args, **kwargs): EXPERIMENT = args[0] try: experiment = Experiments.by_name[EXPERIMENT] except KeyError: print "Error, invalid experiment name %r." % EXPERIMENT sys.exit(1) aggr = aggregate_facts_by_tokens(fact_query.fact_iter, experiment=experiment) def in_branch(experiment, branch): def test_user(user): return user.experiments.get(experiment) == branch return test_user basis = [(branch.name, in_branch(experiment.name, branch.name)) for branch in experiment.branches.values()] logged_out_tests = [ ('total', lambda user: True), ('viewed 3+ pages', lambda user: user.facts['view'] >= 3), ('5+ infinite scrolls', lambda user: user.facts['infinite_scroll'] >= 5), ('2nd day view', lambda user: user.day_bins[1]['view']), ('signed up', lambda user: user.facts['signup']), ('stickered', lambda user: user.facts['sticker']), ('posted', lambda user: user.facts['post']), ] logged_in_tests = [ ('total', lambda user: True), ('stickered ever', lambda user: user.facts['sticker']), ('posted ever', lambda user: user.facts['post']), ('2nd day after signup viewed', lambda user: user.post_signup_bins[1]['view']), ('2nd day after signup stickered', lambda user: user.post_signup_bins[1]['sticker']), ('2nd day after signup posted', lambda user: user.post_signup_bins[1]['post']), ] for metric in experiment.metrics: test = ('%s ever' % metric, lambda user, metric=metric: user.facts[metric]) logged_out_tests.append(test) logged_in_tests.append(test) def get_tables(new_users): return [ ('By Unique Browser Session', gather_results(aggr, logged_out_tests, aggr.browser_sessions, new_users, basis=basis)), ('By Unique User', gather_results(aggr, logged_in_tests, aggr.logged_in_users, new_users, basis=basis)), ] sections = [] if not experiment.logged_in_only: sections.append(('Excluding Old Users', get_tables(True))) # Even if the experiment is logged_out_only, this shows us what users who saw the logged out experiment and then signed up did. sections.append(('All Users', get_tables(False))) context = { 'start': self.start, 'stop': self.stop, 'experiment': EXPERIMENT, 'sections': sections, } output_filename = "/var/canvas/analytics/reports/ab_results_%s_%s.html" % (EXPERIMENT, int(time.time())) with file(output_filename, 'w') as output: output.write(render_jinja_to_string('analytics/ab_results.html', context)) webbrowser_util.open_if_able(output_filename)
def share_comment(comment): return Markup(render_jinja_to_string("share/_share_comment.html", {'comment': comment}))
def submit_quest_form(context): ctx = {} return Markup( render_jinja_to_string('submit_quest/_submit_quest_form.html', update_context(context, ctx)))
def render_jinja(context, jinja_template_name): return render_jinja_to_string(jinja_template_name, context)
def thread_pagination(context): return Markup(mark_safe(render_jinja_to_string('threads/pagination.html', context)))
def jinja_thread_reply(context, comment_details, template='threads/reply.html', is_expanded=False): opts = { 'reply': TemplateComment(comment_details, request_context=context), 'is_expanded': is_expanded, } return Markup(render_jinja_to_string(template, update_context(context, opts)))
def make_message(self, notification, force=False): # This is a hack to get around circular imports. from canvas.templatetags.jinja_base import render_jinja_to_string action = notification.action guid = str(uuid.uuid1()) action_unsubscribe_link = self.make_action_unsubscribe_link( action, notification.recipient) # In the future, use "*_url" for URLs as a standard convention, not "_link". unsubscribe_links = { 'action_unsubscribe_link': action_unsubscribe_link } comment = getattr(notification, 'comment', None) if comment: thread_unsubscribe_link = self.make_thread_unsubscribe_link( comment, notification.recipient) unsubscribe_links[ 'thread_unsubscribe_link'] = thread_unsubscribe_link tracking_query = urlencode({ 'ct': 'email', 'ct_action': action, 'ct_guid': guid, 'ct_user_id': notification.recipient.id, }) # Get Comment or None for the post being replied/remixed recipient_comment = { 'remixed': lambda: comment.reply_content.remix_of.first_caption, 'replied': lambda: comment.replied_comment, 'thread_replied': lambda: comment.parent_comment, }.get(notification.action, lambda: None)() if recipient_comment: recipient_comment = recipient_comment.details() context = { 'notification': notification, 'tracking_query': tracking_query, 'recipient_comment': recipient_comment, 'timestamp': time.time(), 'comments_exist': notification.recipient.comments.exists(), } context.update(unsubscribe_links) context.update( getattr(self, '_{0}_extra_context'.format(action), lambda _: {})(notification)) # Grab the template text_template = (self.get_overriden_template_file(action, "text") or "email/%s.txt" % action) html_template = (self.get_overriden_template_file(action, "body") or "email/%s.html" % action) subject_template = (self.get_overriden_template_file( action, "subject") or "email/%s_subject.txt" % action) # Render it. subject = render_to_string(subject_template, context).strip() try: text = render_to_string(text_template, context) except TemplateDoesNotExist: text = "" html = render_jinja_to_string(html_template, context) # Separate Gmail threads based on post_id by altering the subject line if recipient_comment: thread_id = recipient_comment.id if thread_id: subject = u''.join([subject, ' (post #', str(thread_id), ')']) return EmailMessage(from_email=settings.UPDATES_EMAIL, to_email=notification.recipient.email, subject=subject, body_html=html, body_text=text, recipient=notification.recipient, guid=guid, unsubscribe_links=unsubscribe_links)