1 | #!/usr/bin/python |
---|
2 | import sys |
---|
3 | import os |
---|
4 | |
---|
5 | if __name__ == '__main__': |
---|
6 | cur_file = os.path.abspath(__file__) |
---|
7 | django_dir = os.path.abspath(os.path.join(os.path.dirname(cur_file), '..')) |
---|
8 | sys.path.append(django_dir) |
---|
9 | os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' |
---|
10 | |
---|
11 | import finance_core.models |
---|
12 | import vouchers.models |
---|
13 | |
---|
14 | import util.add_gl_accounts |
---|
15 | import finance_core.util |
---|
16 | |
---|
17 | from django.contrib.auth.models import Group, Permission |
---|
18 | from django.contrib.contenttypes.models import ContentType |
---|
19 | from django.db.models import Q |
---|
20 | |
---|
21 | |
---|
22 | base_structure = ( |
---|
23 | ('Assets', None, ), |
---|
24 | ('Expenses', None, ), |
---|
25 | ('Income', None, ), |
---|
26 | ) |
---|
27 | |
---|
28 | def get_or_create_group(name): |
---|
29 | try: |
---|
30 | group = Group.objects.get(name=name, ) |
---|
31 | except Group.DoesNotExist: |
---|
32 | group = Group(name=name, ) |
---|
33 | group.save() |
---|
34 | return group |
---|
35 | |
---|
36 | def grant_by_codename(principal, codename): |
---|
37 | principal.permissions.add(Permission.objects.get(codename=codename, )) |
---|
38 | |
---|
39 | if __name__ == '__main__': |
---|
40 | if len(finance_core.models.BudgetArea.objects.filter(depth=1)) == 0: |
---|
41 | base = finance_core.models.BudgetArea.add_root(name='Accounts', always=True, use_owner=True, ) |
---|
42 | else: |
---|
43 | base = finance_core.models.BudgetArea.get_by_path(['Accounts',]) |
---|
44 | finance_core.util.mass_add_accounts(base, base_structure, sys.stdout, ) |
---|
45 | util.add_gl_accounts.add_gl_accounts() |
---|
46 | |
---|
47 | # Do the various auth setup |
---|
48 | get_or_create_group('autocreated') |
---|
49 | get_or_create_group('mit') |
---|
50 | treasurers = get_or_create_group('treasurers') |
---|
51 | treasurer_perms = Permission.objects.filter(content_type__app_label__in=['vouchers', 'finance_core', ],) |
---|
52 | for perm in treasurer_perms: |
---|
53 | treasurers.permissions.add(perm) |
---|
54 | treasurers.save() |
---|
55 | downloader = get_or_create_group('downloader') |
---|
56 | grant_by_codename(downloader, 'generate_vouchers', ) |
---|
57 | downloader.save() |
---|