class PositionalOptional(TestTag): start = ttag.IntegerArg() finish = ttag.IntegerArg(required=False) def output(self, data): if 'finish' in data: start, finish = data['start'], data['finish'] else: start, finish = 0, data['start'] return ','.join([str(i) for i in range(start, finish)])
class PositionalOptionalMixed(TestTag): start = ttag.IntegerArg(positional=True) finish = ttag.IntegerArg(positional=True, required=False) step = ttag.IntegerArg() def output(self, data): if 'finish' in data: start, finish = data['start'], data['finish'] else: start, finish = 0, data['start'] return ','.join([str(i) for i in range(start, finish, data['step'])])
class Formconfig(BaseFormTag): context = ttag.BasicArg() for_ = ttag.MultiArg(named=True, required=False) position = ttag.IntegerArg(named=True, required=False) def __init__(self, *args, **kwargs): super(Formconfig, self).__init__(*args, **kwargs) if self._vars['context'] not in ('field', 'row'): raise template.TemplateSyntaxError("First argument must be " "'field' or 'row' (found %r)." % self._vars['context']) def get_fields(self, data): return data.get('for') or [] def render(self, context): data = self.resolve(context) self.set_config('%s_template' % data['context'], data, context, data.get('template')) self.set_config('with', data, context, data.get('with')) self.set_config('only', data, context, data.get('only')) if data.get('for'): self.set_config('position', data, context, data.get('position')) if 'extends' in data: self.set_config('extends_blocks', data, context, self.blocks) return ''
class NamedArg(TestTag): limit = ttag.IntegerArg(default=5, named=True) def output(self, data): if 'limit' in data: return 'The limit is %d' % data['limit'] return 'No limit was specified'
class PositionalMixed(TestTag): limit = ttag.IntegerArg(default=5) as_ = ttag.BasicArg(named=True) def render(self, context): data = self.resolve(context) context[data['as']] = data['limit'] return ''
class ArgumentType(TestTag): age = ttag.IntegerArg(required=False, named=True) name_ = ttag.StringArg(required=False, named=True) url = ttag.ModelInstanceArg(model=models.Link, required=False, named=True) date = ttag.DateArg(required=False, named=True) time = ttag.TimeArg(required=False, named=True) datetime = ttag.DateTimeArg(required=False, named=True) flag = ttag.BooleanArg() def output(self, data): order = 'name age url date time datetime'.split() values = [six.text_type(data[x]) for x in order if x in data] if 'flag' in data: values.append('flag_is_set') return u' '.join(values)
class Repeat(TestTag): count = ttag.IntegerArg() class Meta: block = True end_block = 'done' def render(self, context): data = self.resolve(context) output = [] for i in range(data['count']): context.push() output.append(self.nodelist.render(context)) context.pop() return ''.join(output)
class RepeatWithEmpty(TestTag): count = ttag.IntegerArg() class Meta: block = {'empty': False} end_block = 'stop' def render(self, context): data = self.resolve(context) if not data['count']: return self.nodelist_empty.render(context) output = [] for i in range(data['count']): context.push() output.append(self.nodelist.render(context)) context.pop() return ''.join(output)
class Placeholder(ttag.Tag): """ Outputs an ``<img>`` tag containing an inline base64 encoded placeholder image. For example:: {% placeholder 100x30 "some text" %} The text is optional. Some optional keyword arguments are available which can be used to adjust the output image: ``background`` The background color of the placeholder image. ``border`` The border color (and also that of the 'x' lines crossing the image). ``text_color`` The color of the text. ``text_size`` The size of the text. ``class`` The HTML class to use for this image tag. ``no_antialias`` Don't antialias the final image. """ size = SizeArg() text = ttag.Arg(required=False) background = ttag.Arg(keyword=True, required=False) border = ttag.Arg(keyword=True, required=False) text_color = ttag.Arg(keyword=True, required=False) text_size = ttag.IntegerArg(keyword=True, required=False) class_ = ttag.Arg(keyword=True, required=False) no_antialias = ttag.BooleanArg() def clean(self, cleaned_data): cleaned_data['antialias'] = not cleaned_data.pop('no_antialias', None) return cleaned_data def output(self, data): img = StringIO() html_class = data.pop('class', None) create_placeholder(**data).save(img, 'PNG') img.seek(0) return mark_safe('<img alt="%s" src="data:image/png;base64,%s" %s/>' % (data.get('text') or '', img.read().encode('base64'), html_class and ' class="%s"' % html_class or ''))
class Positional(TestTag): limit = ttag.IntegerArg() def output(self, data): return '%s' % data['limit']
class NamedKeywordArg(NamedArg): limit = ttag.IntegerArg(keyword=True)