def __unicode__(self): word_history_cls = globals()['Word'].__history_mapper__.class_ word_1 = word_history_cls.query.filter_by(id=self.word_id_1) word_2 = word_history_cls.query.filter_by(id=self.word_id_2) word_1 = word_1.filter( word_history_cls.created<self.created+datetime.timedelta(seconds=60) ).order_by('word_history.created DESC').first() language_1 = Language.query.filter_by(id=word_1.language_id).first() word_2 = word_2.filter( word_history_cls.created<self.created+datetime.timedelta(seconds=60) ).order_by('word_history.created DESC').first() language_2 = Language.query.filter_by(id=word_2.language_id).first() # I don't expect the part ID to change relation = Relation.query.filter_by(id=self.relation_id).first() markup = Markup( u'<a href="{0}">{1}</a> <em>{2} of</em> <a href="{3}">{4}</a>' ) markup = markup.format( url_for('words.view', word_data=word_1.word, language_code=language_1.code), word_1.word, relation.part.label.lower(), url_for('words.view', word_data=word_2.word, language_code=language_2.code), word_2.word ) return markup
def toRSSItem(self): title = self.repo.tagname if self.message and len(self.message) > 50: title += " - " + str(Markup.escape(self.message[:50])) + "..." elif self.message: title += " - " + str(Markup.escape(self.message)) if self.dbkeywords: title += " - " + ",".join(self.dbkeywords) description = "<pre>" description += str(self.getpprint(True)) description += "</pre>" if type(title) != unicode: title = unicode(title, 'utf-8') if type(description) != unicode: description = unicode(description, 'utf-8') title = unicodedata.normalize('NFKD', title).encode('ascii', 'ignore') description = unicodedata.normalize('NFKD', description).encode('ascii', 'ignore') guid = Config.rooturl + "/commit/" + self.repo.tagname + "/" + self.uniqueid link = '' if self.repo.viewlink: link = self.repo.viewlink.replace('%ID', self.uniqueid) else: link = guid item = RSSItem( title = title, link = link, description = description, guid = Guid(guid, isPermaLink=0), pubDate = unixToDatetime(self.date) ) return item
def generate_feed(gallery, medias, feed_type=None, feed_url='', nb_items=0): root_album = gallery.albums['.'] cls = Rss201rev2Feed if feed_type == 'rss' else Atom1Feed feed = cls( title=Markup.escape(root_album.title), link='/', feed_url=feed_url, description=Markup.escape(root_album.description).striptags() ) nb_medias = len(medias) nb_items = min(nb_items, nb_medias) if nb_items > 0 else nb_medias for item in medias[:nb_items]: feed.add_item( title=Markup.escape(item.title or item.url), link='%s/#%s' % (item.path, item.url), # unique_id='tag:%s,%s:%s' % (urlparse(link).netloc, # item.date.date(), # urlparse(link).path.lstrip('/')), description='<img src="%s/%s" />' % (item.path, item.thumbnail), # categories=item.tags if hasattr(item, 'tags') else None, author_name=getattr(item, 'author', ''), pubdate=item.date or datetime.now(), ) output_file = os.path.join(root_album.dst_path, feed_url.split('/')[-1]) logger.info('Generate %s feeds: %s', feed_type.upper(), output_file) encoding = 'utf-8' if not compat.PY2 else None with codecs.open(output_file, 'w', encoding) as f: feed.write(f, 'utf-8')
def get_links(self): """get all the news links in the page """ soup = BeautifulSoup(self.page) vote = 0 infos = [] links = [] for link in soup.find_all('a'): l = link['href'] if l.startswith('vote'): vote = 1 elif vote == 1: if l.startswith("item"): l = "%s/%s" % (self.surl, l) infos = [Markup.escape(link.string), Markup.escape(l.strip()), date_internet(datetime.now())] time.sleep(1) vote = 2 elif l.startswith('item') and vote == 2: infos.append("%s/%s" % (self.surl, l)) infos.append(uuid3(NAMESPACE_DNS, infos[1])) links.append(infos) vote = 0 return links
def test_markup_operations(self): # adding two strings should escape the unsafe one unsafe = '<script type="application/x-some-script">alert("foo");</script>' safe = Markup('<em>username</em>') assert unsafe + safe == text_type(escape(unsafe)) + text_type(safe) # string interpolations are safe to use too assert Markup('<em>%s</em>') % '<bad user>' == \ '<em><bad user></em>' assert Markup('<em>%(username)s</em>') % { 'username': '******' } == '<em><bad user></em>' # an escaped object is markup too assert type(Markup('foo') + 'bar') is Markup # and it implements __html__ by returning itself x = Markup("foo") assert x.__html__() is x # it also knows how to treat __html__ objects class Foo(object): def __html__(self): return '<em>awesome</em>' def __unicode__(self): return 'awesome' assert Markup(Foo()) == '<em>awesome</em>' assert Markup('<strong>%s</strong>') % Foo() == \ '<strong><em>awesome</em></strong>' # escaping and unescaping assert escape('"<>&\'') == '"<>&'' assert Markup("<em>Foo & Bar</em>").striptags() == "Foo & Bar" assert Markup("<test>").unescape() == "<test>"
def slugify(value): """ Normalizes string, converts to lowercase, removes non-alpha characters, and converts spaces to hyphens. Took from django sources. """ # TODO Maybe steal again from current Django 1.5dev value = Markup(value).striptags() # value must be unicode per se import unicodedata from unidecode import unidecode # unidecode returns str in Py2 and 3, so in Py2 we have to make # it unicode again value = unidecode(value) if isinstance(value, six.binary_type): value = value.decode("ascii") # still unicode value = unicodedata.normalize("NFKD", value) value = re.sub("[^\w\s-]", "", value).strip().lower() value = re.sub("[-\s]+", "-", value) # we want only ASCII chars value = value.encode("ascii", "ignore") # but Pelican should generally use only unicode return value.decode("ascii")
def slugify(value, substitutions=()): """ Normalizes string, converts to lowercase, removes non-alpha characters, and converts spaces to hyphens. Took from Django sources. """ # TODO Maybe steal again from current Django 1.5dev value = Markup(value).striptags() # value must be unicode per se import unicodedata from unidecode import unidecode # unidecode returns str in Py2 and 3, so in Py2 we have to make # it unicode again value = unidecode(value) if isinstance(value, six.binary_type): value = value.decode('ascii') # still unicode value = unicodedata.normalize('NFKD', value).lower() for src, dst in substitutions: value = value.replace(src.lower(), dst.lower()) value = re.sub('[^\w\s-]', '', value).strip() value = re.sub('[-\s]+', '-', value) # we want only ASCII chars value = value.encode('ascii', 'ignore') # but Pelican should generally use only unicode return value.decode('ascii')
def newline_to_br(eval_ctx, value): """Replaces newline characters with <br> tags Adapted from http://flask.pocoo.org/snippets/28/ """ result = u"\n\n".join(u"<p>%s</p>" % p.replace("\n", Markup("<br>\n")) for p in PARAGRAPH.split(escape(value))) if eval_ctx and eval_ctx.autoescape: result = Markup(result) return result.lstrip()
def newline_to_br(eval_ctx, value): '''Replaces newline characters with <br> tags Adapted from http://flask.pocoo.org/snippets/28/ ''' result = u'\n\n'.join(u'<p>%s</p>' % p.replace('\n', '<br>\n') for p in PARAGRAPH.split(value)) if eval_ctx and eval_ctx.autoescape: result = Markup(result) return result.lstrip()
def error_handler(error): if not isinstance(error, HTTPException): raise error if request_wants_json(): desc = error.get_description(request.environ) desc = Markup(desc[:desc.find('.')]).striptags() error_str = '%s: %s' % (error.name, desc) response = jsonify(status='fail', error=error_str) response.status_code = error.code return response return error
def __unicode__(self): word_history_cls = globals()['Word'].__history_mapper__.class_ part = Part.query.filter_by(id=self.part_id).first() word = word_history_cls.query.filter(word_history_cls.id==self.word_id)\ .filter(word_history_cls.version==self.version).first() language = Language.query.filter(Language.id==self.language_id).first() markup = Markup(u'Added {0} to <a href="{1}">{2}</a> for translations') markup = markup.format(language.label, url_for( 'words.view', word_data=word.word, language_code=language.code), word.word) return markup
def install(request): addon_id = request.GET.get('addon_id', None) if addon_id: try: addon_id = int(addon_id) except ValueError: addon_id = Markup.escape(addon_id) addon_key = request.GET.get('addon_key', None) addon_name = request.GET.get('addon_name', None) if addon_id in addons: addon = addons[addon_id] elif addon_key in addons: addon = addons[addon_key] elif addon_name and addon_id: xpi = prefix('/en-US/firefox/downloads/latest/%s') % addon_id icon = prefix('/en-US/firefox/images/addon_icon/%s') % addon_id addon = {'name': addon_name, 'xpi': xpi, 'icon': icon} else: return HttpResponseNotFound() addon_link = addon.get('link', None) if addon_link: return HttpResponsePermanentRedirect(addon_link) if 'xpi' not in addon: return HttpResponseNotFound() src = request.GET.get('src', 'installservice') addon['xpi'] = urlparams(addon['xpi'], src=src) addon_params = {'URL': addon['xpi']} if 'icon' in addon: addon_params['IconURL'] = addon['icon'] if 'hash' in addon: addon_params['Hash'] = addon['hash'] referrers = ' || '.join(addon.get('referrers', default_referrers)) return render(request, 'services/install.html', {'referrers': referrers, 'addon': addon, 'params': json.dumps({'name': addon_params})})
def get_notes(task_id): ''' Get one or more analyst notes/comments associated with the specified task. ''' task = db.get_task(task_id) if not task: abort(HTTP_NOT_FOUND) if 'ts' in request.args and 'uid' in request.args: ts = request.args.get('ts', '') uid = request.args.get('uid', '') response = handler.get_notes(task.sample_id, [ts, uid]) else: response = handler.get_notes(task.sample_id) if not response: abort(HTTP_BAD_REQUEST) if 'hits' in response and 'hits' in response['hits']: response = response['hits']['hits'] try: for hit in response: hit['_source']['text'] = Markup.escape(hit['_source']['text']) except Exception as e: # TODO: log exception pass return jsonify(response)
def get_default_meta_description(cls, article): summary = Markup(article.summary).striptags() description = textwrap.wrap(summary, META_DESCRIPTION_LENGTH)[0] description = Markup.escape(description) if len(summary) > META_DESCRIPTION_LENGTH: return description + '...' else: return description
def _convert_out(v, o=None): if getattr(filter_func, "is_safe", False) and isinstance(o, SafeData): v = mark_safe(v) elif isinstance(o, EscapeData): v = mark_for_escaping(v) if isinstance(v, SafeData): return Markup(v) if isinstance(v, EscapeData): return Markup.escape(v) # not 100% equivalent, see mod docs return v
def get_escaped_var_value(value): """ Encodes XML reserved chars in value (eg. &, <, >) and also replaces the control chars \n and \t control chars to their ODF counterparts. """ value = Markup.escape(value) return ( value.replace('\n', Markup('<text:line-break/>')) .replace('\t', Markup('<text:tab/>')) .replace('\x0b', '<text:space/>') .replace('\x0c', '<text:space/>') )
def tweet_content(s): def user_url(match): user_id = match.group(1) return Markup(u'<a href="%s">@%s</a>') % (Markup.escape(url_for('user', user_id=user_id.lower())), Markup.escape(user_id)) def tag_url(match): tag_id = match.group(1) return Markup(u'<a href="%s">#%s</a>') % (Markup.escape(url_for('tag', tag_id=tag_id.lower())), Markup.escape(tag_id)) content = Markup.escape(s) content = Markup(re.sub(r'@([a-zA-Z]+)', user_url, content)) content = Markup(re.sub(r'(?<!&)#([a-zA-Z0-9_]+)', tag_url, content)) return content
def create_meta_attribute(cls, article): article.meta = { 'canonical': cls.get_canonical(article), } for key in META_ATTRIBUTES: article_attrib = "meta_%s" % key if hasattr(article, article_attrib): meta_value = Markup.escape(getattr(article, article_attrib)) else: meta_value = getattr(cls, "get_default_%s" % article_attrib)(article) article.meta[key] = meta_value
def getpprint(self, htmlize=False): if not self.initialized: raise Exception("called getpprint on unitialized Commit object") if htmlize: process = lambda x : Markup.escape(x) else: process = lambda x : x eol = "\r\n" s = "" s += "Project:\t %s%s" % (self.repo.name, eol) if htmlize: s += "Project URL:\t <a href=\"%s\">%s</a>%s" % (self.repo.url, self.repo.url, eol) else: s += "Project URL:\t %s %s" % (self.repo.url, eol) s += "Commit Date:\t %s (%s)%s" % (unixToGitDateFormat(self.date), self.date, eol) s += "Log Message:\t %s%s" % (process(self.message), eol) s += eol + eol if self.files: s += "Files:\t\t %s%s" % (process(self.files[0]), eol) for p in self.files[1:14]: s += "\t\t %s%s" % (process(p), eol) if len(self.files) > 15: s += "\t\t ...%s" % (eol) if self.base_paths: plural = len(self.base_paths) > 1 s += "Base Path%s:\t %s%s" % ("s" if plural else "", process(self.base_paths[0]), eol) for p in self.base_paths[1:]: s += "\t\t %s%s" % (process(p), eol) s += "Keywords:\t %s%s" % (", ".join(self.keywords), eol) s += "ID:\t\t %s%s" % (self.uniqueid, eol) internallink = Config.rooturl + "/commit/" + self.repo.tagname + "/" + self.uniqueid if htmlize: s += "Internal:\t <a href=\"%s\">%s</a>%s" % (internallink, internallink, eol) else: s += "Internal:\t %s%s" % (internallink, eol) if self.repo.viewlink: externallink = self.repo.viewlink.replace('%ID', self.uniqueid) if htmlize: s += "External:\t <a href=\"%s\">%s</a>%s" % (externallink, externallink, eol) else: s += "External:\t %s%s" % (externallink, eol) if htmlize: return Markup(s) else: return s
def _render(self, context): result = { 'filters': sorted(self.environment.filters.keys()), 'tests': sorted(self.environment.tests.keys()), 'context': context.get_all() } # # We set the depth since the intent is basically to show the top few # names. TODO: provide user control over this? # if sys.version_info[:2] >= (3, 4): text = pprint.pformat(result, depth=3, compact=True) else: text = pprint.pformat(result, depth=3) text = Markup.escape(text) return text
def render(self, show_title=False, extra_css=None, closing_tag=True, tag='a', **kw): title = kw.get('title') or self.title attrs = { 'title': title, 'class': ' '.join(['icon', extra_css or '']).strip(), } if tag == 'a': attrs['href'] = '#' attrs.update(kw) attrs = ew._Jinja2Widget().j2_attrs(attrs) visible_title = u'' if show_title: visible_title = u' {}'.format(Markup.escape(title)) closing_tag = u'</{}>'.format(tag) if closing_tag else u'' icon = u'<{} {}><i class="{}"></i>{}{}'.format(tag, attrs, self.css, visible_title, closing_tag) return Markup(icon)
def _list_entry(self, context, model, name): parsed_url = urlparse(model.url) netloc, scheme = parsed_url.netloc, parsed_url.scheme is_scheme_valid = scheme in ('http', 'https') tag_text = [] tag_tmpl = '<a class="btn btn-default" href="{1}">{0}</a>' for tag in model.tags.split(','): if tag: tag_text.append(tag_tmpl.format(tag, url_for( 'bookmark.index_view', flt2_tags_contain=tag))) if not netloc: return Markup("""\ {0.title}<br/>{2}<br/>{1}{0.description} """.format( model, ''.join(tag_text), Markup.escape(model.url) )) netloc_tmpl = '<img src="{}{}"/> ' res = netloc_tmpl.format( 'http://www.google.com/s2/favicons?domain=', netloc) title = model.title if model.title else '<EMPTY TITLE>' if is_scheme_valid: res += '<a href="{0.url}">{1}</a>'.format(model, title) else: res += title if self.url_render_mode == 'netloc': res += ' (<a href="{1}">{0}</a>)'.format( netloc, url_for('bookmark.index_view', flt2_url_netloc_match=netloc) ) res += '<br/>' if not is_scheme_valid: res += model.url elif self.url_render_mode is None or self.url_render_mode == 'full': res += '<a href="{0.url}">{0.url}</a>'.format(model) res += '<br/>' if self.url_render_mode != 'netloc': res += tag_tmpl.format( 'netloc:{}'.format(netloc), url_for('bookmark.index_view', flt2_url_netloc_match=netloc) ) res += ''.join(tag_text) description = model.description if description: res += '<br/>' res += description.replace('\n', '<br/>') return Markup(res)
def streamer_page(streamer_name, page): streamer = Streamer.query.filter_by(reddit_username=streamer_name).first_or_404() wpc_stream = streamer.streams.filter_by(type='wpc_stream').first() streams = streamer.streams if wpc_stream: streams = streams.filter(Stream.id != wpc_stream.id) streams = streams.order_by(Stream.actual_start_time.desc().nullslast()).paginate(page, per_page=5) info_form = EditStreamerInfoForm(prefix='info') title_form = EditStreamTitleForm(prefix='title') if current_user.is_authenticated() and current_user == streamer: if request.method == 'POST': if info_form.submit_button.data: if info_form.validate_on_submit(): current_user.populate(info_form) db.session.commit() flash("Updated successfully", category='success') return redirect(url_for('.streamer_page', streamer_name=streamer_name, page=page)) else: return render_template('streamer.html', streamer=streamer, streams=streams, info_form=info_form, title_form=title_form, edit_info=True, edit_title=False, wpc_stream=wpc_stream) elif title_form.submit_button.data: if title_form.validate_on_submit(): wpc_stream.title = title_form.title.data db.session.commit() return jsonify(newTitle=Markup.escape(title_form.title.data)) else: return render_template('streamer.html', streamer=streamer, streams=streams, info_form=info_form, title_form=title_form, edit_info=False, edit_title=True, wpc_stream=wpc_stream) else: info_form.youtube_channel.data = current_user.youtube_channel info_form.twitch_channel.data = current_user.twitch_channel info_form.info.data = current_user.info if wpc_stream: title_form.title.data = wpc_stream.title return render_template('streamer.html', streamer=streamer, streams=streams, info_form=info_form, title_form=title_form, edit_info=False, edit_title=False, wpc_stream=wpc_stream)
def slugify(value, substitutions=()): """ Normalizes string, converts to lowercase, removes non-alpha characters, and converts spaces to hyphens. Took from Django sources. """ # TODO Maybe steal again from current Django 1.5dev value = Markup(value).striptags() # value must be unicode per se import unicodedata from unidecode import unidecode # unidecode returns str in Py2 and 3, so in Py2 we have to make # it unicode again value = unidecode(value) if isinstance(value, six.binary_type): value = value.decode('ascii') # still unicode value = unicodedata.normalize('NFKD', value).lower() # backward compatible covert from 2-tuples to 3-tuples new_subs = [] for tpl in substitutions: try: src, dst, skip = tpl except ValueError: src, dst = tpl skip = False new_subs.append((src, dst, skip)) substitutions = tuple(new_subs) # by default will replace non-alphanum characters replace = True for src, dst, skip in substitutions: orig_value = value value = value.replace(src.lower(), dst.lower()) # if replacement was made then skip non-alphanum # replacement if instructed to do so if value != orig_value: replace = replace and not skip if replace: value = re.sub(r'[^\w\s-]', '', value).strip() value = re.sub(r'[-\s]+', '-', value) else: value = value.strip() # we want only ASCII chars value = value.encode('ascii', 'ignore') # but Pelican should generally use only unicode return value.decode('ascii')
def share_fundopp(self, request, id_=None): '''Import information about a funding opportunity into the index.''' tmpl = util.clean_list(request.values.getlist('useful_links[]')) useful_links = [] for link in tmpl: useful_links.append(util.prep_link(link)) if not id_: id_ = util.slug_id(request.values["title"]) record = { "funder": request.values.get("funder", ''), "title": request.values["title"], "residency": request.values.get("residency"), "gender": request.values.get("gender"), "id": id_, "url": util.prep_link(request.values.get("url",'')), "description": Markup.escape(request.values.get("more_info",'')), "issue_date": request.values.get('issue_date',None), "closing_date": request.values.get('closing_date',None), "funds": request.values.get('funds',''), "funds_exactly_or_upto": request.values.get('funds_exactly_or_upto',''), "useful_links": useful_links, "tags": util.clean_list(request.values.get("tags",'').split(",")), "subjects": util.clean_list(request.values.get("subjects",'').split(",")), "created": datetime.now().isoformat(), "modified": datetime.now().isoformat(), "owner": self.owner.id, "license": CROWDSOURCE_CONTRIB_LICENSE, "origin": "Crowdsourced", "origin_method": "crowdsourced" } # cause ElasticSearch exceptions if null or empty string # so just remove them from the document if not record['issue_date']: del record['issue_date'] if not record['closing_date']: del record['closing_date'] fundfind.dao.FundingOpp.upsert(record) return id_
def contact_form(): is_modal = request.args.get('modal', default=0, type=int) if request.method == "GET": return render_template("contact.html", is_modal=is_modal) field_src = request.args if request.method == "POST": field_src = request.form msg = {"msg": "Your message has been registred.", "category": "success"} try: contact = dict( user_agent = str(request.user_agent), remote_addr = request.remote_addr, created = arrow.utcnow().datetime, fullName = field_src.get('fullName'), companyName = field_src.get('companyName'), subject = field_src.get('subject'), email = field_src.get('email'), message = Markup.escape(field_src.get('message')) ) queries.col_contact().insert(contact) #flash("Your message has been registred.", "success") message = Message("Widukind - new contact from [%s]" % contact['email'], sender=current_app.config.get('MAIL_DEFAULT_SENDER'), recipients=[current_app.config.get('MAIL_ADMINS')]) message.html = '<a href="%s">Admin contacts</a>' % url_for('admin.contacts', _external=True) try: mail.send(message) except Exception as err: current_app.logger.fatal(str(err)) except Exception as err: #flash("Sorry, An unexpected error has occurred. Your message has not registred.", "error") msg = {"msg": "Sorry, An unexpected error has occurred. Your message has not registred.", "category": "error"} current_app.logger.fatal(str(err)) return jsonify({"notify": msg, "redirect": url_for('home', _external=True)})
def comment_reply(request, type, parent_id): parent_comment = Comment.objects.get(id=parent_id) response = {} if not parent_comment: response['error'] = 'Parent comment not found' if request.POST: form = CommentForm(request.POST) if form.is_valid(): comment = CommentForm(request.POST).save(commit=False) comment.thread_id, comment.reverse_thread_id = \ get_thread_id(type, parent_comment.thing_id, parent_comment) comment.author = request.user comment.thing_id = parent_comment.thing_id comment.type = type comment.parent_id = parent_comment.id comment.save() response['content'] = Markup.unescape( Markup( render_to_string(request, 'comments/comment.html', {'comment': comment}) ) ) return render_to_remote_response(request, json_context=response)
def test_forceescape(self): tmpl = env.from_string('{{ x|forceescape }}') assert tmpl.render(x=Markup('<div />')) == u'<div />'
from datacube.index.fields import Field from datacube.model import Dataset, DatasetType, Range from . import _utils as utils # How far to step the number when the user hits up/down. NUMERIC_STEP_SIZE = { "numeric-range": 0.001, "double-range": 0.001, "integer-range": 1, "numeric": 0.001, "double": 0.001, "integer": 1, } CROSS_SYMBOL = Markup('<i class="fa fa-times" aria-label="x"></i>') _LOG = logging.getLogger(__name__) bp = Blueprint("filters", __name__) @bp.app_template_filter("printable_time") def _format_datetime(date): return date.strftime("%Y-%m-%d %H:%M:%S") @bp.app_template_filter("printable_dataset") def _dataset_label(dataset): label = utils.dataset_label(dataset) # If archived, strike out the label. if dataset.archived_time:
def search(self): result = self._check_access_permissions() if result is not None: return result check_slug(self.group, self.request) result = super(GroupSearchController, self).search() result['opts'] = {'search_groupname': self.group.name} if self.request.user not in self.group.members: return result def user_annotation_count(aggregation, userid): for user in aggregation: if user['user'] == userid: return user['count'] return 0 q = query.extract(self.request) users_aggregation = result['search_results'].aggregations.get('users', []) members = [{'username': u.username, 'userid': u.userid, 'count': user_annotation_count(users_aggregation, u.userid), 'faceted_by': _faceted_by_user(self.request, u.username, q)} for u in self.group.members] members = sorted(members, key=lambda k: k['username'].lower()) group_annotation_count = None if self.request.feature('total_shared_annotations'): group_annotation_count = self.request.find_service(name='annotation_stats').group_annotation_count(self.group.pubid) result['stats'] = { 'annotation_count': group_annotation_count, } result['group'] = { 'created': self.group.created.strftime('%B, %Y'), 'description': self.group.description, 'name': self.group.name, 'pubid': self.group.pubid, 'url': self.request.route_url('group_read', pubid=self.group.pubid, slug=self.group.slug), 'members': members, } if self.request.has_permission('admin', self.group): result['group_edit_url'] = self.request.route_url( 'group_edit', pubid=self.group.pubid) result['more_info'] = 'more_info' in self.request.params if not result.get('q'): result['zero_message'] = Markup(_( 'The group “{name}” has not made any annotations yet.').format( name=Markup.escape(self.group.name))) return result
def jsonify_2_script(data): return Markup( anyjson.serialize(data).replace('<script>', '<scri"+"pt>').replace( '</script>', '</scri"+"pt>'))
def utcnow(): return Markup(datetime.datetime.utcnow())
def nl2br(eval_ctx, value): result = u'\n\n'.join(u'<p>%s</p>' % p.replace('\n', '<br>\n') \ for p in _paragraph_re.split(escape(value))) if eval_ctx.autoescape: result = Markup(result) return result
def s2md(text): return Markup( markdown(text, extras=['fenced-code-blocks', 'hilite', 'tables']))
def _render(self, csrf_token): from django.template.defaulttags import CsrfTokenNode return Markup(CsrfTokenNode().render({'csrf_token': csrf_token}))
def render_data(self): return Markup('<br>'.join(self.data or []))
def nl2br(value): result = u'\n\n'.join(u'<p>%s</p>' % p.replace('\n', Markup('<br>\n')) for p in _paragraph_re.split(escape(value))) return result
def _summary_formatter(view, context, model, name): return Markup("%s" % (model.summary)) if model.summary else ""
def test_escaped(self): env = Environment(autoescape=True) tmpl = env.from_string('{{ x is escaped }}|{{ y is escaped }}') assert tmpl.render(x='foo', y=Markup('foo')) == 'False|True'
def public_email(email): """Email address -> publicly displayable email.""" return Markup('<span class="email">%s</span>' % unicode_to_html(email))
def include_jquery(version=None): if version is None: version = current_app.config.get('TOASTR_JQUERY_VERSION') js = ('<script src="//code.jquery.com/' + 'jquery-{0}.min.js"></script>'.format(version)) return Markup(js)
def _prerun_notes_formatter(view, context, model, name): return Markup("%s" % (model.prerun_notes)) if model.prerun_notes else ""
def index(): c = bar_base() return Markup(c.render_embed())
def render(value): return Markup(self.render_markdown(value))
def f_tex2utf(text: JinjaFilterInput, greek: bool = True) -> Markup: """Return output of tex2utf function as escaped Markup.""" if isinstance(text, Markup): return escape(tex2utf(text.unescape(), greek=greek)) else: return Markup(escape(tex2utf(text, greek=greek)))
def embed_content(path: str) -> Markup: """Embed the content of a static file.""" stat = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static') with open(os.path.join(stat, path)) as f: return Markup(f.read())
def search(self): result = self._check_access_permissions() if result is not None: return result check_slug(self.group, self.request) result = super(GroupSearchController, self).search() result['opts'] = {'search_groupname': self.group.name} # If the group has read access only for members and the user is not in that list # return without extra info. if self.group.readable_by == ReadableBy.members and (self.request.user not in self.group.members): return result def user_annotation_count(aggregation, userid): for user in aggregation: if user['user'] == userid: return user['count'] return 0 q = query.extract(self.request) members = [] moderators = [] users_aggregation = result['search_results'].aggregations.get('users', []) # If the group has members provide a list of member info, # otherwise provide a list of moderator info instead. if self.group.members: members = [{'username': u.username, 'userid': u.userid, 'count': user_annotation_count(users_aggregation, u.userid), 'faceted_by': _faceted_by_user(self.request, u.username, q)} for u in self.group.members] members = sorted(members, key=lambda k: k['username'].lower()) else: moderators = [] if self.group.creator: # Pass a list of moderators, anticipating that [self.group.creator] # will change to an actual list of moderators at some point. moderators = [{'username': u.username, 'userid': u.userid, 'count': user_annotation_count(users_aggregation, u.userid), 'faceted_by': _faceted_by_user(self.request, u.username, q)} for u in [self.group.creator]] moderators = sorted(moderators, key=lambda k: k['username'].lower()) group_annotation_count = self.request.find_service(name='annotation_stats').group_annotation_count(self.group.pubid) result['stats'] = { 'annotation_count': group_annotation_count, } result['group'] = { 'created': utc_us_style_date(self.group.created), 'description': self.group.description, 'name': self.group.name, 'pubid': self.group.pubid, 'url': self.request.route_url('group_read', pubid=self.group.pubid, slug=self.group.slug), 'share_subtitle': _('Share group'), 'share_msg': _('Sharing the link lets people view this group:'), 'organization': {'name': self.group.organization.name, 'logo': self._organization_context.logo} } if self.group.type == 'private': result['group']['share_subtitle'] = _('Invite new members') result['group']['share_msg'] = _('Sharing the link lets people join this group:') result['group_users_args'] = [ _('Members'), moderators if self.group.type == 'open' else members, self.group.creator.userid if self.group.creator else None, ] if self.request.has_permission('admin', self.group): result['group_edit_url'] = self.request.route_url( 'group_edit', pubid=self.group.pubid) result['more_info'] = 'more_info' in self.request.params if not result.get('q'): result['zero_message'] = Markup(_( 'The group “{name}” has not made any annotations yet.').format( name=Markup.escape(self.group.name))) result['show_leave_button'] = self.request.user in self.group.members return result
def search(self): # pylint: disable=too-complex result = self._check_access_permissions() if result is not None: return result check_slug(self.group, self.request) result = super().search() result["opts"] = {"search_groupname": self.group.name} # If the group has read access only for members and the user is not in that list # return without extra info. if self.group.readable_by == ReadableBy.members and ( self.request.user not in self.group.members): return result def user_annotation_count(aggregation, userid): for user in aggregation: if user["user"] == userid: return user["count"] return 0 members = [] moderators = [] users_aggregation = result["search_results"].aggregations.get( "users", []) # If the group has members provide a list of member info, # otherwise provide a list of moderator info instead. if self.group.members: members = [{ "username": u.username, "userid": u.userid, "count": user_annotation_count(users_aggregation, u.userid), "faceted_by": _faceted_by_user(self.request, u.username, self.parsed_query_params), } for u in self.group.members] members = sorted(members, key=lambda k: k["username"].lower()) else: moderators = [] if self.group.creator: # Pass a list of moderators, anticipating that [self.group.creator] # will change to an actual list of moderators at some point. moderators = [{ "username": u.username, "userid": u.userid, "count": user_annotation_count(users_aggregation, u.userid), "faceted_by": _faceted_by_user(self.request, u.username, self.parsed_query_params), } for u in [self.group.creator]] moderators = sorted(moderators, key=lambda k: k["username"].lower()) group_annotation_count = self._get_total_annotations_in_group( result, self.request) result["stats"] = {"annotation_count": group_annotation_count} result["group"] = { "created": utc_us_style_date(self.group.created), "description": self.group.description, "name": self.group.name, "pubid": self.group.pubid, "url": self.request.route_url("group_read", pubid=self.group.pubid, slug=self.group.slug), "share_subtitle": _("Share group"), "share_msg": _("Sharing the link lets people view this group:"), } if self.group.organization: result["group"]["organization"] = OrganizationJSONPresenter( self.group.organization, self.request).asdict(summary=True) else: result["group"]["organization"] = None if self.group.type == "private": result["group"]["share_subtitle"] = _("Invite new members") result["group"]["share_msg"] = _( "Sharing the link lets people join this group:") result["group_users_args"] = [ _("Members"), moderators if self.group.type == "open" else members, self.group.creator.userid if self.group.creator else None, ] if self.request.has_permission("admin", self.group): result["group_edit_url"] = self.request.route_url( "group_edit", pubid=self.group.pubid) result["more_info"] = "more_info" in self.request.params if not result.get("q"): result["zero_message"] = Markup( _("The group “{name}” has not made any annotations yet."). format(name=Markup.escape(self.group.name))) result["show_leave_button"] = self.request.user in self.group.members return result
def description(): return (icon("github") + Markup(" GitHub commits ") + icon("caret-right") + Markup(" builds.sr.ht jobs"))
def _product_link(product_name): url = flask.url_for("overview_page", product_name=product_name) return Markup(f"<a href='{url}' class='product-name'>{product_name}</a>")
from flask import Blueprint, url_for, redirect, request, render_template, abort from flask_security.decorators import login_required, roles_required from flask_security.utils import login_user, logout_user, hash_password from flask_security.core import current_user from flask_login import login_user, logout_user from flask_mail import Message from itsdangerous.url_safe import URLSafeTimedSerializer from itsdangerous.exc import BadSignature, SignatureExpired from jinja2 import Markup from datetime import datetime confirm_time = 3 # hours invalid_token_markup = Markup(f"""<h3>Время для подтверждения истекло</h3><br> Проведите повторную регистрацию для подтверждения""") signer = URLSafeTimedSerializer(secret_key=app.config.get("SECRET_KEY")) bp_auth = Blueprint('auth', __name__, url_prefix='/auth', template_folder="templates") # !!!! Переделать - убрать тут и добавить в html def collect_warnings(form_errors) -> list: """Collect all warning in form and return :returns list of strings""" warnings = [] for errors in form_errors.values(): warnings += errors return warnings
def _fast_tojson(obj): return Markup(rapidjson.dumps(obj))
def _description_formatter(view, context, model, name): return Markup("%s" % (model.description)) if model.description else ""
def render_data(self): return Markup('<a href="mailto:%s">%s</a>' % (self.data, self.data))
def _run_info_formatter(view, context, model, name): return Markup("%s" % (model.run_info)) if model.run_info else ""
def _dependencies_formatter(view, context, model, name): return Markup("%s" % (model.dependencies)) if model.dependencies else ""
def _note_formatter(view, context, model, name): return Markup("%s" % (model.note)) if model.note else ""
def tojson_filter(obj, **kwargs): return Markup(htmlsafe_dumps(obj, **kwargs))
def nl2br(eval_ctx, value): value = Markup.escape(value) result = re.sub(_paragraph_re, Markup("<br/>"), value) if eval_ctx.autoescape: result = Markup(result) return result
def _storage_formatter(view, context, model, name): return Markup("%s" % (model.storage)) if model.storage else ""