def get_for_objects_in_bulk(self, objects, all=False): """ Get a dictinary mapping objects ids to dictinary which maps direction to votecount """ object_ids = [o._get_pk_val() for o in objects] if not object_ids: return {} ctype = ContentType.objects.get_for_model(objects[0]) queryset = self.filter(content_type=ctype, object_id__in=object_ids) if not all: # only pick active votes queryset = queryset.filter(is_archived=False) queryset = queryset.values('object_id', 'direction',) queryset = queryset.annotate(vcount=Count("direction")).order_by() vote_dict = {} for votecount in queryset: object_id = votecount['object_id'] votes = vote_dict.setdefault(object_id , {}) if votecount['direction'] >= 10: # sum up all blank votes votes[0] = votes.get(0,0) + votecount['vcount'] votes[votecount['direction']] = votecount['vcount'] return vote_dict
def get_for_objects_in_bulk(self, objects, all=False): """ Get a dictinary mapping objects ids to dictinary which maps direction to votecount """ object_ids = [o._get_pk_val() for o in objects] if not object_ids: return {} ctype = ContentType.objects.get_for_model(objects[0]) queryset = self.filter(content_type=ctype, object_id__in=object_ids) if not all: # only pick active votes queryset = queryset.filter(is_archived=False) queryset = queryset.values( 'object_id', 'direction', ) queryset = queryset.annotate(vcount=Count("direction")).order_by() vote_dict = {} for votecount in queryset: object_id = votecount['object_id'] votes = vote_dict.setdefault(object_id, {}) if votecount['direction'] >= 10: # sum up all blank votes votes[0] = votes.get(0, 0) + votecount['vcount'] votes[votecount['direction']] = votecount['vcount'] return vote_dict
def get_for_objects_in_bulk(self, objects, all=False): """ Get a dictinary mapping objects ids to dictinary which maps direction to votecount Objects can be the string representation of a model like 'appname.Modelname' """ # test if it's a model if isinstance(objects, (str, unicode)): try: model = get_model(*objects.split('.')) if not model: return {} except: return {} else: ctype = ContentType.objects.get_for_model(model) queryset = self.filter(content_type=ctype) else: object_ids = [o._get_pk_val() for o in objects] if not object_ids: return {} ctype = ContentType.objects.get_for_model(objects[0]) queryset = self.filter(content_type=ctype, object_id__in=object_ids) if not all: # only pick active votes queryset = queryset.filter(is_archived=False) queryset = queryset.values('object_id', 'direction',) queryset = queryset.annotate(vcount=Count("direction")).order_by() vote_dict = {} for votecount in queryset: object_id = votecount['object_id'] votes = vote_dict.setdefault(object_id , {}) if votecount['direction'] >= 10: # sum up all blank votes votes[0] = votes.get(0,0) + votecount['vcount'] votes[votecount['direction']] = votecount['vcount'] return vote_dict