def get(self, request, *args, **kwargs): """ View an existing experiment. :param request: a HTTP Request instance :type request: :class:`django.http.HttpRequest` :param experiment_id: the ID of the experiment :rtype: :class:`django.http.HttpResponse` """ experiment_id = kwargs.get('experiment_id', None) if experiment_id is None: return return_response_error(request) try: experiment = Experiment.safe.get(request.user, experiment_id) except PermissionDenied: return return_response_error(request) except Experiment.DoesNotExist: return return_response_not_found(request) if not experiment: return return_response_not_found(request) view_override = self.find_custom_view_override(request, experiment) if view_override is not None: return view_override c = self.get_context_data(request, experiment) template_name = kwargs.get('template_name', None) if template_name is None: template_name = self.template_name return HttpResponse(render_response_index(request, template_name, c))
def _create_download_response(request, datafile_id, disposition='attachment'): # too complex # noqa # Get datafile (and return 404 if absent) try: datafile = DataFile.objects.get(pk=datafile_id) except DataFile.DoesNotExist: return return_response_not_found(request) # Check users has access to datafile if not has_datafile_download_access(request=request, datafile_id=datafile.id): return return_response_error(request) # Send an image that can be seen in the browser if disposition == 'inline' and datafile.is_image(): from tardis.tardis_portal.iiif import download_image args = (request, datafile.id, 'full', 'full', '0', 'native') # Send unconverted image if web-compatible if datafile.get_mimetype() in ('image/gif', 'image/jpeg', 'image/png'): return download_image(*args) # Send converted image return download_image(*args, format='png') # Send local file try: verified_only = True # Query parameter to allow download of unverified files ignore_verif = request.GET.get('ignore_verification_status', '0') # Ensure ignore_verification_status=0 etc works as expected # a bare ?ignore_verification_status is True if ignore_verif.lower() in [u'', u'1', u'true']: verified_only = False # Get file object for datafile file_obj = datafile.get_file(verified_only=verified_only) if not file_obj: # If file path doesn't resolve, return not found if verified_only: return render_error_message(request, "File is unverified, " "please try again later.", status=503) return return_response_not_found(request) wrapper = FileWrapper(file_obj, blksize=65535) response = StreamingHttpResponse(wrapper, content_type=datafile.get_mimetype()) response['Content-Disposition'] = \ '%s; filename="%s"' % (disposition, datafile.filename) return response except IOError: # If we can't read the file, return not found return return_response_not_found(request) except ValueError: # raised when replica not verified TODO: custom excptn redirect = request.META.get( 'HTTP_REFERER', 'http://%s/' % request.META.get('HTTP_HOST')) message = """The file you are trying to access has not yet been verified. Verification is an automated background process. Please try again later or contact the system administrator if the issue persists.""" message = ' '.join(message.split()) # removes spaces redirect = redirect + '#error:' + message return HttpResponseRedirect(redirect)
def _create_download_response(request, datafile_id, disposition='attachment'): # too complex # noqa # Get datafile (and return 404 if absent) try: datafile = DataFile.objects.get(pk=datafile_id) except DataFile.DoesNotExist: return return_response_not_found(request) # Check users has access to datafile if not has_datafile_download_access(request=request, datafile_id=datafile.id): return return_response_error(request) # Send an image that can be seen in the browser if disposition == 'inline' and datafile.is_image(): from tardis.tardis_portal.iiif import download_image args = (request, datafile.id, 'full', 'full', '0', 'native') # Send unconverted image if web-compatible if datafile.get_mimetype() in ('image/gif', 'image/jpeg', 'image/png'): return download_image(*args) # Send converted image return download_image(*args, format='png') # Send local file try: verified_only = True # Query parameter to allow download of unverified files ignore_verif = request.GET.get('ignore_verification_status', '0') # Ensure ignore_verification_status=0 etc works as expected # a bare ?ignore_verification_status is True if ignore_verif.lower() in [u'', u'1', u'true']: verified_only = False # Get file object for datafile file_obj = datafile.get_file(verified_only=verified_only) if not file_obj: # If file path doesn't resolve, return not found if verified_only: return render_error_message(request, "File is unverified, " "please try again later.", status=503) return return_response_not_found(request) wrapper = FileWrapper(file_obj, blksize=65535) response = StreamingHttpResponse(wrapper, content_type=datafile.get_mimetype()) response['Content-Disposition'] = \ '%s; filename="%s"' % (disposition, datafile.filename) return response except IOError: # If we can't read the file, return not found return return_response_not_found(request) except ValueError: # raised when replica not verified TODO: custom excptn redirect = request.META.get('HTTP_REFERER', 'http://%s/' % request.META.get('HTTP_HOST')) message = """The file you are trying to access has not yet been verified. Verification is an automated background process. Please try again later or contact the system administrator if the issue persists.""" message = ' '.join(message.split()) # removes spaces redirect = redirect + '#error:' + message return HttpResponseRedirect(redirect)
def checksums_download(request, dataset_id, **kwargs): dataset = Dataset.objects.get(id=dataset_id) if not dataset: return return_response_not_found(request) type = request.GET.get('type', 'md5') format = request.GET.get('format', 'text') checksums = _get_dataset_checksums(dataset, type) if format == 'text': checksum_doc = ''.join(["%s %s\n" % c for c in checksums]) checksum_doc += '\n' response = HttpResponse(checksum_doc, content_type='text/plain') response['Content-Disposition'] = \ '%s; filename="%s-manifest-md5.txt"' % ( 'attachment', get_filesystem_safe_dataset_name(dataset)) return response elif format == 'json': jdict = {'checksums': []} for c in checksums: jdict['checksums'].append({ 'checksum': c[0], 'file': c[1], 'type': type }) return JsonResponse(jdict) else: raise ValueError("Invalid format. Valid formats are 'text' or 'json'")
def get(self, request, *args, **kwargs): """ :param request: a HTTP request object :type request: :class:`django.http.HttpRequest` :return: The Django response object :rtype: :class:`django.http.HttpResponse` """ dataset_id = kwargs.get('dataset_id', None) if dataset_id is None: return return_response_error(request) dataset = Dataset.objects.get(id=dataset_id) if not dataset: return return_response_not_found(request) c = self.get_context_data(request, dataset, **kwargs) template_name = kwargs.get('template_name', None) if template_name is None: template_name = self.template_name return HttpResponse(render_response_index( request, template_name, c) )
def get(self, request, *args, **kwargs): """ :param request: a HTTP request object :type request: :class:`django.http.HttpRequest` :return: The Django response object :rtype: :class:`django.http.HttpResponse` """ dataset_id = kwargs.get('dataset_id', None) if dataset_id is None: return return_response_error(request) dataset = Dataset.objects.get(id=dataset_id) if not dataset: return return_response_not_found(request) c = self.get_context_data(request, dataset, **kwargs) template_name = kwargs.get('template_name', None) if template_name is None: template_name = self.template_name return HttpResponse(render_response_index(request, template_name, c))
def new_sample(request, experiment_id): try: experiment = Experiment.safe.get(request, experiment_id) except PermissionDenied: return return_response_error(request) except Experiment.DoesNotExist: return return_response_not_found(request) c = Context() c['experiment'] = experiment samples = Sample.objects.filter(experiment=experiment_id) c['sample_count'] = samples.count() + 1 if request.method == 'POST': form = SampleForm(request.POST) if form.is_valid(): sample = form.save(experiment_id, commit=False) sample.save_m2m() request.POST = {'status': "Sample Created."} return _redirect(experiment_id) c['status'] = "Errors exist in form." c["error"] = 'true' else: form = SampleForm(extra=1) c['form'] = form c['status'] = form.errors return HttpResponse(render_response_index(request, 'tardis_portal/experiment_sample.html', c))
def edit_sample(request, experiment_id, sample_id): try: experiment = Experiment.safe.get(request, experiment_id) except PermissionDenied: return return_response_error(request) except Experiment.DoesNotExist: return return_response_not_found(request) c = Context() c['experiment'] = experiment sample = Sample.objects.get(id=sample_id) c['sample_count'] = sample.name if request.POST: form = SampleForm(request.POST, instance=sample, extra=0) if form.is_valid(): full_sample = form.save(experiment_id, commit=False) full_sample.save_m2m() request.POST = {'status': "Sample Created."} return _redirect(experiment_id) c['status'] = "Errors exist in form." c["error"] = 'true' else: form = SampleForm(instance=sample, extra=0) c['form'] = form c['status'] = form.errors return HttpResponse(render_response_index(request, 'tardis_portal/experiment_sample.html', c))
def get(self, request, *args, **kwargs): """ The index view, intended to render the front page of the MyTardis site listing recent experiments. This default view can be overriden by defining a dictionary INDEX_VIEWS in settings which maps SITE_ID's or domain names to an alternative view function (similar to the DATASET_VIEWS or EXPERIMENT_VIEWS overrides). :param request: a HTTP request object :type request: :class:`django.http.HttpRequest` :return: The Django response object :rtype: :class:`django.http.HttpResponse` """ dataset_id = kwargs.get('dataset_id', None) if dataset_id is None: return return_response_error(request) dataset = Dataset.objects.get(id=dataset_id) if not dataset: return return_response_not_found(request) view_override = self.find_custom_view_override(request, dataset) if view_override is not None: return view_override c = self.get_context_data(request, dataset, **kwargs) template_name = kwargs.get('template_name', None) if template_name is None: template_name = self.template_name return HttpResponse(render_response_index(request, template_name, c))
def view_rifcs(request, experiment_id): """View the rif-cs of an existing experiment. :param request: a HTTP Request instance :type request: :class:`django.http.HttpRequest` :param experiment_id: the ID of the experiment to be viewed :type experiment_id: string :rtype: :class:`django.http.HttpResponse` """ try: experiment = Experiment.safe.get(request.user, experiment_id) except PermissionDenied: return return_response_error(request) except Experiment.DoesNotExist: return return_response_not_found(request) try: rifcs_provs = settings.RIFCS_PROVIDERS except AttributeError: rifcs_provs = () from tardis.tardis_portal.publish.publishservice import PublishService pservice = PublishService(rifcs_provs, experiment) context = pservice.get_context() if context is None: # return error page or something return return_response_error(request) template = pservice.get_template() return HttpResponse(render_response_index(request, template, context), content_type="text/xml")
def checksums_download(request, dataset_id, **kwargs): dataset = Dataset.objects.get(id=dataset_id) if not dataset: return return_response_not_found(request) type = request.GET.get('type', 'md5') format = request.GET.get('format', 'text') checksums = _get_dataset_checksums(dataset, type) if format == 'text': checksum_doc = ''.join(["%s %s\n" % c for c in checksums]) checksum_doc += '\n' response = HttpResponse(checksum_doc, content_type='text/plain') response['Content-Disposition'] = \ '%s; filename="%s-manifest-md5.txt"' % ( 'attachment', get_filesystem_safe_dataset_name(dataset)) return response elif format == 'json': jdict = {'checksums': []} for c in checksums: jdict['checksums'].append({'checksum': c[0], 'file': c[1], 'type': type}) return JsonResponse(jdict) else: raise ValueError("Invalid format. Valid formats are 'text' or 'json'")
def experiment_stats(request, epn): """Get stats about an experiment associated with the given EPN. :param request: a HTTP Request instance :type request: :class:`django.http.HttpRequest` :param epn: the EPN (proposal number) of the experiment to be edited :type epn: string :rtype: :class:`django.http.HttpResponse` """ if not (request.META['REMOTE_ADDR'] == '114.30.64.13' or request.META['REMOTE_USER'] == 'admin'): return return_response_error(request) try: epn_param = models.ExperimentParameter.objects.get(name__name__exact='EPN', string_value=epn) experiment_id = epn_param.parameterset.experiment.pk #experiment = models.Experiment.safe.get(request, experiment_id) except PermissionDenied: return return_response_error(request) except models.ExperimentParameter.DoesNotExist: return return_response_not_found(request) # calculate the sum of the datafile sizes datafile_sizes = models.Dataset_File.objects.filter(dataset__experiment=experiment_id).values_list('size', flat=True) json_dict = {} json_dict['total_file_count'] = len(datafile_sizes) json_dict['total_file_size'] = sum(map(long, datafile_sizes)) response = HttpResponse(json.dumps(json_dict), mimetype='application/json') response['Pragma'] = 'no-cache' response['Cache-Control'] = 'no-cache, must-revalidate' return response
def experiment_description(request, experiment_id): """View an existing experiment's description. To be loaded via ajax. :param request: a HTTP Request instance :type request: :class:`django.http.HttpRequest` :param experiment_id: the ID of the experiment to be edited :type experiment_id: string :rtype: :class:`django.http.HttpResponse` """ c = {} try: experiment = Experiment.safe.get(request.user, experiment_id) except PermissionDenied: return return_response_error(request) except Experiment.DoesNotExist: return return_response_not_found(request) c['experiment'] = experiment c['subtitle'] = experiment.title c['nav'] = [{ 'name': 'Data', 'link': '/experiment/view/' }, { 'name': experiment.title, 'link': experiment.get_absolute_url() }] c['authors'] = experiment.experimentauthor_set.all() c['datafiles'] = \ DataFile.objects.filter(dataset__experiments=experiment_id) c['owners'] = experiment.get_owners() # calculate the sum of the datafile sizes c['size'] = DataFile.sum_sizes(c['datafiles']) c['has_download_permissions'] = \ authz.has_experiment_download_access(request, experiment_id) c['has_write_permissions'] = \ authz.has_write_permissions(request, experiment_id) if request.user.is_authenticated(): c['is_owner'] = authz.has_experiment_ownership(request, experiment_id) _add_protocols_and_organizations(request, experiment, c) if 'status' in request.GET: c['status'] = request.GET['status'] if 'error' in request.GET: c['error'] = request.GET['error'] return HttpResponse( render_response_index( request, 'tardis_portal/ajax/experiment_description.html', c))
def _get_related_info(request, experiment_id, related_info_id): if not authz.has_experiment_access(request, experiment_id): return return_response_error(request) try: ps = ExperimentParameterSet.objects.get(schema__namespace=SCHEMA_URI, id=related_info_id) return HttpResponse(json.dumps(_get_dict_from_ps(ps)), content_type='application/json; charset=utf-8') except: return return_response_not_found(request)
def experiment_description(request, experiment_id): """View an existing experiment's description. To be loaded via ajax. :param request: a HTTP Request instance :type request: :class:`django.http.HttpRequest` :param experiment_id: the ID of the experiment to be edited :type experiment_id: string :rtype: :class:`django.http.HttpResponse` """ c = {} try: experiment = Experiment.safe.get(request.user, experiment_id) except PermissionDenied: return return_response_error(request) except Experiment.DoesNotExist: return return_response_not_found(request) c['experiment'] = experiment c['subtitle'] = experiment.title c['nav'] = [{'name': 'Data', 'link': '/experiment/view/'}, {'name': experiment.title, 'link': experiment.get_absolute_url()}] c['authors'] = experiment.experimentauthor_set.all() c['datafiles'] = \ DataFile.objects.filter(dataset__experiments=experiment_id) c['owners'] = experiment.get_owners() # calculate the sum of the datafile sizes c['size'] = DataFile.sum_sizes(c['datafiles']) c['has_download_permissions'] = \ authz.has_experiment_download_access(request, experiment_id) c['has_write_permissions'] = \ authz.has_write_permissions(request, experiment_id) if request.user.is_authenticated(): c['is_owner'] = authz.has_experiment_ownership(request, experiment_id) _add_protocols_and_organizations(request, experiment, c) if 'status' in request.GET: c['status'] = request.GET['status'] if 'error' in request.GET: c['error'] = request.GET['error'] return HttpResponse(render_response_index(request, 'tardis_portal/ajax/experiment_description.html', c))
def get(self, request, *args, **kwargs): """ View an existing experiment. :param request: a HTTP Request instance :type request: :class:`django.http.HttpRequest` :param list args: :param dict kwargs: in kwargs: param int experiment_id: the ID of the experiment :returns: an HttpResponse :rtype: :class:`django.http.HttpResponse` """ experiment_id = kwargs.get('experiment_id', None) if experiment_id is None: return return_response_error(request) try: experiment = Experiment.safe.get(request.user, experiment_id) except PermissionDenied: return return_response_error(request) except Experiment.DoesNotExist: return return_response_not_found(request) if not experiment: return return_response_not_found(request) view_override = self.find_custom_view_override(request, experiment) if view_override is not None: return view_override c = self.get_context_data(request, experiment) template_name = kwargs.get('template_name', None) if template_name is None: template_name = self.template_name return HttpResponse(render_response_index(request, template_name, c))
def experiment_datasets_json(request, experiment_id): try: experiment = Experiment.safe.get(request.user, experiment_id) except Experiment.DoesNotExist: return return_response_not_found(request) has_download_permissions = \ authz.has_experiment_download_access(request, experiment_id) objects = [ get_dataset_info(ds, include_thumbnail=has_download_permissions, exclude=['datafiles']) for ds in experiment.datasets.all().order_by('description')] return HttpResponse(json.dumps(objects), content_type='application/json')
def index(request, experiment_id): try: experiment = Experiment.safe.get(request, experiment_id) except PermissionDenied: return return_response_error(request) except Experiment.DoesNotExist: return return_response_not_found(request) c = Context({'experiment': experiment}) if authz.has_write_permissions(request, experiment_id): template = 'related_info/index.html' else: template = 'related_info/index_ro.html' return HttpResponse(render_response_index(request, template, c))
def index(request, experiment_id): try: experiment = Experiment.safe.get(request.user, experiment_id) except PermissionDenied: return return_response_error(request) except Experiment.DoesNotExist: return return_response_not_found(request) c = {'experiment': experiment} if authz.has_write_permissions(request, experiment_id): template = 'related_info/index.html' else: template = 'related_info/index_ro.html' return HttpResponse(render_response_index(request, template, c))
def index(request, experiment_id): logger.debug('aaa %s' % experiment_id) try: experiment = Experiment.safe.get(request.user, experiment_id) except PermissionDenied: return return_response_error(request) except Experiment.DoesNotExist: return return_response_not_found(request) logger.debug('%s' % experiment) c = {'experiment': experiment} logger.debug('%s' % c) template = 'mongoquery/index.html' logger.debug('%s' % template) return HttpResponse(render_response_index(request, template, c))
def experiment_datasets_json(request, experiment_id): try: experiment = Experiment.safe.get(request.user, experiment_id) except Experiment.DoesNotExist: return return_response_not_found(request) has_download_permissions = \ authz.has_experiment_download_access(request, experiment_id) objects = [ get_dataset_info(ds, include_thumbnail=has_download_permissions, exclude=['datafiles']) for ds in experiment.datasets.all().order_by('description') ] return HttpResponse(json.dumps(objects), content_type='application/json')
def index(request, experiment_id): try: experiment = Experiment.safe.get(request.user, experiment_id) except PermissionDenied: return return_response_error(request) except Experiment.DoesNotExist: return return_response_not_found(request) c = {'experiment': experiment, 'for_code_json': json.dumps(_get_for_codes(), sort_keys=True)} if authz.has_write_permissions(request, experiment_id): template = 'anzsrc_codes/index.html' else: template = 'anzsrc_codes/index_ro.html' return HttpResponse(render_response_index(request, template, c))
def index(request, experiment_id): try: experiment = Experiment.safe.get(request, experiment_id) except PermissionDenied: return return_response_error(request) except Experiment.DoesNotExist: return return_response_not_found(request) c = Context({ 'experiment': experiment, 'for_code_json': json.dumps(_get_for_codes(), sort_keys=True) }) if authz.has_write_permissions(request, experiment_id): template = 'anzsrc_codes/index.html' else: template = 'anzsrc_codes/index_ro.html' return HttpResponse(render_response_index(request, template, c))
def experiment_samples(request, experiment_id): """View a listing of dataset of an existing experiment as ajax loaded tab. :param request: a HTTP Request instance :type request: :class:`django.http.HttpRequest` :param experiment_id: the ID of the experiment to be edited :type experiment_id: string :param template_name: the path of the template to render :type template_name: string :rtype: :class:`django.http.HttpResponse` """ c = Context({'upload_complete_url': reverse('tardis.tardis_portal.views.upload_complete'), 'searchDatafileSelectionForm': getNewSearchDatafileSelectionForm(), }) try: experiment = Experiment.safe.get(request, experiment_id) except PermissionDenied: return return_response_error(request) except Experiment.DoesNotExist: return return_response_not_found(request) c['experiment'] = experiment if 'query' in request.GET: # We've been passed a query to get back highlighted results. # Only pass back matching datafiles # search_query = FacetFixedSearchQuery(backend=HighlightSearchBackend()) sqs = SearchQuerySet(query=search_query) query = SearchQueryString(request.GET['query']) facet_counts = sqs.raw_search(query.query_string() + ' AND experiment_id_stored:%i' % (int(experiment_id)), end_offset=1).facet('dataset_id_stored').highlight().facet_counts() if facet_counts: dataset_id_facets = facet_counts['fields']['dataset_id_stored'] else: dataset_id_facets = [] c['highlighted_datasets'] = [ int(f[0]) for f in dataset_id_facets ] c['file_matched_datasets'] = [] c['search_query'] = query # replace '+'s with spaces elif 'datafileResults' in request.session and 'search' in request.GET: c['highlighted_datasets'] = None c['highlighted_dataset_files'] = [r.pk for r in request.session['datafileResults']] c['file_matched_datasets'] = \ list(set(r.dataset.pk for r in request.session['datafileResults'])) c['search'] = True else: c['highlighted_datasets'] = None c['highlighted_dataset_files'] = None c['file_matched_datasets'] = None c['samples'] = \ Sample.objects.filter(experiment=experiment_id).order_by("id") c['datasets'] = \ Dataset.objects.filter(experiment=experiment_id) c['has_write_permissions'] = \ authz.has_write_permissions(request, experiment_id) c['protocol'] = [] download_urls = experiment.get_download_urls() for key, value in download_urls.iteritems(): c['protocol'] += [[key, value]] if 'status' in request.GET: c['status'] = request.GET['status'] if 'error' in request.GET: c['error'] = request.GET['error'] return HttpResponse(render_response_index(request, 'tardis_portal/ajax/experiment_samples.html', c))