def test_stats_actions(self): """ Testing statistics functions about LR """ statsdata = getLRLast(UPDATE_STAT, 2) self.assertEqual(len(statsdata), 0) resources = resourceInfoType_model.objects.all() for resource in resources: for action in (VIEW_STAT, RETRIEVE_STAT, DOWNLOAD_STAT): saveLRStats(resource, action) self.assertEqual(len(getLRLast(action, 10)), 0) # change the status in published client = Client() client.login(username='******', password='******') resources = resourceInfoType_model.objects.all() for resource in resources: resource.storage_object.publication_status = INGESTED resource.storage_object.save() client.post(ADMINROOT, {"action": "publish_action", ACTION_CHECKBOX_NAME: resource.id}, follow=True) for i in range(0, 2): resource = resourceInfoType_model.objects.get(pk=resources[i].pk) for action in (VIEW_STAT, RETRIEVE_STAT, DOWNLOAD_STAT): saveLRStats(resource, action) self.assertEqual(len(getLRLast(action, 10)), i+1)
def test_stats_actions(self): """ Testing statistics functions about LR """ statsdata = getLRLast(UPDATE_STAT, 2) self.assertEqual(len(statsdata), 0) resources = resourceInfoType_model.objects.all() for resource in resources: for action in (VIEW_STAT, RETRIEVE_STAT, DOWNLOAD_STAT): saveLRStats(resource, action) self.assertEqual(len(getLRLast(action, 10)), 0) # change the status in published client = Client() client.login(username='******', password='******') resources = resourceInfoType_model.objects.all() for resource in resources: resource.storage_object.publication_status = INGESTED resource.storage_object.save() client.post(ADMINROOT, { "action": "publish_action", ACTION_CHECKBOX_NAME: resource.id }, follow=True) for i in range(0, 2): resource = resourceInfoType_model.objects.get(pk=resources[i].pk) for action in (VIEW_STAT, RETRIEVE_STAT, DOWNLOAD_STAT): saveLRStats(resource, action) self.assertEqual(len(getLRLast(action, 10)), i + 1)
def create(xml_file): """ Creates a new storage object instance for the given XML file. This also creates all corresponding object instances. """ # Check if the given input file exists. if not os.path.exists(xml_file): print "Error: input file '{0}' does not exist!".format(xml_file) sys.exit(-1) with open(xml_file, 'r') as _handle: _metadata = _handle.read() storage_object = StorageObject.objects.create(metadata=_metadata) try: _import_xml_from_string(_metadata, storage_object) storage_object.published = True storage_object.save() except: print format_exc() storage_object.delete() print "Error: could not create object model instances, aborting!" return #statistics call saveLRStats("", storage_object.identifier, "", UPDATE_STAT) print "Success: created new storage object {0} with id '{1}'.".format( storage_object, storage_object.identifier)
def view(request, object_id=None): """ Render browse or detail view for the repo2 application. """ # If an object id is given, try to look up the corresponding resource in # the Django database, raising HTTP 404 if it cannot be found. if object_id: resource = get_object_or_404(resourceInfoType_model, pk=object_id) # Convert resource to ElementTree and then to template tuples. resource_tree = resource.export_to_elementtree() lr_content = _convert_to_template_tuples(resource_tree) # we need to know if the resource is published or not resource_published = resource.storage_object.published # Define context for template rendering. context = {'resource': resource, 'lr_content': lr_content, 'RESOURCE_PUBLISHED': resource_published} template = 'repo2/lr_view.html' # For staff users, we have to add LR_EDIT which contains the URL of # the Django admin backend page for this resource. if request.user.is_staff: lr_edit_url = '/{}admin/repo2/resourceinfotype_model/{}/'.format( DJANGO_BASE, object_id) context['LR_EDIT'] = lr_edit_url context['LR_DOWNLOAD'] = "" try: licences = licenceInfoType_model.objects.values("downloadLocation").filter(back_to_distributioninfotype_model__id=object_id) if resource.storage_object.has_download() or resource.storage_object.has_local_download_copy() or len(licences) > 0: context['LR_DOWNLOAD'] = '/{0}repo2/download/{1}/'.format(DJANGO_BASE, object_id) if (not request.user.is_active): context['LR_DOWNLOAD'] = "restricted" except ObjectDoesNotExist: print "Info about licence doesn't exist." # Update statistics and create a report about the user actions on LR if hasattr(resource.storage_object, 'identifier'): sessionid = "" if request.COOKIES: sessionid = request.COOKIES.get('sessionid', '') saveLRStats(request.user.username, resource.storage_object.identifier, sessionid, VIEW_STAT) context['LR_STATS'] = getLRStats(resource.storage_object.identifier) # Otherwise, we just collect all resources from the Django database. else: resources = resourceInfoType_model.objects.all() # Define context for template rendering. context = {'resources': resources} template = 'repo2/resources.html' # Render and return template with the defined context. ctx = RequestContext(request) return render_to_response(template, context, context_instance=ctx)
def testStatActions(self): """ Testing statistics functions about LR """ lrid = "00000000" for action in (UPDATE_STAT, VIEW_STAT, RETRIEVE_STAT, DOWNLOAD_STAT): saveLRStats("anonymous", lrid, "", action) self.assertEqual(len(getLRLast(action, 10)), 1)
def _provide_download(request, resource, download_urls): """ Returns an HTTP response with a download of the given resource. """ if resource.storage_object.has_local_download_copy(): try: dl_path = resource.storage_object.get_download() def dl_stream_generator(): with open(dl_path, 'rb') as _local_data: _chunk = _local_data.read(MAXIMUM_READ_BLOCK_SIZE) while _chunk: yield _chunk _chunk = _local_data.read(MAXIMUM_READ_BLOCK_SIZE) # build HTTP response with a guessed mime type; the response # content is a stream of the download file filemimetype = guess_type(dl_path)[0] or "application/octet-stream" response = HttpResponse(dl_stream_generator(), mimetype=filemimetype) response['Content-Length'] = getsize(dl_path) response['Content-Disposition'] = 'attachment; filename={0}' \ .format(split(dl_path)[1]) # maintain download statistics and return the response for download saveLRStats(resource, request.user.username, _get_sessionid(request), DOWNLOAD_STAT) LOGGER.info("Offering a local download of resource #{0}." \ .format(resource.id)) return response except: LOGGER.warn("An error has occurred while trying to provide the " \ "local download copy of resource #{0}." \ .format(resource.id)) # redirect to a download location, if available elif download_urls: for url in download_urls: status_code = urlopen(url).getcode() if not status_code or status_code < 400: saveLRStats(resource, request.user.username, _get_sessionid(request), DOWNLOAD_STAT) LOGGER.info("Redirecting to {0} for the download of resource " \ "#{1}.".format(url, resource.id)) return redirect(url) LOGGER.warn("No download could be offered for resource #{0}. These " \ "URLs were tried: {1}".format(resource.id, download_urls)) else: LOGGER.error("No download could be offered for resource #{0} with " \ "storage object identifier #{1} although our code " \ "considered it to be downloadable!".format(resource.id, resource.storage_object.identifier)) # no download could be provided return render_to_response('repository/lr_not_downloadable.html', { 'resource': resource, 'reason': 'internal' }, context_instance=RequestContext(request))
def _delete_resource(repository, remote_id, remove=False): if repository.contains(remote_id): so, _dc = repository[remote_id] if remove: # this record is removed from the client's repo, so remove it del repository[remote_id] so.deleted = True so.save() # explicitly write metadata XML and storage object to the storage folder so.update_storage() # update statistics saveLRStats(so.resourceinfotype_model_set.all()[0], DELETE_STAT) return True return False
def view(request, object_id=None): """ Render browse or detail view for the repository application. """ # If an object id is given, try to look up the corresponding resource in # the Django database, raising HTTP 404 if it cannot be found. if object_id: resource = get_object_or_404(resourceInfoType_model, pk=object_id) # Convert resource to ElementTree and then to template tuples. resource_tree = resource.export_to_elementtree() lr_content = _convert_to_template_tuples(resource_tree) # we need to know if the resource is published or not resource_published = resource.storage_object.published # Define context for template rendering. context = {'resource': resource, 'lr_content': lr_content, 'RESOURCE_PUBLISHED': resource_published} template = 'repository/lr_view.html' # For staff users, we have to add LR_EDIT which contains the URL of # the Django admin backend page for this resource. if request.user.is_staff: context['LR_EDIT'] = reverse( 'admin:repository_resourceinfotype_model_change', args=(object_id,)) # in general, only logged in users may download/purchase any resources context['LR_DOWNLOAD'] = request.user.is_active # Update statistics and create a report about the user actions on LR if hasattr(resource.storage_object, 'identifier'): sessionid = "" if request.COOKIES: sessionid = request.COOKIES.get('sessionid', '') saveLRStats(request.user.username, resource.storage_object.identifier, sessionid, VIEW_STAT) context['LR_STATS'] = getLRStats(resource.storage_object.identifier) # Otherwise, we just collect all resources from the Django database. else: resources = resourceInfoType_model.objects.all() # Define context for template rendering. context = {'resources': resources} template = 'repository/resources.html' # Render and return template with the defined context. ctx = RequestContext(request) return render_to_response(template, context, context_instance=ctx)
def import_from_string(xml_string, targetstatus, copy_status, owner_id=None): """ Import a single resource from a string representation of its XML tree, and save it with the given target status. Returns the imported resource object on success, raises and Exception on failure. """ from metashare.repository.models import resourceInfoType_model result = resourceInfoType_model.import_from_string(xml_string, copy_status=copy_status) if not result[0]: msg = u'' if len(result) > 2: msg = u'{}'.format(result[2]) raise Exception(msg) resource = result[0] # Set publication_status for the new object. Also make sure that the # deletion flag is not set (may happen in case of re-importing a previously # deleted resource). resource.storage_object.publication_status = targetstatus resource.storage_object.deleted = False if owner_id: resource.owners.add(owner_id) for edt_grp in User.objects.get(id=owner_id).userprofile \ .default_editor_groups.all(): resource.editor_groups.add(edt_grp) # this also takes care of saving the storage_object resource.save() else: resource.storage_object.save() # explicitly write metadata XML and storage object to the storage folder resource.storage_object.update_storage() # Create log ADDITION message for the new object, but only if we have a user: if owner_id: LogEntry.objects.log_action( user_id=owner_id, content_type_id=ContentType.objects.get_for_model(resource).pk, object_id=resource.pk, object_repr=force_unicode(resource), action_flag=ADDITION) # Update statistics saveLRStats(resource, UPDATE_STAT) return resource
def import_from_string(xml_string, targetstatus, copy_status, owner_id=None): """ Import a single resource from a string representation of its XML tree, and save it with the given target status. Returns the imported resource object on success, raises and Exception on failure. """ from metashare.repository.models import resourceInfoType_model result = resourceInfoType_model.import_from_string(xml_string, copy_status=copy_status) if not result[0]: msg = u'' if len(result) > 2: msg = u'{}'.format(result[2]) raise Exception(msg) resource = result[0] # Set publication_status for the new object. Also make sure that the # deletion flag is not set (may happen in case of re-importing a previously # deleted resource). resource.storage_object.publication_status = targetstatus resource.storage_object.deleted = False if owner_id: resource.owners.add(owner_id) for edt_grp in User.objects.get(id=owner_id).get_profile() \ .default_editor_groups.all(): resource.editor_groups.add(edt_grp) # this also takes care of saving the storage_object resource.save() else: resource.storage_object.save() # explicitly write metadata XML and storage object to the storage folder resource.storage_object.update_storage() # Create log ADDITION message for the new object, but only if we have a user: if owner_id: LogEntry.objects.log_action( user_id = owner_id, content_type_id = ContentType.objects.get_for_model(resource).pk, object_id = resource.pk, object_repr = force_unicode(resource), action_flag = ADDITION ) # Update statistics saveLRStats(resource, UPDATE_STAT) return resource
def import_from_string(xml_string, targetstatus, owner_id=None): """ Import a single resource from a string representation of its XML tree, and save it with the given target status. Returns the imported resource object on success, raises and Exception on failure. """ from metashare.repository.models import resourceInfoType_model result = resourceInfoType_model.import_from_string(xml_string) if not result[0]: msg = u'' if len(result) > 2: msg = u'{}'.format(result[2]) raise Exception(msg) resource = result[0] # Set publication_status for new object. resource.storage_object.publication_status = targetstatus if owner_id: resource.owners.add(owner_id) resource.storage_object.save() # explicitly write metadata XML and storage object to the storage folder resource.storage_object.update_storage() # Create log ADDITION message for the new object, but only if we have a user: if owner_id: LogEntry.objects.log_action( user_id = owner_id, content_type_id = ContentType.objects.get_for_model(resource).pk, object_id = resource.pk, object_repr = force_unicode(resource), action_flag = ADDITION ) # Update statistics saveLRStats(resource, "", "", UPDATE_STAT) return resource
def testStatActions(self): """ Testing statistics functions about LR """ client = self.client_with_user_logged_in(StatsTest.editor_login) xmlfile = open(TESTFIXTURES_ZIP, 'rb') response = client.post(ADMINROOT+'upload_xml/', {'description': xmlfile, 'uploadTerms':'on' }, follow=True) # And verify that we have more than zero resources on the page where we # are being redirected: self.assertContains(response, "Editable Resources") self.assertNotContains(response, '0 Resources') statsdata = getLRLast(UPDATE_STAT, 2) self.assertEqual(len(statsdata), 2) for action in (VIEW_STAT, RETRIEVE_STAT, DOWNLOAD_STAT): for item in statsdata: resource = resourceInfoType_model.objects.get(storage_object__identifier=item['lrid']) saveLRStats(resource, "anonymous", "", action) self.assertEqual(len(getLRLast(action, 10)), 2)
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. resource_tree = resource.export_to_elementtree() lr_content = _convert_to_template_tuples(resource_tree) # Define context for template rendering. context = { 'resource': resource, 'lr_content': lr_content } template = 'repository/lr_view.html' # For staff users, we have to add LR_EDIT which contains the URL of # the Django admin backend page for this resource. if request.user.is_staff: context['LR_EDIT'] = reverse( 'admin:repository_resourceinfotype_model_change', args=(object_id,)) # in general, only logged in users may download/purchase any resources context['LR_DOWNLOAD'] = request.user.is_active # Update statistics and create a report about the user actions on LR if hasattr(resource.storage_object, 'identifier'): sessionid = "" if request.COOKIES: sessionid = request.COOKIES.get('sessionid', '') saveLRStats(resource, request.user.username, sessionid, VIEW_STAT) context['LR_STATS'] = getLRStats(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 update(object_id, xml_file): """ Updates the storage object instance with the given object_id using the contents of the given XML file. """ # Check if the given input file exists. if not os.path.exists(xml_file): print "Error: input file '{0}' does not exist!".format(xml_file) sys.exit(-1) # Check that the storage object instance exists. storage_object = StorageObject.objects.filter(identifier=object_id) if not storage_object: print "Error: no storage object with the given identifier exists!" return else: storage_object = storage_object[0] with open(xml_file, 'r') as _handle: _metadata = _handle.read() try: _import_xml_from_string(_metadata, storage_object) except: print format_exc() print "Error: could not update object model instances, aborting!" return storage_object.metadata = _metadata storage_object.save() #statistics call saveLRStats("", storage_object.identifier, "", UPDATE_STAT) print "Success: updated storage object {0} with id '{1}'.".format( storage_object, storage_object.identifier)
tree = fromstring(temp_file.read()) result = resourceInfoType_model.import_from_elementtree(tree) if result[0]: resource = result[0] if "published" in filename: resource.storage_object.publication_status = 'p' elif "ingested" in filename: resource.storage_object.publication_status = 'g' else: #if internal (if no status given, status default is internal) resource.storage_object.publication_status = 'i' resource.storage_object.save() SUCCESSFUL_IMPORTS += 1 saveLRStats("", resource.storage_object.identifier, "", UPDATE_STAT) except: ERRONEOUS_IMPORTS += 1 print 'Could not import XML file into database!' print traceback.format_exc() temp_file.close() print "Done. Successfully import {0} files into the database, errors " \ "occured in {1} cases.".format(SUCCESSFUL_IMPORTS, ERRONEOUS_IMPORTS)
def unpublish_resources(modeladmin, request, queryset): for obj in queryset: change_resource_status(obj, status=INGESTED, precondition_status=PUBLISHED) if hasattr(obj, 'storage_object') and obj.storage_object is not None: saveLRStats(obj, "", "", INGEST_STAT)
def download(request, object_id): """ Renders the repository download view. """ if object_id and request.user.is_active: resource = get_object_or_404(resourceInfoType_model, pk=object_id) agreement = 0 if request.method == "POST": if request.POST.get('license_agree', False): agreement = 1 elif request.method == "GET": agreement = int(request.GET.get('license_agree', '0')) licences = licenceInfoType_model.objects.values("licence","downloadLocation").filter(back_to_distributioninfotype_model__id=object_id) if agreement == 1: sessionid = "" if request.COOKIES: sessionid = request.COOKIES.get('sessionid', '') if resource.storage_object.has_local_download_copy(): try: #return HttpResponse(resource.storage_object.get_download()) _binary_data = resource.storage_object.get_download() # We use a generic, binary mime type here for version v1. response = HttpResponse(mimetype="application/octet-stream") response['Content-Disposition'] = 'attachment; ' \ 'filename={0}'.format(split(_binary_data)[1]) with open(_binary_data, 'rb') as _local_data: _chunk = _local_data.read(MAXIMUM_READ_BLOCK_SIZE) while _chunk: response.write(_chunk) _chunk = _local_data.read(MAXIMUM_READ_BLOCK_SIZE) saveLRStats(request.user.username, resource.storage_object.identifier, sessionid, DOWNLOAD_STAT) return response except: raise Http404 #redirect on download location else: try: if (len(licences) >0 ): for licenceinfo in licences: urldown = pickle.loads(base64.b64decode(str(licenceinfo['downloadLocation']))) if isinstance(urldown, list) and len(urldown) > 0: code = urlopen(urldown[0]).code if (code / 100 < 4): saveLRStats(request.user.username, resource.storage_object.identifier, sessionid, DOWNLOAD_STAT) return redirect(urldown[0]) except ObjectDoesNotExist: LOGGER.debug("Warning! Info about licence or downloadLocation is wrong.") raise Http404 signature_req = 0 for licenceinfo in licences: licencelabel = LICENCEINFOTYPE_LICENCE_CHOICES['choices'][int(licenceinfo['licence'])][1] if LICENCEINFOTYPE_URLS_LICENCE_CHOICES[licencelabel][1] == "SignatureRequired": signature_req = 1 title = resource.identificationInfo.resourceName dictionary = {'title': title, 'object_id': object_id, 'signature_req': signature_req } return render_to_response('repo2/download.html', dictionary, context_instance=RequestContext(request))
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)
def save_model(self, request, obj, form, change): super(ResourceModelAdmin, self).save_model(request, obj, form, change) #update statistics if hasattr(obj, 'storage_object') and obj.storage_object is not None: saveLRStats("", obj.storage_object.identifier, "", UPDATE_STAT)
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)
tree = fromstring(temp_file.read()) result = resourceInfoType_model.import_from_elementtree(tree) if result[0]: resource = result[0] if "published" in filename: resource.storage_object.publication_status = 'p' elif "ingested" in filename: resource.storage_object.publication_status = 'g' else: #if internal (if no status given, status default is internal) resource.storage_object.publication_status = 'i' resource.storage_object.save() SUCCESSFUL_IMPORTS += 1 saveLRStats(resource, "", "", UPDATE_STAT) except: ERRONEOUS_IMPORTS += 1 print 'Could not import XML file into database!' print traceback.format_exc() temp_file.close() print "Done. Successfully import {0} files into the database, errors " \ "occured in {1} cases.".format(SUCCESSFUL_IMPORTS, ERRONEOUS_IMPORTS)
def test_views(self): # client 1 views all 4 resources client_1 = Client() man = TogetherManager.getManager(Resource.VIEW) response = client_1.get(self.res_1.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(0, len(view_res)) response = client_1.get(self.res_2.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(1, len(view_res)) view_res = man.getTogetherList(self.res_2, 0) self.assertEqual(1, len(view_res)) self.assertEqual(1, man.getTogetherCount(self.res_1, self.res_2)) response = client_1.get(self.res_3.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(2, len(view_res)) view_res = man.getTogetherList(self.res_2, 0) self.assertEqual(2, len(view_res)) view_res = man.getTogetherList(self.res_3, 0) self.assertEqual(2, len(view_res)) response = client_1.get(self.res_4.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(3, len(view_res)) view_res = man.getTogetherList(self.res_2, 0) self.assertEqual(3, len(view_res)) view_res = man.getTogetherList(self.res_3, 0) self.assertEqual(3, len(view_res)) view_res = man.getTogetherList(self.res_4, 0) self.assertEqual(3, len(view_res)) # another client views 2 of the resources, counts are increased client_2 = Client() response = client_2.get(self.res_1.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(3, len(view_res)) response = client_2.get(self.res_2.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(3, len(view_res)) view_res = man.getTogetherList(self.res_2, 0) self.assertEqual(3, len(view_res)) # counts of res_1 and res_2 appearing together is increased self.assertEqual(2, man.getTogetherCount(self.res_1, self.res_2)) # make sure that downloads are no touched man = TogetherManager.getManager(Resource.DOWNLOAD) download_res = man.getTogetherList(self.res_1, 0) self.assertEqual(0, len(download_res)) # make sure that statistics are updated when a resource is # completely removed saveLRStats(self.res_1, UPDATE_STAT) saveLRStats(self.res_2, UPDATE_STAT) saveLRStats(self.res_3, UPDATE_STAT) saveLRStats(self.res_4, UPDATE_STAT) self.assertEquals(9, len(LRStats.objects.all())) # TODO: checka again self.assertEquals(256, len(UsageStats.objects.all())) remove_resource(self.res_1.storage_object) self.assertEquals(7, len(LRStats.objects.all())) # TODO: check again self.assertEquals(192, len(UsageStats.objects.all()))
temp_file = open(fullpath, 'rb') temp_file.seek(0) try: print 'Importing XML file: "{0}"'.format(filename) tree = fromstring(temp_file.read()) result = resourceInfoType_model.import_from_elementtree(tree) if result[0]: resource = result[0] if "published" in filename: resource.storage_object.publication_status = 'p' elif "ingested" in filename: resource.storage_object.publication_status = 'g' else: #if internal (if no status given, status default is internal) resource.storage_object.publication_status = 'i' resource.storage_object.save() SUCCESSFUL_IMPORTS += 1 saveLRStats(resource, UPDATE_STAT) except: ERRONEOUS_IMPORTS += 1 print 'Could not import XML file into database!' print traceback.format_exc() temp_file.close() print "Done. Successfully import {0} files into the database, errors " \ "occured in {1} cases.".format(SUCCESSFUL_IMPORTS, ERRONEOUS_IMPORTS)
def test_views(self): # client 1 views all 4 resources client_1 = Client() man = TogetherManager.getManager(Resource.VIEW) response = client_1.get(self.res_1.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(0, len(view_res)) response = client_1.get(self.res_2.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(1, len(view_res)) view_res = man.getTogetherList(self.res_2, 0) self.assertEqual(1, len(view_res)) self.assertEqual(1, man.getTogetherCount(self.res_1, self.res_2)) response = client_1.get(self.res_3.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(2, len(view_res)) view_res = man.getTogetherList(self.res_2, 0) self.assertEqual(2, len(view_res)) view_res = man.getTogetherList(self.res_3, 0) self.assertEqual(2, len(view_res)) response = client_1.get(self.res_4.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(3, len(view_res)) view_res = man.getTogetherList(self.res_2, 0) self.assertEqual(3, len(view_res)) view_res = man.getTogetherList(self.res_3, 0) self.assertEqual(3, len(view_res)) view_res = man.getTogetherList(self.res_4, 0) self.assertEqual(3, len(view_res)) # another client views 2 of the resources, counts are increased client_2 = Client() response = client_2.get(self.res_1.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(3, len(view_res)) response = client_2.get(self.res_2.get_absolute_url(), follow = True) self.assertEquals(200, response.status_code) view_res = man.getTogetherList(self.res_1, 0) self.assertEqual(3, len(view_res)) view_res = man.getTogetherList(self.res_2, 0) self.assertEqual(3, len(view_res)) # counts of res_1 and res_2 appearing together is increased self.assertEqual(2, man.getTogetherCount(self.res_1, self.res_2)) # make sure that downloads are no touched man = TogetherManager.getManager(Resource.DOWNLOAD) download_res = man.getTogetherList(self.res_1, 0) self.assertEqual(0, len(download_res)) # make sure that statistics are updated when a resource is # completely removed saveLRStats(self.res_1, UPDATE_STAT) saveLRStats(self.res_2, UPDATE_STAT) saveLRStats(self.res_3, UPDATE_STAT) saveLRStats(self.res_4, UPDATE_STAT) self.assertEquals(9, len(LRStats.objects.all())) self.assertEquals(228, len(UsageStats.objects.all())) remove_resource(self.res_1.storage_object) self.assertEquals(7, len(LRStats.objects.all())) self.assertEquals(181, len(UsageStats.objects.all()))