def _update_download_stats(resource, request): """ Updates all relevant statistics counters for a the given successful resource download request. """ # maintain general download statistics if saveLRStats(resource, DOWNLOAD_STAT, request): # update download count in the search index, too update_lr_index_entry(resource) # update download tracker tracker = SessionResourcesTracker.getTracker(request) tracker.add_download(resource, datetime.now()) request.session['tracker'] = tracker
def view(request, resource_name=None, object_id=None): """ Render browse or detail view for the repository application. """ # only published resources may be viewed resource = get_object_or_404(resourceInfoType_model, storage_object__identifier=object_id, storage_object__publication_status=PUBLISHED) if request.path_info != resource.get_absolute_url(): return redirect(resource.get_absolute_url()) # Convert resource to ElementTree and then to template tuples. lr_content = _convert_to_template_tuples( resource.export_to_elementtree(pretty=True)) # get the 'best' language version of a "DictField" and all other versions resource_name = resource.identificationInfo.get_default_resourceName() res_short_names = resource.identificationInfo.resourceShortName.values() description = resource.identificationInfo.get_default_description() other_res_names = [name for name in resource.identificationInfo \ .resourceName.itervalues() if name != resource_name] other_descriptions = [name for name in resource.identificationInfo \ .description.itervalues() if name != description] # Create fields lists url = resource.identificationInfo.url metashare_id = resource.identificationInfo.metaShareId identifier = resource.identificationInfo.identifier resource_type = resource.resourceComponentType.as_subclass().resourceType media_types = set(model_utils.get_resource_media_types(resource)) linguality_infos = set(model_utils.get_resource_linguality_infos(resource)) license_types = set(model_utils.get_resource_license_types(resource)) distribution_info_tuple = None contact_person_tuples = [] metadata_info_tuple = None version_info_tuple = None validation_info_tuples = [] usage_info_tuple = None documentation_info_tuple = None resource_creation_info_tuple = None relation_info_tuples = [] resource_component_tuple = None for _tuple in lr_content[1]: if _tuple[0] == "Distribution": distribution_info_tuple = _tuple elif _tuple[0] == "Contact person": contact_person_tuples.append(_tuple) elif _tuple[0] == "Metadata": metadata_info_tuple = _tuple elif _tuple[0] == "Version": version_info_tuple = _tuple elif _tuple[0] == "Validation": validation_info_tuples.append(_tuple) elif _tuple[0] == "Usage": usage_info_tuple = _tuple elif _tuple[0] == "Resource documentation": documentation_info_tuple = _tuple elif _tuple[0] == "Resource creation": resource_creation_info_tuple = _tuple elif _tuple[0] == "Relation": relation_info_tuples.append(_tuple) elif _tuple[0] == "Resource component": resource_component_tuple = _tuple[1] # Convert resource_component_tuple to nested dictionaries resource_component_dicts = {} validation_dicts = [] relation_dicts = [] # Convert several tuples to dictionaries to facilitate rendering # the templates. contact_person_dicts = [] for item in contact_person_tuples: contact_person_dicts.append(tuple2dict([item])) distribution_dict = tuple2dict([distribution_info_tuple]) resource_component_dict = tuple2dict(resource_component_tuple) resource_creation_dict = tuple2dict([resource_creation_info_tuple]) metadata_dict = tuple2dict([metadata_info_tuple]) usage_dict = tuple2dict([usage_info_tuple]) version_dict = tuple2dict([version_info_tuple]) documentation_dict = tuple2dict([documentation_info_tuple]) for item in validation_info_tuples: validation_dicts.append(tuple2dict([item])) for item in relation_info_tuples: relation_dicts.append(tuple2dict([item])) # Count individual media resource components text_counts = [] video_counts = [] if resource_type == "corpus": for key, value in resource_component_dict['Resource_component']['Corpus_media'].items(): if "Corpus_text" in key and not "numerical" in key and not "ngram" in key: text_counts.append(value) elif "Corpus_video" in key: video_counts.append(value) # Create a list of resource components dictionaries if resource_type == "corpus": for media_type in media_types: if media_type == "text": resource_component_dicts['text'] = \ resource_component_dict['Resource_component'] \ ['Corpus_media']['Corpus_text'] if media_type == "audio": resource_component_dicts['audio'] = \ resource_component_dict['Resource_component'] \ ['Corpus_media']['Corpus_audio'] if media_type == "video": resource_component_dicts['video'] = \ resource_component_dict['Resource_component'] \ ['Corpus_media']['Corpus_video'] if media_type == "image": resource_component_dicts['image'] = \ resource_component_dict['Resource_component'] \ ['Corpus_media']['Corpus_image'] if media_type == "textNgram": resource_component_dicts['textNgram'] = \ resource_component_dict['Resource_component'] \ ['Corpus_media']['Corpus_textNgram'] if media_type == "textNumerical": resource_component_dicts['textNumerical'] = \ resource_component_dict['Resource_component'] \ ['Corpus_media']['Corpus_textNumerical'] elif resource_type == "languageDescription": for media_type in media_types: if media_type == "text": resource_component_dicts['text'] = \ resource_component_dict['Resource_component'] \ ['Language_description_media']['Language_description_text'] if media_type == "image": resource_component_dicts['image'] = \ resource_component_dict['Resource_component'] \ ['Language_description_media']['Language_description_image'] if media_type == "video": resource_component_dicts['video'] = \ resource_component_dict['Resource_component'] \ ['Language_description_media']['Language_description_video'] elif resource_type == "lexicalConceptualResource": for media_type in media_types: if media_type == "text": resource_component_dicts['text'] = \ resource_component_dict['Resource_component'] \ ['Lexical_conceptual_resource_media'] \ ['Lexical_conceptual_resource_text'] if media_type == "audio": resource_component_dicts['audio'] = \ resource_component_dict['Resource_component'] \ ['Lexical_conceptual_resource_media'] \ ['Lexical_conceptual_resource_audio'] if media_type == "video": resource_component_dicts['video'] = \ resource_component_dict['Resource_component'] \ ['Lexical_conceptual_resource_media'] \ ['Lexical_conceptual_resource_video'] if media_type == "image": resource_component_dicts['image'] = \ resource_component_dict['Resource_component'] \ ['Lexical_conceptual_resource_media'] \ ['Lexical_conceptual_resource_image'] elif resource_type == "toolService": resource_component_dicts['toolService'] = \ resource_component_dict['Resource_component'] # Define context for template rendering. context = { 'contact_person_dicts': contact_person_dicts, 'description': description, 'distribution_dict': distribution_dict, 'documentation_dict': documentation_dict, 'license_types': license_types, 'linguality_infos': linguality_infos, 'mediaTypes': media_types, 'metadata_dict': metadata_dict, 'metaShareId': metashare_id, 'identifier': identifier, 'other_res_names': other_res_names, 'other_descriptions': other_descriptions, 'relation_dicts': relation_dicts, 'res_short_names': res_short_names, 'resource': resource, 'resource_component_dicts': resource_component_dicts, 'resource_component_dict': resource_component_dict, 'resourceName': resource_name, 'resourceType': resource_type, 'resource_creation_dict': resource_creation_dict, 'url': url, 'usage_dict': usage_dict, 'validation_dicts': validation_dicts, 'version_dict': version_dict, 'text_counts': text_counts, 'video_counts': video_counts, } template = 'repository/resource_view/lr_view.html' # For users who have edit permission for this resource, we have to add # LR_EDIT which contains the URL of the Django admin backend page # for this resource. if has_edit_permission(request, resource): context['LR_EDIT'] = reverse( 'admin:repository_resourceinfotype_model_change', \ args=(resource.id,)) # Update statistics: if saveLRStats(resource, VIEW_STAT, request): # update view count in the search index, too update_lr_index_entry(resource) # update view tracker tracker = SessionResourcesTracker.getTracker(request) tracker.add_view(resource, datetime.now()) request.session['tracker'] = tracker # Add download/view/last updated statistics to the template context. context['LR_STATS'] = getLRStats(resource.storage_object.identifier) # Add recommendations for 'also viewed' resources context['also_viewed'] = \ _format_recommendations(get_view_recommendations(resource)) # Add recommendations for 'also downloaded' resources context['also_downloaded'] = \ _format_recommendations(get_download_recommendations(resource)) # Add 'more from same' links if get_more_from_same_projects_qs(resource).count(): context['search_rel_projects'] = '{}/repository/search?q={}:{}'.format( DJANGO_URL, MORE_FROM_SAME_PROJECTS, resource.storage_object.identifier) if get_more_from_same_creators_qs(resource).count(): context['search_rel_creators'] = '{}/repository/search?q={}:{}'.format( DJANGO_URL, MORE_FROM_SAME_CREATORS, resource.storage_object.identifier) # Render and return template with the defined context. ctx = RequestContext(request) return render_to_response(template, context, context_instance=ctx)
def view(request, resource_name=None, object_id=None): """ Render browse or detail view for the repository application. """ # only published resources may be viewed resource = get_object_or_404(resourceInfoType_model, storage_object__identifier=object_id, storage_object__publication_status=PUBLISHED) if request.path_info != resource.get_absolute_url(): return redirect(resource.get_absolute_url()) # Convert resource to ElementTree and then to template tuples. lr_content = _convert_to_template_tuples( resource.export_to_elementtree(pretty=True)) # get the 'best' language version of a "DictField" and all other versions resource_name = resource.identificationInfo.get_default_resourceName() res_short_names = resource.identificationInfo.resourceShortName.values() description = resource.identificationInfo.get_default_description() other_res_names = [name for name in resource.identificationInfo \ .resourceName.itervalues() if name != resource_name] other_descriptions = [name for name in resource.identificationInfo \ .description.itervalues() if name != description] # Create fields lists url = resource.identificationInfo.url metashare_id = resource.identificationInfo.metaShareId resource_type = resource.resourceComponentType.as_subclass().resourceType media_types = set(model_utils.get_resource_media_types(resource)) linguality_infos = set(model_utils.get_resource_linguality_infos(resource)) license_types = set(model_utils.get_resource_license_types(resource)) distribution_info_tuple = None contact_person_tuples = [] metadata_info_tuple = None version_info_tuple = None validation_info_tuples = [] usage_info_tuple = None documentation_info_tuple = None resource_creation_info_tuple = None relation_info_tuples = [] for _tuple in lr_content[1]: if _tuple[0] == "Distribution": distribution_info_tuple = _tuple elif _tuple[0] == "Contact person": contact_person_tuples.append(_tuple) elif _tuple[0] == "Metadata": metadata_info_tuple = _tuple elif _tuple[0] == "Version": version_info_tuple = _tuple elif _tuple[0] == "Validation": validation_info_tuples.append(_tuple) elif _tuple[0] == "Usage": usage_info_tuple = _tuple elif _tuple[0] == "Resource documentation": documentation_info_tuple = _tuple elif _tuple[0] == "Resource creation": resource_creation_info_tuple = _tuple elif _tuple[0] == "Relation": relation_info_tuples.append(_tuple) # Preprocess some of the information withing the tuples for better # presentation in the single resource view. # Contact Person: contact_person_dicts = [] #convert contact_person_tuples to dictionaries for item in contact_person_tuples: contact_person_dicts.append(tuple2dict([item])) # Define context for template rendering. context = { 'resource': resource, 'resourceName': resource_name, 'res_short_names': res_short_names, 'description': description, 'other_res_names': other_res_names, 'other_descriptions': other_descriptions, 'lr_content': lr_content, 'distribution_info_tuple': distribution_info_tuple, 'contact_person_tuples': contact_person_tuples, 'metadata_info_tuple': metadata_info_tuple, 'version_info_tuple': version_info_tuple, 'validation_info_tuples': validation_info_tuples, 'usage_info_tuple': usage_info_tuple, 'documentation_info_tuple': documentation_info_tuple, 'resource_creation_info_tuple': resource_creation_info_tuple, 'relation_info_tuples': relation_info_tuples, 'linguality_infos': linguality_infos, 'license_types': license_types, 'resourceType': resource_type, 'mediaTypes': media_types, 'url': url, 'metaShareId': metashare_id, 'contact_person_dicts': contact_person_dicts, } template = 'repository/lr_view.html' # For users who have edit permission for this resource, we have to add # LR_EDIT which contains the URL of the Django admin backend page # for this resource. if has_edit_permission(request, resource): context['LR_EDIT'] = reverse( 'admin:repository_resourceinfotype_model_change', \ args=(resource.id,)) # in general, only logged in users may download/purchase any resources context['LR_DOWNLOAD'] = request.user.is_active # Update statistics: if saveLRStats(resource, VIEW_STAT, request): # update view count in the search index, too update_lr_index_entry(resource) # update view tracker tracker = SessionResourcesTracker.getTracker(request) tracker.add_view(resource, datetime.now()) request.session['tracker'] = tracker # Add download/view/last updated statistics to the template context. context['LR_STATS'] = getLRStats(resource.storage_object.identifier) # Add recommendations for 'also viewed' resources context['also_viewed'] = \ _format_recommendations(get_view_recommendations(resource)) # Add recommendations for 'also downloaded' resources context['also_downloaded'] = \ _format_recommendations(get_download_recommendations(resource)) # Add 'more from same' links if get_more_from_same_projects_qs(resource).count(): context['search_rel_projects'] = '{}/repository/search?q={}:{}'.format( DJANGO_URL, MORE_FROM_SAME_PROJECTS, resource.storage_object.identifier) if get_more_from_same_creators_qs(resource).count(): context['search_rel_creators'] = '{}/repository/search?q={}:{}'.format( DJANGO_URL, MORE_FROM_SAME_CREATORS, resource.storage_object.identifier) # Render and return template with the defined context. ctx = RequestContext(request) return render_to_response(template, context, context_instance=ctx)