InterSIS / django-rest-encrypted-lookup

Replace Rest Framework's IntegerField pk or id lookups with encrypted strings.
GNU General Public License v3.0
27 stars 8 forks source link

RetrieveModelMixin in the inherited ViewSet cannot work #11

Closed lyn5466 closed 9 years ago

lyn5466 commented 9 years ago

Hi,

I have a problem when working with this package. My viewset and url settings are shown as below.

views.py

class ActivityLogViewSet(viewsets.mixins.CreateModelMixin,
                         viewsets.mixins.ListModelMixin,
                         viewsets.mixins.RetrieveModelMixin,
                         EncryptedLookupGenericViewSet):
    """
    API endpoint that allows activity logs to be viewed or recorded.
    """
    queryset = ActivityLog.objects.all()
    serializer_class = ActivityLogSerializer
    lookup_field = 'id'

urls.py

router = routers.SimpleRouter()
router.register(r'activitylogs', views.ActivityLogViewSet)

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include(router.urls)),
]

When my server start running, it did not provide a retrieve function. Here is my query in browser: http://127.0.0.1:8000/activitylogs/vq6dtebgu3ipafvkcsuwa4z3wi where vq6dtebgu3ipafvkcsuwa4z3wi is the encrypted pk. Then, I get errors as below:

Request Method: GET
Request URL:    http://127.0.0.1:8000/activitylogs/vq6dtebgu3ipafvkcsuwa4z3wi/
Django Version: 1.8.4
Exception Type: AttributeError
Exception Value:    
'ActivityLogViewSet' object has no attribute 'request'
Exception Location: /Applications/anaconda/lib/python2.7/site-packages/rest_framework/generics.py in get_serializer_context, line 77
Python Executable:  /Applications/anaconda/bin/python
Python Version: 2.7.9
Python Path:    
['/Users/mtk09566/Documents/project/logserver',
 '/Applications/anaconda/lib/python27.zip',
 '/Applications/anaconda/lib/python2.7',
 '/Applications/anaconda/lib/python2.7/plat-darwin',
 '/Applications/anaconda/lib/python2.7/plat-mac',
 '/Applications/anaconda/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Applications/anaconda/lib/python2.7/lib-tk',
 '/Applications/anaconda/lib/python2.7/lib-old',
 '/Applications/anaconda/lib/python2.7/lib-dynload',
 '/Applications/anaconda/lib/python2.7/site-packages/cryptography-0.8-py2.7-macosx-10.5-x86_64.egg',
 '/Applications/anaconda/lib/python2.7/site-packages/setuptools-14.3-py2.7.egg',
 '/Applications/anaconda/lib/python2.7/site-packages',
 '/Applications/anaconda/lib/python2.7/site-packages/Sphinx-1.2.3-py2.7.egg',
 '/Applications/anaconda/lib/python2.7/site-packages/aeosa']

Hope you can help me solve this problem. Thanks!

JASchilz commented 9 years ago

Acknowledged! Will take a look this weekend.

JASchilz commented 9 years ago

One idea and one question:

In ActivityLogViewset, try moving EncryptedLookupGenericViewSet to be the class's first parent. For example:

class ActivityLogViewSet(EncryptedLookupGenericViewSet,
                         viewsets.mixins.CreateModelMixin,
                         viewsets.mixins.ListModelMixin,
                         viewsets.mixins.RetrieveModelMixin):

I doubt that this will help, however I haven't worked with these packages in a while; maybe it's the solution.

Also, what is your version of django-rest-framework? Given that, I can try to replicate the issue with more confidence. I haven't used 3.2.x, but I would like to extend support to it.

lyn5466 commented 9 years ago

Hi,

Much thanks for your reply!

Exception Type: AttributeError
Exception Value: 'ActivityLogViewSet' object has no attribute 'request'

More details for reference:

JASchilz commented 9 years ago

I was able to replicate it and solve it. Try version 0.10.1 .

The easiest way I've found to upgrade a single package is:

pip uninstall django-rest-encrypted-lookup
pip install django-rest-encrypted-lookup

Using the --upgrade flag upgraded other packages that I didn't want upgraded.

You're invited to pull request in documentation for a basic urls.py, such as you outlined, into README. If you don't get to it, I will later. (:

Hope this works.

lyn5466 commented 9 years ago

Hi JASchilz, The new version works! Thanks for helping!

JASchilz commented 9 years ago

My pleasure! I'll add an integration test alongside the unit tests to help prevent a similar problem in the future. Thanks for the docs!