def _draw_item(self, canvas, item, tpl_data, content, margin_x, margin_y): font_size = _extract_font_size(item['font_size']) styles = { 'alignment': ALIGNMENTS[item['text_align']], 'textColor': item.get('color') or '#000000', 'backColor': item.get('background_color') or None, 'borderPadding': (0, 0, 4, 0), 'fontSize': font_size, 'leading': font_size } if item['bold'] and item['italic']: styles['fontName'] = FONT_STYLES[item['font_family']][3] elif item['italic']: styles['fontName'] = FONT_STYLES[item['font_family']][2] elif item['bold']: styles['fontName'] = FONT_STYLES[item['font_family']][1] else: styles['fontName'] = FONT_STYLES[item['font_family']][0] for update in values_from_signal( signals.event.designer.update_badge_style.send(self.template, item=item, styles=styles), as_list=True): styles.update(update) style = ParagraphStyle({}) for key, value in styles.items(): setattr(style, key, value) item_x = float(item['x']) / PIXELS_CM * cm item_y = float(item['y']) / PIXELS_CM * cm item_width = item['width'] / PIXELS_CM * cm item_height = (item['height'] / PIXELS_CM * cm) if item.get('height') is not None else None if isinstance(content, Image.Image): canvas.drawImage(ImageReader(content), margin_x + item_x, self.height - margin_y - item_height - item_y, item_width, item_height, mask='auto') else: content = strip_tags(content) for line in content.splitlines(): p = Paragraph(line, style) available_height = (tpl_data.height_cm - (item_y / PIXELS_CM)) * cm w, h = p.wrap(item_width, available_height) if w > item_width or h > available_height: # TODO: add warning pass p.drawOn(canvas, margin_x + item_x, self.height - margin_y - item_y - h) item_y += h
def __init__(self, rh, event_, **kwargs): assert event_ == kwargs.setdefault('event', event_) self.event = event_ WPDecorated.__init__(self, rh, **kwargs) start_dt_local = event_.start_dt_display.astimezone(event_.display_tzinfo) end_dt_local = event_.end_dt_display.astimezone(event_.display_tzinfo) dates = ' ({})'.format(to_unicode(format_date(start_dt_local, format='long'))) if start_dt_local.date() != end_dt_local.date(): if start_dt_local.year == end_dt_local.year and start_dt_local.month == end_dt_local.month: dates = ' ({}-{})'.format(start_dt_local.day, to_unicode(format_date(end_dt_local, format='long'))) else: dates = ' ({} - {})'.format(to_unicode(format_date(start_dt_local, format='long')), to_unicode(format_date(end_dt_local, format='long'))) self.title = '{} {}'.format(strip_tags(self.event.title), dates) page_title = kwargs.get('page_title') if page_title: self.title += ': {}'.format(strip_tags(page_title))
def _fix_title(self, title, categ_id, _ws_re=re.compile(r'\s+')): orig = title title = HTMLParser().unescape(strip_tags(title)) title = _ws_re.sub(' ', title).strip() if title != orig: self.print_warning('Sanitized category title', event_id=categ_id) self.print_warning('%[red!]{}%[reset] => %[green!]{}%[reset]'.format(orig, title)) return title
def __init__(self, rh, event_, active_menu_item=None, **kwargs): assert event_ == kwargs.setdefault('event', event_) self.event = event_ self.title = strip_tags(self.event.title) kwargs['base_layout_params'] = { 'active_menu_item': active_menu_item or self.sidemenu_option, 'event_types': [(et.name, et.title) for et in EventType] } WPDecorated.__init__(self, rh, **kwargs)
def _draw_item(self, canvas, item, tpl_data, content, margin_x, margin_y): style = ParagraphStyle({}) style.alignment = ALIGNMENTS[item['text_align']] style.textColor = COLORS[item['color']] style.fontSize = _extract_font_size(item['font_size']) style.leading = style.fontSize if item['bold'] and item['italic']: style.fontName = FONT_STYLES[item['font_family']][3] elif item['italic']: style.fontName = FONT_STYLES[item['font_family']][2] elif item['bold']: style.fontName = FONT_STYLES[item['font_family']][1] else: style.fontName = FONT_STYLES[item['font_family']][0] item_x = float(item['x']) / PIXELS_CM * cm item_y = float(item['y']) / PIXELS_CM * cm item_width = item['width'] / PIXELS_CM * cm item_height = (item['height'] / PIXELS_CM * cm) if item.get('height') is not None else None if isinstance(content, Image): canvas.drawImage(ImageReader(content), margin_x + item_x, self.height - margin_y - item_height - item_y, item_width, item_height) else: content = strip_tags(content) for line in content.splitlines(): p = Paragraph(line, style) available_height = (tpl_data.height_cm - (item_y / PIXELS_CM)) * cm w, h = p.wrap(item_width, available_height) if w > item_width or h > available_height: # TODO: add warning pass p.drawOn(canvas, margin_x + item_x, self.height - margin_y - item_y - h) item_y += h
def serialize_event_for_json_ld(event, full=False): data = { '@context': 'http://schema.org', '@type': 'Event', 'url': event.external_url, 'name': event.title, 'startDate': event.start_dt_local.isoformat(), 'endDate': event.end_dt_local.isoformat(), 'location': { '@type': 'Place', 'name': event.venue_name or 'No location set', 'address': event.address or 'No address set' } } if full and event.description: data['description'] = strip_tags(event.description) if full and event.person_links: data['performer'] = list(map(serialize_person_for_json_ld, event.person_links)) if full and event.has_logo: data['image'] = event.external_logo_url return data
def serialize_event_for_json_ld(event, full=False): data = { '@context': 'http://schema.org', '@type': 'Event', 'url': event.external_url, 'name': event.title, 'startDate': event.start_dt_local.isoformat(), 'endDate': event.end_dt_local.isoformat(), 'location': { '@type': 'Place', 'name': event.venue_name or 'No location set', 'address': event.address or 'No address set' } } if full and event.description: data['description'] = strip_tags(event.description) if full and event.person_links: data['performer'] = map(serialize_person_for_json_ld, event.person_links) if full and event.has_logo: data['image'] = event.external_logo_url return data
class InvenioRecordConverter(InvenioRecordConverterBase): """ Main converter class. Converts record from InvenioConverter in format readable by a plugin. """ conversion = [('088', 'reportNumbers', lambda x: [ number for number in x[0]['a'][0].split(' ') if number != '(Confidential)' ]), ('100', 'primaryAuthor', lambda x: x[0] if 'Primary Author' in x[0].get('e', []) else {}, InvenioAuthorConverter), ('100', 'speaker', lambda x: x[0] if 'Speaker' in x[0].get('e', []) else {}, InvenioAuthorConverter), ('111', APPEND, None, InvenioPlaceTimeConverter111), ('245', 'title', lambda x: x[0]['a'][0]), ('518', APPEND, None, InvenioPlaceTimeConverter518), ('520', 'summary', lambda x: strip_tags(x[0]['a'][0])), ('700', 'secondaryAuthor', None, InvenioAuthorConverter), ('61124', 'meetingName', lambda x: str(x[0]['a'][0])), ('8564', 'materials', lambda x: x, InvenioLinkConverter)]
class ContributionRecordSchema(RecordSchema, ContributionSchema): class Meta: model = Contribution indexable = ('title', 'description', 'location', 'persons') non_indexable = ('contribution_id', 'type', 'contribution_type', 'event_id', 'url', 'category_id', 'category_path', 'start_dt', 'end_dt', 'duration') fields = RecordSchema.Meta.fields + non_indexable _data = fields.Function(lambda contrib: ContributionSchema( only=ContributionRecordSchema.Meta.indexable).dump(contrib)) category_path = fields.Function( lambda c, ctx: _get_category_chain(c.event, ctx.get('categories'))) url = mm.Function(lambda contrib: url_for( 'contributions.display_contribution', contrib, _external=True)) @post_dump def _transform(self, data, **kwargs): if contribution_type := data.pop('contribution_type', None): data['type_format'] = contribution_type if desc := data['_data'].get('description'): data['_data']['description'] = strip_tags(desc).strip()
def _draw_item(self, canvas, item, tpl_data, content, margin_x, margin_y): style = ParagraphStyle({}) style.alignment = ALIGNMENTS[item['text_align']] style.textColor = COLORS[item['color']] style.fontSize = _extract_font_size(item['font_size']) style.leading = style.fontSize if item['bold'] and item['italic']: style.fontName = FONT_STYLES[item['font_family']][3] elif item['italic']: style.fontName = FONT_STYLES[item['font_family']][2] elif item['bold']: style.fontName = FONT_STYLES[item['font_family']][1] else: style.fontName = FONT_STYLES[item['font_family']][0] item_x = float(item['x']) / PIXELS_CM * cm item_y = float(item['y']) / PIXELS_CM * cm item_width = item['width'] / PIXELS_CM * cm item_height = (item['height'] / PIXELS_CM * cm) if item.get('height') is not None else None if isinstance(content, Image.Image): canvas.drawImage(ImageReader(content), margin_x + item_x, self.height - margin_y - item_height - item_y, item_width, item_height) else: content = strip_tags(content) for line in content.splitlines(): p = Paragraph(line, style) available_height = (tpl_data.height_cm - (item_y / PIXELS_CM)) * cm w, h = p.wrap(item_width, available_height) if w > item_width or h > available_height: # TODO: add warning pass p.drawOn(canvas, margin_x + item_x, self.height - margin_y - item_y - h) item_y += h
def test_strip_tags(input, output): assert strip_tags(input) == output assert type(input) is type(output)
def _transform(self, data, **kwargs): if desc := data['_data'].get('description'): data['_data']['description'] = strip_tags(desc).strip()
def test_strip_tags(input, output): assert strip_tags(input) == output assert isinstance(input, type(output))
def _transform(self, data, **kwargs): data['type_format'] = data.pop('event_type') if desc := data['_data'].get('description'): data['_data']['description'] = strip_tags(desc).strip()
def _normalize_path(self, path): return secure_filename(strip_tags(path))
def test_strip_tags(): assert strip_tags('foo <strong>bar</strong>') == 'foo bar'
def _strip_html(self, data, **kwargs): data['description'] = strip_tags(data['description']) return data
def _sanitize_title(title, _ws_re=re.compile(r'\s+')): title = convert_to_unicode(title) title = HTMLParser().unescape(strip_tags(title)) return _ws_re.sub(' ', title).strip()
def _transform(self, data, **kwargs): if desc := data.get('content'): data['content'] = strip_tags(desc).strip()
def _strip_html(self, data, **kwargs): data['content'] = strip_tags(data['content']) return data
def sanitize_user_input(string, html=False): string = convert_to_unicode(string) if not html: string = HTMLParser().unescape(strip_tags(string)) return WHITESPACE_RE.sub(' ', string).strip()