api for username
authorJan Szejko <janek37@gmail.com>
Mon, 18 Jun 2018 08:41:28 +0000 (10:41 +0200)
committerJan Szejko <janek37@gmail.com>
Mon, 18 Jun 2018 08:41:28 +0000 (10:41 +0200)
src/api/handlers.py
src/api/urls.py

index 3e4e093..ab1a44a 100644 (file)
@@ -656,16 +656,18 @@ class PictureHandler(BaseHandler):
 
 class UserDataHandler(BaseHandler):
     model = BookUserData
 
 class UserDataHandler(BaseHandler):
     model = BookUserData
-    fields = ('state',)
+    fields = ('state', 'username')
     allowed_methods = ('GET', 'POST')
 
     allowed_methods = ('GET', 'POST')
 
-    def read(self, request, slug):
+    def read(self, request, slug=None):
+        if not request.user.is_authenticated():
+            return rc.FORBIDDEN
+        if slug is None:
+            return {'username': request.user.username}
         try:
             book = Book.objects.get(slug=slug)
         except Book.DoesNotExist:
             return rc.NOT_FOUND
         try:
             book = Book.objects.get(slug=slug)
         except Book.DoesNotExist:
             return rc.NOT_FOUND
-        if not request.user.is_authenticated():
-            return rc.FORBIDDEN
         try:
             data = BookUserData.objects.get(book=book, user=request.user)
         except BookUserData.DoesNotExist:
         try:
             data = BookUserData.objects.get(book=book, user=request.user)
         except BookUserData.DoesNotExist:
index 006abed..f90ec3e 100644 (file)
@@ -80,6 +80,7 @@ urlpatterns = [
     url(r'^reading/(?P<slug>[a-z0-9-]+)/$', reading_resource, name='api_reading'),
     url(r'^reading/(?P<slug>[a-z0-9-]+)/(?P<state>[a-z]+)/$', reading_resource, name='api_reading'),
     url(r'^shelf/(?P<state>[a-z]+)/$', shelf_resource, name='api_shelf'),
     url(r'^reading/(?P<slug>[a-z0-9-]+)/$', reading_resource, name='api_reading'),
     url(r'^reading/(?P<slug>[a-z0-9-]+)/(?P<state>[a-z]+)/$', reading_resource, name='api_reading'),
     url(r'^shelf/(?P<state>[a-z]+)/$', shelf_resource, name='api_shelf'),
+    url(r'^username/$', reading_resource, name='api_username'),
 
     # objects details
     url(r'^books/(?P<book>[a-z0-9-]+)/$', book_resource, name="api_book"),
 
     # objects details
     url(r'^books/(?P<book>[a-z0-9-]+)/$', book_resource, name="api_book"),