Skip to content

Commit

Permalink
[api] Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pandafy committed Jun 18, 2020
1 parent 27c44af commit dcd0868
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 30 deletions.
13 changes: 8 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ Setup (integrate into an existing Django project)
'allauth',
'allauth.account',
'allauth.socialaccount',
# rest framework
'rest_framework',
'drf_yasg',
'openwisp_users',
'django.contrib.admin',
# notifications module
Expand Down Expand Up @@ -356,7 +359,7 @@ Live documentation

.. image:: docs/images/api-docs.png

A general live API documentation (following the OpenAPI specification) at ``/api/v1/docs/``.
A general live API documentation (following the OpenAPI specification) is available at ``/api/v1/docs/``.

Browsable web interface
~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -379,7 +382,7 @@ This authentication scheme uses Django's default session backend for authenticat
http -a username:password <HTTP verb> <api url>
When browsing the API via the `Live documentation <#live-documentation>`_
or the `Browsable web page <#browsable-web-interface>`_, you can use
or the `Browsable web interface <#browsable-web-interface>`_, you can use
the session authentication by logging in the django admin.

Pagination
Expand All @@ -397,7 +400,7 @@ List of endpoints
~~~~~~~~~~~~~~~~~

Since the detailed explanation is contained in the `Live documentation <#live-documentation>`_
and in the `Browsable web page <#browsable-web-interface>`_ of each point,
and in the `Browsable web page <#browsable-web-interface>`_ of each endpoint,
here we'll provide just a list of the available endpoints,
for further information please open the URL of the endpoint in your browser.

Expand Down Expand Up @@ -736,10 +739,10 @@ API views
#########

The API view classes can be extended into other django applications as well. Note
that it is not required for extending openwisp-ipam to your app and this change
that it is not required for extending openwisp-notifications to your app and this change
is required only if you plan to make changes to the API views.

Create a view file as done in `views.py <https://github.com/openwisp/openwisp-ipam/tree/master/tests/openwisp2/sample_ipam/views.py>`_
Create a view file as done in `views.py <https://github.com/openwisp/openwisp-notifications/blob/master/tests/openwisp2/sample_notifications/views.py>`_

For more information regarding Django REST Framework API views, please refer to the
`"Generic views" section in the Django REST Framework documentation <https://www.django-rest-framework.org/api-guide/generic-views/>`_.
Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions openwisp_notifications/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ def get_notification_url(self, obj):
f'admin:{Notification._meta.app_label}_{Notification._meta.model_name}_change',
args=(obj.id,),
)
url = _get_absolute_url(url)
return url
return _get_absolute_url(url)
8 changes: 3 additions & 5 deletions openwisp_notifications/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class BaseNotificationView(GenericAPIView):
serializer_class = NotificationSerializer

def get_queryset(self):
user = self.request.user
return self.queryset.filter(recipient=user)
return self.queryset.filter(recipient=self.request.user)


class NotificationListView(BaseNotificationView, ListModelMixin):
Expand All @@ -55,10 +54,9 @@ def _mark_notification_read(self, notification_id):
data = {'error': _(str(e))}
status_code = status.HTTP_404_NOT_FOUND
else:
data = {'detail': 'Notification marked as read.'}
status_code = status.HTTP_200_OK
notification.mark_as_read()

data = {'detail': _('Notification marked as read.')}
status_code = status.HTTP_200_OK
return Response(data, status_code)


Expand Down
10 changes: 5 additions & 5 deletions openwisp_notifications/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ def test_list_notification_api(self):
url = reverse(f'{self.app_label}:api_list_notifications')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['count'], 20)
self.assertEqual(response.data['count'], number_of_notifications)
self.assertEqual(
response.data['next'],
'http://testserver/api/v1/openwisp_notifications/notifications/?page=2',
f'http://testserver/api/v1/{self.app_label}/notifications/?page=2',
)
self.assertEqual(response.data['previous'], None)
self.assertEqual(len(response.data['results']), 10)

next_response = self.client.get(response.data['next'])
self.assertEqual(next_response.status_code, 200)
self.assertEqual(next_response.data['count'], 20)
self.assertEqual(next_response.data['count'], number_of_notifications)
self.assertEqual(
next_response.data['next'], None,
)
self.assertEqual(
next_response.data['previous'],
'http://testserver/api/v1/openwisp_notifications/notifications/',
f'http://testserver/api/v1/{self.app_label}/notifications/',
)
self.assertEqual(len(next_response.data['results']), 10)

Expand All @@ -68,7 +68,7 @@ def test_read_notification_api(self):
response.data, {'error': 'Notification matching query does not exist.'}
)

with self.subTest('Test for existing notificaton'):
with self.subTest('Test for valid notificaton'):
url = reverse(f'{self.app_label}:api_read_notifications', args=(n.id,))
response = self.client.patch(url)
self.assertEqual(response.status_code, 200)
Expand Down
8 changes: 5 additions & 3 deletions tests/openwisp2/sample_notifications/urls.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from django.conf.urls import include, url
from django.conf.urls import url

from . import views

app_name = 'sample_notifications'

urlpatterns = [
url(
r'^api/v1/openwisp_notifications/notifications/$',
r'^api/v1/sample_notifications/notifications/$',
views.list_notifications,
name='api_list_notifications',
),
url(
r'^api/v1/openwisp_notifications/notifications/(?P<notification_id>[0-9A-Fa-f-]+)/$',
r'^api/v1/sample_notifications/notifications/(?P<notification_id>[0-9A-Fa-f-]+)/$',
views.read_notification,
name='api_read_notifications',
),
Expand Down
19 changes: 9 additions & 10 deletions tests/openwisp2/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@
# you are extending the app and modifing the API views in your extended
# application.
urlpatterns += [
url(r'^', include('openwisp2.sample_notifications.urls', namespace='sample_notifications'))
url(
r'^',
include(
'openwisp2.sample_notifications.urls', namespace='sample_notifications'
),
)
]
else:
# Load openwisp_ipam api views: This can be used
# when you are extending the app but not making
# any changes in the API views.

# Load openwisp_notifications api views: This can be used when you are
# extending the app but not making any changes in the API views.
urlpatterns += [
url(
r'',
include('openwisp_notifications.urls'),

),
url(r'', include('openwisp_notifications.urls'),),
]

0 comments on commit dcd0868

Please sign in to comment.