prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Tokenize person lookup to work with "name last_name"
  `lookups.PersonLookup` won't show 'Piotr Banaszkiewicz' for 'Piotr Ban' input – because it only looks up `personal` or `family` or `email`…
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `workshops/lookups.py`
  Content:
  ```
  1 from django.contrib.auth.models import Group
  2 from django.db.models import Q
  3 
  4 from selectable.base import ModelLookup
  5 from selectable.registry import registry
  6 from selectable.decorators import login_required
  7 
  8 from workshops import models
  9 
  10 
  11 @login_required
  12 class EventLookup(ModelLookup):
  13     model = models.Event
  14     search_fields = ('slug__icontains', )
  15 
  16 
  17 @login_required
  18 class HostLookup(ModelLookup):
  19     model = models.Host
  20     search_fields = (
  21         'domain__icontains',
  22         'fullname__icontains'
  23     )
  24 
  25 
  26 @login_required
  27 class PersonLookup(ModelLookup):
  28     model = models.Person
  29     search_fields = (
  30         'personal__icontains',
  31         'family__icontains',
  32         'email__icontains',
  33         'username__icontains'
  34     )
  35 
  36 
  37 @login_required
  38 class AdminLookup(ModelLookup):
  39     """The same as PersonLookup, but allows only to select administrators.
  40 
  41     Administrator is anyone with superuser power or in "administrators" group.
  42     """
  43     model = models.Person
  44     search_fields = (
  45         'personal__icontains',
  46         'family__icontains',
  47         'email__icontains',
  48         'username__icontains'
  49     )
  50 
  51     def get_query(self, request, term):
  52         results = super().get_query(request, term)
  53         admin_group = Group.objects.get(name='administrators')
  54         results = results.filter(
  55             Q(is_superuser=True) | Q(groups__in=[admin_group])
  56         )
  57         return results
  58 
  59 
  60 @login_required
  61 class AirportLookup(ModelLookup):
  62     model = models.Airport
  63     search_fields = (
  64         'iata__icontains',
  65         'fullname__icontains'
  66     )
  67 
  68 
  69 registry.register(EventLookup)
  70 registry.register(HostLookup)
  71 registry.register(PersonLookup)
  72 registry.register(AdminLookup)
  73 registry.register(AirportLookup)
  74
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/workshops/lookups.py b/workshops/lookups.py
  --- a/workshops/lookups.py
  +++ b/workshops/lookups.py
  @@ -1,3 +1,7 @@
  +from functools import reduce
  +import operator
  +import re
  +
   from django.contrib.auth.models import Group
   from django.db.models import Q
   
  @@ -33,6 +37,32 @@
           'username__icontains'
       )
   
  +    def get_query(self, request, term):
  +        """Override this method to allow for additional lookup method: """
  +        # original code from selectable.base.ModelLookup.get_query:
  +        qs = self.get_queryset()
  +        if term:
  +            search_filters = []
  +            if self.search_fields:
  +                for field in self.search_fields:
  +                    search_filters.append(Q(**{field: term}))
  +
  +            # tokenizing part
  +            tokens = re.split('\s+', term)
  +            if len(tokens) == 2:
  +                name1, name2 = tokens
  +                complex_q = (
  +                    Q(personal__icontains=name1) & Q(family__icontains=name2)
  +                ) | (
  +                    Q(personal__icontains=name2) & Q(family__icontains=name1)
  +                )
  +                search_filters.append(complex_q)
  +
  +            # this is brilliant: it applies OR to all search filters
  +            qs = qs.filter(reduce(operator.or_, search_filters))
  +
  +        return qs
  +
   
   @login_required
   class AdminLookup(ModelLookup):
  
verification_info: {"golden_diff": "diff --git a/workshops/lookups.py b/workshops/lookups.py\n--- a/workshops/lookups.py\n+++ b/workshops/lookups.py\n@@ -1,3 +1,7 @@\n+from functools import reduce\n+import operator\n+import re\n+\n from django.contrib.auth.models import Group\n from django.db.models import Q\n \n@@ -33,6 +37,32 @@\n         'username__icontains'\n     )\n \n+    def get_query(self, request, term):\n+        \"\"\"Override this method to allow for additional lookup method: \"\"\"\n+        # original code from selectable.base.ModelLookup.get_query:\n+        qs = self.get_queryset()\n+        if term:\n+            search_filters = []\n+            if self.search_fields:\n+                for field in self.search_fields:\n+                    search_filters.append(Q(**{field: term}))\n+\n+            # tokenizing part\n+            tokens = re.split('\\s+', term)\n+            if len(tokens) == 2:\n+                name1, name2 = tokens\n+                complex_q = (\n+                    Q(personal__icontains=name1) & Q(family__icontains=name2)\n+                ) | (\n+                    Q(personal__icontains=name2) & Q(family__icontains=name1)\n+                )\n+                search_filters.append(complex_q)\n+\n+            # this is brilliant: it applies OR to all search filters\n+            qs = qs.filter(reduce(operator.or_, search_filters))\n+\n+        return qs\n+\n \n @login_required\n class AdminLookup(ModelLookup):\n", "issue": "Tokenize person lookup to work with \"name last_name\"\n`lookups.PersonLookup` won't show 'Piotr Banaszkiewicz' for 'Piotr Ban' input \u2013 because it only looks up `personal` or `family` or `email`\u2026\n\n", "before_files": [{"content": "from django.contrib.auth.models import Group\nfrom django.db.models import Q\n\nfrom selectable.base import ModelLookup\nfrom selectable.registry import registry\nfrom selectable.decorators import login_required\n\nfrom workshops import models\n\n\n@login_required\nclass EventLookup(ModelLookup):\n    model = models.Event\n    search_fields = ('slug__icontains', )\n\n\n@login_required\nclass HostLookup(ModelLookup):\n    model = models.Host\n    search_fields = (\n        'domain__icontains',\n        'fullname__icontains'\n    )\n\n\n@login_required\nclass PersonLookup(ModelLookup):\n    model = models.Person\n    search_fields = (\n        'personal__icontains',\n        'family__icontains',\n        'email__icontains',\n        'username__icontains'\n    )\n\n\n@login_required\nclass AdminLookup(ModelLookup):\n    \"\"\"The same as PersonLookup, but allows only to select administrators.\n\n    Administrator is anyone with superuser power or in \"administrators\" group.\n    \"\"\"\n    model = models.Person\n    search_fields = (\n        'personal__icontains',\n        'family__icontains',\n        'email__icontains',\n        'username__icontains'\n    )\n\n    def get_query(self, request, term):\n        results = super().get_query(request, term)\n        admin_group = Group.objects.get(name='administrators')\n        results = results.filter(\n            Q(is_superuser=True) | Q(groups__in=[admin_group])\n        )\n        return results\n\n\n@login_required\nclass AirportLookup(ModelLookup):\n    model = models.Airport\n    search_fields = (\n        'iata__icontains',\n        'fullname__icontains'\n    )\n\n\nregistry.register(EventLookup)\nregistry.register(HostLookup)\nregistry.register(PersonLookup)\nregistry.register(AdminLookup)\nregistry.register(AirportLookup)\n", "path": "workshops/lookups.py"}], "after_files": [{"content": "from functools import reduce\nimport operator\nimport re\n\nfrom django.contrib.auth.models import Group\nfrom django.db.models import Q\n\nfrom selectable.base import ModelLookup\nfrom selectable.registry import registry\nfrom selectable.decorators import login_required\n\nfrom workshops import models\n\n\n@login_required\nclass EventLookup(ModelLookup):\n    model = models.Event\n    search_fields = ('slug__icontains', )\n\n\n@login_required\nclass HostLookup(ModelLookup):\n    model = models.Host\n    search_fields = (\n        'domain__icontains',\n        'fullname__icontains'\n    )\n\n\n@login_required\nclass PersonLookup(ModelLookup):\n    model = models.Person\n    search_fields = (\n        'personal__icontains',\n        'family__icontains',\n        'email__icontains',\n        'username__icontains'\n    )\n\n    def get_query(self, request, term):\n        \"\"\"Override this method to allow for additional lookup method: \"\"\"\n        # original code from selectable.base.ModelLookup.get_query:\n        qs = self.get_queryset()\n        if term:\n            search_filters = []\n            if self.search_fields:\n                for field in self.search_fields:\n                    search_filters.append(Q(**{field: term}))\n\n            # tokenizing part\n            tokens = re.split('\\s+', term)\n            if len(tokens) == 2:\n                name1, name2 = tokens\n                complex_q = (\n                    Q(personal__icontains=name1) & Q(family__icontains=name2)\n                ) | (\n                    Q(personal__icontains=name2) & Q(family__icontains=name1)\n                )\n                search_filters.append(complex_q)\n\n            # this is brilliant: it applies OR to all search filters\n            qs = qs.filter(reduce(operator.or_, search_filters))\n\n        return qs\n\n\n@login_required\nclass AdminLookup(ModelLookup):\n    \"\"\"The same as PersonLookup, but allows only to select administrators.\n\n    Administrator is anyone with superuser power or in \"administrators\" group.\n    \"\"\"\n    model = models.Person\n    search_fields = (\n        'personal__icontains',\n        'family__icontains',\n        'email__icontains',\n        'username__icontains'\n    )\n\n    def get_query(self, request, term):\n        results = super().get_query(request, term)\n        admin_group = Group.objects.get(name='administrators')\n        results = results.filter(\n            Q(is_superuser=True) | Q(groups__in=[admin_group])\n        )\n        return results\n\n\n@login_required\nclass AirportLookup(ModelLookup):\n    model = models.Airport\n    search_fields = (\n        'iata__icontains',\n        'fullname__icontains'\n    )\n\n\nregistry.register(EventLookup)\nregistry.register(HostLookup)\nregistry.register(PersonLookup)\nregistry.register(AdminLookup)\nregistry.register(AirportLookup)\n", "path": "workshops/lookups.py"}]}