Reduce the number of database queries done by the Submission.get_flags method in the demos app.

Do some PEP8 work while I’m here.

Fixes #1821.