(post, user, sub_type=None, update=False)
Creates subscription to a post. Returns a list of subscriptions.
Creates subscription to a post. Returns a list of subscriptions.
Creates subscription to a post . Returns a list of subscriptions .
def create_subscription(post, user, sub_type=None, update=False):
    """
    Creates subscription to a post. Returns a list of subscriptions.
    """
    subs = Subscription.objects.filter(post=post.root, user=user)
    sub = subs.first()

    default = Subscription.TYPE_MAP.get(user.profile.message_prefs,
                                        Subscription.LOCAL_MESSAGE)

    empty = sub_type is None
    # Get the current sub type from what's given or the existing sub
    sub_type = None if empty else sub_type
    # No type has been given so default
    sub_type = sub_type or default

    # Ensure the sub type is not set to something wrote
    if sub and update:
        # Update an existing subscription
        sub.type = sub_type
        sub.save()
    else:
        # Drop all existing subscriptions for the user by default.
        subs.delete()
        Subscription.objects.create(post=post.root, user=user, type=sub_type)

    # Recompute subscription count
    subs_count = Subscription.objects.filter(post=post.root).exclude(type=Subscription.NO_MESSAGES).count()

    # Update root subscription counts.
    Post.objects.filter(pk=post.root.pk).update(subs_count=subs_count)

    # Delete following cache
    delete_cache(FOLLOWING, user)
def create_subscription ( post , user , sub_type = None , update = False ) : subs = Subscription . objects . filter ( post = post . root , user = user ) sub = subs . first ( ) default = Subscription . TYPE_MAP . get ( user . profile . message_prefs , Subscription . LOCAL_MESSAGE ) empty = sub_type is None # Get the current sub type from what's given or the existing sub sub_type = None if empty else sub_type # No type has been given so default sub_type = sub_type or default # Ensure the sub type is not set to something wrote if sub and update : # Update an existing subscription sub . type = sub_type sub . save ( ) else : # Drop all existing subscriptions for the user by default. subs . delete ( ) Subscription . objects . create ( post = post . root , user = user , type = sub_type ) # Recompute subscription count subs_count = Subscription . objects . filter ( post = post . root ) . exclude ( type = Subscription . NO_MESSAGES ) . count ( ) # Update root subscription counts. Post . objects . filter ( pk = post . root . pk ) . update ( subs_count = subs_count ) # Delete following cache delete_cache ( FOLLOWING , user )