Exemplo n.º 1
0
    def test_form_list(self):
        view = DataViewSet.as_view({'get': 'list'})
        request = self.factory.get('/', **self.extra)
        response = view(request)
        self.assertEqual(response.status_code, 200)
        formid = self.xform.pk
        data = {
            u'transportation_2011_07_25':
            'http://testserver/api/v1/data/bob/%s' % formid
        }
        self.assertDictEqual(response.data, data)
        response = view(request, owner='bob', formid=formid)
        self.assertEqual(response.status_code, 200)
        self.assertIsInstance(response.data, list)
        self.assertTrue(self.xform.surveys.count())
        dataid = self.xform.surveys.all()[0].pk

        data = {
            u'_bamboo_dataset_id': u'',
            # u'_deleted_at': None,
            u'_attachments': [],
            u'_geolocation': [None, None],
            u'_xform_id_string': u'transportation_2011_07_25',
            u'transport/available_transportation_types_to_referral_facility':
            u'none',
            u'_status': u'submitted_via_web',
            u'_id': dataid
        }
        self.assertDictContainsSubset(data, response.data[0])
        response = view(request, owner='bob', formid=formid, dataid=dataid)
        self.assertEqual(response.status_code, 200)
        self.assertIsInstance(response.data, dict)
        self.assertDictContainsSubset(data, response.data)
Exemplo n.º 2
0
 def test_data_with_formid_as_string(self):
     view = DataViewSet.as_view({'get': 'list'})
     request = self.factory.get('/', **self.extra)
     formid = "random_string"
     response = view(request, owner='bob', formid=formid)
     self.assertEqual(response.status_code, 404)
     formid = self.xform.id_string
     response = view(request, owner='bob', formid=formid)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(len(response.data), 4)
Exemplo n.º 3
0
 def test_data_with_query_parameter(self):
     view = DataViewSet.as_view({'get': 'list'})
     request = self.factory.get('/', **self.extra)
     formid = self.xform.pk
     dataid = self.xform.surveys.all()[0].pk
     response = view(request, owner='bob', formid=formid)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(len(response.data), 4)
     query_str = '{"_id": "%s"}' % dataid
     request = self.factory.get('/?query=%s' % query_str, **self.extra)
     response = view(request, owner='bob', formid=formid)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(len(response.data), 1)
Exemplo n.º 4
0
 def test_data_with_dataid_as_string(self):
     view = DataViewSet.as_view({'get': 'list'})
     request = self.factory.get('/', **self.extra)
     formid = self.xform.pk
     dataid = "random_string"
     request = self.factory.get('/', **self.extra)
     response = view(request, owner='bob', formid=formid, dataid=dataid)
     self.assertEqual(response.status_code, 200)
     self.assertEqual(len(response.data), 0)
     dataid = self.xform.surveys.all()[0].pk
     request = self.factory.get('/', **self.extra)
     response = view(request, owner='bob', formid=formid, dataid=dataid)
     self.assertEqual(response.status_code, 200)
     # a dict object instead of a list
     self.assertIsInstance(response.data, dict)
Exemplo n.º 5
0
 def test_add_form_tag_propagates_to_data_tags(self):
     """Test that when a tag is applied on an xform,
     it propagates to the instance submissions
     """
     view = XFormViewSet.as_view({
         'get': 'labels',
         'post': 'labels',
         'delete': 'labels'
     })
     # no tags
     request = self.factory.get('/', **self.extra)
     response = view(request, owner='bob', pk=1, formid=1)
     self.assertEqual(response.data, [])
     # add tag "hello"
     request = self.factory.post('/', data={"tags": "hello"}, **self.extra)
     response = view(request, owner='bob', pk=1, formid=1)
     self.assertEqual(response.status_code, 201)
     self.assertEqual(response.data, [u'hello'])
     for i in self.xform.surveys.all():
         self.assertIn(u'hello', i.tags.names())
     # remove tag "hello"
     request = self.factory.delete('/', data={"tags": "hello"},
                                   **self.extra)
     response = view(request, owner='bob', pk=1, formid=1, label='hello')
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.data, [])
     for i in self.xform.surveys.all():
         self.assertNotIn(u'hello', i.tags.names())
     view = DataViewSet.as_view({'get': 'labels'})
     request = self.factory.get('/', **self.extra)
     response = view(request, owner='bob', formid="random_string",
                     dataid=i.pk)
     self.assertEqual(response.status_code, 404)
     response = view(request, owner='bob', formid=self.xform.pk,
                     dataid="random")
     self.assertEqual(response.status_code, 404)
     response = view(request, owner='bob', formid=self.xform.pk,
                     dataid=i.pk)
     self.assertEqual(response.status_code, 200)
Exemplo n.º 6
0
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from datacollection.views import UploadFormView, IndexView

from rest_framework import routers
from api.views import DataViewSet, UploadData

# router = routers.DefaultRouter()
# router.register(r'data', DataViewSet)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', IndexView.as_view(), name='index'),
    path('upload/', UploadFormView.as_view(), name='upload'),
    path('api/get', DataViewSet.as_view()),
    path('api-auth/', include('rest_framework.urls',
                              namespace="rest_framework")),
    path('api/upload/', UploadData)
]
Exemplo n.º 7
0
 def test_anon_form_list(self):
     view = DataViewSet.as_view({'get': 'list'})
     request = self.factory.get('/')
     response = view(request)
     self.assertEqual(response.status_code, 401)
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.contrib import admin
from rest_framework import routers
from api.views import DataViewSet, HoursViewSet

router = routers.DefaultRouter()
router.register(r'services', DataViewSet)
router.register(r'hours', HoursViewSet)
hours_list = HoursViewSet.as_view({
    'get': 'list'
})

data_list = DataViewSet.as_view({
    'get': 'list'
})

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]