def setUp(self): self.p1 = Parent( name = 'John Smith', email = '*****@*****.**', phone = '0400000000' ) self.p1.save() self.n1 = Ninja( name = 'Ninja One', email = '*****@*****.**', gender = Ninja.MALE, postcode = 6000, school = 'Example Primary School', school_year = 4, allergies = 'Bullshit', attended_workshop = True, referral = '4chan', laptop = True, aim = "To hack the gibson", general_knowledge = knowledge.NOTHING, scratch_knowledge = knowledge.NOTHING, codecademy_knowledge = knowledge.NOTHING, language_experience = 'Brainfsck', black_belt = True, photo_release = True, parent = self.p1 ) self.n1.save() self.n2 = Ninja( name = 'Ninja Two', email = '*****@*****.**', gender = Ninja.FEMALE, postcode = 6000, school = 'Example Primary School', school_year = 4, attended_workshop = False, referral = '4chan', laptop = True, aim = "To hack the gibson", general_knowledge = knowledge.NOTHING, scratch_knowledge = knowledge.NOTHING, codecademy_knowledge = knowledge.NOTHING, language_experience = 'Brainfsck', black_belt = False, photo_release = False, parent = self.p1 ) self.n2.save()
def index(request): return render(request, 'ninjas/index.html', { 'ninja_stats' : Ninja.get_stats(), 'parent_stats': Parent.get_stats() })
def upload(request): if request.method == 'POST': form = CSVImportForm(request.POST, request.FILES) if not form.is_valid(): return render(request, 'ninjas/upload/failure.html') else: # form is valid, start parsing CSV. # Load the sessions that we need. sessions = { '2 August': DojoSession.objects.get(date = datetime.date(2014, 8, 2)), '9 August': DojoSession.objects.get(date = datetime.date(2014, 8, 9)), '16 August': DojoSession.objects.get(date = datetime.date(2014, 8, 16)), '23 August': DojoSession.objects.get(date = datetime.date(2014, 8, 23)), '30 August': DojoSession.objects.get(date = datetime.date(2014, 8, 30)), '6 September': DojoSession.objects.get(date = datetime.date(2014, 9, 6)), '13 September': DojoSession.objects.get(date = datetime.date(2014, 9, 13)), '20 September': DojoSession.objects.get(date = datetime.date(2014, 9, 20)), } print sessions # First detect dialect csvfile = request.FILES['csv'] dialect = csv.Sniffer().sniff(csvfile.read(2048)) csvfile.seek(0) # Open reader and parse each row. reader = csv.DictReader(csvfile, dialect=dialect) for row in reader: # First, check if Ninja exists. same = Ninja.objects.filter(name = row['Full Name']) if same: ninja = same[0] else: ninja = Ninja(name = row['Full Name']) if row['Gender'] == 'Male': ninja.gender = ninja.MALE elif row['Gender'] == 'Female': ninja.gender = ninja.FEMALE ninja.email = row['Email Address'] ninja.school = row['School'] ninja.school_year = row['School Year'] ninja.postcode = row['Postcode'] ninja.allergies = csv_tools.none_catch(row['Allergies/Dietary Restrictions']) ninja.attended_workshop = csv_tools.yes_no(row['Been Before?']) ninja.referral = row['Referral'] ninja.laptop = csv_tools.yes_no(row['Laptop']) ninja.aim = row['aim'] = row['Aim'] ninja.general_knowledge = csv_tools.knowledge_parse(row['Coding Knowledge']) ninja.scratch_knowledge = csv_tools.knowledge_parse(row['Scratch Knowledge']) ninja.codecademy_knowledge = csv_tools.knowledge_parse(row['Codecademy Knowledge']) ninja.language_experience = row['Programming Languages'] ninja.black_belt = csv_tools.yes_no(row['Black Belt'], fuzzy = True) ninja.photo_release = csv_tools.yes_no(row['Photo Permission'], fuzzy = True) # Now for the parent/guardian same = Parent.objects.filter(email = row['Parent/Guardian Email']) if same: pa = same[0] else: pa = Parent(email = row['Parent/Guardian Email']) pa.name = row['Parent/Guardian Name'] pa.phone = '0' + row['Phone Number'] pa.save() ninja.parent = pa ninja.save() # Availabilities for session in sessions: if row['Availability [Saturday %s]' % session] == 'Available': ninja.availabilities.add(sessions[session]) return render(request, 'ninjas/upload/success.html') else: return render(request, 'ninjas/upload/failure.html')
class NinjaTestCase(TestCase): def setUp(self): self.p1 = Parent( name = 'John Smith', email = '*****@*****.**', phone = '0400000000' ) self.p1.save() self.n1 = Ninja( name = 'Ninja One', email = '*****@*****.**', gender = Ninja.MALE, postcode = 6000, school = 'Example Primary School', school_year = 4, allergies = 'Bullshit', attended_workshop = True, referral = '4chan', laptop = True, aim = "To hack the gibson", general_knowledge = knowledge.NOTHING, scratch_knowledge = knowledge.NOTHING, codecademy_knowledge = knowledge.NOTHING, language_experience = 'Brainfsck', black_belt = True, photo_release = True, parent = self.p1 ) self.n1.save() self.n2 = Ninja( name = 'Ninja Two', email = '*****@*****.**', gender = Ninja.FEMALE, postcode = 6000, school = 'Example Primary School', school_year = 4, attended_workshop = False, referral = '4chan', laptop = True, aim = "To hack the gibson", general_knowledge = knowledge.NOTHING, scratch_knowledge = knowledge.NOTHING, codecademy_knowledge = knowledge.NOTHING, language_experience = 'Brainfsck', black_belt = False, photo_release = False, parent = self.p1 ) self.n2.save() def test_get_short_name(self): self.assertEqual(self.n1.get_short_name(), 'Ninja O') def test_get_call_url(self): self.assertEqual(self.n1.get_call_url(), 'tel:0400000000') def test_get_email_url(self): self.assertEqual(self.n1.get_email_url(), 'mailto:[email protected]') def test_parent_required_lower(self): self.n1.school_year = 5 self.n1.save() self.assertTrue(self.n1.parent_required()) def test_parent_required_border(self): self.n1.school_year = 6 self.n1.save() self.assertTrue(self.n1.parent_required()) def test_parent_required_higher(self): self.n1.school_year = 7 self.n1.save() self.assertFalse(self.n1.parent_required()) def test_get_new(self): self.assertEqual(Ninja.get_new(), 1) def test_get_returning(self): self.assertEqual(Ninja.get_returning(), 1) def test_get_allergies(self): a = Ninja.get_allergies() self.assertEqual(len(a), 1) self.assertEqual(a[0], self.n1) def test_get_photos(self): a = Ninja.get_photos() self.assertEqual(len(a), 1) self.assertEqual(a[0], self.n2) def test_get_male(self): self.assertEqual(Ninja.get_male(), 1) def test_get_female(self): self.assertEqual(Ninja.get_female(), 1) def test_get_suburb(self): self.assertEqual(self.n1.get_suburb(), 'CITY DELIVERY CENTRE') def test_get_suburb_list(self): self.assertIn('PERTH', self.n1.get_suburb_list())