1 | from django.db import models |
---|
2 | import treebeard.mp_tree |
---|
3 | import settings |
---|
4 | |
---|
5 | |
---|
6 | class BudgetArea(treebeard.mp_tree.MP_Node): |
---|
7 | name = models.CharField(max_length=50) |
---|
8 | comment = models.TextField(blank=True) |
---|
9 | always = models.BooleanField(blank=True, default=False) |
---|
10 | |
---|
11 | # Contact / ACL information |
---|
12 | # If not specified, inherit from parent |
---|
13 | owner = models.EmailField(help_text = 'Email address of the officer responsible for the area', blank=True) # owner of the budget area |
---|
14 | interested = models.EmailField(help_text='Email address of parties interested in the area', blank=True) # interested parties (ie, whole committee) |
---|
15 | use_owner = models.BooleanField(default=False, blank=True) |
---|
16 | |
---|
17 | def contact_address(self,): |
---|
18 | address = '' |
---|
19 | if self.use_owner: |
---|
20 | address = self.owner or self.interested |
---|
21 | else: |
---|
22 | address = self.interested or self.owner |
---|
23 | return address or self.get_parent().contact_address() |
---|
24 | |
---|
25 | def mark_used(self, term, comment=""): |
---|
26 | BudgetAreaTerm.objects.get_or_create( |
---|
27 | budget_area=self, |
---|
28 | budget_term=term, |
---|
29 | defaults={'comment':comment} |
---|
30 | ) |
---|
31 | |
---|
32 | @classmethod |
---|
33 | def get_by_path(cls, path): |
---|
34 | root = BudgetArea.objects.get(name=path[0], depth=1) |
---|
35 | node = root |
---|
36 | for name in path[1:]: |
---|
37 | node = node.get_children().filter(name=name)[0] |
---|
38 | return node |
---|
39 | |
---|
40 | def dump_to_html(self): |
---|
41 | struct = self.dump_bulk() |
---|
42 | return self.struct_to_html(struct, depth=0) |
---|
43 | |
---|
44 | def struct_to_html(self, struct, depth=0): |
---|
45 | def format_data(data): |
---|
46 | return "<em>"+data['name']+"</em> "+unicode(data) |
---|
47 | prefix = "\t"*depth |
---|
48 | html = prefix+"<ul>\n" |
---|
49 | html = html + "\n".join( |
---|
50 | [("%(prefix)s\t<li>%(data)s\n%(children)s\t%(prefix)s</li>\n" % { |
---|
51 | 'prefix':prefix, |
---|
52 | 'data':format_data(elem['data']), |
---|
53 | 'children':('children' in elem and self.struct_to_html(elem['children'], depth+1) or '') |
---|
54 | }) for elem in struct]) |
---|
55 | html = html + prefix + "</ul>\n" |
---|
56 | return html |
---|
57 | |
---|
58 | def __unicode__(self,): |
---|
59 | return u"%s [%s] (%s)" % (self.name, self.contact_address(), self.always, ) |
---|
60 | |
---|
61 | |
---|
62 | class BudgetTerm(models.Model): |
---|
63 | name = models.CharField(max_length=20) |
---|
64 | slug = models.SlugField(max_length=20) |
---|
65 | start_date = models.DateField() |
---|
66 | end_date = models.DateField() |
---|
67 | submit_deadline = models.DateField() |
---|
68 | |
---|
69 | def __unicode__(self,): |
---|
70 | return "%s (%s to %s [due %s])" % (self.name, self.start_date, self.end_date, self.submit_deadline, ) |
---|
71 | |
---|
72 | |
---|
73 | class BudgetAreaTerm(models.Model): |
---|
74 | budget_area = models.ForeignKey(BudgetArea) |
---|
75 | budget_term = models.ForeignKey(BudgetTerm) |
---|
76 | comment = models.TextField(blank=True, ) |
---|
77 | |
---|
78 | def __unicode__(self,): |
---|
79 | return "%s during %s" % (self.budget_area, self.budget_term, ) |
---|
80 | |
---|
81 | |
---|
82 | class ReimbursementRequest(models.Model): |
---|
83 | submitter = models.CharField(max_length=10) # MIT username of submitter |
---|
84 | check_to_name = models.CharField(max_length=50, verbose_name="check recipient's name") |
---|
85 | check_to_email = models.EmailField(verbose_name="email address for check pickup") |
---|
86 | check_to_addr = models.TextField(blank=True, verbose_name="address for check mailing", help_text="For most requests, this should be blank for pickup in SAO (W20-549)") |
---|
87 | amount = models.DecimalField(max_digits=7, decimal_places=2) |
---|
88 | budget_area = models.ForeignKey(BudgetArea) |
---|
89 | budget_term = models.ForeignKey(BudgetTerm) |
---|
90 | |
---|
91 | def coerce_full_email(email): |
---|
92 | if '@' in email: return email |
---|
93 | else: return email + '@' + settings.DEFAULT_DOMAIN |
---|