mkmoisen / blog

A Python blog using Flask and SQLAlchemy and which powers my website, http://matthewmoisen.com
MIT License
2 stars 0 forks source link

Add tests for canonical urls #90

Open mkmoisen opened 7 years ago

mkmoisen commented 7 years ago

Where do I create URLs? I seem to mix and match between the python code and the templates.

For example, the/ url logic is mixed is in the template, and the backend. The backend sends a flag of "is_project" if the post is the intro post to a project, and then the front end determines the URL as /project/.

/blog/category/ is similar to /, except this is for category urls, not post urls.

/projects/ is similar to / and /blog/category/ except it only returns projects (is_project is always true)

/blog/category/url_name/ does it better. It has its own make_url function that sets the proper url. The template category-posts.html simply prints it out.

project_introduction_posts = db.session.query(ProjectPost).filter_by(order_no=0)

def make_url(post):
    if post.url_name == 'resume':
        return '/resume/'
    if post.id in project_introduction_posts:
        # Only project introduction posts get /projects/ url, every other post gets the normal /blog/
        # Unless we wanted the other posts to get /projects/<url_name>/ post ...
        # If we wanted that, I would need to add the few posts for the python blog to the wordpress url resolution..
        return '/projects/{}/'.format(post.url_name)
    return '/blog/{}/'.format(post.url_name)

get_project_table_of_contents method looks similar to /blog/category/ except it doesn't handle resume. It is used by post to find the TOC if this post is within a project.

mkmoisen commented 7 years ago

Ok, I standardized it.

I took the make_url out of /blog/category/<url_name>/ and renamed it to make_post_url. I also added a make_category_url.

Everything now calls these, and the URLs are created on the backend. All the url logic in the front end has been removed.

The only exception is get_project_table_of_contents. Since this gets all of the related project posts for a single post anyways, there is no need to call out to make_post_url.