1 | #!/usr/bin/env 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 settings |
---|
12 | |
---|
13 | import finance_core.models |
---|
14 | import vouchers.models |
---|
15 | |
---|
16 | import util.add_gl_accounts |
---|
17 | import finance_core.util |
---|
18 | |
---|
19 | from django.contrib.auth.models import User, Group, Permission |
---|
20 | from django.contrib.contenttypes.models import ContentType |
---|
21 | from django.db.models import Q |
---|
22 | |
---|
23 | |
---|
24 | base_structure = ( |
---|
25 | ('Assets', None, ), |
---|
26 | ('Expenses', None, ), |
---|
27 | ('Income', None, ), |
---|
28 | ) |
---|
29 | |
---|
30 | def get_or_create_group(name): |
---|
31 | try: |
---|
32 | group = Group.objects.get(name=name, ) |
---|
33 | except Group.DoesNotExist: |
---|
34 | group = Group(name=name, ) |
---|
35 | group.save() |
---|
36 | return group |
---|
37 | |
---|
38 | def grant_by_codename(principal, codename): |
---|
39 | principal.permissions.add(Permission.objects.get(codename=codename, )) |
---|
40 | |
---|
41 | if __name__ == '__main__': |
---|
42 | if len(finance_core.models.BudgetArea.objects.filter(depth=1)) == 0: |
---|
43 | base = finance_core.models.BudgetArea.add_root(name='Accounts', always=True, use_owner=True, ) |
---|
44 | else: |
---|
45 | base = finance_core.models.BudgetArea.get_by_path(['Accounts',]) |
---|
46 | finance_core.util.mass_add_accounts(base, base_structure, sys.stdout, ) |
---|
47 | util.add_gl_accounts.add_gl_accounts() |
---|
48 | |
---|
49 | # Do the various auth setup |
---|
50 | get_or_create_group('autocreated') |
---|
51 | get_or_create_group('mit') |
---|
52 | local_auth_only = get_or_create_group('local-auth-only') |
---|
53 | treasurers = get_or_create_group('treasurers') |
---|
54 | treasurer_perms = Permission.objects.filter(content_type__app_label__in=['vouchers', 'finance_core', ],) |
---|
55 | for perm in treasurer_perms: |
---|
56 | treasurers.permissions.add(perm) |
---|
57 | treasurers.save() |
---|
58 | gdownloader = get_or_create_group('downloader') |
---|
59 | grant_by_codename(gdownloader, 'generate_vouchers', ) |
---|
60 | gdownloader.save() |
---|
61 | try: |
---|
62 | udown = User.objects.get(username='downloader', ) |
---|
63 | except User.DoesNotExist: |
---|
64 | udown = User.objects.create_user(username='downloader', email=settings.SERVER_EMAIL, ) |
---|
65 | udown.is_active = False |
---|
66 | udown.is_staff = True |
---|
67 | udown.groups.add(local_auth_only) |
---|
68 | udown.groups.add(gdownloader) |
---|
69 | udown.save() |
---|