Fix #1056 - Add flag to accept URLs without a trailing slash
This commit is contained in:
parent
058964bce7
commit
c7c26b1740
@ -20,74 +20,87 @@ from mediagoblin.tools.routing import add_route
|
|||||||
add_route(
|
add_route(
|
||||||
"mediagoblin.federation.user",
|
"mediagoblin.federation.user",
|
||||||
"/api/user/<string:username>/",
|
"/api/user/<string:username>/",
|
||||||
"mediagoblin.federation.views:user_endpoint"
|
"mediagoblin.federation.views:user_endpoint",
|
||||||
|
match_slash=False
|
||||||
)
|
)
|
||||||
|
|
||||||
add_route(
|
add_route(
|
||||||
"mediagoblin.federation.user.profile",
|
"mediagoblin.federation.user.profile",
|
||||||
"/api/user/<string:username>/profile",
|
"/api/user/<string:username>/profile/",
|
||||||
"mediagoblin.federation.views:profile_endpoint"
|
"mediagoblin.federation.views:profile_endpoint",
|
||||||
|
match_slash=False
|
||||||
)
|
)
|
||||||
|
|
||||||
# Inbox and Outbox (feed)
|
# Inbox and Outbox (feed)
|
||||||
add_route(
|
add_route(
|
||||||
"mediagoblin.federation.feed",
|
"mediagoblin.federation.feed",
|
||||||
"/api/user/<string:username>/feed",
|
"/api/user/<string:username>/feed/",
|
||||||
"mediagoblin.federation.views:feed_endpoint"
|
"mediagoblin.federation.views:feed_endpoint",
|
||||||
|
match_slash=False
|
||||||
)
|
)
|
||||||
|
|
||||||
add_route(
|
add_route(
|
||||||
"mediagoblin.federation.user.uploads",
|
"mediagoblin.federation.user.uploads",
|
||||||
"/api/user/<string:username>/uploads",
|
"/api/user/<string:username>/uploads/",
|
||||||
"mediagoblin.federation.views:uploads_endpoint"
|
"mediagoblin.federation.views:uploads_endpoint",
|
||||||
|
match_slash=False
|
||||||
)
|
)
|
||||||
|
|
||||||
add_route(
|
add_route(
|
||||||
"mediagoblin.federation.inbox",
|
"mediagoblin.federation.inbox",
|
||||||
"/api/user/<string:username>/inbox",
|
"/api/user/<string:username>/inbox/",
|
||||||
"mediagoblin.federation.views:inbox_endpoint"
|
"mediagoblin.federation.views:inbox_endpoint",
|
||||||
|
match_slash=False
|
||||||
)
|
)
|
||||||
|
|
||||||
add_route(
|
add_route(
|
||||||
"mediagoblin.federation.inbox_minor",
|
"mediagoblin.federation.inbox_minor",
|
||||||
"/api/user/<string:username>/inbox/minor",
|
"/api/user/<string:username>/inbox/minor/",
|
||||||
"mediagoblin.federation.views:inbox_minor_endpoint"
|
"mediagoblin.federation.views:inbox_minor_endpoint",
|
||||||
|
match_slash=False
|
||||||
)
|
)
|
||||||
|
|
||||||
add_route(
|
add_route(
|
||||||
"mediagoblin.federation.inbox_major",
|
"mediagoblin.federation.inbox_major",
|
||||||
"/api/user/<string:username>/inbox/major",
|
"/api/user/<string:username>/inbox/major/",
|
||||||
"mediagoblin.federation.views:inbox_major_endpoint"
|
"mediagoblin.federation.views:inbox_major_endpoint",
|
||||||
|
match_slash=False
|
||||||
)
|
)
|
||||||
|
|
||||||
add_route(
|
add_route(
|
||||||
"mediagoblin.federation.inbox_direct",
|
"mediagoblin.federation.inbox_direct",
|
||||||
"/api/user/<string:username>/inbox/direct",
|
"/api/user/<string:username>/inbox/direct/",
|
||||||
"mediagoblin.federation.views:inbox_endpoint"
|
"mediagoblin.federation.views:inbox_endpoint",
|
||||||
|
match_slash=False
|
||||||
)
|
)
|
||||||
|
|
||||||
add_route(
|
add_route(
|
||||||
"mediagoblin.federation.inbox_direct_minor",
|
"mediagoblin.federation.inbox_direct_minor",
|
||||||
"/api/user/<string:username>/inbox/direct/minor",
|
"/api/user/<string:username>/inbox/direct/minor",
|
||||||
"mediagoblin.federation.views:inbox_minor_endpoint"
|
"mediagoblin.federation.views:inbox_minor_endpoint",
|
||||||
|
match_slash=False
|
||||||
)
|
)
|
||||||
|
|
||||||
add_route(
|
add_route(
|
||||||
"mediagoblin.federation.inbox_direct_major",
|
"mediagoblin.federation.inbox_direct_major",
|
||||||
"/api/user/<string:username>/inbox/direct/major",
|
"/api/user/<string:username>/inbox/direct/major",
|
||||||
"mediagoblin.federation.views:inbox_major_endpoint"
|
"mediagoblin.federation.views:inbox_major_endpoint",
|
||||||
|
match_slash=False
|
||||||
)
|
)
|
||||||
|
|
||||||
# object endpoints
|
# object endpoints
|
||||||
add_route(
|
add_route(
|
||||||
"mediagoblin.federation.object",
|
"mediagoblin.federation.object",
|
||||||
"/api/<string:object_type>/<string:id>",
|
"/api/<string:object_type>/<string:id>/",
|
||||||
"mediagoblin.federation.views:object_endpoint"
|
"mediagoblin.federation.views:object_endpoint",
|
||||||
|
match_slash=False
|
||||||
)
|
)
|
||||||
|
|
||||||
add_route(
|
add_route(
|
||||||
"mediagoblin.federation.object.comments",
|
"mediagoblin.federation.object.comments",
|
||||||
"/api/<string:object_type>/<string:id>/comments",
|
"/api/<string:object_type>/<string:id>/comments/",
|
||||||
"mediagoblin.federation.views:object_comments"
|
"mediagoblin.federation.views:object_comments",
|
||||||
|
match_slash=False
|
||||||
)
|
)
|
||||||
|
|
||||||
add_route(
|
add_route(
|
||||||
@ -104,18 +117,21 @@ add_route(
|
|||||||
|
|
||||||
add_route(
|
add_route(
|
||||||
"mediagoblin.webfinger.well-known.webfinger",
|
"mediagoblin.webfinger.well-known.webfinger",
|
||||||
"/.well-known/webfinger",
|
"/.well-known/webfinger/",
|
||||||
"mediagoblin.federation.views:lrdd_lookup"
|
"mediagoblin.federation.views:lrdd_lookup",
|
||||||
|
match_slash=False
|
||||||
)
|
)
|
||||||
|
|
||||||
add_route(
|
add_route(
|
||||||
"mediagoblin.webfinger.whoami",
|
"mediagoblin.webfinger.whoami",
|
||||||
"/api/whoami",
|
"/api/whoami/",
|
||||||
"mediagoblin.federation.views:whoami"
|
"mediagoblin.federation.views:whoami",
|
||||||
|
match_slash=False
|
||||||
)
|
)
|
||||||
|
|
||||||
add_route(
|
add_route(
|
||||||
"mediagoblin.federation.activity_view",
|
"mediagoblin.federation.activity_view",
|
||||||
"/<string:username>/activity/<string:id>",
|
"/<string:username>/activity/<string:id>/",
|
||||||
"mediagoblin.federation.views:activity_view"
|
"mediagoblin.federation.views:activity_view",
|
||||||
|
match_slash=False
|
||||||
)
|
)
|
||||||
|
@ -188,7 +188,7 @@ class TestAPI(object):
|
|||||||
# Lets change the image uploader to be self.other_user, this is easier
|
# Lets change the image uploader to be self.other_user, this is easier
|
||||||
# than uploading the image as someone else as the way self.mocked_oauth_required
|
# than uploading the image as someone else as the way self.mocked_oauth_required
|
||||||
# and self._upload_image.
|
# and self._upload_image.
|
||||||
id = int(data["object"]["id"].split("/")[-1])
|
id = int(data["object"]["id"].split("/")[-2])
|
||||||
media = MediaEntry.query.filter_by(id=id).first()
|
media = MediaEntry.query.filter_by(id=id).first()
|
||||||
media.uploader = self.other_user.id
|
media.uploader = self.other_user.id
|
||||||
media.save()
|
media.save()
|
||||||
@ -232,14 +232,14 @@ class TestAPI(object):
|
|||||||
image = json.loads(response.body.decode())["object"]
|
image = json.loads(response.body.decode())["object"]
|
||||||
|
|
||||||
# Check everything has been set on the media correctly
|
# Check everything has been set on the media correctly
|
||||||
id = int(image["id"].split("/")[-1])
|
id = int(image["id"].split("/")[-2])
|
||||||
media = MediaEntry.query.filter_by(id=id).first()
|
media = MediaEntry.query.filter_by(id=id).first()
|
||||||
assert media.title == title
|
assert media.title == title
|
||||||
assert media.description == description
|
assert media.description == description
|
||||||
assert media.license == license
|
assert media.license == license
|
||||||
|
|
||||||
# Check we're being given back everything we should on an update
|
# Check we're being given back everything we should on an update
|
||||||
assert int(image["id"].split("/")[-1]) == media.id
|
assert int(image["id"].split("/")[-2]) == media.id
|
||||||
assert image["displayName"] == title
|
assert image["displayName"] == title
|
||||||
assert image["content"] == description
|
assert image["content"] == description
|
||||||
assert image["license"] == license
|
assert image["license"] == license
|
||||||
@ -288,7 +288,7 @@ class TestAPI(object):
|
|||||||
request = test_app.get(object_uri)
|
request = test_app.get(object_uri)
|
||||||
|
|
||||||
image = json.loads(request.body.decode())
|
image = json.loads(request.body.decode())
|
||||||
entry_id = int(image["id"].split("/")[-1])
|
entry_id = int(image["id"].split("/")[-2])
|
||||||
entry = MediaEntry.query.filter_by(id=entry_id).first()
|
entry = MediaEntry.query.filter_by(id=entry_id).first()
|
||||||
|
|
||||||
assert request.status_code == 200
|
assert request.status_code == 200
|
||||||
@ -319,7 +319,7 @@ class TestAPI(object):
|
|||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
# Find the objects in the database
|
# Find the objects in the database
|
||||||
media_id = int(data["object"]["id"].split("/")[-1])
|
media_id = int(data["object"]["id"].split("/")[-2])
|
||||||
media = MediaEntry.query.filter_by(id=media_id).first()
|
media = MediaEntry.query.filter_by(id=media_id).first()
|
||||||
comment = media.get_comments()[0]
|
comment = media.get_comments()[0]
|
||||||
|
|
||||||
@ -382,7 +382,7 @@ class TestAPI(object):
|
|||||||
response, comment_data = self._activity_to_feed(test_app, activity)
|
response, comment_data = self._activity_to_feed(test_app, activity)
|
||||||
|
|
||||||
# change who uploaded the comment as it's easier than changing
|
# change who uploaded the comment as it's easier than changing
|
||||||
comment_id = int(comment_data["object"]["id"].split("/")[-1])
|
comment_id = int(comment_data["object"]["id"].split("/")[-2])
|
||||||
comment = MediaComment.query.filter_by(id=comment_id).first()
|
comment = MediaComment.query.filter_by(id=comment_id).first()
|
||||||
comment.author = self.other_user.id
|
comment.author = self.other_user.id
|
||||||
comment.save()
|
comment.save()
|
||||||
|
@ -28,15 +28,22 @@ url_map = Map()
|
|||||||
|
|
||||||
|
|
||||||
class MGRoute(Rule):
|
class MGRoute(Rule):
|
||||||
def __init__(self, endpoint, url, controller):
|
def __init__(self, endpoint, url, controller, match_slash=True):
|
||||||
Rule.__init__(self, url, endpoint=endpoint)
|
Rule.__init__(self, url, endpoint=endpoint)
|
||||||
self.gmg_controller = controller
|
self.gmg_controller = controller
|
||||||
|
self.match_slash = match_slash
|
||||||
|
|
||||||
def empty(self):
|
def empty(self):
|
||||||
new_rule = Rule.empty(self)
|
new_rule = Rule.empty(self)
|
||||||
new_rule.gmg_controller = self.gmg_controller
|
new_rule.gmg_controller = self.gmg_controller
|
||||||
return new_rule
|
return new_rule
|
||||||
|
|
||||||
|
def match(self, path, *args, **kwargs):
|
||||||
|
if not (self.match_slash or path.endswith("/")):
|
||||||
|
path = path + "/"
|
||||||
|
|
||||||
|
return super(MGRoute, self).match(path, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def endpoint_to_controller(rule):
|
def endpoint_to_controller(rule):
|
||||||
endpoint = rule.endpoint
|
endpoint = rule.endpoint
|
||||||
@ -52,11 +59,11 @@ def endpoint_to_controller(rule):
|
|||||||
return view_func
|
return view_func
|
||||||
|
|
||||||
|
|
||||||
def add_route(endpoint, url, controller):
|
def add_route(endpoint, url, controller, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Add a route to the url mapping
|
Add a route to the url mapping
|
||||||
"""
|
"""
|
||||||
url_map.add(MGRoute(endpoint, url, controller))
|
url_map.add(MGRoute(endpoint, url, controller, *args, **kwargs))
|
||||||
|
|
||||||
|
|
||||||
def mount(mountpoint, routes):
|
def mount(mountpoint, routes):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user