| 1 | #!/usr/bin/python |
|---|
| 2 | import csv |
|---|
| 3 | import datetime |
|---|
| 4 | import os |
|---|
| 5 | import sys |
|---|
| 6 | |
|---|
| 7 | if __name__ == '__main__': |
|---|
| 8 | cur_file = os.path.abspath(__file__) |
|---|
| 9 | django_dir = os.path.abspath(os.path.join(os.path.dirname(cur_file), '..')) |
|---|
| 10 | sys.path.append(django_dir) |
|---|
| 11 | os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' |
|---|
| 12 | |
|---|
| 13 | from django.db import transaction |
|---|
| 14 | import django.contrib.auth.models |
|---|
| 15 | import reversion |
|---|
| 16 | |
|---|
| 17 | import groups.models |
|---|
| 18 | |
|---|
| 19 | def canonicalize_email(email): |
|---|
| 20 | if '@' in email: return email |
|---|
| 21 | elif email == '': return '' |
|---|
| 22 | else: return email + "@mit.edu" |
|---|
| 23 | |
|---|
| 24 | def db_parse_date(string): |
|---|
| 25 | return datetime.datetime.strptime(string, '%d-%b-%y').date() |
|---|
| 26 | |
|---|
| 27 | def convert_to_int(number): |
|---|
| 28 | if number == 'n/a': return None |
|---|
| 29 | if number == '': return None |
|---|
| 30 | if number == 'none': return None |
|---|
| 31 | else: return int(number) |
|---|
| 32 | |
|---|
| 33 | def import_group(d): |
|---|
| 34 | g = groups.models.Group() |
|---|
| 35 | g.pk = d['ASA_STUDENT_GROUP_KEY'] |
|---|
| 36 | g.name = d['STUDENT_GROUP_NAME'] |
|---|
| 37 | g.abbreviation = d['STUDENT_GROUP_ACRONYM'] |
|---|
| 38 | g.description = d['STUDENT_GROUP_DESCRIPTION'] |
|---|
| 39 | cat_name = d['GROUP_ACTIVITY_CATEGORY'] |
|---|
| 40 | try: |
|---|
| 41 | g.activity_category = groups.models.ActivityCategory.objects.get(name=cat_name) |
|---|
| 42 | except groups.models.ActivityCategory.DoesNotExist: |
|---|
| 43 | print ">> Unknown category '%s' on group '%s'" % (cat_name, g.name, ) |
|---|
| 44 | pass |
|---|
| 45 | class_name = d['GROUP_CLASS'] |
|---|
| 46 | status_name = d['GROUP_STATUS'] |
|---|
| 47 | funding_name = d['GROUP_FUNDING_TYPE'] |
|---|
| 48 | if class_name == 'Standard': |
|---|
| 49 | if status_name == 'Provisional': |
|---|
| 50 | class_name = 'Unfunded' |
|---|
| 51 | status_name = 'Active' |
|---|
| 52 | elif status_name == 'Active': |
|---|
| 53 | class_name = 'MIT-funded' |
|---|
| 54 | elif status_name == 'Suspended' or status_name == 'Derecognized': |
|---|
| 55 | class_name = 'MIT-funded' |
|---|
| 56 | g.group_class = groups.models.GroupClass.objects.get(name=class_name) |
|---|
| 57 | g.group_status = groups.models.GroupStatus.objects.get(name=status_name) |
|---|
| 58 | if funding_name == 'none': |
|---|
| 59 | g.group_funding = None |
|---|
| 60 | else: |
|---|
| 61 | g.group_funding = groups.models.GroupFunding.objects.get(name=funding_name) |
|---|
| 62 | g.website_url = d['WEBSITE_URL'] |
|---|
| 63 | g.constitution_url = d['CONSTITUTION_WEB_URL'] |
|---|
| 64 | g.meeting_times = d['MEETING_TIMES'] |
|---|
| 65 | g.advisor_name = d['ADVISOR'] |
|---|
| 66 | g.num_undergrads = convert_to_int(d['NUM_OF_UNDERGRADUATE']) |
|---|
| 67 | g.num_grads = convert_to_int(d['NUM_OF_GRADUATE']) |
|---|
| 68 | g.num_community = convert_to_int(d['NUM_OF_COMMUNITY']) |
|---|
| 69 | g.num_other = convert_to_int(d['NUM_OF_OTHERS']) |
|---|
| 70 | g.group_email = canonicalize_email(d['STUDENT_GROUP_EMAIL']) |
|---|
| 71 | g.officer_email = canonicalize_email(d['OFFICER_EMAIL']) |
|---|
| 72 | try: |
|---|
| 73 | g.main_account_id = convert_to_int(d['MAIN_ACCOUNT_ID']) |
|---|
| 74 | except ValueError: |
|---|
| 75 | if d['MAIN_ACCOUNT_ID'] == "contact LWard": |
|---|
| 76 | print "Ignoring account ID contact LWard..." |
|---|
| 77 | g.main_account_id = None |
|---|
| 78 | else: |
|---|
| 79 | raise |
|---|
| 80 | g.funding_account_id= convert_to_int(d['FUNDING_ACCOUNT_ID']) |
|---|
| 81 | g.athena_locker = d['ATHENA_LOCKER'] |
|---|
| 82 | g.recognition_date = db_parse_date(d['RECOGNITION_DATE']) |
|---|
| 83 | g.update_date = db_parse_date(d['UPDATE_DATE']) |
|---|
| 84 | g.updater = d['UPDATER'] |
|---|
| 85 | g.save() |
|---|
| 86 | |
|---|
| 87 | if d['NOTE']: |
|---|
| 88 | notes, created = groups.models.GroupNote.objects.get_or_create(group=g, author='importer/legacy-notes@SYSTEM', ) |
|---|
| 89 | notes.body = d['NOTE'] |
|---|
| 90 | notes.acl_read_group = False |
|---|
| 91 | notes.acl_read_office = False |
|---|
| 92 | notes.save() |
|---|
| 93 | |
|---|
| 94 | if d['OTHER_ACCOUNT_IDS']: |
|---|
| 95 | accounts, created = groups.models.GroupNote.objects.get_or_create(group=g, author='importer/other-accounts@SYSTEM', ) |
|---|
| 96 | accounts.body = d['OTHER_ACCOUNT_IDS'] |
|---|
| 97 | accounts.acl_read_group = True |
|---|
| 98 | accounts.acl_read_office = True |
|---|
| 99 | accounts.save() |
|---|
| 100 | |
|---|
| 101 | @transaction.commit_on_success |
|---|
| 102 | def import_groups(reader): |
|---|
| 103 | with reversion.create_revision(): |
|---|
| 104 | for line in reader: |
|---|
| 105 | #print line |
|---|
| 106 | import_group(line) |
|---|
| 107 | importer = django.contrib.auth.models.User.objects.get(username='importer@SYSTEM', ) |
|---|
| 108 | reversion.set_user(importer) |
|---|
| 109 | reversion.set_comment("Groups importer") |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | if __name__ == '__main__': |
|---|
| 113 | indb = sys.stdin |
|---|
| 114 | reader = csv.DictReader(indb) |
|---|
| 115 | import_groups(reader) |
|---|