def get_preview_image(preview_object, include_censored=False): """Returns a media_object for rendering a media preview image""" min_width, min_height = g.preview_image_min_size max_width, max_height = g.preview_image_max_size source_width = preview_object['width'] source_height = preview_object['height'] if source_width <= max_width and source_height <= max_height: width = source_width height = source_height else: max_ratio = float(max_height) / max_width source_ratio = float(source_height) / source_width if source_ratio >= max_ratio: height = max_height width = int((height * source_width) / source_height) else: width = max_width height = int((width * source_height) / source_width) if width < min_width and height < min_height: return None url = g.image_resizing_provider.resize_image(preview_object, width) img_html = format_html( _IMAGE_PREVIEW_TEMPLATE, css_class="preview", url=url, width=width, height=height, ) if include_censored: censored_url = g.image_resizing_provider.resize_image( preview_object, width, censor_nsfw=True, ) censored_img_html = format_html( _IMAGE_PREVIEW_TEMPLATE, css_class="censored-preview", url=censored_url, width=width, height=height, ) img_html += censored_img_html media_object = { "type": "media-preview", "width": width, "height": height, "content": img_html, } return media_object
def _oembed_post(thing, **embed_options): subreddit = thing.subreddit_slow if (not can_view_link_comments(thing) or subreddit.type in Subreddit.private_types): raise ForbiddenError(errors.POST_NOT_ACCESSIBLE) live = '' if embed_options.get('live'): time = datetime.now(g.tz).isoformat() live = 'data-card-created="{}"'.format(time) script = '' if not embed_options.get('omitscript', False): script = format_html( SCRIPT_TEMPLATE, embedly_script=EMBEDLY_SCRIPT, ) link_url = UrlParser(thing.make_permalink_slow(force_domain=True)) link_url.update_query(ref='share', ref_source='embed') author_name = "" if not thing._deleted: author = thing.author_slow if author._deleted: author_name = _("[account deleted]") else: author_name = author.name html = format_html( POST_EMBED_TEMPLATE, live_data_attr=live, link_url=link_url.unparse(), title=websafe(thing.title), subreddit_url=make_url_https(subreddit.path), subreddit_name=subreddit.name, script=script, ) oembed_response = dict( _OEMBED_BASE, type="rich", title=thing.title, author_name=author_name, html=html, ) return oembed_response
def media_embed(cls, media_object): height = 500 params = {} if c.site: # play it safe when in a qproc if getattr(c.user, "pref_show_stylesheets", True): params["stylesr"] = c.site.name url = urlparse.urlunparse(( None, g.media_domain, "/live/%s/embed" % media_object["event_id"], None, urllib.urlencode(params), None, )) content = format_html(_EMBED_TEMPLATE, url=url, height=height) return MediaEmbed( height=height, width=710, content=content, sandbox=False, )
def media_embed(cls, media_object): height = 500 params = {} if c.site: # play it safe when in a qproc if (getattr(c.user, "pref_show_stylesheets", True) and not isinstance(c.site, FakeSubreddit)): params["stylesr"] = c.site.name url = urlparse.urlunparse(( None, g.media_domain, "/live/%s/embed" % media_object["event_id"], None, urllib.urlencode(params), None, )) content = format_html(_EMBED_TEMPLATE, url=url, height=height) return MediaEmbed( height=height, width=710, content=content, sandbox=False, )
def _oembed_post(thing, **embed_options): subreddit = thing.subreddit_slow if (not can_view_link_comments(thing) or subreddit.type in Subreddit.private_types): raise ForbiddenError(errors.POST_NOT_ACCESSIBLE) live = '' if embed_options.get('live'): time = datetime.now(g.tz).isoformat() live = 'data-card-created="{}"'.format(time) script = '' if not embed_options.get('omitscript', False): script = format_html(SCRIPT_TEMPLATE, embedly_script=EMBEDLY_SCRIPT, ) link_url = UrlParser(thing.make_permalink_slow(force_domain=True)) link_url.update_query(ref='share', ref_source='embed') author_name = "" if not thing._deleted: author = thing.author_slow if author._deleted: author_name = _("[account deleted]") else: author_name = author.name html = format_html(POST_EMBED_TEMPLATE, live_data_attr=live, link_url=link_url.unparse(), title=websafe(thing.title), subreddit_url=make_url_https(subreddit.path), subreddit_name=subreddit.name, script=script, ) oembed_response = dict(_OEMBED_BASE, type="rich", title=thing.title, author_name=author_name, html=html, ) return oembed_response
def _oembed_comment(thing, **embed_options): link = thing.link_slow subreddit = link.subreddit_slow if (not can_view_link_comments(link) or subreddit.type in Subreddit.private_types): raise ForbiddenError(errors.COMMENT_NOT_ACCESSIBLE) if not thing._deleted: author = thing.author_slow if author._deleted: author_name = _("[account deleted]") else: author_name = author.name title = _('%(author)s\'s comment from discussion "%(title)s"') % { "author": author_name, "title": _force_unicode(link.title), } else: author_name = "" title = "" parent = "true" if embed_options.get('parent') else "false" html = format_html( embeds.get_inject_template(embed_options.get('omitscript')), media=g.media_domain, parent=parent, live="true" if embed_options.get('live') else "false", created=datetime.now(g.tz).isoformat(), comment=thing.make_permalink_slow(force_domain=True), link=link.make_permalink_slow(force_domain=True), title=websafe(title), uuid=uuid1(), ) oembed_response = dict( _OEMBED_BASE, type="rich", title=title, author_name=author_name, html=html, ) if author_name: oembed_response['author_url'] = make_url_https('/user/' + author_name) return oembed_response
def _oembed_comment(thing, **embed_options): link = thing.link_slow subreddit = link.subreddit_slow if (not can_view_link_comments(link) or subreddit.type in Subreddit.private_types): raise ForbiddenError(errors.COMMENT_NOT_ACCESSIBLE) if not thing._deleted: author = thing.author_slow if author._deleted: author_name = _("[account deleted]") else: author_name = author.name title = _('%(author)s\'s comment from discussion "%(title)s"') % { "author": author_name, "title": _force_unicode(link.title), } else: author_name = "" title = "" parent = "true" if embed_options.get('parent') else "false" html = format_html(embeds.get_inject_template(embed_options.get('omitscript')), media=g.media_domain, parent=parent, live="true" if embed_options.get('live') else "false", created=datetime.now(g.tz).isoformat(), comment=thing.make_permalink_slow(force_domain=True), link=link.make_permalink_slow(force_domain=True), title=websafe(title), uuid=uuid1(), ) oembed_response = dict(_OEMBED_BASE, type="rich", title=title, author_name=author_name, html=html, ) if author_name: oembed_response['author_url'] = make_url_https('/user/' + author_name) return oembed_response
def _oembed_comment(thing, **embed_options): link = thing.link_slow if not can_view_link_comments(link): raise ForbiddenError("Cannot access this comment.") if not thing._deleted: author = thing.author_slow if author._deleted: author_name = _("[account deleted]") else: author_name = author.name title = _('%(author)s\'s comment from discussion "%(title)s"') % { "author": author_name, "title": link.title, } else: author_name = "" title = "" html = format_html( embeds.get_inject_template(), media=g.media_domain, parent="true" if embed_options.get('parent') else "false", live="true" if embed_options.get('live') else "false", created=datetime.now(g.tz).isoformat(), comment=thing.make_permalink_slow(force_domain=True), link=link.make_permalink_slow(force_domain=True), title=websafe(title), ) oembed_response = dict( _OEMBED_BASE, type="rich", title=title, author_name=author_name, html=html, ) if author_name: oembed_response['author_url'] = make_url_https('/user/' + author_name) return oembed_response
def _oembed_comment(thing, **embed_options): link = thing.link_slow if not can_view_link_comments(link): raise ForbiddenError("Cannot access this comment.") if not thing._deleted: author = thing.author_slow if author._deleted: author_name = _("[account deleted]") else: author_name = author.name title = _('%(author)s\'s comment from discussion "%(title)s"') % { "author": author_name, "title": _force_unicode(link.title), } else: author_name = "" title = "" html = format_html(embeds.get_inject_template(), media=g.media_domain, parent="true" if embed_options.get('parent') else "false", live="true" if embed_options.get('live') else "false", created=datetime.now(g.tz).isoformat(), comment=thing.make_permalink_slow(force_domain=True), link=link.make_permalink_slow(force_domain=True), title=websafe(title), ) oembed_response = dict(_OEMBED_BASE, type="rich", title=title, author_name=author_name, html=html, ) if author_name: oembed_response['author_url'] = make_url_https('/user/' + author_name) return oembed_response
def POST_promote_note(self, form, jquery, link, note): if promote.is_promo(link): text = PromotionLog.add(link, note) form.find(".notes").children(":last").after( format_html("<p>%s</p>", text))