diff --git a/huxley/core/admin/committee.py b/huxley/core/admin/committee.py index 43b76439f..d37e35f60 100644 --- a/huxley/core/admin/committee.py +++ b/huxley/core/admin/committee.py @@ -4,7 +4,7 @@ import csv from django.conf.urls import url -from django.contrib import admin +from django.contrib import admin, messages from django.urls import reverse from django.http import HttpResponseRedirect @@ -17,14 +17,23 @@ def load(self, request): '''Import a CSV file containing committees.''' committees = request.FILES reader = csv.reader(committees['csv'].read().decode('utf-8').splitlines()) + failed_uploads = [] for row in reader: if row and row[0] != 'Name': - special = False if row[3] == '0' or row[3] == 'False' or not row[3] else True - com = Committee(name=row[0], - full_name=row[1], - delegation_size=int(row[2]), - special=special) - com.save() + committee_check = Committee.objects.filter(name__exact=row[1]).exists() + if committee_check: + special = False if row[3] == '0' or row[3] == 'False' or not row[3] else True + com = Committee(name=row[0], + full_name=row[1], + delegation_size=int(row[2]), + special=special) + com.save() + else: + failed_uploads.append(row) + if failed_uploads: + messages.error( + request, 'Not all secretariat members could upload. These rows failed: ' + + str(failed_uploads)) return HttpResponseRedirect(reverse('admin:core_committee_changelist')) diff --git a/huxley/core/admin/delegate.py b/huxley/core/admin/delegate.py index cfe958eb9..09ebc83d8 100644 --- a/huxley/core/admin/delegate.py +++ b/huxley/core/admin/delegate.py @@ -12,7 +12,7 @@ from googleapiclient.discovery import build from google.oauth2 import service_account -from huxley.core.models import Assignment, Delegate, School +from huxley.core.models import Assignment, Delegate, School, Committee class DelegateAdmin(admin.ModelAdmin): @@ -66,23 +66,31 @@ def load(self, request): assignments[assignment.committee.name, assignment.country.name, assignment.registration.school.name, ] = assignment + failed_uploads = [] for row in reader: - if row: - if row[1] == 'Committee': - continue - school = School.objects.get(name=str(row[3])) - assignment = assignments[str(row[1]), str(row[2]), row[3], ] - email = str(row[4]) - delg = list( - Delegate.objects.filter(name=str(row[0]), email=email)) - if len(delg) == 1: - Delegate.objects.filter(name=str( - row[0]), email=email).update(assignment=assignment) + if row and row[0] != 'Name': + committee_check = Committee.objects.filter(name__exact=row[1]).exists() + school_check = School.objects.filter(name=row[3]).exists() + if committee_check and school_check: + school = School.objects.get(name=str(row[3])) + assignment = assignments[str(row[1]), str(row[2]), row[3], ] + email = str(row[4]) + delg = list( + Delegate.objects.filter(name=str(row[0]), email=email)) + if len(delg) == 1: + Delegate.objects.filter(name=str( + row[0]), email=email).update(assignment=assignment) + else: + Delegate.objects.create(name=row[0], + school=school, + email=email, + assignment=assignment) else: - Delegate.objects.create(name=row[0], - school=school, - email=email, - assignment=assignment) + failed_uploads.append(row) + if failed_uploads: + messages.error( + request, 'Not all delegates could upload. These rows failed because the committee or school could not be matched: ' + + str(failed_uploads)) return HttpResponseRedirect(reverse('admin:core_delegate_changelist')) diff --git a/huxley/core/admin/secretariat_member.py b/huxley/core/admin/secretariat_member.py index f506c8182..90d86c7d3 100644 --- a/huxley/core/admin/secretariat_member.py +++ b/huxley/core/admin/secretariat_member.py @@ -4,9 +4,10 @@ import csv from django.conf.urls import url -from django.contrib import admin +from django.contrib import admin, messages from django.urls import reverse from django.http import HttpResponseRedirect +from django.utils import html from huxley.core.models import Committee, SecretariatMember @@ -16,12 +17,22 @@ def load(self, request): '''Import a CSV file containing secretariat members.''' members = request.FILES reader = csv.reader(members['csv'].read().decode('utf-8').splitlines()) + failed_uploads = [] for row in reader: - row_committee = Committee.objects.get(name__exact=row[1]) - head_chair = True if (len(row) > 2 and row[2] == "TRUE") else False - sm = SecretariatMember( - name=row[0], committee=row_committee, is_head_chair=head_chair) - sm.save() + if row and row[0] != 'Name': + committee_check = Committee.objects.filter(name__exact=row[1]).exists() + if committee_check: + row_committee = Committee.objects.get(name__exact=row[1]) + head_chair = True if (len(row) > 2 and row[2] == "TRUE") else False + sm = SecretariatMember( + name=row[0], committee=row_committee, is_head_chair=head_chair) + sm.save() + else: + failed_uploads.append(row) + if failed_uploads: + messages.error( + request, 'Not all secretariat members could upload. These rows failed because the committee name could not be found: ' + + str(failed_uploads)) return HttpResponseRedirect( reverse('admin:core_secretariatmember_changelist'))