Exemple #1
0
def populate_foreign_key_caches(model, objects_to_populate, fields=None):
    """
    Populates caches for the given related Model in instances of objects
    which have a ForeignKey relationship to it, specified as a list of
    (object list, related attribute name list) two-tuples.

    If a list of field names is given, only the given fields will be
    looked up and related object caches will be populated with a dict of
    the specified fields. Otherwise, complete model instances will be
    retrieved.
    """
    # Get all related object ids for the appropriate fields
    related_object_ids = []
    for objects, attrs in objects_to_populate:
        related_object_ids.append(tuple(tuple(getattr(obj, '%s_id' % attr)
                                              for attr in attrs)
                                  for obj in objects))
    unique_ids = tuple(set(pk for pk in flatten(related_object_ids) if pk))
    related_objects = fetch_model_dict(model, unique_ids, fields)

    # Fill related object caches
    for (objects, attrs), related_ids in itertools.izip(objects_to_populate,
                                                        related_object_ids):
        for obj, related_ids_for_obj in itertools.izip(objects,
                                                       related_ids):
            for attr, related_object in itertools.izip(attrs, (related_objects.get(pk, None)
                                                               for pk in related_ids_for_obj)):
                setattr(obj, '_%s_cache' % attr, related_object)
Exemple #2
0
def populate_foreign_key_caches(model, objects_to_populate, fields=None):
    """
    Populates caches for the given related Model in instances of objects
    which have a ForeignKey relationship to it, specified as a list of
    (object list, related attribute name list) two-tuples.

    If a list of field names is given, only the given fields will be
    looked up and related object caches will be populated with a dict of
    the specified fields. Otherwise, complete model instances will be
    retrieved.
    """
    # Get all related object ids for the appropriate fields
    related_object_ids = []
    for objects, attrs in objects_to_populate:
        related_object_ids.append(
            tuple(
                tuple(getattr(obj, '%s_id' % attr) for attr in attrs)
                for obj in objects))
    unique_ids = tuple(set(pk for pk in flatten(related_object_ids) if pk))
    related_objects = fetch_model_dict(model, unique_ids, fields)

    # Fill related object caches
    for (objects,
         attrs), related_ids in itertools.izip(objects_to_populate,
                                               related_object_ids):
        for obj, related_ids_for_obj in itertools.izip(objects, related_ids):
            for attr, related_object in itertools.izip(
                    attrs,
                (related_objects.get(pk, None) for pk in related_ids_for_obj)):
                setattr(obj, '_%s_cache' % attr, related_object)
Exemple #3
0
def update_reputation(manager, changes):
    """
    Updates User reputation scores where changes are specified as
    two-tuples of (User id, reputation score change), ensuring that
    a User's reputation score can't go below 1.
    """
    change_count = len(changes)
    cursor = connection.cursor()
    cursor.execute(
        'UPDATE auth_user SET reputation = CASE %s ELSE reputation END '
        'WHERE id IN (%s)' %
        (' '.join(['WHEN id = %s THEN MAX(1, reputation + %s)'] *
                  change_count), ','.join(['%s'] * change_count)),
        flatten(changes) + [c[0] for c in changes])
    transaction.commit_unless_managed()
Exemple #4
0
def update_reputation(manager, changes):
    """
    Updates User reputation scores where changes are specified as
    two-tuples of (User id, reputation score change), ensuring that
    a User's reputation score can't go below 1.
    """
    change_count = len(changes)
    cursor = connection.cursor()
    cursor.execute(
        'UPDATE auth_user SET reputation = CASE %s ELSE reputation END '
        'WHERE id IN (%s)' % (
        ' '.join(['WHEN id = %s THEN MAX(1, reputation + %s)'] * change_count),
        ','.join(['%s'] * change_count)),
        flatten(changes) + [c[0] for c in changes])
    transaction.commit_unless_managed()