In your Post
model (where you have the slug) you can redefine the .first_or_404
or .get_or_404
methods to deal with this and rising accordingly. But I suggest to define a new method to use in these circumstances.
This is a clean solution. The logic is in the right place (the place that knows what the information is).
The problem is how to manage the redirects.
You can pass a redirect
instance to the abort
call.
Like in the following pseudo-python-code:
class Post(db.Model):
def get_or_redirect_or_404(self, slug):
r = self.query.filter_by(new_url, old_url)
if not r:
# there's no model. abort normally
abort(404)
elseif r.old_url == slug:
# old url used, we need redirect
#
abort(redirect(... r.new_url ...))