def get_user(request, name): user = get_object_or_404(User, username=name) stats = {'total': user.record_set.count()} for t in StatusTypes.types: stats[t.name] = 0 for d in user.record_set.values('status_type').annotate(count=Count('status_type')).order_by(): stats[StatusTypes.to_name(d['status_type'])] = d['count'] uncategorized = Uncategorized(user) categories = [] for c in [uncategorized] + list(user.category_set.annotate(record_count=Count('record'))): categories.append({'id': c.id, 'name': c.name, 'count': c.record_count}) result = { 'name': user.username, 'joined_at': _serialize_datetime(user.date_joined), 'stats': stats, 'categories': categories, } if request.GET.get('include_library_items', 'true') == 'true': result['library_items'] = [{ 'id': record.id, 'title': record.title, 'status': _serialize_status(record), 'category': _category_as_dict(record.category or uncategorized), 'updated_at': _serialize_datetime(record.updated_at), } for record in user.record_set.order_by('-updated_at')] return result
def _work_as_dict(work, include_watchers=False): watchers = {'total': work.index.record_count} if include_watchers: for t in StatusTypes.types: watchers[t.name] = [] for record in work.record_set.order_by('status_type', 'user__username'): watchers[StatusTypes.to_name(record.status_type)].append(record.user.username) else: for t in StatusTypes.types: watchers[t.name] = 0 for d in work.record_set.values('status_type').annotate(count=Count('status_type')).order_by(): watchers[StatusTypes.to_name(d['status_type'])] = d['count'] return { 'title': work.title, 'rank': work.index.rank, 'watchers': watchers, }