web/lib/django/contrib/sites/managers.py
author ymh <ymh.work@gmail.com>
Tue, 15 Mar 2011 10:34:41 +0100
changeset 65 3d18d15135f1
parent 38 77b6da96e6f1
permissions -rw-r--r--
put video url in segment object
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
from django.conf import settings
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
from django.db import models
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
from django.db.models.fields import FieldDoesNotExist
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
class CurrentSiteManager(models.Manager):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
    "Use this to limit objects to those associated with the current site."
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
    def __init__(self, field_name='site'):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
        super(CurrentSiteManager, self).__init__()
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
        self.__field_name = field_name
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
        self.__is_validated = False
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
    def get_query_set(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
        if not self.__is_validated:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
            try:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
                self.model._meta.get_field(self.__field_name)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
            except FieldDoesNotExist:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
                raise ValueError("%s couldn't find a field named %s in %s." % \
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
                    (self.__class__.__name__, self.__field_name, self.model._meta.object_name))
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
            self.__is_validated = True
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
        return super(CurrentSiteManager, self).get_query_set().filter(**{self.__field_name + '__id__exact': settings.SITE_ID})