def test_iterator_when_not_forbidden_format(self): """Ensure other formats than self.formats return iterators/generators, not lists.""" format_ = 'json' self.assertNotIn(format_, self.formats) rvs = ReportsViewSet() mock_request = MagicMock() mock_request.query_params = QueryDict() result = rvs.listify(self.iterable, mock_request, format=format_) self.assertNotEqual(type(result), type(list()))
def test_listify_format_as_param(self): """Regression test: make sure it's possible to iterate through results serialized with CSV or YAML serializer. This test uses 'format' function parameter.""" rvs = ReportsViewSet() for format_ in self.formats: with self.subTest(format=format_): mock_request = MagicMock() mock_request.query_params = QueryDict() result = rvs.listify(self.iterable, mock_request, format=format_) self.assertEqual(type(result), type(list()))
def test_listify_query_param(self): """Regression test: make sure it's possible to iterate through results serialized with CSV or YAML serializer. This test uses `?format' query param.""" rvs = ReportsViewSet() for format_ in self.formats: with self.subTest(format=format_): mock_request = MagicMock() mock_request.query_params = QueryDict( 'format={}'.format(format_)) result = rvs.listify(self.iterable, mock_request) self.assertEqual(type(result), type(list()))
def all_activity_over_time(request): """Display number of workshops (of differend kinds), instructors and learners over some specific period of time.""" if 'submit' in request.GET: form = AllActivityOverTimeForm(request.GET) if form.is_valid(): data = ReportsViewSet().get_all_activity_over_time( start=form.cleaned_data['start'], end=form.cleaned_data['end'], ) else: data = None else: form = AllActivityOverTimeForm(initial={ 'start': datetime.date.today() - datetime.timedelta(days=365), 'end': datetime.date.today(), }) data = None context = { 'title': 'All activity over time', 'form': form, 'data': data, } return render(request, 'reports/all_activity_over_time.html', context)
def test_embedded_iterator_listified(self): """Regression: test if advanced structure, generated e.g. by `all_activity_over_time` report, doesn't raise RepresenterError when used with YAML renderer.""" t = TestBase() t.setUp() t._setUpTags() format_ = 'yaml' self.assertIn(format_, self.formats) rvs = ReportsViewSet() mock_request = MagicMock() mock_request.query_params = QueryDict() data = rvs.all_activity_over_time(mock_request, format=format_).data self.assertEqual(type(data['missing']['attendance']), type([])) self.assertEqual(type(data['missing']['instructors']), type([]))
def instructors_by_date(request): '''Show who taught between begin_date and end_date.''' form = DebriefForm() if 'begin_date' in request.GET and 'end_date' in request.GET: form = DebriefForm(request.GET) if form.is_valid(): start_date = form.cleaned_data['begin_date'] end_date = form.cleaned_data['end_date'] mode = form.cleaned_data['mode'] rvs = ReportsViewSet() tasks = rvs.instructors_by_time_queryset( start_date, end_date, only_TTT=(mode == 'TTT'), only_non_TTT=(mode == 'nonTTT'), ) emails = tasks.filter(person__may_contact=True) \ .exclude(person__email=None) \ .values_list('person__email', flat=True) else: start_date = None end_date = None tasks = None emails = None mode = 'all' context = { 'title': 'List of instructors by time period', 'form': form, 'all_tasks': tasks, 'emails': emails, 'start_date': start_date, 'end_date': end_date, 'mode': mode, } return render(request, 'reports/instructors_by_date.html', context)