from django.conf.urls import url, include

from rest_framework import routers

from .views import (ElectionViewSet, ElectionTypeViewSet,
                    ElectionSubTypeViewSet, OrganisationViewSet)


class EERouter(routers.DefaultRouter):

    def get_lookup_regex(self, viewset, lookup_prefix=''):
        # we identify organisations by
        # (organisation_type, official_identifier, start_date)
        # but DRF doesn't do composite keys
        # (much like the rest of django)
        # so it needs a bit of... gentle persuasion
        if viewset == OrganisationViewSet:
            return r'(?P<organisation_type>[-\w]+)/(?P<official_identifier>[-\w]+)/(?P<date>\d{4}-\d{2}-\d{2})'
        return super().get_lookup_regex(viewset, lookup_prefix)


router = EERouter()
router.register(r'elections', ElectionViewSet)
router.register(r'election_types', ElectionTypeViewSet)
router.register(r'election_subtypes', ElectionSubTypeViewSet)
router.register(r'organisations', OrganisationViewSet)

routes = router.get_urls()

urlpatterns = [
    url(r'^', include(routes)),

    url(
        r'^organisations/(?P<organisation_type>[-\w]+)/(?P<official_identifier>[-\w]+)/$',
        OrganisationViewSet.as_view({'get': 'filter'})
    ),
    url(
        r'^organisations/(?P<organisation_type>[-\w]+)/(?P<official_identifier>[-\w]+)\.(?P<format>[a-z0-9]+)/?$',
        OrganisationViewSet.as_view({'get': 'filter'})
    ),
]

@@ -1074,16 +1074,301 @@
 tes)),%0A%0A
+    url(%0A        r'%5Eorganisations/(?P%3Corganisation_type%3E%5B-%5Cw%5D+)/$',%0A        OrganisationViewSet.as_view(%7B'get': 'filter'%7D)%0A    ),%0A    url(%0A        r'%5Eorganisations/(?P%3Corganisation_type%3E%5B-%5Cw%5D+)%5C.(?P%3Cformat%3E%5Ba-z0-9%5D+)/?$',%0A        OrganisationViewSet.as_view(%7B'get': 'filter'%7D)%0A    ),%0A%0A
     url(

add filter route for /organisations/{type}