def test_date_trunc(self): _checkin = Checkin.objects.create(logged_at='2015-11-01 11:14:01') checkin = Checkin.objects. \ annotate(day=DateTrunc('logged_at', 'day'), hour=DateTrunc('logged_at', 'hour')). \ get(pk=_checkin.pk) assert checkin.day == datetime(2015, 11, 1, 0, 0, 0) assert checkin.hour == datetime(2015, 11, 1, 11, 0, 0)
def export_total_check_in(request, *args, **kwargs): from_date = request.POST.get('from_date') to_date = request.POST.get('to_date') begin_datetime_start_of_day = datetime.combine( datetime.strptime(from_date, '%Y-%m-%d'), datetime.min.time()) end_datetime_start_of_day = datetime.combine( datetime.strptime(to_date, '%Y-%m-%d'), datetime.min.time()) response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="check_ins.csv"' writer = csv.writer(response) tagquery = LocationTag.objects.filter(created_at__gte=begin_datetime_start_of_day, created_at__lte=end_datetime_start_of_day) \ .annotate(date=DateTrunc('created_at', precision='day')) filteredTagQuery = [] for tags in tagquery: if tags.location.service == 'IN': filteredTagQuery.append(tags) writer.writerow( ['Subscription', 'Username', 'Email', 'Timestamp', 'Location']) writer.writerow([len(filteredTagQuery)]) for tags in filteredTagQuery: writer.writerow([ tags.subscription, tags.subscription.user.username, tags.subscription.user.email, tags.created_at, tags.location ]) return response
def _get_data(qs): data = qs.filter(created_at__gte=begin_datetime_start_of_day, created_at__lte=end_datetime_start_of_day) \ .annotate(date=DateTrunc('created_at', precision='day')) \ .values("date") \ .annotate(volume=Count("date")) \ .order_by("date") data = [{"date": d['date'].date(), "volume": d['volume']} for d in data] return data
def volume_by_date(self): results = self.qs \ .annotate( date=DateTrunc('time_received', precision=self.precision())) \ .values("date") \ .annotate(volume=Count("date")) \ .order_by("date") return results
def _get_user_data(): # filter this to only count active subscriptions total_active_subs = Subscription.objects.all().count() data = LocationTag.objects.filter(created_at__gte=begin_datetime_start_of_day, created_at__lte=end_datetime_start_of_day) \ .annotate(date=DateTrunc('created_at', precision='day')) \ .values("date", "subscription") \ .distinct() \ .order_by("date") data = dict(Counter(d['date'].date() for d in data)) data = [{"date": date, "volume": "{0:.2f}".format(subs/total_active_subs * 100.0)} for date, subs in data.items()] return data
def export_check_in_by_user(request, *args, **kwargs): from_date = request.POST.get('from_date') to_date = request.POST.get('to_date') begin_datetime_start_of_day = datetime.combine( datetime.strptime(from_date, '%Y-%m-%d'), datetime.min.time()) end_datetime_start_of_day = datetime.combine( datetime.strptime(to_date, '%Y-%m-%d'), datetime.min.time()) response = HttpResponse(content_type='text/csv') response[ 'Content-Disposition'] = 'attachment; filename="check_ins_by_user.csv"' writer = csv.writer(response) tagquery = LocationTag.objects.filter(created_at__gte=begin_datetime_start_of_day, created_at__lte=end_datetime_start_of_day) \ .annotate(date=DateTrunc('created_at', precision='day')) filteredTagQuery = [] for tags in tagquery: if tags.location.service == 'IN': filteredTagQuery.append(tags) userObjects = [] for tags in filteredTagQuery: newUserObj = User.objects.filter(id=tags.subscription.user_id) if not any(elem in userObjects for elem in newUserObj): userObjects.append(newUserObj[0]) writer.writerow( ['Username', 'Email', 'First Name', 'Last Name', 'Total Checked In']) writer.writerow([len(userObjects)]) for users in userObjects: total = 0 for tag in filteredTagQuery: if tag.subscription.user.username == users.username: total = total + 1 writer.writerow([ users.username, users.email, users.first_name, users.last_name, total ]) return response
def export_check_out_by_location(request, *args, **kwargs): from_date = request.POST.get('from_date') to_date = request.POST.get('to_date') begin_datetime_start_of_day = datetime.combine( datetime.strptime(from_date, '%Y-%m-%d'), datetime.min.time()) end_datetime_start_of_day = datetime.combine( datetime.strptime(to_date, '%Y-%m-%d'), datetime.min.time()) response = HttpResponse(content_type='text/csv') response[ 'Content-Disposition'] = 'attachment; filename="check_outs_by_location.csv"' writer = csv.writer(response) tagquery = LocationTag.objects.filter(created_at__gte=begin_datetime_start_of_day, created_at__lte=end_datetime_start_of_day) \ .annotate(date=DateTrunc('created_at', precision='day')) filteredTagQuery = [] for tags in tagquery: if tags.location.service == 'OUT': filteredTagQuery.append(tags) locationObjects = [] for tags in filteredTagQuery: newLocationObj = Location.objects.filter(id=tags.location.id) if not any(elem in locationObjects for elem in newLocationObj): locationObjects.append(newLocationObj[0]) writer.writerow(['Name', 'Service', 'Total Checked Out']) writer.writerow([len(locationObjects)]) for location in locationObjects: total = 0 for tag in filteredTagQuery: if tag.location.id == location.id: total = total + 1 writer.writerow([location.name, location.service, total]) return response