How to send a review request to a function in my own extension?

29 views
Skip to first unread message

Dave England

unread,
Jul 31, 2024, 12:27:52 PM7/31/24
to Review Board Development
I'm currently writing an extension for Review Board.
I want to pass the current review request (with it's id) to a function in Python.

Here is some of my code:
urls.py:
```
urlpatterns = [
    path("test/<int:review_request_id>/", views.test_function)
]
```

views.py:
```
from reviewboard.reviews.models import ReviewRequest
from my_extension.printer import Printer

def test_function(request, review_request_id):
    review_request = ReviewRequest.get_review_request()

   # function I would like to pass the current ReviewRequest
   printer = Printer(review_request)
   result = printer.generate_report()
   response.write(result)

   return response
```

I'm having the following issue:
Error: ReviewRequest.get_review_request() missing 1 required positional argument: 'self'

As far as I understood this error message, I have to pass an instance of a review request to this function. How can I do that / How should I change my code? I would appreciate any help.

David Trowbridge

unread,
Jul 31, 2024, 12:33:05 PM7/31/24
to reviewb...@googlegroups.com
Dave,

You can fetch the review request instance with:

review_request = ReviewRequest.objects.for_id(review_request_id)

David

--

---
You received this message because you are subscribed to the Google Groups "Review Board Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to reviewboard-d...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/reviewboard-dev/da3ef2ad-ed42-40fc-8f34-b0d1a00a7bc3n%40googlegroups.com.

Dave England

unread,
Aug 1, 2024, 5:18:50 AM8/1/24
to Review Board Development
David,

Thank you so much for your answer! It works now. 
I'm also wondering if there is any way to get the review request id of the review request the user has currently opened? I tried get_display_id() but had no luck (was the same error as the issue I had before).
I'm trying to do something like this:
url = "/my_extension/%d" % review_request_id
Thanks again.

David Trowbridge

unread,
Aug 2, 2024, 3:38:06 PM8/2/24
to reviewb...@googlegroups.com
get_display_id() should be the correct method. Can you share what your code looks like for that, and the exact error you're seeing?

David

Dave England

unread,
Aug 3, 2024, 6:28:43 AM8/3/24
to Review Board Development
My code looks like this:
```
from reviewboard.reviews.models import ReviewRequest

review_request_id = ReviewRequest.get_display_id()
```

the error:
TypeError: ReviewRequest.get_display_id() missing 1 required positional argument: 'self', referer: http://test.review.com/admin/

David Trowbridge

unread,
Aug 3, 2024, 1:23:35 PM8/3/24
to reviewb...@googlegroups.com
Dave,

What's the context of this? Is there a particular extension hook you're running this code in?

David



Dave England

unread,
Aug 3, 2024, 4:39:22 PM8/3/24
to Review Board Development
Sorry for the inconvenience. I'm trying to get the id inside the HeaderAction class. Here is my code:

extension.py:
```
from reviewboard.extensions.base import Extension
from reviewboard.extensions.hooks import URLHook, ActionHook
from reviewboard.actions import BaseAction, BaseMenuAction
from reviewboard.reviews.models import ReviewRequest

class HeaderMenu(BaseMenuAction):
    action_id = 'header-dropdown'
    label = 'Menu'

class HeaderAction(BaseAction):
    review_request_id = ReviewRequest.get_display_id()
    action_id = 'header-item'
    label = 'Action Button'
    parent_id = 'header-dropdown'
    url = '/my_extension/%d/' % review_request_id

class ReviewExportExtension(Extension):
    def initialize(self) -> None:
        ActionHook(self, actions = [HeaderMenu(), HeaderAction(),])
        urlpatterns = [
            path('my_extension/', include('my_extension.urls')),
             ]

        URLHook(self, urlpatterns)
```

David Trowbridge

unread,
Aug 6, 2024, 3:33:28 PM8/6/24
to reviewb...@googlegroups.com
Dave,

I haven't actually run this, but it should be doable with something like this:

from reviewboard.extensions.base import Extension
from reviewboard.extensions.hooks import URLHook, ActionHook
from reviewboard.actions import BaseAction, BaseMenuAction
from reviewboard.reviews.models import ReviewRequest
from reviewboard.urls import reviewable_url_names, review_request_url_names

all_review_request_url_names = reviewable_url_names + review_request_url_names



class HeaderMenu(BaseMenuAction):
    action_id = 'header-dropdown'
    apply_to = all_review_request_url_names

    label = 'Menu'

class HeaderAction(BaseAction):
    review_request_id = ReviewRequest.get_display_id()
    action_id = 'header-item'
    label = 'Action Button'
    parent_id = 'header-dropdown'

    def get_url(self, *, context):
        review_request = context['review_request']
        return f'/my_extension/{review_request.display_id}/'


class ReviewExportExtension(Extension):
    def initialize(self) -> None:
        ActionHook(self, actions = [HeaderMenu(), HeaderAction(),])
        urlpatterns = [
            path('my_extension/', include('my_extension.urls')),
        ]

        URLHook(self, urlpatterns)



What I changed from your code:
- Added apply_to to ensure that the action only renders for review request-related things
- Changed the `url` attribute to be a `get_url` method, which pulls the current review request out of the page rendering context.


David

Dave England

unread,
Aug 9, 2024, 1:15:24 PM8/9/24
to Review Board Development
Thank you so much! It works now.
Reply all
Reply to author
Forward
0 new messages