Commits

Martin Skala committed 6de83b2

ROA_FILTERS method to define an authentication method

  • Participants
  • Parent commits 2e96c39

Comments (0)

Files changed (5)

django_roa/db/models.py

 ROA_MODEL_NAME_MAPPING = getattr(settings, 'ROA_MODEL_NAME_MAPPING', [])
 ROA_HEADERS = getattr(settings, 'ROA_HEADERS', {})
 ROA_FORMAT = getattr(settings, "ROA_FORMAT", 'json')
+ROA_FILTERS = getattr(settings, 'ROA_FILTERS', {})
 
 DEFAULT_CHARSET = getattr(settings, 'DEFAULT_CHARSET', 'utf-8')
 
             
             if force_update or pk_set and not self.id is None:
                 record_exists = True
-                resource = Resource(self.get_resource_url_detail(), headers=ROA_HEADERS)
+                resource = Resource(self.get_resource_url_detail(),
+                                    headers=ROA_HEADERS,
+                                    filters=ROA_FILTERS)
                 try:
                     logger.debug(u"""Modifying : "%s" through %s
                                   with payload "%s" and GET args "%s" """ % (
                     raise ROAException(e)
             else:
                 record_exists = False
-                resource = Resource(self.get_resource_url_list(), headers=ROA_HEADERS)
+                resource = Resource(self.get_resource_url_list(),
+                                    headers=ROA_HEADERS,
+                                    filters=ROA_FILTERS)
                 try:
                     logger.debug(u"""Creating  : "%s" through %s
                                   with payload "%s" and GET args "%s" """ % (
                 % (self._meta.object_name, self._meta.pk.attname)
                 
         # Deletion in cascade should be done server side.
-        resource = Resource(self.get_resource_url_detail())
+        resource = Resource(self.get_resource_url_detail(),
+                            headers=ROA_HEADERS,
+                            filters=ROA_FILTERS)
         
         logger.debug(u"""Deleting  : "%s" through %s""" % \
             (unicode(self), unicode(resource.uri)))

django_roa/db/query.py

 ROA_ARGS_NAMES_MAPPING = getattr(settings, 'ROA_ARGS_NAMES_MAPPING', {})
 ROA_HEADERS = getattr(settings, 'ROA_HEADERS', {})
 ROA_FORMAT = getattr(settings, "ROA_FORMAT", 'json')
+ROA_FILTERS = getattr(settings, 'ROA_FILTERS', {})
 
 DEFAULT_CHARSET = getattr(settings, 'DEFAULT_CHARSET', 'utf-8')
 
         An iterator over the results from applying this QuerySet to the
         remote web service.
         """
-        resource = Resource(self.model.get_resource_url_list(), headers=ROA_HEADERS)
-
+        resource = Resource(self.model.get_resource_url_list(),
+                            headers=ROA_HEADERS,
+                            filters=ROA_FILTERS)
         try:
             parameters = self.query.parameters
             logger.debug(u"""Requesting: "%s" through %s
         # a staticmethod for get_resource_url_count and avoid to set it
         # for all model without relying on get_resource_url_list
         instance = clone.model()
-        resource = Resource(instance.get_resource_url_count(), headers=ROA_HEADERS)
-        
+        resource = Resource(instance.get_resource_url_count(),
+                            headers=ROA_HEADERS,
+                            filters=ROA_FILTERS)
         try:
             parameters = clone.query.parameters
             logger.debug(u"""Counting  : "%s" through %s
         
         return cnt
 
-    def _get_from_id_or_pk(self, id=None, pk=None):
+    def _get_from_id_or_pk(self, id=None, pk=None, **kwargs):
         """
         Returns an object given an id or pk, request directly with the
         get_resource_url_detail method without filtering on ids
             instance.id = id
         else:
             instance.pk = pk
-        resource = Resource(instance.get_resource_url_detail(), headers=ROA_HEADERS)
-        
+        resource = Resource(instance.get_resource_url_detail(),
+                            headers=ROA_HEADERS,
+                            filters=ROA_FILTERS,
+                            **kwargs)
         try:
             parameters = clone.query.parameters
             logger.debug(u"""Retrieving : "%s" through %s

examples/django_roa_client/settings.py

 ROA_ARGS_NAMES_MAPPING = {
     'ORDER_BY': 'order',
 }
+# Enable HTTP authentication through django-piston
+from restkit import BasicAuth
+ROA_FILTERS = [ BasicAuth('django-roa', 'roa'), ]
+# Disable authentication through django-piston
+#ROA_FILTERS = []
 
 ## Logging settings
 import logging

examples/django_roa_client/tests.py

     RemotePageWithRelationsThrough
 from django_roa_client.forms import TestForm, RemotePageForm
 
+ROA_FILTERS = getattr(settings, 'ROA_FILTERS', {})
+
 class ROATestCase(TestCase):
     
     def setUp(self):
 
     def tearDown(self):
         RemotePage.objects.all().delete()
- 
 
 class ROAUserTestCase(ROATestCase):
     
         settings.ROA_FORMAT = 'custom'
         page = RemotePage.objects.create(title=u'A custom serialized page')
         self.assertEqual(repr(page), '<RemotePage: A custom serialized page (1)>')
-        r = Resource('http://127.0.0.1:8081/django_roa_server/remotepage/')
+        r = Resource('http://127.0.0.1:8081/django_roa_server/remotepage/', filters=ROA_FILTERS)
         response = r.get(**{'format': 'custom'})
         self.assertEqual(repr(response.body_string()), '\'<?xml version="1.0" encoding="utf-8"?>\\n<django-test version="1.0">\\n <object pk="1" model="django_roa_server.remotepage">\\n  <field type="CharField" name="title">A custom serialized page</field>\\n </object>\\n</django-test>\'')
         self.assertEqual(len(RemotePage.objects.all()), 1)

examples/django_roa_server/urls.py

     RemotePageWithRelationsHandler, RemotePageWithNamedRelationsHandler, \
     RemotePageWithNamedRelationsCountHandler, RemotePageWithRelationsThroughHandler
 
-remote_pages = Resource(handler=RemotePageHandler)
-remote_pages_count = Resource(handler=RemotePageCountHandler)
+# Enable HTTP authentication through django-piston
+ad = { 'authentication': HttpBasicAuthentication(
+   realm="django-roa-server", 
+   auth_func = lambda username, password: username == 'django-roa' and password == 'roa'
+)}
+# Disable authentication through django-piston
+#ad = { 'authentication': None}
 
-remote_pages_with_many_fields = Resource(handler=RemotePageWithManyFieldsHandler)
-remote_pages_with_many_fields_count = Resource(handler=RemotePageWithManyFieldsCountHandler)
+remote_pages = Resource(handler=RemotePageHandler, **ad)
+remote_pages_count = Resource(handler=RemotePageCountHandler, **ad)
 
-remote_pages_with_boolean_fields = Resource(handler=RemotePageWithBooleanFieldsHandler)
-remote_pages_with_boolean_fields_count = Resource(handler=RemotePageWithBooleanFieldsCountHandler)
+remote_pages_with_many_fields = Resource(handler=RemotePageWithManyFieldsHandler, **ad)
+remote_pages_with_many_fields_count = Resource(handler=RemotePageWithManyFieldsCountHandler, **ad)
 
-remote_pages_with_custom_slug = Resource(handler=RemotePageWithCustomSlugHandler)
-remote_pages_with_custom_slug_count = Resource(handler=RemotePageWithCustomSlugCountHandler)
+remote_pages_with_boolean_fields = Resource(handler=RemotePageWithBooleanFieldsHandler, **ad)
+remote_pages_with_boolean_fields_count = Resource(handler=RemotePageWithBooleanFieldsCountHandler, **ad)
 
-remote_pages_with_overridden_urls = Resource(handler=RemotePageWithOverriddenUrlsHandler)
-remote_pages_with_overridden_urls_count = Resource(handler=RemotePageWithOverriddenUrlsCountHandler)
+remote_pages_with_custom_slug = Resource(handler=RemotePageWithCustomSlugHandler, **ad)
+remote_pages_with_custom_slug_count = Resource(handler=RemotePageWithCustomSlugCountHandler, **ad)
 
-remote_pages_with_relations = Resource(handler=RemotePageWithRelationsHandler)
-remote_pages_with_relations_count = Resource(handler=RemotePageWithRelationsHandler)
+remote_pages_with_overridden_urls = Resource(handler=RemotePageWithOverriddenUrlsHandler, **ad)
+remote_pages_with_overridden_urls_count = Resource(handler=RemotePageWithOverriddenUrlsCountHandler, **ad)
 
-remote_pages_with_relations_through = Resource(handler=RemotePageWithRelationsThroughHandler)
-remote_pages_with_relations_through_count = Resource(handler=RemotePageWithRelationsThroughHandler)
+remote_pages_with_relations = Resource(handler=RemotePageWithRelationsHandler, **ad)
+remote_pages_with_relations_count = Resource(handler=RemotePageWithRelationsHandler, **ad)
 
-remote_pages_with_named_relations = Resource(handler=RemotePageWithNamedRelationsHandler)
-remote_pages_with_named_relations_count = Resource(handler=RemotePageWithNamedRelationsCountHandler)
+remote_pages_with_relations_through = Resource(handler=RemotePageWithRelationsThroughHandler, **ad)
+remote_pages_with_relations_through_count = Resource(handler=RemotePageWithRelationsThroughHandler, **ad)
 
-users = Resource(handler=UserHandler)
-messages = Resource(handler=MessageHandler)
-permissions = Resource(handler=PermissionHandler)
-groups = Resource(handler=GroupHandler)
+remote_pages_with_named_relations = Resource(handler=RemotePageWithNamedRelationsHandler, **ad)
+remote_pages_with_named_relations_count = Resource(handler=RemotePageWithNamedRelationsCountHandler, **ad)
+
+users = Resource(handler=UserHandler, **ad)
+messages = Resource(handler=MessageHandler, **ad)
+permissions = Resource(handler=PermissionHandler, **ad)
+groups = Resource(handler=GroupHandler, **ad)
 
 urlpatterns = patterns('',
     # Remote pages counts