Beispiel #1
0
    def open_csv(self):
        # get the CSV file.
        # csv_url = datasetrevision.end_point
        # el API no tiene IOC para cargar al request con el bucket_name
        from core.lib.datastore import active_datastore
        import urllib

        account = self.account
        bucket_name = account.get_preference(
            'account_bucket_name') if account.get_preference(
                'account_bucket_name') else settings.AWS_BUCKET_NAME

        datasetrevision = self.datasetrevision
        # detect local vs URL
        end_point = datasetrevision.end_point
        if end_point.find('file://') == 0:
            self.csv_url = active_datastore.build_url(
                bucket_name, datasetrevision.end_point.replace("file://", ""))
        else:
            self.csv_url = end_point

        logger.info('OPEN CSV. url:{}'.format(self.csv_url))
        # guardar el archivo con un nombre vinculado a su URL para no repetir esta descarga
        # en procesos de paginado
        import base64, os.path, tempfile
        # full /tmp disk filename = os.path.join(tempfile.gettempdir(), base64.b64encode(self.csv_url))
        filename = os.path.join('/mnt/all', base64.b64encode(self.csv_url))

        exists = os.path.isfile(filename)
        self.csv_local_file = filename
        logger.info('OPEN CSV check file:{} ({})'.format(filename, exists))

        if exists:
            return self.csv_local_file

        try:
            # csv_file = urllib2.urlopen(csv_url)
            self.csv_local_file, headers = urllib.urlretrieve(
                self.csv_url, self.csv_local_file)
            logger.info('OPEN CSV. local:{}'.format(self.csv_local_file))
        except Exception, e:
            logger.error(e)
            return None
Beispiel #2
0
    def open_csv(self):
        # get the CSV file.
        # csv_url = datasetrevision.end_point
        # el API no tiene IOC para cargar al request con el bucket_name
        from core.lib.datastore import active_datastore
        import urllib
        
        account = self.account
        bucket_name = account.get_preference('account_bucket_name') if account.get_preference('account_bucket_name') else settings.AWS_BUCKET_NAME

        datasetrevision = self.datasetrevision
        # detect local vs URL
        end_point = datasetrevision.end_point
        if end_point.find('file://') == 0:
            self.csv_url = active_datastore.build_url(bucket_name, datasetrevision.end_point.replace("file://", ""))
        else:
            self.csv_url = end_point
            
        logger.info('OPEN CSV. url:{}'.format(self.csv_url))
        # guardar el archivo con un nombre vinculado a su URL para no repetir esta descarga
        # en procesos de paginado
        import base64, os.path, tempfile
        # full /tmp disk filename = os.path.join(tempfile.gettempdir(), base64.b64encode(self.csv_url))
        filename = os.path.join('/mnt/all', base64.b64encode(self.csv_url))
        
        exists = os.path.isfile(filename)
        self.csv_local_file = filename
        logger.info('OPEN CSV check file:{} ({})'.format(filename, exists))
        
        if exists:
            return self.csv_local_file
            
        try:
            # csv_file = urllib2.urlopen(csv_url)
            self.csv_local_file, headers = urllib.urlretrieve(self.csv_url, self.csv_local_file)
            logger.info('OPEN CSV. local:{}'.format(self.csv_local_file))
        except Exception, e:
            logger.error(e)
            return None
Beispiel #3
0
@require_http_methods(["GET"])
def download(request, dataset_id, slug):
    """ download dataset file directly """
    logger = logging.getLogger(__name__)

    # get public url for datastream id
    try:
        dataset_revision_id = DatasetRevision.objects.get_last_published_id(dataset_id)
        dataset = DatasetDBDAO().get(request.auth_manager.language, dataset_revision_id=dataset_revision_id)
    except Exception, e:
        logger.info("Can't find the dataset: %s [%s]" % (dataset_id, str(e)))
        raise Http404
    else:
        filename = dataset['filename'].encode('utf-8')
        # ensure it's a downloadable file on S3
        if dataset['end_point'][:7] != "file://":
            return HttpResponse("No downloadable file!")

        url = active_datastore.build_url(
            request.bucket_name,
            dataset['end_point'].replace("file://", ""),
            {'response-content-disposition': 'attachment; filename={0}'.format(filename)}
        )
                
        content_type = settings.CONTENT_TYPES.get(settings.IMPL_TYPES.get(dataset['impl_type']))
        redirect = HttpResponse(status=302, mimetype=content_type)
        redirect['Location'] = url

        return redirect
Beispiel #4
0
    # get public url for datastream id
    try:
        dataset_revision_id = Dataset.objects.get(
            pk=dataset_id).last_published_revision.id
        dataset = DatasetDBDAO().get(request.user,
                                     dataset_revision_id=dataset_revision_id)
    except Exception, e:
        logger.info("Can't find the dataset: %s [%s]" % (dataset_id, str(e)))
        raise Http404
    else:
        filename = dataset['filename'].encode('utf-8')
        # ensure it's a downloadable file on S3
        if dataset['end_point'][:7] != "file://":
            return HttpResponse("No downloadable file!")

        url = active_datastore.build_url(
            request.bucket_name, dataset['end_point'].replace("file://", ""))

        impl_type = settings.IMPL_TYPES.get(str(dataset['impl_type']))
        content_type = settings.CONTENT_TYPES.get(impl_type)
        if settings.DEBUG:
            logger.info(
                'Dataset download %s -- %s -- %s -- %s -- %s' %
                (filename, content_type, url, dataset['impl_type'], impl_type))
        """ no funciona asi 
        redirect = HttpResponse(status=302, content_type=content_type)
        redirect['Location'] = url
        redirect['Content-Disposition'] = 'attachment; filename="{0}"'.format(filename)
        """

        redirect = HttpResponse(
            content_type=content_type