def group_home(request, group_slug): group = get_object_or_404(Group, slug=group_slug) projects = alphanum_sort(group.projects.accessible_to(request.user), 'name') archived_projects = [p for p in projects if p.is_archived] context = { 'group': group, 'projects': projects, 'has_archived_projects': len(archived_projects) > 0, } return render(request, 'squad/group.jinja2', context)
def group_home(request, group_slug): group = get_object_or_404(Group, slug=group_slug) # Optimize number of queries to get ProjectStatus for each project projects_queryset = group.projects.accessible_to(request.user) projects_queryset = projects_queryset.prefetch_related( Prefetch('builds', queryset=Build.objects.order_by('-datetime').only( 'id', 'project_id'))) if request.user.is_authenticated: projects_queryset = projects_queryset.prefetch_related( Prefetch('subscriptions', queryset=Subscription.objects.filter(user=request.user), to_attr='user_subscriptions')) has_archived_projects = False latest_build_ids = {} projects = projects_queryset.all() for project in projects: project.latest_build = None try: build_id = project.builds.all()[0].id latest_build_ids[build_id] = project except IndexError: pass if not has_archived_projects and project.is_archived: has_archived_projects = True for build in Build.objects.filter(id__in=latest_build_ids.keys() ).prefetch_related('status').only('id'): latest_build_ids[build.id].latest_build = build if len(projects) == 0 and not group.can_submit_results(request.user): raise Http404() context = { 'group': group, 'projects': alphanum_sort(projects, 'name'), 'has_archived_projects': has_archived_projects, } response = render(request, 'squad/group.jinja2', context) return response
def compare_projects(request): comparison = None group = None projects = None comparison_type = request.GET.get('comparison_type', 'test') transitions = __get_transitions(request) group_slug = request.GET.get('group') if group_slug: group = get_object_or_404(Group, slug=group_slug) qs = group.projects.accessible_to(request.user).prefetch_related( Prefetch('builds', queryset=Build.objects.order_by('-datetime'))) projects = alphanum_sort(qs, 'slug') filters = [] for key, value in request.GET.items(): if 'project_' in key and len(key.split('_')) == 2: project_id = key.split('_')[1] filters.append(Q(project_id=project_id) & Q(version=value)) if len(filters) > 1: build_filters = reduce(lambda x, y: x | y, filters) builds = Build.objects.filter(build_filters) comparison_class = __get_comparison_class(comparison_type) comparison = comparison_class.compare_builds(*builds) if comparison_type == 'test' and len(transitions): comparison.apply_transitions( [t for t, checked in transitions.items() if checked]) comparison.results = __paginate(comparison.results, request) context = { 'group': group, 'projects': projects, 'comparison': comparison, 'comparison_type': comparison_type, 'transitions': transitions, } return render(request, 'squad/compare_projects.jinja2', context)
def test_desc_sort(self): projects = Project.objects.all() projects_sorted = alphanum_sort(projects, 'name') # v1.10 -> v1.2 -> v1.1 self.assertEqual([self.p2, self.p3, self.p1], projects_sorted)
def test_asc_sort(self): projects = Project.objects.all() projects_sorted = alphanum_sort(projects, 'name', reverse=False) # v1.1 -> v1.2 -> v1.10 self.assertEqual([self.p1, self.p3, self.p2], projects_sorted)