Esempio n. 1
0
def add_discussion_attributes(context, model):
	model = iterify(model)
	if len(model) == 0:
		return ''

	content_type = ContentType.objects.get_for_model(model[0].__class__)
	id_list = [m.id for m in model]

	headers = RootHeader.objects \
		.filter(content_type = content_type, object_id__in = id_list) \
		.values('id', 'object_id', 'last_comment', 'comment_count', 'is_locked')

	if 'user' in context and context['user'].is_authenticated():
		header_ids = [h['id'] for h in headers]
		user_attributes = UserDiscussionAttribute.objects \
			.filter(user = context['user'], discussion__in = header_ids) \
			.values('discussion_id', 'time', 'watch')
		user_attributes = dict([(a['discussion_id'], a) for a in user_attributes])
		for header in headers:
			header.update(user_attributes.get(header['id'], {}))
	headers = dict([(h['object_id'], h) for h in headers])

	for obj in model:
		hdr = headers.get(obj.pk, {})
		obj.last_comment = hdr.get('last_comment', None)
		obj.comment_count = hdr.get('comment_count', 0)
		obj.is_locked = hdr.get('is_locked', False)
		obj.rootheader_id = hdr.get('id', None)
		obj.discussion_display_time = hdr.get('time', None)
		obj.discussion_watch = hdr.get('watch', None)
		if obj.last_comment and obj.discussion_display_time:
			obj.new_comments = obj.discussion_display_time < obj.last_comment
		else:
			obj.new_comments = None
	return ''
def get_lookups(models):
	lookups = {}
	content_types = []

	for model in models:
		model = iterify(model)

		id_list = []
		last_object = None
		object_cls = None
		for o in model:
			if o is None:
				continue
			id_list.append(o.pk)
			last_object = o
			if object_cls is None:
				object_cls = o.__class__
			elif object_cls is not o.__class__:
				raise ValueError('Heterogenous list')

		if last_object is not None:
			content_type = ContentType.objects.get_for_model(last_object.__class__)
			lookups.setdefault(content_type, [])
			lookups[content_type] += id_list
			content_types.append(content_type)
		else:
			content_types.append(None)

	return lookups, content_types
def add_discussion_attributes_heterogenous(context, *models):
	objects_sorted = {}
	for model in models:
		for instance in iterify(model):
			objects_sorted.setdefault(instance.__class__, [])
			objects_sorted[instance.__class__].append(instance)
	add_discussion_attributes(context, *objects_sorted.values())
	return ''
Esempio n. 4
0
def add_hitcount(model):
	model = iterify(model)
	content_type = None
	if len(model) == 0:
		return ''
	id_list = [o.pk for o in model]
	content_type = ContentType.objects.get_for_model(model[0].__class__)
	hitcounts = HitCount \
		.objects \
		.filter(content_type=content_type, object_id__in=id_list) \
		.values_list('object_id', 'hits')
	hitcounts = dict(hitcounts)
	for obj in model:
		obj.display_count = hitcounts.get(obj.pk, 0)
	return ''
Esempio n. 5
0
def get_lookups(models):
	lookups = {}
	content_types = []

	for model in models:
		model = iterify(model)

		id_list = []
		last_object = None
		for o in model:
			id_list.append(o.pk)
			last_object = o

		if last_object is not None:
			content_type = ContentType.objects.get_for_model(last_object.__class__)
			lookups.setdefault(content_type, [])
			lookups[content_type] += id_list
			content_types.append(content_type)
		else:
			content_types.append(None)

	return lookups, content_types
def add_discussion_attributes_heterogenous(context, object_list):
	objects_sorted = {}
	for instance in iterify(object_list):
		objects_sorted.setdefault(instance.__class__, [])
		objects_sorted[instance.__class__].append(instance)
	return add_discussion_attributes(context, *objects_sorted.values())