def test_upload_existing_user(self): """ Check if uploading existing users ends up with them having new role assigned. This is a special case of upload feature: if user uploads a person that already exists we should only assign new role and event to that person. """ csv = """personal,family,email,event,role Harry,Potter,[email protected],foobar,Helper """ data, _ = upload_person_task_csv(StringIO(csv)) # simulate user clicking "Use this user" next to matched person data[0]['existing_person_id'] = \ Person.objects.get(email='*****@*****.**').pk # self.client is authenticated user so we have access to the session store = self.client.session store['bulk-add-people'] = data store.save() # send exactly what's in 'data' payload = { "personal": data[0]['personal'], "family": data[0]['family'], "email": data[0]['email'], "event": data[0]['event'], "role": data[0]['role'], "confirm": "Confirm", } people_pre = set(Person.objects.all()) tasks_pre = set( Task.objects.filter(person=self.harry, event__slug="foobar")) rv = self.client.post(reverse('person_bulk_add_confirmation'), payload, follow=True) self.assertEqual(rv.status_code, 200) people_post = set(Person.objects.all()) tasks_post = set( Task.objects.filter(person=self.harry, event__slug="foobar")) # make sure no-one new was added self.assertSetEqual(people_pre, people_post) # make sure that Harry was assigned a new role self.assertNotEqual(tasks_pre, tasks_post)
def test_upload_existing_user(self): """ Check if uploading existing users ends up with them having new role assigned. This is a special case of upload feature: if user uploads a person that already exists we should only assign new role and event to that person. """ csv = """personal,family,email,event,role Harry,Potter,[email protected],foobar,Helper """ data, _ = upload_person_task_csv(StringIO(csv)) # simulate user clicking "Use this user" next to matched person data[0]['existing_person_id'] = \ Person.objects.get(email='*****@*****.**').pk # self.client is authenticated user so we have access to the session store = self.client.session store['bulk-add-people'] = data store.save() # send exactly what's in 'data' payload = { "personal": data[0]['personal'], "family": data[0]['family'], "email": data[0]['email'], "event": data[0]['event'], "role": data[0]['role'], "confirm": "Confirm", } people_pre = set(Person.objects.all()) tasks_pre = set(Task.objects.filter(person=self.harry, event__slug="foobar")) rv = self.client.post(reverse('person_bulk_add_confirmation'), payload, follow=True) self.assertEqual(rv.status_code, 200) people_post = set(Person.objects.all()) tasks_post = set(Task.objects.filter(person=self.harry, event__slug="foobar")) # make sure no-one new was added self.assertSetEqual(people_pre, people_post) # make sure that Harry was assigned a new role self.assertNotEqual(tasks_pre, tasks_post)
def test_removing_entry2(self): """Make sure entries are removed by the view.""" data, _ = upload_person_task_csv(StringIO(self.csv)) # self.client is authenticated user so we have access to the session store = self.client.session store['bulk-add-people'] = data store.save() self.client.get(reverse('person_bulk_add_remove_entry', args=[2])) # need to recreate session store = self.client.session data = store['bulk-add-people'] self.assertEqual(2, len(data)) self.assertEqual(data[0]['personal'], 'Harry') self.assertEqual(data[1]['personal'], 'Hermione')
def test_removing_entry1(self): """Make sure entries are removed by the view.""" data, _ = upload_person_task_csv(StringIO(self.csv)) # self.client is authenticated user so we have access to the session store = self.client.session store["bulk-add-people"] = data store.save() self.client.get(reverse("person_bulk_add_remove_entry", args=[1])) # need to recreate session store = self.client.session data = store["bulk-add-people"] self.assertEqual(2, len(data)) self.assertEqual(data[0]["personal"], "Harry") self.assertEqual(data[1]["personal"], "Ron")
def test_upload_existing_user_existing_task(self): """ Check if uploading existing user and assigning existing task to that user is silent (ie. no Task nor Person is being created). """ foobar = Event.objects.get(slug="foobar") instructor = Role.objects.get(name="instructor") Task.objects.create(person=self.harry, event=foobar, role=instructor) csv = """personal,family,email,event,role Harry,Potter,[email protected],foobar,instructor """ data, _ = upload_person_task_csv(StringIO(csv)) # self.client is authenticated user so we have access to the session store = self.client.session store['bulk-add-people'] = data store.save() # send exactly what's in 'data' payload = { "personal": data[0]['personal'], "family": data[0]['family'], "email": data[0]['email'], "event": data[0]['event'], "role": data[0]['role'], "confirm": "Confirm", } tasks_pre = set( Task.objects.filter(person=self.harry, event__slug="foobar")) users_pre = set(Person.objects.all()) rv = self.client.post(reverse('person_bulk_add_confirmation'), payload, follow=True) tasks_post = set( Task.objects.filter(person=self.harry, event__slug="foobar")) users_post = set(Person.objects.all()) self.assertEqual(tasks_pre, tasks_post) self.assertEqual(users_pre, users_post) self.assertEqual(rv.status_code, 200)
def test_upload_existing_user_existing_task(self): """ Check if uploading existing user and assigning existing task to that user is silent (ie. no Task nor Person is being created). """ foobar = Event.objects.get(slug="foobar") instructor = Role.objects.get(name="Instructor") Task.objects.create(person=self.harry, event=foobar, role=instructor) csv = """personal,family,email,event,role Harry,Potter,[email protected],foobar,Instructor """ data, _ = upload_person_task_csv(StringIO(csv)) # self.client is authenticated user so we have access to the session store = self.client.session store['bulk-add-people'] = data store.save() # send exactly what's in 'data' payload = { "personal": data[0]['personal'], "family": data[0]['family'], "email": data[0]['email'], "event": data[0]['event'], "role": data[0]['role'], "confirm": "Confirm", } tasks_pre = set(Task.objects.filter(person=self.harry, event__slug="foobar")) users_pre = set(Person.objects.all()) rv = self.client.post(reverse('person_bulk_add_confirmation'), payload, follow=True) tasks_post = set(Task.objects.filter(person=self.harry, event__slug="foobar")) users_post = set(Person.objects.all()) self.assertEqual(tasks_pre, tasks_post) self.assertEqual(users_pre, users_post) self.assertEqual(rv.status_code, 200)
def test_attendance_increases(self): """ Check if uploading tasks with role "learner" increase event's attendance. """ foobar = Event.objects.get(slug="foobar") self.assertEqual(foobar.attendance, 0) foobar.save() csv = """personal,family,email,event,role Harry,Potter,[email protected],foobar,learner """ data, _ = upload_person_task_csv(StringIO(csv)) # simulate user clicking "Use this user" next to matched person data[0]['existing_person_id'] = \ Person.objects.get(email='*****@*****.**').pk # self.client is authenticated user so we have access to the session store = self.client.session store['bulk-add-people'] = data store.save() # send exactly what's in 'data' payload = { "personal": data[0]['personal'], "family": data[0]['family'], "email": data[0]['email'], "event": data[0]['event'], "role": data[0]['role'], "confirm": "Confirm", } self.client.post(reverse('person_bulk_add_confirmation'), payload, follow=True) # instead of refreshing, we have to get a "fresh" object, because # `attendance` is a cached property foobar = Event.objects.get(slug="foobar") self.assertEqual(foobar.attendance, 1)
def person_bulk_add(request): if request.method == 'POST': form = PersonBulkAddForm(request.POST, request.FILES) if form.is_valid(): charset = request.FILES['file'].charset or settings.DEFAULT_CHARSET stream = io.TextIOWrapper(request.FILES['file'].file, charset) try: persons_tasks, empty_fields = upload_person_task_csv(stream) except csv.Error as e: messages.add_message( request, messages.ERROR, "Error processing uploaded .CSV file: {}".format(e)) except UnicodeDecodeError as e: messages.add_message( request, messages.ERROR, "Please provide a file in {} encoding." .format(charset)) else: if empty_fields: msg_template = ("The following required fields were not" " found in the uploaded file: {}") msg = msg_template.format(', '.join(empty_fields)) messages.add_message(request, messages.ERROR, msg) else: # instead of insta-saving, put everything into session # then redirect to confirmation page which in turn saves # the data request.session['bulk-add-people'] = persons_tasks return redirect('person_bulk_add_confirmation') else: form = PersonBulkAddForm() context = { 'title': 'Bulk Add People', 'form': form, 'charset': settings.DEFAULT_CHARSET, } return render(request, 'workshops/person_bulk_add_form.html', context)
def person_bulk_add(request): if request.method == 'POST': form = PersonBulkAddForm(request.POST, request.FILES) if form.is_valid(): charset = request.FILES['file'].charset or settings.DEFAULT_CHARSET stream = io.TextIOWrapper(request.FILES['file'].file, charset) try: persons_tasks, empty_fields = upload_person_task_csv(stream) except csv.Error as e: messages.add_message( request, messages.ERROR, "Error processing uploaded .CSV file: {}".format(e)) except UnicodeDecodeError as e: messages.add_message( request, messages.ERROR, "Please provide a file in {} encoding.".format(charset)) else: if empty_fields: msg_template = ("The following required fields were not" " found in the uploaded file: {}") msg = msg_template.format(', '.join(empty_fields)) messages.add_message(request, messages.ERROR, msg) else: # instead of insta-saving, put everything into session # then redirect to confirmation page which in turn saves # the data request.session['bulk-add-people'] = persons_tasks return redirect('person_bulk_add_confirmation') else: form = PersonBulkAddForm() context = { 'title': 'Bulk Add People', 'form': form, 'charset': settings.DEFAULT_CHARSET, } return render(request, 'workshops/person_bulk_add_form.html', context)
def test_jobs_created(self): data, _ = upload_person_task_csv(StringIO(self.csv)) # simulate user clicking "Use this user" next to matched person data[0]['existing_person_id'] = \ Person.objects.get(email='*****@*****.**').pk data[1]['existing_person_id'] = \ Person.objects.get(email='*****@*****.**').pk data[2]['existing_person_id'] = \ Person.objects.get(email='*****@*****.**').pk # self.client is authenticated user so we have access to the session store = self.client.session store['bulk-add-people'] = data store.save() # send exactly what's in 'data' payload = { "personal": [data[0]['personal'], data[1]['personal'], data[2]['personal']], "family": [data[0]['family'], data[1]['family'], data[2]['family']], "email": [data[0]['email'], data[1]['email'], data[2]['email']], "event": [data[0]['event'], data[1]['event'], data[2]['event']], "role": [data[0]['role'], data[1]['role'], data[2]['role']], "confirm": "Confirm", } # empty tasks and no jobs scheduled tasks_pre = Task.objects.filter( person__in=Person.objects.filter(email__in=[ '*****@*****.**', '*****@*****.**', '*****@*****.**', ]), event__slug="test-event", ) self.assertQuerysetEqual(tasks_pre, []) rqjobs_pre = RQJob.objects.all() self.assertQuerysetEqual(rqjobs_pre, []) # send data in rv = self.client.post(reverse('person_bulk_add_confirmation'), payload, follow=True) self.assertEqual(rv.status_code, 200) # 3 tasks created tasks_post = Task.objects.filter( person__in=Person.objects.filter(email__in=[ '*****@*****.**', '*****@*****.**', '*****@*****.**', ]), event__slug="test-event", ) self.assertEqual(len(tasks_post), 3) # 2 jobs created (because only 2 entries have right role='instructor') rqjobs_post = RQJob.objects.all() self.assertEqual(len(rqjobs_post), 2) # ensure the job ids are mentioned in the page output content = rv.content.decode('utf-8') for job in rqjobs_post: self.assertIn(job.job_id, content)
def compute_from_string(self, csv_str): ''' wrap up buffering the raw string & parsing ''' csv_buf = StringIO(csv_str) # compute and return return upload_person_task_csv(csv_buf)
def make_data(self): csv_str = self.make_csv_data() # upload_person_task_csv gets thoroughly tested in # UploadPersonTaskCSVTestCase data, _ = upload_person_task_csv(StringIO(csv_str)) return data