Optimize /json/class_subjects view

Fix some badly-written queries and use prefetch_related() to reduce
the number of queries.
Optimize /json/sections view

Fix some badly-written queries and use prefetch_related() to reduce
the number of queries.
Save return value of ResourceType.get_or_create so we don't query it
over and over again
Remove a double query in ClassSection.initial_rooms()