prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Adapt page usage to the new refarm pages interface
  At https://github.com/fidals/refarm-site/issues/240 we changed page templates and page context interfaces.
  
  Adapt site code to the new interface.
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `shopelectro/context.py`
  Content:
  ```
  1 from functools import partial
  2 
  3 from django.conf import settings
  4 from django.shortcuts import get_object_or_404
  5 
  6 from catalog import newcontext
  7 from images.models import Image
  8 from pages import models as pages_models, newcontext as pages_newcontext
  9 from shopelectro import models, request_data
  10 
  11 
  12 # @todo #255:60m  Improve `SortingOption` interface.
  13 #  Now it's located in context and this is wrong.
  14 #  Maybe refactor `CATEGORY_SORTING_OPTIONS`.
  15 class SortingOption:
  16     def __init__(self, index=0):
  17         options = settings.CATEGORY_SORTING_OPTIONS[index]
  18         self.label = options['label']
  19         self.field = options['field']
  20         self.direction = options['direction']
  21 
  22     @property
  23     def directed_field(self):
  24         return self.direction + self.field
  25 
  26 
  27 class Page(newcontext.Context):
  28 
  29     def __init__(self, page, tags: newcontext.Tags):
  30         self._page = page
  31         self._tags = tags
  32 
  33     def context(self):
  34         def template_context(page, tag_titles, tags):
  35             return {
  36                 'page': page,
  37                 'tag_titles': tag_titles,
  38                 'tags': tags,
  39             }
  40 
  41         tags_qs = self._tags.qs()
  42         self._page.get_template_render_context = partial(
  43             template_context, self._page, tags_qs.as_title(), tags_qs
  44         )
  45 
  46         return {
  47             'page': self._page,
  48         }
  49 
  50 
  51 class Catalog(newcontext.Context):
  52 
  53     def __init__(self, request_data_: request_data.Catalog):
  54         self.request_data = request_data_
  55 
  56     @property
  57     def page(self):
  58         return get_object_or_404(
  59             pages_models.ModelPage,
  60             slug=self.request_data.slug
  61         )
  62 
  63     @property
  64     def category(self):
  65         return self.page.model
  66 
  67     def context(self) -> dict:
  68         all_tags = newcontext.Tags(models.Tag.objects.all())
  69 
  70         selected_tags = newcontext.tags.ParsedTags(
  71             tags=all_tags,
  72             raw_tags=self.request_data.tags,
  73         )
  74         if self.request_data.tags:
  75             selected_tags = newcontext.tags.Checked404Tags(selected_tags)
  76 
  77         products = (
  78             models.Product.objects.active()
  79             .filter_descendants(self.category)
  80             .tagged_or_all(selected_tags.qs())
  81             .order_by(SortingOption(index=self.request_data.sorting_index).directed_field)
  82         )
  83 
  84         """
  85         We have to use separated variable for pagination.
  86 
  87         Because paginated QuerySet can not used as QuerySet.
  88         It's not the most strong place of Django ORM, of course.
  89         :return: ProductsContext with paginated QuerySet inside
  90         """
  91         # @todo #683:30m Remove *Tags and *Products suffixes from catalog.newcontext classes.
  92         #  Rename Checked404Tags to ExistingOr404.
  93         paginated = newcontext.products.PaginatedProducts(
  94             products=products,
  95             url=self.request_data.request.path,
  96             page_number=self.request_data.pagination_page_number,
  97             per_page=self.request_data.pagination_per_page,
  98         )
  99 
  100         images = newcontext.products.ProductImages(paginated.products, Image.objects.all())
  101         brands = newcontext.products.ProductBrands(paginated.products, all_tags)
  102         grouped_tags = newcontext.tags.GroupedTags(
  103             tags=newcontext.tags.TagsByProducts(all_tags, products)
  104         )
  105         page = Page(self.page, selected_tags)
  106         category = newcontext.category.Context(self.category)
  107         params = {
  108             'view_type': self.request_data.get_view_type(),
  109             'sorting_options': settings.CATEGORY_SORTING_OPTIONS.values(),
  110             'limits': settings.CATEGORY_STEP_MULTIPLIERS,
  111             'sort': self.request_data.sorting_index,
  112         }
  113 
  114         return {
  115             **params,
  116             **pages_newcontext.Contexts([
  117                 page, category, paginated,
  118                 images, brands, grouped_tags
  119             ]).context()
  120         }
  121
  ```
  --- 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/shopelectro/context.py b/shopelectro/context.py
  --- a/shopelectro/context.py
  +++ b/shopelectro/context.py
  @@ -1,5 +1,3 @@
  -from functools import partial
  -
   from django.conf import settings
   from django.shortcuts import get_object_or_404
   
  @@ -31,18 +29,15 @@
           self._tags = tags
   
       def context(self):
  -        def template_context(page, tag_titles, tags):
  -            return {
  -                'page': page,
  -                'tag_titles': tag_titles,
  -                'tags': tags,
  -            }
  -
           tags_qs = self._tags.qs()
  -        self._page.get_template_render_context = partial(
  -            template_context, self._page, tags_qs.as_title(), tags_qs
  -        )
  -
  +        # use dirty patch here, because it's the most simple method
  +        # to make shared templates work.
  +        # For example `templates/layout/metadata.html`.
  +        self._page.display = {
  +            'page': self._page,
  +            'tag_titles': tags_qs.as_title(),
  +            'tags': tags_qs,
  +        }
           return {
               'page': self._page,
           }
  
verification_info: {"golden_diff": "diff --git a/shopelectro/context.py b/shopelectro/context.py\n--- a/shopelectro/context.py\n+++ b/shopelectro/context.py\n@@ -1,5 +1,3 @@\n-from functools import partial\n-\n from django.conf import settings\n from django.shortcuts import get_object_or_404\n \n@@ -31,18 +29,15 @@\n         self._tags = tags\n \n     def context(self):\n-        def template_context(page, tag_titles, tags):\n-            return {\n-                'page': page,\n-                'tag_titles': tag_titles,\n-                'tags': tags,\n-            }\n-\n         tags_qs = self._tags.qs()\n-        self._page.get_template_render_context = partial(\n-            template_context, self._page, tags_qs.as_title(), tags_qs\n-        )\n-\n+        # use dirty patch here, because it's the most simple method\n+        # to make shared templates work.\n+        # For example `templates/layout/metadata.html`.\n+        self._page.display = {\n+            'page': self._page,\n+            'tag_titles': tags_qs.as_title(),\n+            'tags': tags_qs,\n+        }\n         return {\n             'page': self._page,\n         }\n", "issue": "Adapt page usage to the new refarm pages interface\nAt https://github.com/fidals/refarm-site/issues/240 we changed page templates and page context interfaces.\r\n\r\nAdapt site code to the new interface.\r\n\n", "before_files": [{"content": "from functools import partial\n\nfrom django.conf import settings\nfrom django.shortcuts import get_object_or_404\n\nfrom catalog import newcontext\nfrom images.models import Image\nfrom pages import models as pages_models, newcontext as pages_newcontext\nfrom shopelectro import models, request_data\n\n\n# @todo #255:60m  Improve `SortingOption` interface.\n#  Now it's located in context and this is wrong.\n#  Maybe refactor `CATEGORY_SORTING_OPTIONS`.\nclass SortingOption:\n    def __init__(self, index=0):\n        options = settings.CATEGORY_SORTING_OPTIONS[index]\n        self.label = options['label']\n        self.field = options['field']\n        self.direction = options['direction']\n\n    @property\n    def directed_field(self):\n        return self.direction + self.field\n\n\nclass Page(newcontext.Context):\n\n    def __init__(self, page, tags: newcontext.Tags):\n        self._page = page\n        self._tags = tags\n\n    def context(self):\n        def template_context(page, tag_titles, tags):\n            return {\n                'page': page,\n                'tag_titles': tag_titles,\n                'tags': tags,\n            }\n\n        tags_qs = self._tags.qs()\n        self._page.get_template_render_context = partial(\n            template_context, self._page, tags_qs.as_title(), tags_qs\n        )\n\n        return {\n            'page': self._page,\n        }\n\n\nclass Catalog(newcontext.Context):\n\n    def __init__(self, request_data_: request_data.Catalog):\n        self.request_data = request_data_\n\n    @property\n    def page(self):\n        return get_object_or_404(\n            pages_models.ModelPage,\n            slug=self.request_data.slug\n        )\n\n    @property\n    def category(self):\n        return self.page.model\n\n    def context(self) -> dict:\n        all_tags = newcontext.Tags(models.Tag.objects.all())\n\n        selected_tags = newcontext.tags.ParsedTags(\n            tags=all_tags,\n            raw_tags=self.request_data.tags,\n        )\n        if self.request_data.tags:\n            selected_tags = newcontext.tags.Checked404Tags(selected_tags)\n\n        products = (\n            models.Product.objects.active()\n            .filter_descendants(self.category)\n            .tagged_or_all(selected_tags.qs())\n            .order_by(SortingOption(index=self.request_data.sorting_index).directed_field)\n        )\n\n        \"\"\"\n        We have to use separated variable for pagination.\n\n        Because paginated QuerySet can not used as QuerySet.\n        It's not the most strong place of Django ORM, of course.\n        :return: ProductsContext with paginated QuerySet inside\n        \"\"\"\n        # @todo #683:30m Remove *Tags and *Products suffixes from catalog.newcontext classes.\n        #  Rename Checked404Tags to ExistingOr404.\n        paginated = newcontext.products.PaginatedProducts(\n            products=products,\n            url=self.request_data.request.path,\n            page_number=self.request_data.pagination_page_number,\n            per_page=self.request_data.pagination_per_page,\n        )\n\n        images = newcontext.products.ProductImages(paginated.products, Image.objects.all())\n        brands = newcontext.products.ProductBrands(paginated.products, all_tags)\n        grouped_tags = newcontext.tags.GroupedTags(\n            tags=newcontext.tags.TagsByProducts(all_tags, products)\n        )\n        page = Page(self.page, selected_tags)\n        category = newcontext.category.Context(self.category)\n        params = {\n            'view_type': self.request_data.get_view_type(),\n            'sorting_options': settings.CATEGORY_SORTING_OPTIONS.values(),\n            'limits': settings.CATEGORY_STEP_MULTIPLIERS,\n            'sort': self.request_data.sorting_index,\n        }\n\n        return {\n            **params,\n            **pages_newcontext.Contexts([\n                page, category, paginated,\n                images, brands, grouped_tags\n            ]).context()\n        }\n", "path": "shopelectro/context.py"}], "after_files": [{"content": "from django.conf import settings\nfrom django.shortcuts import get_object_or_404\n\nfrom catalog import newcontext\nfrom images.models import Image\nfrom pages import models as pages_models, newcontext as pages_newcontext\nfrom shopelectro import models, request_data\n\n\n# @todo #255:60m  Improve `SortingOption` interface.\n#  Now it's located in context and this is wrong.\n#  Maybe refactor `CATEGORY_SORTING_OPTIONS`.\nclass SortingOption:\n    def __init__(self, index=0):\n        options = settings.CATEGORY_SORTING_OPTIONS[index]\n        self.label = options['label']\n        self.field = options['field']\n        self.direction = options['direction']\n\n    @property\n    def directed_field(self):\n        return self.direction + self.field\n\n\nclass Page(newcontext.Context):\n\n    def __init__(self, page, tags: newcontext.Tags):\n        self._page = page\n        self._tags = tags\n\n    def context(self):\n        tags_qs = self._tags.qs()\n        # use dirty patch here, because it's the most simple method\n        # to make shared templates work.\n        # For example `templates/layout/metadata.html`.\n        self._page.display = {\n            'page': self._page,\n            'tag_titles': tags_qs.as_title(),\n            'tags': tags_qs,\n        }\n        return {\n            'page': self._page,\n        }\n\n\nclass Catalog(newcontext.Context):\n\n    def __init__(self, request_data_: request_data.Catalog):\n        self.request_data = request_data_\n\n    @property\n    def page(self):\n        return get_object_or_404(\n            pages_models.ModelPage,\n            slug=self.request_data.slug\n        )\n\n    @property\n    def category(self):\n        return self.page.model\n\n    def context(self) -> dict:\n        all_tags = newcontext.Tags(models.Tag.objects.all())\n\n        selected_tags = newcontext.tags.ParsedTags(\n            tags=all_tags,\n            raw_tags=self.request_data.tags,\n        )\n        if self.request_data.tags:\n            selected_tags = newcontext.tags.Checked404Tags(selected_tags)\n\n        products = (\n            models.Product.objects.active()\n            .filter_descendants(self.category)\n            .tagged_or_all(selected_tags.qs())\n            .order_by(SortingOption(index=self.request_data.sorting_index).directed_field)\n        )\n\n        \"\"\"\n        We have to use separated variable for pagination.\n\n        Because paginated QuerySet can not used as QuerySet.\n        It's not the most strong place of Django ORM, of course.\n        :return: ProductsContext with paginated QuerySet inside\n        \"\"\"\n        # @todo #683:30m Remove *Tags and *Products suffixes from catalog.newcontext classes.\n        #  Rename Checked404Tags to ExistingOr404.\n        paginated = newcontext.products.PaginatedProducts(\n            products=products,\n            url=self.request_data.request.path,\n            page_number=self.request_data.pagination_page_number,\n            per_page=self.request_data.pagination_per_page,\n        )\n\n        images = newcontext.products.ProductImages(paginated.products, Image.objects.all())\n        brands = newcontext.products.ProductBrands(paginated.products, all_tags)\n        grouped_tags = newcontext.tags.GroupedTags(\n            tags=newcontext.tags.TagsByProducts(all_tags, products)\n        )\n        page = Page(self.page, selected_tags)\n        category = newcontext.category.Context(self.category)\n        params = {\n            'view_type': self.request_data.get_view_type(),\n            'sorting_options': settings.CATEGORY_SORTING_OPTIONS.values(),\n            'limits': settings.CATEGORY_STEP_MULTIPLIERS,\n            'sort': self.request_data.sorting_index,\n        }\n\n        return {\n            **params,\n            **pages_newcontext.Contexts([\n                page, category, paginated,\n                images, brands, grouped_tags\n            ]).context()\n        }\n", "path": "shopelectro/context.py"}]}