def to_representation(self, instance): value = super(ChannelMetadataSerializer, self).to_representation(instance) value.update({"num_coach_contents": get_num_coach_contents(instance.root)}) # if the request includes a GET param 'include_fields', add the requested calculated fields if 'request' in self.context: include_fields = self.context['request'].GET.get('include_fields', '').split(',') if include_fields: # build querysets for the full set of channel nodes, as well as those that are unrenderable channel_nodes = ContentNode.objects.filter(channel_id=instance.id) unrenderable_nodes = channel_nodes.exclude(renderable_contentnodes_without_topics_q_filter) if 'total_resources' in include_fields: # count the total number of renderable non-topic resources in the channel # (note: it's faster to count them all and then subtract the unrenderables, of which there are fewer) value['total_resources'] = channel_nodes.count() - unrenderable_nodes.count() if 'total_file_size' in include_fields: # count the total file size of files associated with renderable content nodes # (note: it's faster to count them all and then subtract the unrenderables, of which there are fewer) value['total_file_size'] = _total_file_size(channel_nodes) - _total_file_size(unrenderable_nodes) if 'on_device_resources' in include_fields: # count the total number of resources from the channel already available value['on_device_resources'] = channel_nodes.filter(available=True).exclude(kind=content_kinds.TOPIC).count() if 'on_device_file_size' in include_fields: # count the total size of available files associated with the channel value['on_device_file_size'] = _total_file_size(_files_for_nodes(channel_nodes).filter(available=True)) return value
def get_num_coach_contents(self, instance): user = self.context["request"].user if user.is_facility_user: # exclude anon users if user.roles.exists() or user.is_superuser: # must have coach role or higher return get_num_coach_contents(instance) # all other conditions return 0 return 0
def to_representation(self, instance): value = super(ContentNodeSlimSerializer, self).to_representation(instance) # if the request includes a GET param 'include_fields', add the requested calculated fields if 'request' in self.context: include_fields = self.context['request'].GET.get('include_fields', '').split(',') if include_fields: if 'num_coach_contents' in include_fields: value['num_coach_contents'] = get_num_coach_contents(instance) return value
def to_representation(self, instance, progress=None, last_active=None): if progress is None: if 'request' not in self.context: progress = [{ 'total_progress': 0, 'log_count_total': 0, 'log_count_complete': 0 }] else: kwargs = self.context['view'].kwargs progress_dict, last_active_dict = get_progress_and_last_active( instance, **kwargs) progress = progress_dict.get(instance.content_id) last_active = last_active_dict.get(instance.content_id) value = super(ContentReportSerializer, self).to_representation(instance) value['progress'] = progress value['last_active'] = last_active value['num_coach_contents'] = get_num_coach_contents(instance) return value
def get_num_coach_contents(self, instance): # If for exporting, only show what is available on server. For importing, # show all of the coach contents in the topic. for_export = self.context['request'].query_params.get('for_export', None) return get_num_coach_contents(instance, filter_available=for_export)