|
3 | 3 | from django.contrib.auth.decorators import login_required
|
4 | 4 | from django.contrib.auth.models import User
|
5 | 5 | from django.db import connection, transaction
|
6 |
| -from django.db.models import Q |
| 6 | +from django.db.models import Count, Q |
7 | 7 | from django.http import (
|
8 | 8 | Http404,
|
9 | 9 | HttpResponse,
|
@@ -692,25 +692,109 @@ def global_search(request):
|
692 | 692 | patches = patches_by_messageid(cleaned_id)
|
693 | 693 |
|
694 | 694 | if not patches:
|
695 |
| - patches = ( |
696 |
| - Patch.objects.select_related() |
697 |
| - .filter(name__icontains=searchterm) |
698 |
| - .order_by( |
699 |
| - "created", |
| 695 | + patches_query = ( |
| 696 | + Patch.objects.select_related("targetversion", "committer") |
| 697 | + .prefetch_related( |
| 698 | + "authors", |
| 699 | + "reviewers", |
| 700 | + "tags", |
| 701 | + "patchoncommitfest_set__commitfest", |
| 702 | + "mailthread_set", |
700 | 703 | )
|
701 |
| - .all() |
| 704 | + .select_related("cfbot_branch") |
| 705 | + .filter(name__icontains=searchterm) |
702 | 706 | )
|
703 | 707 |
|
| 708 | + # Apply filters using the same logic as patchlist |
| 709 | + if request.GET.get("status", "-1") != "-1": |
| 710 | + try: |
| 711 | + status = int(request.GET["status"]) |
| 712 | + patches_query = patches_query.filter( |
| 713 | + patchoncommitfest__status=status |
| 714 | + ).distinct() |
| 715 | + except ValueError: |
| 716 | + pass |
| 717 | + |
| 718 | + if request.GET.get("targetversion", "-1") != "-1": |
| 719 | + if request.GET["targetversion"] == "-2": |
| 720 | + patches_query = patches_query.filter(targetversion_id__isnull=True) |
| 721 | + else: |
| 722 | + try: |
| 723 | + ver_id = int(request.GET["targetversion"]) |
| 724 | + patches_query = patches_query.filter(targetversion_id=ver_id) |
| 725 | + except ValueError: |
| 726 | + pass |
| 727 | + |
| 728 | + if request.GET.getlist("tag"): |
| 729 | + try: |
| 730 | + tag_ids = [int(t) for t in request.GET.getlist("tag")] |
| 731 | + for tag_id in tag_ids: |
| 732 | + patches_query = patches_query.filter(tags__id=tag_id) |
| 733 | + patches_query = patches_query.distinct() |
| 734 | + except ValueError: |
| 735 | + pass |
| 736 | + |
| 737 | + # Apply sorting based on sortkey parameter (adapted for Django ORM) |
| 738 | + sortkey = request.GET.get("sortkey", "1") |
| 739 | + if sortkey == "2": # Latest mail |
| 740 | + patches_query = patches_query.order_by("-modified") # Use modified as proxy |
| 741 | + elif sortkey == "-2": |
| 742 | + patches_query = patches_query.order_by("modified") |
| 743 | + elif sortkey == "3": # Num cfs |
| 744 | + patches_query = patches_query.annotate( |
| 745 | + num_cfs=Count("patchoncommitfest") |
| 746 | + ).order_by("-num_cfs") |
| 747 | + elif sortkey == "-3": |
| 748 | + patches_query = patches_query.annotate( |
| 749 | + num_cfs=Count("patchoncommitfest") |
| 750 | + ).order_by("num_cfs") |
| 751 | + elif sortkey == "4": # ID |
| 752 | + patches_query = patches_query.order_by("id") |
| 753 | + elif sortkey == "-4": |
| 754 | + patches_query = patches_query.order_by("-id") |
| 755 | + elif sortkey == "5": # Patch name |
| 756 | + patches_query = patches_query.order_by("name") |
| 757 | + elif sortkey == "-5": |
| 758 | + patches_query = patches_query.order_by("-name") |
| 759 | + elif sortkey == "8": # CF |
| 760 | + patches_query = patches_query.order_by("patchoncommitfest__commitfest__id") |
| 761 | + elif sortkey == "-8": |
| 762 | + patches_query = patches_query.order_by("-patchoncommitfest__commitfest__id") |
| 763 | + else: # Default: Created (sortkey 1) |
| 764 | + patches_query = patches_query.order_by("created") |
| 765 | + |
| 766 | + patches = patches_query.all() |
| 767 | + |
704 | 768 | if len(patches) == 1:
|
705 | 769 | patch = patches[0]
|
706 | 770 | return HttpResponseRedirect(f"/patch/{patch.id}/")
|
707 | 771 |
|
| 772 | + # Use the existing filter form |
| 773 | + form = CommitFestFilterForm(request.GET) |
| 774 | + |
| 775 | + # Get user profile for timestamp preferences |
| 776 | + userprofile = None |
| 777 | + if request.user.is_authenticated: |
| 778 | + try: |
| 779 | + from pgcommitfest.userprofile.models import UserProfile |
| 780 | + |
| 781 | + userprofile = UserProfile.objects.get(user=request.user) |
| 782 | + except UserProfile.DoesNotExist: |
| 783 | + pass |
| 784 | + |
708 | 785 | return render(
|
709 | 786 | request,
|
710 | 787 | "patchsearch.html",
|
711 | 788 | {
|
712 | 789 | "patches": patches,
|
713 | 790 | "title": "Patch search results",
|
| 791 | + "searchterm": searchterm, |
| 792 | + "form": form, |
| 793 | + "cf": None, # No specific commitfest context |
| 794 | + "sortkey": int(request.GET.get("sortkey") or "1"), |
| 795 | + "tags_data": get_tags_data(), |
| 796 | + "all_tags": {t.id: t for t in Tag.objects.all()}, |
| 797 | + "userprofile": userprofile, |
714 | 798 | },
|
715 | 799 | )
|
716 | 800 |
|
|
0 commit comments