Add LocalSite.objects.build_q, for optimized LocalSite querying.
Add LocalSite.objects.build_q, for optimized LocalSite querying.

This introduces `LocalSite.objects.build_q()`, which builds a `Q()`
result based on the provided `LocalSite`/`LocalSite.ALL` value and
whether Local Site functionality is used in the deployment.

If either `LocalSite.ALL` is passed or Local Sites are not in use, the
result will be an empty `Q()`, which will be optimized out when joining
with another.

If Local Sites are used, this will construct a `Q(local_site=...)`
value.

It also provides validation on arguments, ensuring that a `LocalSite`
instance cannot be accepted if the database does not contain any
Local Sites. This will mostly help us with writing correct tests and
to catch errors with stale caches.

This will greatly help us build queries in the most optimized way
possible. On normal deployments without Local Sites, these will be left
off entirely, allowing the database to make more optimized query plans.
Future changes will begin to introduce usage of this method.

Testing Done:
Successfully made use of this in some in-progress changes, and
verified with those unit tests.

Reviewed at https://reviews.reviewboard.org/r/12355/

reviewboard/reviewboard,reviewboard/reviewboard,reviewboard/reviewboard,reviewboard/reviewboard