Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added route checking before add to route to prevent duplication #183

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions rest_framework_extensions/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ class ExtendedActionLinkRouterMixin(object):
_routs = routes[2:4] + routes[:2] # first routes should be dynamic (because of urlpatterns position matters)
# left self.routs for backward

def route_is_in(self, target_route, route_list):
for route in route_list:
if route.url == target_route.name and route.name == target_route.name and route.mapping == target_route.mapping and route.initkwargs == target_route.initkwargs:
return True
return False

def get_routes(self, viewset):
"""
Augment `self.routes` with any dynamically generated routes.
Expand All @@ -80,20 +86,27 @@ def get_routes(self, viewset):
if self.is_dynamic_route(route):
# Dynamic routes (@list_route or @detail_route decorator)
if self.is_list_dynamic_route(route):
ret += self.get_dynamic_routes_instances(
temporary_route_list = self.get_dynamic_routes_instances(
viewset,
route,
self._filter_by_list_dynamic_routes(dynamic_routes)
)
for temporary_route in temporary_route_list:
if not self.route_is_in(temporary_route, ret):
ret.append(temporary_route)
else:
ret += self.get_dynamic_routes_instances(
temporary_route_list = self.get_dynamic_routes_instances(
viewset,
route,
self._filter_by_detail_dynamic_routes(dynamic_routes)
)
for temporary_route in temporary_route_list:
if not self.route_is_in(temporary_route, ret):
ret.append(temporary_route)
else:
# Standard route
ret.append(route)
if route not in ret:
ret.append(route)

return ret

Expand Down