Thanks Christian.

I appreciate your help.  I went digging through the database model to 
understand the schema better and ended up using the model directly per your 
example since I do not need the caching (this is just a low-usage internal 
review site).  I cleaned up the code per your other suggestions and 
everything is working for getting the votes into the database cleanly.

Two questions remain in regards to final polishing:
1) what is the best way to add a banner update based upon the response of 
the request
2) what is the best way to remove the "Ship It" link since I have replaced 
those with discrete votes?  Previous requests seem to point to just getting 
rid of the link by updating the CSS.

Regards,
Eric


On Wednesday, May 11, 2016 at 7:04:18 PM UTC+12, Christian Hammond wrote:
>
> Hi Eric,
>
> A couple comments:
>
> 1) allowed_methods is a tuple, so it needs to be
>
>     allowed_methods = ('POST',)
>
> Without that comma, it becomes ('P', 'O', 'S', 'T'), preventing HTTP POST 
> from working.
>
> 2) resources.reviews.get_object(request, *args, **kwargs) will only work 
> if the URL for your resource has all the same regex capture groups that the 
> review resource has. That basically means it must be a child object.
>
> Since it's not (assuming you're not doing anything really hacky and 
> custom), you'll need to provide all those as keyword arguments to 
> get_object(). I believe you'll need:
>
>     review = resources.reviews.get_object(request, review_request_id=<ID>, 
> review_id=<ID>)
>
> Alternatively, you could query from the database directly with 
> Review.objects.get(review_id=<ID>). The former will perform a query that 
> will filter out replies, and will also perform some local caching (which 
> will prevent extra database queries if performing the same get_object() 
> within the same request).
>
> 3) Your function will need to return proper webapi error objects for the 
> error cases, and a tuple in the final return case. For a HTTP POST, you 
> want:
>
>     return 201, {
>         self.item_result_key: yourdata,
>     }
>
> Hope that helps!
>
> - Christian
>
> -- 
> Christian Hammond
> President/CEO of Beanbag <https://www.beanbaginc.com/>
> Makers of Review Board <https://www.reviewboard.org/>
>
> On Tue, May 10, 2016 at 10:28 PM, Eric Holmberg <eric.omn...@gmail.com 
> <javascript:>> wrote:
>
>> Hi Christian,
>>
>> Here is the core of the extension.  The `model` and `uri_object_key` 
>> issues are due to the resources.review.get_object() request.  Maybe I 
>> should be going directly to the database model instead of going through the 
>> webapi?
>>
>> class SampleExtensionResource(WebAPIResource):
>>     """Resource for review voting"""
>>     name = 'ballot_box'
>>     uri_name = 'ballot_box'
>>     allowed_methods = ('POST')
>>
>>
>>     def has_access_permissions(self, request, *args, **kwargs):
>>         return review_request.is_accessible_by(request.user)
>>     
>>     def create(self, request, *args, **kwargs):
>>         
>>         try:
>>             # v--- problem is there
>>             review = resources.review.get_object(request, *args, **kwargs
>> )
>>         except ObjectDoesNotExist:
>>              # TODO - need to return a proper failure message here
>>             return HttpResponse("Unable to lookup review")
>>
>>
>>         if not resources.review.has_modify_permissions(request, review):
>>             return self.get_no_access_error(request.user)
>>
>>
>>         comment_kwargs = {
>>                 'body_top_text_type': 'plain',
>>                 'body_bottom': '', 'body_bottom_text_type': 'plain',
>>                 'public': 1,
>>                 'ship_it': True,
>>                 'body_top': "TODO - Add real comment here"
>>         }
>>         new_comment = self.create_comment(fields=(), review=review, **
>> comment_kwargs)
>>         review.general_comments.add(new_comment)
>>
>>
>>         # TODO - probably need a json response
>>         response = "OK"
>>         return HttpResponse(response)
>>
>> Regards,
>> Eric
>>
>> On Friday, May 6, 2016 at 10:07:31 PM UTC+12, Christian Hammond wrote:
>>>
>>> Hi Eric,
>>>
>>> The `model` and `uri_object_key` are configurations for a resource's 
>>> implementation, and aren't related to the data provided by the caller.
>>>
>>> The `model` attribute is optionally used to associate a WebAPIResource 
>>> subclass with a particular database model. If set, many of the operations 
>>> (GET requests to lists or to objects, DELETEs, etc.) are implemented for 
>>> you, but otherwise, you have to implement those yourself (by overriding 
>>> get(), get_list(), delete(), etc.).
>>>
>>> `uri_object_key` defines the name of the capture group in the URL's 
>>> regex that captures the iD used to look up the appropriate object (defined 
>>> by `model`) in the database.
>>>
>>> You don't need to do anything with Backbone to make use of the API. a 
>>> $.post() should be fine. However, it sounds like maybe the WebAPIResource 
>>> isn't implemented fully.
>>>
>>> Can you show me the code you've written, how you're calling into the 
>>> API, and what the results are when you do so? We can help you write the 
>>> WebAPIResource to do what you need.
>>>
>>> Christian
>>>
>>> -- 
>>> Christian Hammond
>>> President/CEO of Beanbag <https://www.beanbaginc.com/>
>>> Makers of Review Board <https://www.reviewboard.org/>
>>>
>>> On Wed, May 4, 2016 at 9:04 PM, Eric Holmberg <eric.omn...@gmail.com> 
>>> wrote:
>>>
>>>> I have created a WebAPIResource extension based upon the instructions 
>>>> in 
>>>> https://www.reviewboard.org/docs/manual/2.0/extending/extensions/webapi/ 
>>>> which is called based upon the user clicking on a menu item which was 
>>>> created by ReviewRequestDropdownActionHook.
>>>>
>>>> How do I actually do the POST call?
>>>>
>>>> I tried just the usual jQuery $.post(...), but djblets/webapi/
>>>> resources.base.py tries to get the "model" and "uri_object_keys" and 
>>>> fails since they are not part of the post data.  It looks like I need to 
>>>> create a view and model by deriving from Backbone.View.extend 
>>>> Backbone.Model.extend, 
>>>> but I haven't found any concise examples that I can understand well enough 
>>>> to use as a template.
>>>>
>>>> My use case:
>>>>
>>>>    1. Based upon a UI event, post to the WebAPIResource
>>>>    2. In the WebAPIResource.create method:
>>>>       1. Lookup current review, post a comment based upon the approval 
>>>>       request (similar to a ship-it comment)
>>>>       2. Log vote into a new database model
>>>>       3. Query the existing votes and if everything looks good, 
>>>>       trigger a merge of the change or rejection email
>>>>    
>>>>
>>>> Note that I'm not a Javascript or web programmer since my background is 
>>>> C++ and Python, so I'm sorry if my question is elementary.
>>>>
>>>> Regards,
>>>> Eric
>>>>
>>>>
>>>>
>>>>
>>>> -- 
>>>> Supercharge your Review Board with Power Pack: 
>>>> https://www.reviewboard.org/powerpack/
>>>> Want us to host Review Board for you? Check out RBCommons: 
>>>> https://rbcommons.com/
>>>> Happy user? Let us know! https://www.reviewboard.org/users/
>>>> --- 
>>>> You received this message because you are subscribed to the Google 
>>>> Groups "reviewboard" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send 
>>>> an email to reviewboard...@googlegroups.com.
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>> -- 
>> Supercharge your Review Board with Power Pack: 
>> https://www.reviewboard.org/powerpack/
>> Want us to host Review Board for you? Check out RBCommons: 
>> https://rbcommons.com/
>> Happy user? Let us know! https://www.reviewboard.org/users/
>> --- 
>> You received this message because you are subscribed to the Google Groups 
>> "reviewboard" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to reviewboard...@googlegroups.com <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
Supercharge your Review Board with Power Pack: 
https://www.reviewboard.org/powerpack/
Want us to host Review Board for you? Check out RBCommons: 
https://rbcommons.com/
Happy user? Let us know! https://www.reviewboard.org/users/
--- 
You received this message because you are subscribed to the Google Groups 
"reviewboard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to reviewboard+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to