def update_subs(new_user_id): """Update subs to send added/removed for collections with user_rel.""" for sub in Subscription.objects.filter(connection=this.ws.connection): params = loads(sub.params_ejson) pub = API.get_pub_by_name(sub.publication) # calculate the querysets prior to update pre = collections.OrderedDict([ (col, query) for col, query in API.sub_unique_objects(sub, params, pub) ]) # save the subscription with the updated user_id sub.user_id = new_user_id sub.save() # calculate the querysets after the update post = collections.OrderedDict([ (col, query) for col, query in API.sub_unique_objects(sub, params, pub) ]) # first pass, send `added` for objs unique to `post` for col_post, query in post.items(): try: qs_pre = pre[col_post] query = query.exclude( pk__in=qs_pre.order_by().values('pk'), ) except KeyError: # collection not included pre-auth, everything is added. pass for obj in query: this.ws.send(col_post.obj_change_as_msg(obj, ADDED)) # second pass, send `removed` for objs unique to `pre` for col_pre, query in pre.items(): try: qs_post = post[col_pre] query = query.exclude( pk__in=qs_post.order_by().values('pk'), ) except KeyError: # collection not included post-auth, everything is removed. pass for obj in query: this.ws.send(col_pre.obj_change_as_msg(obj, REMOVED))