def validate(self, value, adapt=True): if value in ['', None] and self.nullable: return '' super(UTF8, self).validate(value) for c in self.must_contain: if c not in value: raise ValidationError( ('Invalid email address {}. ' 'A valid email address must contain at least ' 'one @ characters'.format(str(value))) ) return force_utf8(value)
def math_to_image(self, s, filename_or_obj, color='black', dpi=None, fontsize=10): """ This function is a thread safe modification of matplotlib's mathtext:math_to_image. The MathTextParser render's font caching mechanism makes it impossible to use with multiple threads. Thus we first have to acquire a lock from matplotlib RendererAgg and block other threads from entering render while it's in process. Given a math expression, renders it in a closely-clipped bounding box to an image file. *s* A math expression. The math portion should be enclosed in dollar signs. *filename_or_obj* A filepath or writable file-like object to write the image data to. *color* Text color *dpi* Override the output dpi, otherwise use the default associated with the output format. *fontsize* The font size, defaults to 10. """ try: s = unicode(s) except UnicodeDecodeError: s = unicode(filters.force_utf8(s).decode('utf-8')) RendererAgg.lock.acquire() try: self.math_text_parser.to_png(filename_or_obj, s, color=color, dpi=dpi, fontsize=fontsize) except (ParseFatalException, AttributeError): # Probably some invalid arguments supplied for math parser # We can most likely ignore them pass finally: RendererAgg.lock.release()
def _normalize(self, text): return filters.force_utf8(' '.join(text.strip(' \t').split()))