Improve performance of counting statistics of submissions for contest overview page of AWS

This is one we talked about in https://github.com/cms-dev/cms/issues/313 with advised changes (moved everything to contest.py and reformatted SQL statements).