web/lib/tagging/generic.py
author ymh <ymh.work@gmail.com>
Fri, 22 Jan 2010 18:23:34 +0100
changeset 11 f236caaceb43
permissions -rw-r--r--
add pois
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
from django.contrib.contenttypes.models import ContentType
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
def fetch_content_objects(tagged_items, select_related_for=None):
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
    """
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
    Retrieves ``ContentType`` and content objects for the given list of
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
    ``TaggedItems``, grouping the retrieval of content objects by model
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
    type to reduce the number of queries executed.
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
    This results in ``number_of_content_types + 1`` queries rather than
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
    the ``number_of_tagged_items * 2`` queries you'd get by iterating
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
    over the list and accessing each item's ``object`` attribute.
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
    A ``select_related_for`` argument can be used to specify a list of
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
    of model names (corresponding to the ``model`` field of a
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
    ``ContentType``) for which ``select_related`` should be used when
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    retrieving model instances.
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
    """
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
    if select_related_for is None: select_related_for = []
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    # Group content object pks by their content type pks
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    objects = {}
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    for item in tagged_items:
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
        objects.setdefault(item.content_type_id, []).append(item.object_id)
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    # Retrieve content types and content objects in bulk
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    content_types = ContentType._default_manager.in_bulk(objects.keys())
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    for content_type_pk, object_pks in objects.iteritems():
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        model = content_types[content_type_pk].model_class()
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
        if content_types[content_type_pk].model in select_related_for:
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
            objects[content_type_pk] = model._default_manager.select_related().in_bulk(object_pks)
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
        else:
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
            objects[content_type_pk] = model._default_manager.in_bulk(object_pks)
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    # Set content types and content objects in the appropriate cache
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    # attributes, so accessing the 'content_type' and 'object'
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    # attributes on each tagged item won't result in further database
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    # hits.
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
    for item in tagged_items:
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        item._object_cache = objects[item.content_type_id][item.object_id]
f236caaceb43 add pois
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        item._content_type_cache = content_types[item.content_type_id]