def message_conversation_by_id36( request: Request, conversation_id36: str) -> MessageConversation: """Get a conversation specified by {conversation_id36} in the route.""" query = request.query(MessageConversation).filter_by( conversation_id=id36_to_id(conversation_id36)) return get_resource(request, query)
def topic_by_id36(request: Request, topic_id36: str) -> Topic: """Get a topic specified by {topic_id36} in the route (or 404).""" try: topic_id = id36_to_id(topic_id36) except ValueError: raise HTTPNotFound query = ( request.query(Topic) .include_deleted() .include_removed() .filter_by(topic_id=topic_id) ) try: topic = get_resource(request, query) except HTTPNotFound: raise HTTPNotFound("Topic not found (or it was deleted)") # if there's also a group specified in the route, check that it's the same group as # the topic was posted in, otherwise redirect to correct group if "path" in request.matchdict: path_from_route = request.matchdict["path"].lower() if path_from_route != topic.group.path: raise HTTPFound(topic.permalink) return topic
def after_id36(self, id36: str) -> PaginatedQuery: """Restrict the query to results after an id36 (generative).""" if self.before_id: raise ValueError("Can't set both before and after restrictions") self.after_id = id36_to_id(id36) return self
def comment_by_id36(request: Request, comment_id36: str) -> Comment: """Get a comment specified by {comment_id36} in the route (or 404).""" query = (request.query(Comment).include_removed().filter_by( comment_id=id36_to_id(comment_id36))) try: return get_resource(request, query) except HTTPNotFound: raise HTTPNotFound("Comment not found (or it was deleted)")
def notification_by_comment_id36(request: Request, comment_id36: str) -> CommentNotification: """Get a comment notification specified by {comment_id36} in the route. Looks up a comment notification for the logged-in user with the {comment_id36} specified in the route. """ if not request.user: raise HTTPForbidden comment_id = id36_to_id(comment_id36) query = request.query(CommentNotification).filter_by(user=request.user, comment_id=comment_id) return get_resource(request, query)
def topic_by_id36(request: Request, topic_id36: str) -> Topic: """Get a topic specified by {topic_id36} in the route (or 404).""" query = ( request.query(Topic) .include_deleted() .include_removed() .filter_by(topic_id=id36_to_id(topic_id36)) ) topic = get_resource(request, query) # if there's also a group specified in the route, check that it's the same # group as the topic was posted in, otherwise redirect to correct group if 'group_path' in request.matchdict: path_from_route = request.matchdict['group_path'].lower() if path_from_route != topic.group.path: raise HTTPFound(topic.permalink) return topic
def comment_by_id36(request: Request, comment_id36: str) -> Comment: """Get a comment specified by {comment_id36} in the route (or 404).""" comment_id = id36_to_id(comment_id36) query = request.query(Comment).filter_by(comment_id=comment_id) return get_resource(request, query)
def test_negative_id36_conversion_blocked(): """Ensure the ID36 conversion function doesn't accept negative numbers.""" with raises(ValueError): id36_to_id('-1')
def test_zero_id36_conversion_blocked(): """Ensure the ID36 conversion function doesn't accept zero.""" with raises(ValueError): id36_to_id('0')
def test_reversed_conversion_from_id36(): """Make sure an ID36->ID->ID36 conversion returns to original value.""" original = 'h2l4pe' assert id_to_id36(id36_to_id(original)) == original
def test_reversed_conversion_from_id(): """Make sure an ID->ID36->ID conversion returns to original value.""" original = 48102983 assert id36_to_id(id_to_id36(original)) == original
def test_id36_to_id(): """Make sure an ID36->ID conversion is correct.""" assert id36_to_id('x48l4z') == 2002502915