Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed starter issue: CSV file headers. #762

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions huxley/core/admin/committee.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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: '
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
request, 'Not all secretariat members could upload. These rows failed: '
request, 'Not all Committees could upload. These rows failed: '

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JK let's just revert this file

+ str(failed_uploads))

return HttpResponseRedirect(reverse('admin:core_committee_changelist'))

Expand Down
40 changes: 24 additions & 16 deletions huxley/core/admin/delegate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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'))

Expand Down
23 changes: 17 additions & 6 deletions huxley/core/admin/secretariat_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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'))
Expand Down