Example #1
0
    def test_update_assignments(self):
        '''It should correctly update the set of country assignments.'''
        cm1 = TestCommittees.new_committee(name='CM1').id
        cm2 = TestCommittees.new_committee(name='CM2').id
        ct1 = TestCountries.new_country(name='CT1').id
        ct2 = TestCountries.new_country(name='CT2').id
        ct3 = TestCountries.new_country(name='CT3').id
        s1 = TestSchools.new_school(name='S1').id
        s2 = TestSchools.new_school(name='S2').id

        Assignment.objects.bulk_create([
            Assignment(committee_id=cm, country_id=ct, school_id=s1)
            for ct in [ct1, ct2]
            for cm in [cm1, cm2]
        ])

        # TODO: Also assert on delegate deletion.
        updates = [
            (cm1, ct1, s1, False),
            (cm1, ct2, s1, False),
            (cm1, ct3, s1, False),   # ADDED
            # (cm2, ct1, s1), # DELETED
            (cm2, ct2, s2, False),   # UPDATED
            (cm2, ct3, s2, False),   # ADDED
        ]

        Assignment.update_assignments(updates)
        new_assignments = [a[1:] for a in Assignment.objects.all().values_list()]

        self.assertEquals(set(updates), set(new_assignments))
Example #2
0
    def test_update_assignments(self):
        '''It should correctly update the set of country assignments.'''
        cm1 = TestCommittees.new_committee(name='CM1').id
        cm2 = TestCommittees.new_committee(name='CM2').id
        ct1 = TestCountries.new_country(name='CT1').id
        ct2 = TestCountries.new_country(name='CT2').id
        ct3 = TestCountries.new_country(name='CT3').id
        s1 = TestSchools.new_school(name='S1').id
        s2 = TestSchools.new_school(name='S2').id

        Assignment.objects.bulk_create([
            Assignment(committee_id=cm, country_id=ct, school_id=s1)
            for ct in [ct1, ct2]
            for cm in [cm1, cm2]
        ])

        # TODO: Also assert on delegate deletion.
        updates = [
            (cm1, ct1, s1),
            (cm1, ct2, s1),
            (cm1, ct3, s1),   # ADDED
            # (cm2, ct1, s1), # DELETED
            (cm2, ct2, s2),   # UPDATED
            (cm2, ct3, s2),   # ADDED
        ]

        Assignment.update_assignments(updates)
        new_assignments = [a[1:] for a in Assignment.objects.all().values_list()]

        self.assertEquals(set(updates), set(new_assignments))
Example #3
0
    def load(self, request):
        '''Loads new Assignments.'''
        assignments = request.FILES
        reader = csv.reader(assignments['csv'])

        def get_model(model, name, cache):
            if not name in cache:
                cache[name] = model.objects.get(name=name)
            return cache[name]

        def generate_assigments(reader):
            committees = {}
            countries = {}
            schools = {}
            for row in reader:
                if (row[0] == 'School' and row[1] == 'Committee'
                        and row[2] == 'Country'):
                    continue  # skip the first row if it is a header
                committee = get_model(Committee, row[1], committees)
                country = get_model(Country, row[2], countries)
                school = get_model(School, row[0], schools)
                if len(row) < 4:
                    rejected = False  # allow for the rejected field to be null
                else:
                    rejected = (
                        row[3].lower() == 'true'
                    )  # use the provided value if admin provides it
                yield (committee.id, country.id, school.id, rejected)

        Assignment.update_assignments(generate_assigments(reader))
        return HttpResponseRedirect(
            reverse('admin:core_assignment_changelist'))
Example #4
0
    def load(self, request):
        """Loads new Assignments."""
        assignments = request.FILES
        reader = csv.reader(assignments["csv"])

        def get_model(model, name, cache):
            if not name in cache:
                cache[name] = model.objects.get(name=name)
            return cache[name]

        def generate_assigments(reader):
            committees = {}
            countries = {}
            schools = {}
            for row in reader:
                if row[0] == "School" and row[1] == "Committee" and row[2] == "Country":
                    continue  # skip the first row if it is a header
                committee = get_model(Committee, row[1], committees)
                country = get_model(Country, row[2], countries)
                school = get_model(School, row[0], schools)
                if len(row) < 4:
                    rejected = False  # allow for the rejected field to be null
                else:
                    rejected = row[3].lower() == "true"  # use the provided value if admin provides it
                yield (committee.id, country.id, school.id, rejected)

        Assignment.update_assignments(generate_assigments(reader))
        return HttpResponseRedirect(reverse("admin:core_assignment_changelist"))
Example #5
0
    def new_assignment(**kwargs):
        test_committee = kwargs.pop("committee", None) or TestCommittees.new_committee()
        test_school = kwargs.pop("school", None) or TestSchools.new_school()
        test_country = kwargs.pop("country", None) or TestCountries.new_country()

        a = Assignment(
            committee=test_committee, school=test_school, country=test_country, rejected=kwargs.pop("rejected", False)
        )
        a.save()
        return a
Example #6
0
    def new_assignment(**kwargs):
        test_committee = TestCommittees.new_committee()
        test_school = TestSchools.new_school()
        test_country = TestCountries.new_country()

        a = Assignment(committee=kwargs.pop('committee', test_committee),
                       school=kwargs.pop('school', test_school),
                       country=kwargs.pop('country', test_country),
                       rejected=kwargs.pop('rejected', False))
        a.save()
        return a
Example #7
0
def new_assignment(**kwargs):
    test_committee = kwargs.pop('committee', None) or new_committee()
    test_school = kwargs.pop('school', None) or new_school()
    test_country = kwargs.pop('country', None) or new_country()

    a = Assignment(
        committee=test_committee,
        school=test_school,
        country=test_country,
        rejected=kwargs.pop('rejected', False),)
    a.save()
    return a
Example #8
0
    def new_assignment(**kwargs):
        test_committee = TestCommittees.new_committee()
        test_school = TestSchools.new_school()
        test_country = TestCountries.new_country()

        a = Assignment(
                committee=kwargs.pop('committee', test_committee),
                school=kwargs.pop('school', test_school),
                country=kwargs.pop('country', test_country),
                rejected=kwargs.pop('rejected', False))
        a.save()
        return a
Example #9
0
def new_assignment(**kwargs):
    test_committee = kwargs.pop('committee', None) or new_committee()
    test_registration = kwargs.pop('registration', None) or new_registration()
    test_country = kwargs.pop('country', None) or new_country()

    a = Assignment(
        committee=test_committee,
        registration=test_registration,
        country=test_country,
        rejected=kwargs.pop('rejected', False),
    )
    a.save()
    return a
Example #10
0
def new_assignment(**kwargs):
    test_committee = kwargs.pop('committee', None) or new_committee()
    test_school = kwargs.pop('school', None) or new_school()
    test_country = kwargs.pop('country', None) or new_country()

    a = Assignment(
        committee=test_committee,
        school=test_school,
        country=test_country,
        rejected=kwargs.pop('rejected', False),
    )
    a.save()
    return a
Example #11
0
def new_assignment(**kwargs):
    test_committee = kwargs.pop('committee', None) or new_committee()
    test_registration = kwargs.pop('registration', None) or new_registration()
    test_country = kwargs.pop('country', None) or new_country()
    test_paper = kwargs.pop('paper', None)

    a = Assignment(
        committee=test_committee,
        registration=test_registration,
        country=test_country,
        paper=test_paper,
        rejected=kwargs.pop('rejected', False), )
    a.save()
    return a
Example #12
0
    def test_update_assignments(self):
        '''It should correctly update the set of country assignments.'''
        cm1 = models.new_committee(name='CM1')
        cm2 = models.new_committee(name='CM2')
        ct1 = models.new_country(name='CT1')
        ct2 = models.new_country(name='CT2')
        ct3 = models.new_country(name='CT3')
        s1 = models.new_school(name='S1')
        r1 = models.new_registration(school=s1)
        s2 = models.new_school(name='S2')
        r2 = models.new_registration(school=s2)

        Assignment.objects.bulk_create([
            Assignment(committee_id=cm.id,
                       country_id=ct.id,
                       registration_id=r1.id) for ct in [ct1, ct2]
            for cm in [cm1, cm2]
        ])

        a = Assignment.objects.get(committee_id=cm2.id, country_id=ct2.id)
        d1 = models.new_delegate(school=s1, assignment=a)
        d2 = models.new_delegate(school=s1, assignment=a)

        # TODO: Also assert on delegate deletion.
        updates = [
            (cm1, ct1, s1, False),
            (cm1, ct2, s1, False),
            (cm1, ct3, s1, False),  # ADDED
            # (cm2, ct1, s1), # DELETED
            (cm2, ct2, s2, False),  # UPDATED
            (cm2, ct3, s2, False),  # ADDED
        ]

        all_assignments = [
            (cm1.id, ct1.id, r1.id, False),
            (cm1.id, ct2.id, r1.id, False),
            (cm1.id, ct3.id, r1.id, False),
            (cm2.id, ct2.id, r2.id, False),
            (cm2.id, ct3.id, r2.id, False),
            (cm2.id, ct1.id, r1.id, False),
        ]

        Assignment.update_assignments(updates)
        assignments = [a[1:-1] for a in Assignment.objects.all().values_list()]
        delegates = Delegate.objects.all()
        self.assertEquals(set(all_assignments), set(assignments))
        self.assertEquals(len(delegates), 2)
Example #13
0
    def test_update_assignments(self):
        '''It should correctly update the set of country assignments.'''
        cm1 = models.new_committee(name='CM1')
        cm2 = models.new_committee(name='CM2')
        ct1 = models.new_country(name='CT1')
        ct2 = models.new_country(name='CT2')
        ct3 = models.new_country(name='CT3')
        s1 = models.new_school(name='S1')
        r1 = models.new_registration(school=s1)
        s2 = models.new_school(name='S2')
        r2 = models.new_registration(school=s2)

        Assignment.objects.bulk_create([
            Assignment(
                committee_id=cm.id, country_id=ct.id, registration_id=r1.id)
            for ct in [ct1, ct2] for cm in [cm1, cm2]
        ])

        a = Assignment.objects.get(committee_id=cm2.id, country_id=ct2.id)
        d1 = models.new_delegate(school=s1, assignment=a)
        d2 = models.new_delegate(school=s1, assignment=a)

        # TODO: Also assert on delegate deletion.
        updates = [
            (cm1, ct1, s1, False),
            (cm1, ct2, s1, False),
            (cm1, ct3, s1, False),  # ADDED
            # (cm2, ct1, s1), # DELETED
            (cm2, ct2, s2, False),  # UPDATED
            (cm2, ct3, s2, False),  # ADDED
        ]

        all_assignments = [
            (cm1.id, ct1.id, r1.id, False),
            (cm1.id, ct2.id, r1.id, False),
            (cm1.id, ct3.id, r1.id, False),
            (cm2.id, ct2.id, r2.id, False),
            (cm2.id, ct3.id, r2.id, False),
            (cm2.id, ct1.id, r1.id, False),
        ]

        Assignment.update_assignments(updates)
        assignments = [a[1:-1] for a in Assignment.objects.all().values_list()]
        delegates = Delegate.objects.all()
        self.assertEquals(set(all_assignments), set(assignments))
        self.assertEquals(len(delegates), 2)
Example #14
0
    def load(self, request):
        '''Loads new Assignments.'''
        assignments = request.FILES
        reader = csv.reader(
            assignments['csv'].read().decode('utf-8').splitlines())

        def get_model(model, name, cache):
            name = name.strip()
            if not name in cache:
                try:
                    cache[name] = model.objects.get(name=name)
                except model.DoesNotExist:
                    cache[name] = name
            return cache[name]

        def generate_assignments(reader):
            committees = {}
            countries = {}
            schools = {}

            for row in reader:
                if len(row) == 0:
                    continue

                if (row[0] == 'School' and row[1] == 'Committee'
                        and row[2] == 'Country'):
                    continue  # skip the first row if it is a header

                while len(row) < 3:
                    row.append(
                        ""
                    )  # extend the row to have the minimum proper num of columns

                if len(row) < 4:
                    rejected = False  # allow for the rejected field to be null
                else:
                    rejected = (
                        row[3].lower() == 'true'
                    )  # use the provided value if admin provides it

                committee = get_model(Committee, row[1], committees)
                country = get_model(Country, row[2], countries)
                school = get_model(School, row[0], schools)
                yield (committee, country, school, rejected)

        failed_rows = Assignment.update_assignments(
            generate_assignments(reader))
        if failed_rows:
            # Format the message with HTML to put each failed assignment on a new line
            messages.error(
                request,
                html.format_html(
                    'Assignment upload aborted. These assignments failed:<br/>'
                    + '<br/>'.join(failed_rows)))

        return HttpResponseRedirect(
            reverse('admin:core_assignment_changelist'))
Example #15
0
 def test_create_position_paper(self):
     '''Tests that an assigment creates a new position paper upon
        being saved for the first time, but not on subsequent saves.'''
     a = Assignment(committee_id=1, country_id=1, registration_id=1)
     self.assertTrue(a.paper == None)
     a.save()
     self.assertTrue(a.paper != None)
     paper_id = a.paper.id
     a.paper.graded = True
     a.paper.save()
     a.save()
     self.assertTrue(a.paper.graded)
     self.assertEquals(a.paper.id, paper_id)
     a.paper = None
     a.save()
     self.assertFalse(a.paper == None)
     self.assertFalse(a.paper.id == paper_id)
Example #16
0
    def load(self, request):
        '''Loads new Assignments.'''
        assignments = request.FILES
        reader = csv.reader(assignments['csv'])

        def get_model(model, name, cache):
            if not name in cache:
                cache[name] = model.objects.get(name=name)
            return cache[name]

        def generate_assigments(reader):
            committees = {}
            countries = {}
            schools = {}
            for row in reader:
                if (len(row[4]) < 2): #ignore the first row because of headers
                    committee = get_model(Committee, row[2], committees)
                    country = get_model(Country, row[3], countries)
                    school = get_model(School, row[0], schools)
                    yield (committee.id, country.id, school.id)

        Assignment.update_assignments(generate_assigments(reader))
        return HttpResponseRedirect(reverse('admin:core_assignment_changelist'))
Example #17
0
 def test_create_position_paper(self):
     '''Tests that an assigment creates a new position paper upon
        being saved for the first time, but not on subsequent saves.'''
     a = Assignment(committee_id=1, country_id=1, registration_id=1)
     self.assertTrue(a.paper == None)
     a.save()
     self.assertTrue(a.paper != None)
     paper_id = a.paper.id
     a.paper.graded = True
     a.paper.save()
     a.save()
     self.assertTrue(a.paper.graded)
     self.assertEquals(a.paper.id, paper_id)
     a.paper = None
     a.save()
     self.assertFalse(a.paper == None)
     self.assertFalse(a.paper.id == paper_id)
Example #18
0
    def test_update_assignments(self):
        '''It should correctly update the set of country assignments.'''
        cm1 = TestCommittees.new_committee(name='CM1')
        cm2 = TestCommittees.new_committee(name='CM2')
        ct1 = TestCountries.new_country(name='CT1')
        ct2 = TestCountries.new_country(name='CT2')
        ct3 = TestCountries.new_country(name='CT3')
        s1 = TestSchools.new_school(name='S1')
        s2 = TestSchools.new_school(name='S2')

        Assignment.objects.bulk_create([
            Assignment(committee_id=cm.id, country_id=ct.id, school_id=s1.id)
            for ct in [ct1, ct2]
            for cm in [cm1, cm2]
        ])

        a = Assignment.objects.get(committee_id=cm2.id, country_id=ct2.id)
        d1 = TestDelegates.new_delegate(school=s1, assignment=a)
        d2 = TestDelegates.new_delegate(school=s1, assignment=a)

        # TODO: Also assert on delegate deletion.
        updates = [
            (cm1, ct1, s1, False),
            (cm1, ct2, s1, False),
            (cm1, ct3, s1, False),   # ADDED
            # (cm2, ct1, s1), # DELETED
            (cm2, ct2, s2, False),   # UPDATED
            (cm2, ct3, s2, False),   # ADDED
        ]

        Assignment.update_assignments(updates)
        new_assignments = [a[1:] for a in Assignment.objects.all().values_list()]
        delegates = Delegate.objects.all()
        updates = [(cm.id, ct.id, s.id, rej) for cm, ct, s, rej in updates]
        self.assertEquals(set(updates), set(new_assignments))
        self.assertEquals(len(delegates), 2)
Example #19
0
    def load(self, request):
        '''Loads new Assignments.'''
        assignments = request.FILES
        reader = csv.reader(assignments['csv'])

        def get_model(model, name, cache):
            name = name.strip()
            if not name in cache:
                try:
                    cache[name] = model.objects.get(name=name)
                except model.DoesNotExist:
                    cache[name] = name
            return cache[name]

        def generate_assignments(reader):
            committees = {}
            countries = {}
            schools = {}

            for row in reader:
                if (row[0]=='School' and row[1]=='Committee' and row[2]=='Country'):
                    continue # skip the first row if it is a header

                while len(row) < 3:
                    row.append("") # extend the row to have the minimum proper num of columns

                if len(row) < 4:
                    rejected = False # allow for the rejected field to be null
                else:
                    rejected = (row[3].lower() == 'true') # use the provided value if admin provides it

                committee = get_model(Committee, row[1], committees)
                country = get_model(Country, row[2], countries)
                school = get_model(School, row[0], schools)
                yield (committee, country, school, rejected)


        failed_rows = Assignment.update_assignments(generate_assignments(reader))
        if failed_rows:
            # Format the message with HTML to put each failed assignment on a new line
            messages.error(request,
                html.format_html('Assignment upload aborted. These assignments failed:<br/>' + '<br/>'.join(failed_rows)))

        return HttpResponseRedirect(reverse('admin:core_assignment_changelist'))
Example #20
0
from xlrd import open_workbook

s = open_workbook('Country Matrix.xlsx').sheet_by_index(0)

country_range = s.nrows - 2
committee_range = 22

for row in range(3, country_range):
    Country.objects.get_or_create(name=s.cell(row, 0).value,
                                  special=(True if row > 204 else False))

for col in range(1, committee_range):
    Committee.objects.get_or_create(
        name=s.cell(1, col).value,
        full_name=s.cell(2, col).value,
        delegation_size=(1 if s.cell(0, col).value == 'SINGLE' else 2),
        special=(True if col > 7 else False))

for row in range(3, country_range):
    for col in range(1, committee_range):
        if s.cell(row, col).value:
            print s.cell(1, col).value
            print s.cell(2, col).value
            print s.cell(row, 0).value
            print s.cell(row, col).value
            print
            country = Country.objects.get(name=s.cell(row, 0).value)
            committee = Committee.objects.get(name=s.cell(1, col).value)
            assignment = Assignment(committee=committee, country=country)
            assignment.save()
Example #21
0
sys.path.append(abspath(dirname(dirname(__file__))))
os.environ['DJANGO_SETTINGS_MODULE'] = 'huxley.settings'

from huxley.core.models import Country, Committee, Assignment
from xlrd import open_workbook

s = open_workbook('Country Matrix.xlsx').sheet_by_index(0)

country_range = s.nrows-2
committee_range = 22

for row in range(3, country_range):
	Country.objects.get_or_create(name=s.cell(row, 0).value, special=(True if row > 204 else False))

for col in range(1, committee_range):
	Committee.objects.get_or_create(name=s.cell(1, col).value, full_name=s.cell(2, col).value, delegation_size=(1 if s.cell(0, col).value == 'SINGLE' else 2), special=(True if col > 7 else False))

for row in range(3, country_range):
	for col in range(1, committee_range):
		if s.cell(row, col).value:
			print s.cell(1, col).value
			print s.cell(2, col).value
			print s.cell(row, 0).value
			print s.cell(row,col).value
			print
			country = Country.objects.get(name=s.cell(row, 0).value)
			committee = Committee.objects.get(name=s.cell(1, col).value)
			assignment = Assignment(committee=committee, country=country)
			assignment.save()