-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[checklist] Refactoring of checklist
Refactoring includes - Use of `request.user` instead of passing `user_id` in every HTTP request - Updating of the view only after successful API response - Renamed status of each item from `finished` to `checked` - Use of `ChecklistItem` non-database resource API instead of using HTTP request parameters - Moving of front-end API logic from view to model - CSS hyphens everywhere instead of underscores - Improving Python code (renaming variables, removing redundant code) - Start checklist item IDs from 0 instead of 1. Testing done: Basic CRUD functionality remains. Ensured that the views will not update unless there is a successful response from the server. Reviewed at https://reviews.reviewboard.org/r/7176/
- Loading branch information
1 parent
b747249
commit 27afa78
Showing
10 changed files
with
456 additions
and
342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
from __future__ import unicode_literals | ||
|
||
from checklist_item import checklist_item_resource | ||
from checklist_resource import checklist_resource | ||
from checklist_template import checklist_template_resource | ||
|
||
|
||
__all__ = ['checklist_resource', 'checklist_template_resource'] | ||
__all__ = ['checklist_resource', 'checklist_item_resource', | ||
'checklist_template_resource'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
from __future__ import unicode_literals | ||
|
||
from django.utils import six | ||
from djblets.webapi.decorators import (webapi_login_required, | ||
webapi_response_errors, | ||
webapi_request_fields) | ||
from djblets.webapi.errors import DOES_NOT_EXIST | ||
from reviewboard.webapi.base import WebAPIResource | ||
from reviewboard.webapi.decorators import webapi_check_local_site | ||
|
||
from checklist.models import ReviewChecklist as Checklist | ||
|
||
|
||
class ChecklistItemResource(WebAPIResource): | ||
"""Provide information for individual checklist items of a checklist.""" | ||
|
||
name = 'checklist_item' | ||
uri_object_key = 'checklist_item_id' | ||
allowed_methods = ('GET', 'POST', 'PUT', 'DELETE') | ||
|
||
fields = { | ||
'id': { | ||
'type': six.text_type, | ||
'description': 'The id of the checklist item.', | ||
}, | ||
'description': { | ||
'type': six.text_type, | ||
'description': 'The description of the checklist item.', | ||
}, | ||
'checked': { | ||
'type': bool, | ||
'description': 'Whether the checklist item is finished.', | ||
}, | ||
} | ||
|
||
def get_parent_object(self, pk): | ||
"""Return the parent checklist object.""" | ||
return self._parent_resource.model.objects.get(pk=pk) | ||
|
||
@webapi_login_required | ||
@webapi_response_errors(DOES_NOT_EXIST) | ||
@webapi_check_local_site | ||
def get(self, request, api_format, checklist_id, checklist_item_id, *args, | ||
**kwargs): | ||
"""Return the individual checklist item.""" | ||
try: | ||
checklist = self.get_parent_object(checklist_id) | ||
except Checklist.ObjectDoesNotExist: | ||
return DOES_NOT_EXIST | ||
|
||
if six.text_type(checklist_item_id) not in checklist.checklist_items: | ||
return DOES_NOT_EXIST | ||
|
||
item = checklist.checklist_items.get(six.text_type(checklist_item_id)) | ||
return 200, {self.item_result_key: item} | ||
|
||
@webapi_login_required | ||
@webapi_response_errors(DOES_NOT_EXIST) | ||
@webapi_check_local_site | ||
def get_list(self, request, api_format, checklist_id, *args, **kwargs): | ||
"""Return a list of checklist items in the checklist specified.""" | ||
try: | ||
checklist = self.get_parent_object(checklist_id) | ||
except Checklist.ObjectDoesNotExist: | ||
return DOES_NOT_EXIST | ||
|
||
return 200, {self.item_result_key: checklist.checklist_items} | ||
|
||
@webapi_request_fields( | ||
required={ | ||
'description': { | ||
'type': six.text_type, | ||
'description': 'The description of the checklist item.', | ||
}, | ||
} | ||
) | ||
@webapi_login_required | ||
@webapi_response_errors(DOES_NOT_EXIST) | ||
@webapi_check_local_site | ||
def create(self, request, api_format, checklist_id, description=None, | ||
*args, **kwargs): | ||
"""Add a new checklist item to the checklist.""" | ||
try: | ||
checklist = self.get_parent_object(checklist_id) | ||
except Checklist.ObjectDoesNotExist: | ||
return DOES_NOT_EXIST | ||
|
||
item = checklist.add_item(description) | ||
return 201, {self.item_result_key: item} | ||
|
||
@webapi_request_fields( | ||
optional={ | ||
'description': { | ||
'type': six.text_type, | ||
'description': 'The description of the checklist item.', | ||
}, | ||
'checked': { | ||
'type': bool, | ||
'description': 'Whether the checklist item is completed.', | ||
}, | ||
} | ||
) | ||
@webapi_login_required | ||
@webapi_response_errors(DOES_NOT_EXIST) | ||
@webapi_check_local_site | ||
def update(self, request, api_format, checklist_id, checklist_item_id, | ||
description=None, checked=None, *args, **kwargs): | ||
"""Update a checklist item, whether edited or toggled.""" | ||
try: | ||
checklist = self.get_parent_object(checklist_id) | ||
except Checklist.ObjectDoesNotExist: | ||
return DOES_NOT_EXIST | ||
|
||
item = checklist.edit_item(checklist_item_id, description, checked) | ||
return 200, {self.item_result_key: item} | ||
|
||
@webapi_login_required | ||
@webapi_response_errors(DOES_NOT_EXIST) | ||
@webapi_check_local_site | ||
def delete(self, request, api_format, checklist_id, checklist_item_id, | ||
*args, **kwargs): | ||
"""Delete a checklist item in the checklist.""" | ||
try: | ||
checklist = self.get_parent_object(checklist_id) | ||
except Checklist.ObjectDoesNotExist: | ||
return DOES_NOT_EXIST | ||
|
||
checklist.delete_item(checklist_item_id) | ||
return 204, {} | ||
|
||
|
||
checklist_item_resource = ChecklistItemResource() |
Oops, something went wrong.