def test_view_explicit_earliest(self): url = reverse('api:export-members') data = {'earliest': universal_date_format(datetime.date.today())} response = self.client.get(url, data, format='json') content = response.content.decode('utf-8') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(json.loads(content), self.expecting)
def test_view(self): # turn dates into strings for the sake of this test for i, event in enumerate(self.expecting): for date in ['start', 'end']: self.expecting[i][date] = universal_date_format( self.expecting[i][date], ) # test only JSON output url = reverse(self.url) response = self.client.get(url, format='json') content = response.content.decode('utf-8') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(json.loads(content), self.expecting)
def _setUpEvents(self): """Set up a bunch of events and record some statistics.""" today = datetime.date.today() # Create a test host test_host = Organization.objects.create(domain="example.com", fullname="Test Organization") # Create one new published event for each day in the next 10 days. for t in range(1, 11): event_start = today + datetime.timedelta(days=t) date_string = universal_date_format(event_start) slug = "{0}-upcoming".format(date_string) url = "http://example.org/" + ("{0}".format(t) * 20) e = Event.objects.create( start=event_start, slug=slug, host=test_host, url=url, country="US", venue="School", address="Overthere", latitude=1, longitude=2, ) # Create one new event for each day from 10 days ago to 3 days ago for t in range(3, 11): event_start = today + datetime.timedelta(days=-t) date_string = universal_date_format(event_start) Event.objects.create( start=event_start, slug="{0}-past".format(date_string), host=test_host, ) # create a past event that has no admin fee specified event_start = today + datetime.timedelta(days=-4) Event.objects.create( start=event_start, end=today + datetime.timedelta(days=-1), slug="{}-past-uninvoiced".format( universal_date_format(event_start)), host=test_host, ) # Create an event that started yesterday and ends tomorrow # with no fee event_start = today + datetime.timedelta(days=-1) event_end = today + datetime.timedelta(days=1) Event.objects.create( start=event_start, end=event_end, slug="ends-tomorrow-ongoing", host=test_host, url="http://example.org/ends-tomorrow-ongoing", country="US", venue="School", address="Overthere", latitude=1, longitude=2, ) # Create an event that ends today with no fee event_start = today + datetime.timedelta(days=-1) event_end = today Event.objects.create( start=event_start, end=event_end, slug="ends-today-ongoing", host=test_host, url="http://example.org/ends-today-ongoing", country="US", venue="School", address="Overthere", latitude=1, longitude=2, ) # Create an event that starts today with a fee event_start = today event_end = today + datetime.timedelta(days=1) Event.objects.create( start=event_start, end=event_end, slug="starts-today-ongoing", host=test_host, ) # create a full-blown event that got cancelled e = Event.objects.create( start=event_start, end=event_end, slug="starts-today-cancelled", url="http://example.org/cancelled-event", latitude=-10.0, longitude=10.0, country="US", venue="University", address="Phenomenal Street", host=test_host, ) tags = Tag.objects.filter(name__in=["SWC", "cancelled"]) e.tags.set(tags) # Record some statistics about events. self.num_upcoming = 0 for e in Event.objects.all(): e.is_past_event = e.start < today and (e.end is None or e.end < today) if e.url and (e.start > today): self.num_upcoming += 1
def _setUpEvents(self): '''Set up a bunch of events and record some statistics.''' today = datetime.date.today() # Create a test host test_host = Organization.objects.create(domain='example.com', fullname='Test Organization') # Create one new published event for each day in the next 10 days. for t in range(1, 11): event_start = today + datetime.timedelta(days=t) date_string = universal_date_format(event_start) slug = '{0}-upcoming'.format(date_string) url = 'http://example.org/' + ('{0}'.format(t) * 20) e = Event.objects.create(start=event_start, slug=slug, host=test_host, admin_fee=100, url=url, invoice_status='not-invoiced', country='US', venue='School', address='Overthere', latitude=1, longitude=2) # Create one new event for each day from 10 days ago to # 3 days ago, half invoiced invoice = itertools.cycle(['invoiced', 'not-invoiced']) for t in range(3, 11): event_start = today + datetime.timedelta(days=-t) date_string = universal_date_format(event_start) Event.objects.create(start=event_start, slug='{0}-past'.format(date_string), host=test_host, admin_fee=100, invoice_status=next(invoice)) # create a past event that has no admin fee specified, yet it needs # invoice event_start = today + datetime.timedelta(days=-4) Event.objects.create( start=event_start, end=today + datetime.timedelta(days=-1), slug='{}-past-uninvoiced'.format( universal_date_format(event_start)), host=test_host, admin_fee=None, invoice_status='not-invoiced', ) # Create an event that started yesterday and ends tomorrow # with no fee, and without specifying whether they've been # invoiced. event_start = today + datetime.timedelta(days=-1) event_end = today + datetime.timedelta(days=1) Event.objects.create(start=event_start, end=event_end, slug='ends-tomorrow-ongoing', host=test_host, admin_fee=0, url='http://example.org/ends-tomorrow-ongoing', country='US', venue='School', address='Overthere', latitude=1, longitude=2) # Create an event that ends today with no fee, and without # specifying whether the fee has been invoiced. event_start = today + datetime.timedelta(days=-1) event_end = today Event.objects.create(start=event_start, end=event_end, slug='ends-today-ongoing', host=test_host, admin_fee=0, url='http://example.org/ends-today-ongoing', country='US', venue='School', address='Overthere', latitude=1, longitude=2) # Create an event that starts today with a fee, and without # specifying whether the fee has been invoiced. event_start = today event_end = today + datetime.timedelta(days=1) Event.objects.create(start=event_start, end=event_end, slug='starts-today-ongoing', host=test_host, admin_fee=100) # create a full-blown event that got cancelled e = Event.objects.create(start=event_start, end=event_end, slug='starts-today-cancelled', url='http://example.org/cancelled-event', latitude=-10.0, longitude=10.0, country='US', venue='University', address='Phenomenal Street', host=test_host) tags = Tag.objects.filter(name__in=['SWC', 'cancelled']) e.tags.set(tags) # Record some statistics about events. self.num_uninvoiced_events = 0 self.num_upcoming = 0 for e in Event.objects.all(): e.is_past_event = e.start < today and (e.end is None or e.end < today) if e.invoice_status == 'not-invoiced' and e.is_past_event: self.num_uninvoiced_events += 1 if e.url and (e.start > today): self.num_upcoming += 1
def handle(self, *args, **options): fields = [ 'start', 'slug', 'online', 'badge', 'learners', 'completed this', 'completed this [%]', 'completed other', 'completed other [%]', 'no badge', 'no badge [%]', 'taught at least once', 'taught at least once [%]', ] writer = csv.DictWriter(self.stdout, fieldnames=fields) writer.writeheader() for training in self.trainings(): badge = self.badge_type(training.tags.all()) learners = self.learners(training) learners_len = learners.count() completed_len = learners.filter(badges=badge, award__event=training).count() completed_other_len = learners.filter(badges=badge) \ .exclude(award__event=training) \ .count() no_badge_len = learners.exclude(badges=badge).count() # Django tries to optimize every query; for example here I had to # cast to list explicitly to achieve a query without any # WHEREs to task__role__name (which self.learners() unfortunately # has to add). learners2 = Person.objects.filter( pk__in=list(learners.values_list('pk', flat=True))) # 1. Grab people who received a badge for this training # 2. Count how many times each of them taught instructors = learners2.filter(award__badge=badge, award__event=training)\ .annotate( num_taught=Count( Case( When( task__role__name='instructor', # task__event__start__gte=training.start, then=Value(1) ), output_field=IntegerField() ) ) ) # 3. Get only people who taught at least once # 4. And count them instructors_taught_at_least_once = instructors \ .filter(num_taught__gt=0) \ .aggregate(Count('num_taught'))['num_taught__count'] or 0 record = { fields[0]: universal_date_format(training.start), fields[1]: training.slug, fields[2]: int(self.online_tag in training.tags.all()), fields[3]: badge.title, fields[4]: learners_len, fields[5]: completed_len, fields[6]: self.percent(completed_len, learners_len), fields[7]: completed_other_len, fields[8]: self.percent(completed_other_len, learners_len), fields[9]: no_badge_len, fields[10]: self.percent(no_badge_len, learners_len), fields[11]: instructors_taught_at_least_once, fields[12]: self.percent(instructors_taught_at_least_once, learners_len), } writer.writerow(record)
def _setUpEvents(self): '''Set up a bunch of events and record some statistics.''' today = datetime.date.today() # Create a test host test_host = Organization.objects.create(domain='example.com', fullname='Test Organization') # Create one new published event for each day in the next 10 days. for t in range(1, 11): event_start = today + datetime.timedelta(days=t) date_string = universal_date_format(event_start) slug = '{0}-upcoming'.format(date_string) url = 'http://example.org/' + ('{0}'.format(t) * 20) e = Event.objects.create( start=event_start, slug=slug, host=test_host, admin_fee=100, url=url, invoice_status='not-invoiced', country='US', venue='School', address='Overthere', latitude=1, longitude=2) # Create one new event for each day from 10 days ago to # 3 days ago, half invoiced invoice = itertools.cycle(['invoiced', 'not-invoiced']) for t in range(3, 11): event_start = today + datetime.timedelta(days=-t) date_string = universal_date_format(event_start) Event.objects.create(start=event_start, slug='{0}-past'.format(date_string), host=test_host, admin_fee=100, invoice_status=next(invoice)) # create a past event that has no admin fee specified, yet it needs # invoice event_start = today + datetime.timedelta(days=-4) Event.objects.create( start=event_start, end=today + datetime.timedelta(days=-1), slug='{}-past-uninvoiced'.format( universal_date_format(event_start) ), host=test_host, admin_fee=None, invoice_status='not-invoiced', ) # Create an event that started yesterday and ends tomorrow # with no fee, and without specifying whether they've been # invoiced. event_start = today + datetime.timedelta(days=-1) event_end = today + datetime.timedelta(days=1) Event.objects.create( start=event_start, end=event_end, slug='ends-tomorrow-ongoing', host=test_host, admin_fee=0, url='http://example.org/ends-tomorrow-ongoing', country='US', venue='School', address='Overthere', latitude=1, longitude=2) # Create an event that ends today with no fee, and without # specifying whether the fee has been invoiced. event_start = today + datetime.timedelta(days=-1) event_end = today Event.objects.create( start=event_start, end=event_end, slug='ends-today-ongoing', host=test_host, admin_fee=0, url='http://example.org/ends-today-ongoing', country='US', venue='School', address='Overthere', latitude=1, longitude=2) # Create an event that starts today with a fee, and without # specifying whether the fee has been invoiced. event_start = today event_end = today + datetime.timedelta(days=1) Event.objects.create(start=event_start, end=event_end, slug='starts-today-ongoing', host=test_host, admin_fee=100) # create a full-blown event that got cancelled e = Event.objects.create(start=event_start, end=event_end, slug='starts-today-cancelled', url='http://example.org/cancelled-event', latitude=-10.0, longitude=10.0, country='US', venue='University', address='Phenomenal Street', host=test_host) tags = Tag.objects.filter(name__in=['SWC', 'cancelled']) e.tags.set(tags) # Record some statistics about events. self.num_uninvoiced_events = 0 self.num_upcoming = 0 for e in Event.objects.all(): e.is_past_event = e.start < today and (e.end is None or e.end < today) if e.invoice_status == 'not-invoiced' and e.is_past_event: self.num_uninvoiced_events += 1 if e.url and (e.start > today): self.num_upcoming += 1