prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  librarian merge queue fixes
  Closes #6807
  
  - allows flexible sorting with ?order=asc or desc -- piggy backs on #6785 
  - adds total counts to Open and Closed
  - removes "All"
  - fixes bug where page? persists when switching modes -- fixes **half** of #6782 (i.e. mode part, not submitter!)
  
  
  <!-- What does this PR achieve? [feature|hotfix|fix|refactor] -->
  
  ### Stakeholders
  <!-- @ tag stakeholders of this bug -->
  @jimchamp 
  
  <!-- Attribution Disclaimer: By proposing this pull request, I affirm to have made a best-effort and exercised my discretion to make sure relevant sections of this code which substantially leverage code suggestions, code generation, or code snippets from sources (e.g. Stack Overflow, GitHub) have been annotated with basic attribution so reviewers & contributors may have confidence and access to the correct context to evaluate and use this code. -->
  
  
  </issue>
  <code>
  [start of openlibrary/plugins/upstream/edits.py]
  1 """Librarian Edits
  2 """
  3 
  4 import json
  5 import web
  6 
  7 from openlibrary import accounts
  8 from openlibrary.core.edits import CommunityEditsQueue, get_status_for_view
  9 from infogami.utils import delegate
  10 from infogami.utils.view import render_template
  11 
  12 
  13 def create_request(olids: str, username: str, comment: str = None):
  14     work_ids = olids.split(',')
  15     return CommunityEditsQueue.submit_work_merge_request(
  16         work_ids,
  17         submitter=username,
  18         comment=comment,
  19     )
  20 
  21 
  22 class community_edits_queue(delegate.page):
  23     path = '/merges'
  24 
  25     def POST(self):
  26         def response(status='ok', **kwargs):
  27             return {'status': status, **kwargs}
  28 
  29         i = web.input(
  30             work_ids="",  # Comma-separated OLIDs (OL1W,OL2W,OL3W,...,OL111W)
  31             rtype="merge-works",
  32             mrid=None,
  33             action=None,  # create, approve, decline, comment, unassign, create-merged
  34             comment=None,
  35         )
  36         user = accounts.get_current_user()
  37         username = user['key'].split('/')[-1]
  38         if i.mrid:  # We are updating an existing merge request
  39             if i.action == 'comment':
  40                 if i.comment:
  41                     CommunityEditsQueue.comment_request(i.mrid, username, i.comment)
  42                     return delegate.RawText(
  43                         json.dumps(response()), content_type="application/json"
  44                     )
  45                 else:
  46                     return delegate.RawText(
  47                         json.dumps(
  48                             response(
  49                                 status='error', error='No comment sent in request.'
  50                             )
  51                         )
  52                     )
  53             elif i.action == 'claim':
  54                 result = CommunityEditsQueue.assign_request(i.mrid, username)
  55                 return delegate.RawText(
  56                     json.dumps(response(**result)), content_type="application/json"
  57                 )
  58             elif i.action == 'unassign':
  59                 CommunityEditsQueue.unassign_request(i.mrid)
  60                 status = get_status_for_view(CommunityEditsQueue.STATUS['PENDING'])
  61                 return delegate.RawText(json.dumps(response(newStatus=status)))
  62             else:
  63                 if i.action == "decline":
  64                     status = CommunityEditsQueue.STATUS['DECLINED']
  65                 elif i.action == 'approve':
  66                     status = CommunityEditsQueue.STATUS['MERGED']
  67                 CommunityEditsQueue.update_request_status(
  68                     i.mrid, status, username, comment=i.comment
  69                 )
  70                 return delegate.RawText(
  71                     json.dumps(response()), content_type="application/json"
  72                 )
  73         elif i.rtype == "merge-works":
  74             if i.action == 'create':
  75                 result = create_request(i.work_ids, username, i.comment)
  76                 resp = (
  77                     response(id=result)
  78                     if result
  79                     else response(
  80                         status='error',
  81                         error='A request to merge these works has already been submitted.',
  82                     )
  83                 )
  84                 return delegate.RawText(
  85                     json.dumps(resp), content_type="application/json"
  86                 )
  87             elif i.action == 'create-merged':
  88                 result = CommunityEditsQueue.submit_work_merge_request(
  89                     i.work_ids.split(','),
  90                     submitter=username,
  91                     reviewer=username,
  92                     status=CommunityEditsQueue.STATUS['MERGED'],
  93                 )
  94                 return delegate.RawText(
  95                     json.dumps(response(id=result)), content_type='application/json'
  96                 )
  97 
  98     def GET(self):
  99         i = web.input(page=1, limit=25, mode="open", submitter=None, reviewer=None)
  100         merge_requests = CommunityEditsQueue.get_requests(
  101             page=int(i.page),
  102             limit=int(i.limit),
  103             mode=i.mode,
  104             submitter=i.submitter,
  105             reviewer=i.reviewer,
  106             order='created desc',
  107         ).list()
  108 
  109         total_found = CommunityEditsQueue.get_counts_by_mode(
  110             mode=i.mode, submitter=i.submitter, reviewer=i.reviewer
  111         )
  112         return render_template(
  113             'merge_queue/merge_queue',
  114             total_found,
  115             merge_requests=merge_requests,
  116         )
  117 
  118     def extract_olids(self, url):
  119         query_string = url.split('?')[1]
  120         split_params = query_string.split('&')
  121         params = {}
  122         for p in split_params:
  123             kv = p.split('=')
  124             params[kv[0]] = kv[1]
  125         return params['records'].split(',')
  126 
  127 
  128 class ui_partials(delegate.page):
  129     path = '/merges/partials'
  130 
  131     def GET(self):
  132         i = web.input(type=None, comment='')
  133         if i.type == 'comment':
  134             component = render_template('merge_queue/comment', comment_str=i.comment)
  135             return delegate.RawText(component)
  136 
  137 
  138 def setup():
  139     pass
  140
  [end of openlibrary/plugins/upstream/edits.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/openlibrary/plugins/upstream/edits.py b/openlibrary/plugins/upstream/edits.py
  --- a/openlibrary/plugins/upstream/edits.py
  +++ b/openlibrary/plugins/upstream/edits.py
  @@ -96,19 +96,26 @@
                   )
   
       def GET(self):
  -        i = web.input(page=1, limit=25, mode="open", submitter=None, reviewer=None)
  +        i = web.input(
  +            page=1, limit=25, mode="open", submitter=None, reviewer=None, order='desc'
  +        )
           merge_requests = CommunityEditsQueue.get_requests(
               page=int(i.page),
               limit=int(i.limit),
               mode=i.mode,
               submitter=i.submitter,
               reviewer=i.reviewer,
  -            order='created desc',
  +            order=f'created {i.order}',
           ).list()
   
  -        total_found = CommunityEditsQueue.get_counts_by_mode(
  -            mode=i.mode, submitter=i.submitter, reviewer=i.reviewer
  -        )
  +        total_found = {
  +            "open": CommunityEditsQueue.get_counts_by_mode(
  +                mode='open', submitter=i.submitter, reviewer=i.reviewer
  +            ),
  +            "closed": CommunityEditsQueue.get_counts_by_mode(
  +                mode='closed', submitter=i.submitter, reviewer=i.reviewer
  +            ),
  +        }
           return render_template(
               'merge_queue/merge_queue',
               total_found,
  
verification_info: {"golden_diff": "diff --git a/openlibrary/plugins/upstream/edits.py b/openlibrary/plugins/upstream/edits.py\n--- a/openlibrary/plugins/upstream/edits.py\n+++ b/openlibrary/plugins/upstream/edits.py\n@@ -96,19 +96,26 @@\n                 )\n \n     def GET(self):\n-        i = web.input(page=1, limit=25, mode=\"open\", submitter=None, reviewer=None)\n+        i = web.input(\n+            page=1, limit=25, mode=\"open\", submitter=None, reviewer=None, order='desc'\n+        )\n         merge_requests = CommunityEditsQueue.get_requests(\n             page=int(i.page),\n             limit=int(i.limit),\n             mode=i.mode,\n             submitter=i.submitter,\n             reviewer=i.reviewer,\n-            order='created desc',\n+            order=f'created {i.order}',\n         ).list()\n \n-        total_found = CommunityEditsQueue.get_counts_by_mode(\n-            mode=i.mode, submitter=i.submitter, reviewer=i.reviewer\n-        )\n+        total_found = {\n+            \"open\": CommunityEditsQueue.get_counts_by_mode(\n+                mode='open', submitter=i.submitter, reviewer=i.reviewer\n+            ),\n+            \"closed\": CommunityEditsQueue.get_counts_by_mode(\n+                mode='closed', submitter=i.submitter, reviewer=i.reviewer\n+            ),\n+        }\n         return render_template(\n             'merge_queue/merge_queue',\n             total_found,\n", "issue": "librarian merge queue fixes\nCloses #6807\r\n\r\n- allows flexible sorting with ?order=asc or desc -- piggy backs on #6785 \r\n- adds total counts to Open and Closed\r\n- removes \"All\"\r\n- fixes bug where page? persists when switching modes -- fixes **half** of #6782 (i.e. mode part, not submitter!)\r\n\r\n\r\n<!-- What does this PR achieve? [feature|hotfix|fix|refactor] -->\r\n\r\n### Stakeholders\r\n<!-- @ tag stakeholders of this bug -->\r\n@jimchamp \r\n\r\n<!-- Attribution Disclaimer: By proposing this pull request, I affirm to have made a best-effort and exercised my discretion to make sure relevant sections of this code which substantially leverage code suggestions, code generation, or code snippets from sources (e.g. Stack Overflow, GitHub) have been annotated with basic attribution so reviewers & contributors may have confidence and access to the correct context to evaluate and use this code. -->\r\n\n", "before_files": [{"content": "\"\"\"Librarian Edits\n\"\"\"\n\nimport json\nimport web\n\nfrom openlibrary import accounts\nfrom openlibrary.core.edits import CommunityEditsQueue, get_status_for_view\nfrom infogami.utils import delegate\nfrom infogami.utils.view import render_template\n\n\ndef create_request(olids: str, username: str, comment: str = None):\n    work_ids = olids.split(',')\n    return CommunityEditsQueue.submit_work_merge_request(\n        work_ids,\n        submitter=username,\n        comment=comment,\n    )\n\n\nclass community_edits_queue(delegate.page):\n    path = '/merges'\n\n    def POST(self):\n        def response(status='ok', **kwargs):\n            return {'status': status, **kwargs}\n\n        i = web.input(\n            work_ids=\"\",  # Comma-separated OLIDs (OL1W,OL2W,OL3W,...,OL111W)\n            rtype=\"merge-works\",\n            mrid=None,\n            action=None,  # create, approve, decline, comment, unassign, create-merged\n            comment=None,\n        )\n        user = accounts.get_current_user()\n        username = user['key'].split('/')[-1]\n        if i.mrid:  # We are updating an existing merge request\n            if i.action == 'comment':\n                if i.comment:\n                    CommunityEditsQueue.comment_request(i.mrid, username, i.comment)\n                    return delegate.RawText(\n                        json.dumps(response()), content_type=\"application/json\"\n                    )\n                else:\n                    return delegate.RawText(\n                        json.dumps(\n                            response(\n                                status='error', error='No comment sent in request.'\n                            )\n                        )\n                    )\n            elif i.action == 'claim':\n                result = CommunityEditsQueue.assign_request(i.mrid, username)\n                return delegate.RawText(\n                    json.dumps(response(**result)), content_type=\"application/json\"\n                )\n            elif i.action == 'unassign':\n                CommunityEditsQueue.unassign_request(i.mrid)\n                status = get_status_for_view(CommunityEditsQueue.STATUS['PENDING'])\n                return delegate.RawText(json.dumps(response(newStatus=status)))\n            else:\n                if i.action == \"decline\":\n                    status = CommunityEditsQueue.STATUS['DECLINED']\n                elif i.action == 'approve':\n                    status = CommunityEditsQueue.STATUS['MERGED']\n                CommunityEditsQueue.update_request_status(\n                    i.mrid, status, username, comment=i.comment\n                )\n                return delegate.RawText(\n                    json.dumps(response()), content_type=\"application/json\"\n                )\n        elif i.rtype == \"merge-works\":\n            if i.action == 'create':\n                result = create_request(i.work_ids, username, i.comment)\n                resp = (\n                    response(id=result)\n                    if result\n                    else response(\n                        status='error',\n                        error='A request to merge these works has already been submitted.',\n                    )\n                )\n                return delegate.RawText(\n                    json.dumps(resp), content_type=\"application/json\"\n                )\n            elif i.action == 'create-merged':\n                result = CommunityEditsQueue.submit_work_merge_request(\n                    i.work_ids.split(','),\n                    submitter=username,\n                    reviewer=username,\n                    status=CommunityEditsQueue.STATUS['MERGED'],\n                )\n                return delegate.RawText(\n                    json.dumps(response(id=result)), content_type='application/json'\n                )\n\n    def GET(self):\n        i = web.input(page=1, limit=25, mode=\"open\", submitter=None, reviewer=None)\n        merge_requests = CommunityEditsQueue.get_requests(\n            page=int(i.page),\n            limit=int(i.limit),\n            mode=i.mode,\n            submitter=i.submitter,\n            reviewer=i.reviewer,\n            order='created desc',\n        ).list()\n\n        total_found = CommunityEditsQueue.get_counts_by_mode(\n            mode=i.mode, submitter=i.submitter, reviewer=i.reviewer\n        )\n        return render_template(\n            'merge_queue/merge_queue',\n            total_found,\n            merge_requests=merge_requests,\n        )\n\n    def extract_olids(self, url):\n        query_string = url.split('?')[1]\n        split_params = query_string.split('&')\n        params = {}\n        for p in split_params:\n            kv = p.split('=')\n            params[kv[0]] = kv[1]\n        return params['records'].split(',')\n\n\nclass ui_partials(delegate.page):\n    path = '/merges/partials'\n\n    def GET(self):\n        i = web.input(type=None, comment='')\n        if i.type == 'comment':\n            component = render_template('merge_queue/comment', comment_str=i.comment)\n            return delegate.RawText(component)\n\n\ndef setup():\n    pass\n", "path": "openlibrary/plugins/upstream/edits.py"}]}