actions.py 2.25 KB
Newer Older
frekk's avatar
frekk committed
1
2
3
from django.contrib import messages
from django.core.exceptions import ValidationError
from django.db import IntegrityError
4
from django.db.models import Q
frekk's avatar
frekk committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

from .models import OldMember
from memberdb.models import Member

def import_old_member(modeladmin, request, queryset):
    """
    admin action: Import the selected OldMember records into the new MemberDB format
    don't overwrite records if they already exist (matching by username)
    """
    num_success = 0
    total = queryset.count()
    for om in queryset:
        try:
            # create a new Member object
            nm = Member(username=om.username)

            # fudge the data as much as we can, people will have to renew memberships and check this anyway
            nm.first_name, nm.last_name = om.real_name.split(" ", 1)
            nm.display_name = om.real_name
            nm.is_guild = om.guild_member
            nm.phone_number = om.phone_number
26
27
            nm.id_number = om.student_no
            nm.id_desc = "student"
frekk's avatar
frekk committed
28
            nm.email_address = om.email_address
frekk's avatar
frekk committed
29
30
            if om.membership_type == 1: # O'day special
                # O'day special or student
frekk's avatar
frekk committed
31
                nm.is_student = True
frekk's avatar
frekk committed
32
33
34
            elif om.membership_type == 2: # student
                nm.is_student = True
            else: # non-student
frekk's avatar
frekk committed
35
                nm.is_student = False
frekk's avatar
frekk committed
36

frekk's avatar
frekk committed
37
38
            if (nm.username == '' or nm.username is None):
                raise ValidationError("username cannot be blank")
39
40
41
42
43
44
45

            # try to prevent creating duplicate records on import
            is_dupe = Q(username=nm.username) | (Q(first_name=nm.first_name) & Q(last_name=nm.last_name)) | Q(id_number=nm.id_number)
            dupes = Member.objects.filter(is_dupe)
            if (dupes.count() > 0):
                raise ValidationError("suspected duplicate member record")
                
frekk's avatar
frekk committed
46
47
48
            nm.save()
            num_success += 1
        except BaseException as e:
49
            breakpoint()
frekk's avatar
frekk committed
50
51
52
53
54
55
            modeladmin.message_user(request, 'Could not import record (%s): %s' % (om, e), level=messages.ERROR)
        
    if (num_success > 0):
        modeladmin.message_user(request, 'Successfully imported %d of %d records.' % (num_success, total))

import_old_member.short_description = "Import selected records to new MemberDB"