PageRenderTime 103ms CodeModel.GetById 40ms app.highlight 10ms RepoModel.GetById 51ms app.codeStats 0ms

/django_excel_templates/helpers.py

http://django-excel-templates.googlecode.com/
Python | 95 lines | 79 code | 10 blank | 6 comment | 23 complexity | ae6b967072fc0e9ba92d24648634f426 MD5 | raw file
 1def Break():
 2    try:
 3      import dbgp.client
 4      dbgp.client.brk(port=9000)
 5    except: pass  
 6      
 7"""  Place to hold helper crud """
 8
 9import ExcelReports
10import exceptions
11from django.db.models import related
12from django.db.models.fields import AutoField
13
14def get_items(list):
15    items = []
16    if list.find(',') > 0:
17        items = striplist(list.strip(',').split(','))
18    else:
19        items.append(list.strip())
20    return items
21
22def striplist(l):
23    list = []
24    for x in l:
25        list.append(x.strip())
26    return list
27
28def isodd(num):
29    return num & 1 and True or False
30
31def abstract():
32    ''' simple trick to make an abstract base method '''
33    import inspect
34    caller = inspect.getouterframes(inspect.currentframe())[1][3]
35    raise NotImplementedError(caller + ' must be implemented in subclass')
36
37def get_relation_list(model,FK=False):
38    """
39    Iterates through model to find ManyToMany Fields or ForeignKey Fields.
40    Returns dictionary of field names mapped to values.
41    """
42    results = {}
43    for field_list in [model._meta.fields, model._meta.many_to_many]:
44        for field in field_list:
45            related_model_name = None
46            related_model_app_name = None
47            if (not field.name[-4:] == '_ptr') and (not field.__class__ == AutoField): 
48                if issubclass(field.__class__, related.RelatedField):
49                    if not FK:
50                        if not field.__class__ == related.ForeignKey:
51                            related_model_app_name = field.rel.to.__module__.split('.')[0]
52                            if not related_model_app_name == 'django':
53                                related_model_name = field.rel.to.__name__
54                                full_related_model_name = '.'.join([field.rel.to.__module__, related_model_name])
55                                try:
56                                    get_values = 'val = model.%s.all()' % field.name
57                                    exec get_values
58                                except exceptions.AttributeError:
59                                    get_values = 'val = [model.%s,]' % field.name
60                                    exec get_values
61
62                                results[field.name] = val
63                            else:
64                                continue
65                    else:
66                        if not field.__class__ == related.ManyToManyField:
67                            related_model_app_name = field.rel.to.__module__.split('.')[0]
68                            if not related_model_app_name == 'django':
69                                related_model_name = field.rel.to.__name__
70                                full_related_model_name = '.'.join([field.rel.to.__module__, related_model_name])
71                                get_value = 'val = model.%s' % field.name
72                                exec get_value
73                                results[field.name] = val
74                            else:
75                                continue
76
77    return results
78
79def convert_to_letter(num):
80    alpha = 'abcdefghijklmnopqrstuvwxyz'.upper()
81    if num < 26:
82        return alpha[num]
83    i = 0
84    letters = ''
85    first_letter = ''
86    second_letter =''
87    while num > 25:
88        first_letter = alpha[i]
89        num -= 26
90        i += 1
91            
92    second_letter = alpha[num]
93    letters = first_letter + second_letter
94    
95    return letters