Project

General

Profile

Download (1.24 KB) Statistics
| Branch: | Tag: | Revision:

calebasse / calebasse / dossiers / templatetags / dossiers.py @ a9520794

1
import copy
2

    
3
from django import template
4
from django import forms
5
from django.utils.datastructures import SortedDict
6

    
7
register = template.Library()
8

    
9
def get_fieldset(parser, token):
10
    try:
11
        name, fields, as_, variable_name, from_, form = token.split_contents()
12
    except ValueError:
13
        raise template.TemplateSyntaxError('bad arguments for %r'  % token.split_contents()[0])
14

    
15
    return FieldSetNode(fields.split(','), variable_name, form)
16

    
17
get_fieldset = register.tag(get_fieldset)
18

    
19

    
20
class FieldSetNode(template.Node):
21
    def __init__(self, fields, variable_name, form_variable):
22
        self.fields = fields
23
        self.variable_name = variable_name
24
        self.form_variable = form_variable
25

    
26
    def render(self, context):
27
        form = template.Variable(self.form_variable).resolve(context)
28
        new_form = copy.copy(form)
29
        new_form.fields = SortedDict([(key, form.fields[key]) for key in self.fields if key in self.fields])
30
        context[self.variable_name] = new_form
31

    
32
        return u''
33

    
34
def phone(value):
35
    result = ""
36
    if len(value) == 10:
37
        for i in range(2, 11, 2):
38
            result += value[i-2:i] + " "
39
        result = result[:-1]
40
    else:
41
        result = value
42
    return result
43

    
44
register.filter('phone', phone)
(2-2/2)