Exemplo n.º 1
0
def create_dataset(request):
    user = authorize(request)

    title = request.REQUEST.get('title','Untitled dataset')
    srid = int(request.REQUEST.get('srid', 4326))
    geometry_type=request.REQUEST.get('geometry_type', 'GEOMETRY')
    columns_definitions=json.loads(request.REQUEST.get('columns_definitions', "{}"))
    columns_definitions=((key, value) for key, value in columns_definitions.items())

    if 'parent' in request.REQUEST:
        parent = Page.objects.get(slug=request.REQUEST['parent'])
        authorize(request, parent, add=True)
    else:
        parent = get_data_page_for_user(request.user)

    ds = SpatialiteDriver.create_dataset(
        title=title,
        parent=parent,
        srid=srid,
        geometry_type=geometry_type,
        columns_definitions=columns_definitions,
        owner=request.user
    )

    dispatch.api_accessed.send(sender=DataResource, instance=ds, user=user)
    dispatch.dataset_created.send(sender=DataResource, instance=ds, user=user)
    return json_or_jsonp(request, {'path' : ds.slug }, code=201)
Exemplo n.º 2
0
def verify_rest_url(request):
    f = VerifyRestUrlForm(request.GET)
    if f.is_valid():
        params = f.cleaned_data
        url = params['rest_url']
        try:
            ts = requests.get(url)
            ts_xml = etree.XML(ts.text.encode('utf-8'))
            if 'timeSeriesResponse' in ts_xml.tag:
                return json_or_jsonp(request, ts.status_code)
            elif 'Collection' in ts_xml.tag:
                return json_or_jsonp(request, ts.status_code)
            else:
                return json_or_jsonp(request, 400)
        except:
            return json_or_jsonp(request, 400)
Exemplo n.º 3
0
def derive_dataset(request, slug):
    user = authorize(request)

    title = request.REQUEST.get('title', 'Untitled dataset')
    parent_dataresource=slug

    if 'parent_page' in request.REQUEST:
        parent_page = Page.objects.get(slug=request.REQUEST['parent_page'])
        authorize(request, parent_page, add=True)
    else:
        parent_page = get_data_page_for_user(request.user)

    parent_dataresource = DataResource.objects.get(slug=parent_dataresource)
    authorize(request, parent_dataresource, view=True)

    ds = SpatialiteDriver.derive_dataset(
        title=title,
        parent_page=parent_page,
        parent_dataresource=parent_dataresource,
        owner=request.user
    )

    dispatch.api_accessed.send(sender=DataResource, instance=ds, user=user)
    dispatch.dataset_created.send(sender=DataResource, instance=ds, user=user)
    return json_or_jsonp(request, {'path': ds.slug}, code=201)
Exemplo n.º 4
0
def full_schema(request, slug=None, *args, **kwargs):
    s = get_object_or_404(DataResource, slug=slug)
    user = authorize(request, s, view=True)

    r = [{'name': n, 'kind' : t} for n, t in s.driver_instance.full_schema().items()]
    dispatch.api_accessed.send(sender=DataResource, instance=s, user=user)
    return json_or_jsonp(request, r)
Exemplo n.º 5
0
def transform_file(request, shortkey, *args, **kwargs):
    res = hydroshare.get_resource_by_shortkey(shortkey)
    if res.reference_type == 'soap':
        client = Client(res.url)
        response = client.service.GetValues(':'+res.data_site_code, ':'+res.variable_code, '', '', '')
    elif res.reference_type == 'rest':
        r = requests.get(res.url)
        response = str(r.text)
    waterml_1 = etree.XML(response)
    wml_string = etree.tostring(waterml_1)
    s = StringIO(wml_string)
    dom = etree.parse(s)
    module_dir = os.path.dirname(__file__)
    xsl_location = os.path.join(module_dir, "static/ref_ts/xslt/WaterML1_1_timeSeries_to_WaterML2.xsl")
    xslt = etree.parse(xsl_location)
    transform = etree.XSLT(xslt)
    newdom = transform(dom)
    d = datetime.date.today()
    date = '{0}_{1}_{2}'.format(d.month, d.day, d.year)
    xml_name = '{0}-{1}-{2}'.format(res.title.replace(" ", ""), date, 'wml_2_0.xml')
    with open(xml_name, 'wb') as f:
        f.write(newdom)
    xml_file = open(xml_name, 'r')
    ResourceFile.objects.filter(object_id=res.pk, resource_file__contains='wml_2_0').delete()
    hydroshare.add_resource_files(res.short_id, xml_file)
    f = ResourceFile.objects.filter(object_id=res.pk, resource_file__contains='wml_2_0')[0].resource_file
    data = {
        'status_code': 200,
        'xml_name': xml_name,
        'xml_size': f.size,
        'xml_link': f.url
    }
    os.remove(xml_name)
    # print(etree.tostring(newdom, pretty_print=True))
    return json_or_jsonp(request, data)
Exemplo n.º 6
0
def get_rows(request,
             slug=None,
             ogc_fid_start=None,
             ogc_fid_end=None,
             limit=None,
             *args,
             **kwargs):
    ds = get_object_or_404(DataResource, slug=slug)
    user = authorize(request, ds, view=True)
    ds.driver_instance.ready_data_resource()
    format = request.REQUEST.get('format', 'wkt')

    if ogc_fid_end:
        rows = ds.driver_instance.get_rows(int(ogc_fid_start),
                                           int(ogc_fid_end),
                                           geometry_format=format)
    elif limit:
        rows = ds.driver_instance.get_rows(int(ogc_fid_start),
                                           limit=int(limit),
                                           geometry_format=format)
    else:
        rows = ds.driver_instance.get_rows(int(ogc_fid_start),
                                           geometry_format=format)

    dispatch.api_accessed.send(sender=DataResource, instance=ds, user=user)
    dispatch.features_retrieved.send(sender=DataResource,
                                     instance=ds,
                                     user=user,
                                     count=len(rows))
    return json_or_jsonp(request, rows)
Exemplo n.º 7
0
def schema(request, slug=None, *args, **kwargs):
    s = get_object_or_404(DataResource, slug=slug)
    user = authorize(request, s, view=True)

    r = [{'name': n} for n in s.driver_instance.schema()]
    dispatch.api_accessed.send(sender=DataResource, instance=s, user=user)
    return json_or_jsonp(request, r)
Exemplo n.º 8
0
    def get_resource_list(self):
        params = utils.create_form(GetResourceList.GetResourceListForm,
                                   self.request)
        if params.is_valid():
            r = params.cleaned_data
            if r['dc']:
                r['dc'] = json.loads(r['dc'])
            else:
                r['dc'] = {}

            ret = []
            resource_table = hydroshare.get_resource_list(**r)
            originator = get_user(self.request)

            for resources in resource_table:
                for r in filter(
                        lambda x: x.public or x.view_users.filter(
                            pk=originator.pk).exists() or x.view_groups.
                        filter(pk__in=[g.pk for g in originator.groups.all()]),
                        resources):
                    ret.append(r.short_id)

            return json_or_jsonp(self.request, ret)
        else:
            raise exceptions.ValidationError('invalid request')
Exemplo n.º 9
0
def derive_dataset(request, slug):
    user = authorize(request)

    title = request.REQUEST.get('title', 'Untitled dataset')
    parent_dataresource=slug

    if 'parent_page' in request.REQUEST:
        parent_page = Page.objects.get(slug=request.REQUEST['parent_page'])
        authorize(request, parent_page, add=True)
    else:
        parent_page = get_data_page_for_user(request.user)

    parent_dataresource = DataResource.objects.get(slug=parent_dataresource)
    authorize(request, parent_dataresource, view=True)

    ds = SpatialiteDriver.derive_dataset(
        title=title,
        parent_page=parent_page,
        parent_dataresource=parent_dataresource,
        owner=request.user
    )

    dispatch.api_accessed.send(sender=DataResource, instance=ds, user=user)
    dispatch.dataset_created.send(sender=DataResource, instance=ds, user=user)
    return json_or_jsonp(request, {'path': ds.slug}, code=201)
Exemplo n.º 10
0
def create_dataset(request):
    user = authorize(request)

    title = request.REQUEST.get('title','Untitled dataset')
    srid = int(request.REQUEST.get('srid', 4326))
    geometry_type=request.REQUEST.get('geometry_type', 'GEOMETRY')
    columns_definitions=json.loads(request.REQUEST.get('columns_definitions', "{}"))
    columns_definitions=((key, value) for key, value in columns_definitions.items())

    if 'parent' in request.REQUEST:
        parent = Page.objects.get(slug=request.REQUEST['parent'])
        authorize(request, parent, add=True)
    else:
        parent = get_data_page_for_user(request.user)

    ds = SpatialiteDriver.create_dataset(
        title=title,
        parent=parent,
        srid=srid,
        geometry_type=geometry_type,
        columns_definitions=columns_definitions,
        owner=request.user
    )

    dispatch.api_accessed.send(sender=DataResource, instance=ds, user=user)
    dispatch.dataset_created.send(sender=DataResource, instance=ds, user=user)
    return json_or_jsonp(request, {'path' : ds.slug }, code=201)
Exemplo n.º 11
0
def update_row(request, slug=None, ogc_fid=None, *args, **kwargs):
    ds = get_object_or_404(DataResource, slug=slug)
    user = authorize(request, ds, edit=True)

    schema = {k for k in ds.driver_instance.schema()}
    row = {k: v for k, v in request.REQUEST.items() if k in schema}
    try:
        payload = json.loads(request.body)
        for k in [x for x in payload if x in schema]:
            row[k] = payload[k]
    except:
        pass  # just in case there's JSON in the payuload

    if ogc_fid is None:
        ogc_fid = row['OGC_FID']

    bbox = GEOSGeometry(
        ds.driver_instance.get_row(int(ogc_fid),
                                   geometry_format='wkt')['GEOMETRY']).envelope
    result = ds.driver_instance.update_row(int(ogc_fid), **row)
    dispatch.api_accessed.send(sender=DataResource, instance=ds, user=user)
    dispatch.features_updated.send(sender=DataResource,
                                   instance=ds,
                                   user=user,
                                   count=1,
                                   fid=ogc_fid,
                                   bbox=bbox)
    return json_or_jsonp(request, result)
Exemplo n.º 12
0
    def get_revisions(self, pk):
        authorize(self.request, pk, view=True)

        js = {
            arrow.get(bag.timestamp).isoformat(): bag.bag.url
            for bag in hydroshare.get_revisions(pk)
        }
        return json_or_jsonp(self.request, js)
Exemplo n.º 13
0
def search_sites(request):
    f = ReferencedSitesForm(request.GET)
    if f.is_valid():
        params = f.cleaned_data
        url = params['wsdl_url']
        sites = ts_utils.sites_from_soap(url)

        return json_or_jsonp(request, sites)
Exemplo n.º 14
0
def search_variables(request):
    f = ReferencedVariablesForm(request.GET)
    if f.is_valid():
        params = f.cleaned_data
        url = params['wsdl_url']
        site = params['site']
        variables = ts_utils.site_info_from_soap(url, site=site)

        return json_or_jsonp(request, variables)
Exemplo n.º 15
0
    def list_group_members(self, pk):
        res = UserResource()
        bundles = []
        for u in hydroshare.list_group_members(pk):
            bundle = res.build_bundle(obj=u, request=self.request)
            bundles.append(res.full_dehydrate(bundle, for_list=True))
        list_json = res.serialize(None, bundles, "application/json")

        return json_or_jsonp(self.request, list_json)
Exemplo n.º 16
0
    def list_group_members(self, pk):
        res = UserResource()
        bundles = []
        for u in hydroshare.list_group_members(pk):
            bundle = res.build_bundle(obj=u, request=self.request)
            bundles.append(res.full_dehydrate(bundle, for_list=True))
        list_json = res.serialize(None, bundles, "application/json")

        return json_or_jsonp(self.request, list_json)
Exemplo n.º 17
0
def create_dataset_with_parent_geometry(request, slug):
    user = authorize(request)

    title = request.REQUEST.get('title', 'Untitled dataset')
    parent_dataresource = slug
    srid = int(request.REQUEST.get('srid', 4326))
    geometry_type = request.REQUEST.get('geometry_type', 'GEOMETRY')
    columns_definitions = json.loads(request.REQUEST.get('columns_definitions', "{}"))
    columns_definitions = ((key, value) for key, value in columns_definitions.items())
    parent_key = request.REQUEST.get('parent_key', None)
    child_key = request.REQUEST.get('child_key', None)
    csv = None

    if len(request.FILES.keys()) > 0:
        csvfile = NamedTemporaryFile(suffix='csv')
        csvfile.write(request.FILES[request.FILES.keys().next()].read())
        csvfile.flush()
        csv = pandas.DataFrame.from_csv(csvfile.name)

    if 'parent_page' in request.REQUEST:
        parent_page = Page.objects.get(slug=request.REQUEST['parent_page'])
        authorize(request, parent_page, add=True)
    else:
        parent_page = get_data_page_for_user(request.user)

    parent_dataresource = DataResource.objects.get(slug=parent_dataresource)
    authorize(request, parent_page, view=True)

    if csv:
        ds = SpatialiteDriver.join_data_with_existing_geometry(
            title=title,
            parent=parent_page,
            new_data=csv,
            join_field_in_existing_data=parent_key,
            join_field_in_new_data=child_key,
            parent_dataresource=parent_dataresource,
            srid=srid,
            geometry_type=geometry_type,
            owner=request.user
        )
    else:
        ds = SpatialiteDriver.create_dataset_with_parent_geometry(
            title=title,
            parent=parent_page,
            parent_dataresource=parent_dataresource,
            srid=srid,
            columns_definitions=columns_definitions,
            geometry_type=geometry_type,
            owner=request.user
        )

    dispatch.api_accessed.send(sender=DataResource, instance=ds, user=user)
    dispatch.dataset_created.send(sender=ds, user=user)
    return json_or_jsonp(request, {'path': ds.slug}, code=201)
Exemplo n.º 18
0
def create_dataset_with_parent_geometry(request, slug):
    user = authorize(request)

    title = request.REQUEST.get('title', 'Untitled dataset')
    parent_dataresource = slug
    srid = int(request.REQUEST.get('srid', 4326))
    geometry_type = request.REQUEST.get('geometry_type', 'GEOMETRY')
    columns_definitions = json.loads(request.REQUEST.get('columns_definitions', "{}"))
    columns_definitions = ((key, value) for key, value in columns_definitions.items())
    parent_key = request.REQUEST.get('parent_key', None)
    child_key = request.REQUEST.get('child_key', None)
    csv = None

    if len(request.FILES.keys()) > 0:
        csvfile = NamedTemporaryFile(suffix='csv')
        csvfile.write(request.FILES[request.FILES.keys().next()].read())
        csvfile.flush()
        csv = pandas.DataFrame.from_csv(csvfile.name)

    if 'parent_page' in request.REQUEST:
        parent_page = Page.objects.get(slug=request.REQUEST['parent_page'])
        authorize(request, parent_page, add=True)
    else:
        parent_page = get_data_page_for_user(request.user)

    parent_dataresource = DataResource.objects.get(slug=parent_dataresource)
    authorize(request, parent_page, view=True)

    if csv:
        ds = SpatialiteDriver.join_data_with_existing_geometry(
            title=title,
            parent=parent_page,
            new_data=csv,
            join_field_in_existing_data=parent_key,
            join_field_in_new_data=child_key,
            parent_dataresource=parent_dataresource,
            srid=srid,
            geometry_type=geometry_type,
            owner=request.user
        )
    else:
        ds = SpatialiteDriver.create_dataset_with_parent_geometry(
            title=title,
            parent=parent_page,
            parent_dataresource=parent_dataresource,
            srid=srid,
            columns_definitions=columns_definitions,
            geometry_type=geometry_type,
            owner=request.user
        )

    dispatch.api_accessed.send(sender=DataResource, instance=ds, user=user)
    dispatch.dataset_created.send(sender=ds, user=user)
    return json_or_jsonp(request, {'path': ds.slug}, code=201)
Exemplo n.º 19
0
    def add_resource_file(self, pk, filename=None):
        authorize(self.request, pk, edit=True)

        if filename:
            rf = self.request.FILES.values()[0]
            rf.name = filename
            f = hydroshare.add_resource_files(pk, rf)
            return HttpResponse(f.resource_file.url, content_type='text/plain')
        else:
            fs = hydroshare.add_resource_files(pk, self.request.FILES.values())
            return json_or_jsonp(self.request, { f.name: f.resource_file.url for f in fs})
Exemplo n.º 20
0
def get_his_urls(request):
    service_url = 'http://hiscentral.cuahsi.org/webservices/hiscentral.asmx/GetWaterOneFlowServiceInfo'
    r = requests.get(service_url)
    if r.status_code == 200:
        response = r.text.encode('utf-8')
        root = etree.XML(response)
    url_list = []
    for element in root.iter():
        if "servURL" in element.tag:
            url_list.append(element.text)
    return json_or_jsonp(request, url_list)
Exemplo n.º 21
0
def time_series_from_service(request):
    f = GetTSValuesForm(request.GET)
    if f.is_valid():
        params = f.cleaned_data
        ref_type = params['ref_type']
        url = params['service_url']
        site = params.get('site')
        variable = params.get('variable')
        if ref_type == 'rest':
            ts = ts_utils.time_series_from_service(url, ref_type)
        else:
            ts = ts_utils.time_series_from_service(url, ref_type, site_name_or_code=site, variable_code=variable)
        return json_or_jsonp(request, ts)
Exemplo n.º 22
0
    def add_resource_file(self, pk, filename=None):
        authorize(self.request, pk, edit=True)

        if filename:
            rf = self.request.FILES.values()[0]
            rf.name = filename
            f = hydroshare.add_resource_files(pk, rf)
            return HttpResponse(f.resource_file.url, content_type='text/plain')
        else:
            fs = hydroshare.add_resource_files(pk, self.request.FILES.values())
            return json_or_jsonp(self.request,
                                 {f.name: f.resource_file.url
                                  for f in fs})
Exemplo n.º 23
0
def get_row(request, slug=None, ogc_fid=None, *args, **kwargs):
    ds = get_object_or_404(DataResource, slug=slug)
    ds.driver_instance.ready_data_resource()
    user = authorize(request, ds, view=True)

    format = request.REQUEST.get('format', 'wkt')
    try:
        row = ds.driver_instance.get_row(int(ogc_fid), geometry_format=format)
    except:
        row = None

    dispatch.api_accessed.send(sender=DataResource, instance=ds, user=user)
    dispatch.features_retrieved.send(sender=DataResource, instance=ds, user=user, count=1, fid=ogc_fid)
    return json_or_jsonp(request, row)
Exemplo n.º 24
0
def get_row(request, slug=None, ogc_fid=None, *args, **kwargs):
    ds = get_object_or_404(DataResource, slug=slug)
    ds.driver_instance.ready_data_resource()
    user = authorize(request, ds, view=True)

    format = request.REQUEST.get('format', 'wkt')
    try:
        row = ds.driver_instance.get_row(int(ogc_fid), geometry_format=format if format != 'geojsonreal' else 'geojson')
    except:
        row = None

    dispatch.api_accessed.send(sender=DataResource, instance=ds, user=user)
    dispatch.features_retrieved.send(sender=DataResource, instance=ds, user=user, count=1, fid=ogc_fid)
    return json_or_jsonp(request, geojson_transform(request, row))
Exemplo n.º 25
0
    def list_groups(self):
        params = CreateOrListGroups.ListGroupsForm(self.request.REQUEST)
        if params.is_valid():
            r = params.cleaned_data
            res = UserResource()
            bundles = []
            for u in hydroshare.list_users(r['query'], r['status'], r['start'], r['count']):
                bundle = res.build_bundle(obj=u, request=self.request)
                bundles.append(res.full_dehydrate(bundle, for_list=True))
            list_json = res.serialize(None, bundles, "application/json")

            return json_or_jsonp(self.request, list_json)
        else:
            raise exceptions.ValidationError('invalid request')
Exemplo n.º 26
0
def get_rows(request, slug=None, ogc_fid_start=None, ogc_fid_end=None, limit=None, *args, **kwargs):
    ds = get_object_or_404(DataResource, slug=slug)
    user = authorize(request, ds, view=True)
    ds.driver_instance.ready_data_resource()
    format = request.REQUEST.get('format', 'wkt')

    if ogc_fid_end:
        rows = ds.driver_instance.get_rows(int(ogc_fid_start), int(ogc_fid_end), geometry_format=format if format != 'geojsonreal' else 'geojson')
    elif limit:
        rows = ds.driver_instance.get_rows(int(ogc_fid_start), limit=int(limit), geometry_format=format if format != 'geojsonreal' else 'geojson')
    else:
        rows = ds.driver_instance.get_rows(int(ogc_fid_start), geometry_format=format if format != 'geojsonreal' else 'geojson')

    dispatch.api_accessed.send(sender=DataResource, instance=ds, user=user)
    dispatch.features_retrieved.send(sender=DataResource, instance=ds, user=user, count=len(rows))
    return json_or_jsonp(request, geojson_transform(request, rows))
Exemplo n.º 27
0
    def list_groups(self):
        params = utils.create_form(CreateOrListGroups.ListGroupsForm,
                                   self.request)
        if params.is_valid():
            r = params.cleaned_data
            res = GroupResource()
            bundles = []
            for g in hydroshare.list_groups(r['query'], r['start'],
                                            r['count']):
                bundle = res.build_bundle(obj=g, request=self.request)
                bundles.append(res.full_dehydrate(bundle, for_list=True))
            list_json = res.serialize(None, bundles, "application/json")

            return json_or_jsonp(self.request, list_json)
        else:
            raise exceptions.ValidationError('invalid request')
Exemplo n.º 28
0
def query(request, slug=None, **kwargs):
    ds = get_object_or_404(DataResource, slug=slug)
    user = authorize(request, ds, view=True)

    ds.driver_instance.ready_data_resource()
    maybeint = lambda x: int(x) if x else None

    geometry_mbr = [float(kwargs[k]) for k in ['x1', 'y1', 'x2', 'y2']
                    ] if 'x1' in kwargs else None
    srid = kwargs['srid'] if 'srid' in kwargs else None
    geometry = request.REQUEST.get('g', None)
    geometry_format = request.REQUEST.get('format', 'geojson')
    geometry_operator = request.REQUEST.get('op', 'intersects')
    limit = maybeint(request.REQUEST.get('limit', None))
    start = maybeint(request.REQUEST.get('start', None))
    end = maybeint(request.REQUEST.get('end', None))
    only = request.REQUEST.get('only', None)
    if only:
        only = only.split(',')

    rest = {
        k: v
        for k, v in request.REQUEST.items()
        if k not in {'limit', 'start', 'end', 'only', 'g', 'op', 'format'}
    }

    rows = ds.driver_instance.query(query_mbr=geometry_mbr,
                                    query_geometry=geometry,
                                    geometry_format=geometry_format,
                                    geometry_operator=geometry_operator,
                                    query_geometry_srid=srid,
                                    limit=limit,
                                    start=start,
                                    end=end,
                                    only=only,
                                    **rest)

    dispatch.api_accessed.send(sender=DataResource, instance=ds, user=user)
    dispatch.features_retrieved.send(sender=DataResource,
                                     instance=ds,
                                     user=user,
                                     count=len(rows))
    return json_or_jsonp(request, rows)
Exemplo n.º 29
0
def add_row(request, slug=None, *args, **kwargs):
    ds = get_object_or_404(DataResource, slug=slug)
    user = authorize(request, ds, edit=True)

    schema = {k for k in ds.driver_instance.schema()}
    row = {k: v for k, v in request.REQUEST.items() if k in schema}

    try:
        payload = json.loads(request.body)
        for k in [x for x in payload if x in schema]:
            row[k] = payload[k]
    except:
        pass # just in case there's JSON in the payuload

    new_rec = ds.driver_instance.add_row(**row)
    bbox = GEOSGeometry(row['GEOMETRY']).envelope

    dispatch.api_accessed.send(sender=DataResource, instance=ds, user=user)
    dispatch.features_created.send(sender=DataResource, instance=ds, user=user, count=1, bbox=bbox)
    return json_or_jsonp(request, new_rec, code=201)
Exemplo n.º 30
0
def add_row(request, slug=None, *args, **kwargs):
    ds = get_object_or_404(DataResource, slug=slug)
    user = authorize(request, ds, edit=True)

    schema = {k for k in ds.driver_instance.schema()}
    row = {k: v for k, v in request.REQUEST.items() if k in schema}

    try:
        payload = json.loads(request.body)
        for k in [x for x in payload if x in schema]:
            row[k] = payload[k]
    except:
        pass # just in case there's JSON in the payuload

    new_rec = ds.driver_instance.add_row(**row)
    bbox = GEOSGeometry(row['GEOMETRY']).envelope

    dispatch.api_accessed.send(sender=DataResource, instance=ds, user=user)
    dispatch.features_created.send(sender=DataResource, instance=ds, user=user, count=1, bbox=bbox)
    return json_or_jsonp(request, new_rec, code=201)
Exemplo n.º 31
0
    def get_resource_list(self):
        params = ResourceCRUD.GetResourceListForm(self.request.REQUEST)
        if params.is_valid():
            r = params.cleaned_data
            if r['dc']:
                r['dc'] = json.loads(r['dc'])
            else:
                r['dc'] = {}

            ret = []
            resource_table = hydroshare.get_resource_list(**r)
            originator = get_user(self.request)

            for resources in resource_table:
                for r in filter(lambda x: authorize(self.request, x.short_id, view=True), resources):
                    ret.append(r.short_id)

            return json_or_jsonp(self.request, ret)
        else:
            raise exceptions.ValidationError('invalid request')
Exemplo n.º 32
0
def update_row(request, slug=None, ogc_fid=None, *args, **kwargs):
    ds = get_object_or_404(DataResource, slug=slug)
    user = authorize(request, ds, edit=True)

    schema = {k for k in ds.driver_instance.schema()}
    row = { k : v for k, v in request.REQUEST.items() if k in schema }
    try:
        payload = json.loads(request.body)
        for k in [x for x in payload if x in schema]:
            row[k] = payload[k]
    except:
        pass # just in case there's JSON in the payuload

    if ogc_fid is None:
        ogc_fid = row['OGC_FID']

    bbox = GEOSGeometry(ds.driver_instance.get_row(int(ogc_fid), geometry_format='wkt')['GEOMETRY']).envelope
    result = ds.driver_instance.update_row(int(ogc_fid), **row)
    dispatch.api_accessed.send(sender=DataResource, instance=ds, user=user)
    dispatch.features_updated.send(sender=DataResource, instance=ds, user=user, count=1, fid=ogc_fid, bbox=bbox)
    return json_or_jsonp(request, result)
Exemplo n.º 33
0
def query(request, slug=None, **kwargs):
    ds = get_object_or_404(DataResource, slug=slug)
    user = authorize(request, ds, view=True)

    ds.driver_instance.ready_data_resource()
    maybeint = lambda x: int(x) if x else None

    geometry_mbr = [float(kwargs[k]) for k in ['x1', 'y1', 'x2', 'y2']] if 'x1' in kwargs else None
    srid = kwargs['srid'] if 'srid' in kwargs else None
    geometry = request.REQUEST.get('g', None)
    geometry_format = request.REQUEST.get('format', 'geojson')
    geometry_operator = request.REQUEST.get('op', 'intersects')
    limit = maybeint(request.REQUEST.get('limit', None))
    start = maybeint(request.REQUEST.get('start', None))
    end = maybeint(request.REQUEST.get('end', None))
    only = request.REQUEST.get('only', None)
    if only:
        only = only.split(',')

    rest = {k: v for k, v in request.REQUEST.items() if
            k not in {'limit', 'start', 'end', 'only', 'g', 'op', 'format', 'api_key','callback','jsonp', '_'}}

    rows = ds.driver_instance.query(
        query_mbr=geometry_mbr,
        query_geometry=geometry,
        geometry_format=geometry_format if geometry_format != 'geojsonreal' else 'geojson',
        geometry_operator=geometry_operator,
        query_geometry_srid=srid,
        limit=limit,
        start=start,
        end=end,
        only=only,
        **rest
    )

    dispatch.api_accessed.send(sender=DataResource, instance=ds, user=user)
    dispatch.features_retrieved.send(sender=DataResource, instance=ds, user=user, count=len(rows))
    return json_or_jsonp(request, geojson_transform(request, rows))
Exemplo n.º 34
0
def create_resource_aggregation(request, *args, **kwargs):
    frm = CreateAggregationForm(request.POST)
    if frm.is_valid():
        dcterms = [
            { 'term': 'T', 'content': frm.cleaned_data['title']},
            { 'term': 'AB', 'content': frm.cleaned_data['abstract'] or frm.cleaned_data['title']},
            { 'term': 'DTS', 'content': now().isoformat()}
        ]
        for cn in frm.cleaned_data['contributors'].split(','):
            cn = cn.strip()
            dcterms.append({'term' : 'CN', 'content' : cn})
        for cr in frm.cleaned_data['creators'].split(','):
            cr = cr.strip()
            dcterms.append({'term' : 'CR', 'content' : cr})

        agg = hydroshare.create_resource(
            resource_type='ResourceAggregation',
            owner=request.user,
            title=frm.cleaned_data['title'],
            keywords=[k.strip() for k in frm.cleaned_data['keywords'].split(',')] if frm.cleaned_data['keywords'] else None,
            dublin_metadata=dcterms,
            content=frm.cleaned_data['abstract'] or frm.cleaned_data['title']
        )

        if frm.cleaned_data.get('resource_permalink'):
            hs_res_permalink = frm.cleaned_data.get('resource_permalink')
            trash, hs_res_shortkey = os.path.split(hs_res_permalink[:-1])
            hs_res = hydroshare.get_resource_by_shortkey(hs_res_shortkey)
            description = frm.cleaned_data.get('resource_description') or ''
            Resource.objects.create(resource_short_id=hs_res.short_id or hs_res_shortkey,
                                    resource_description=description,
                                    content_object=agg)

        if frm.cleaned_data.get('more'):
            data = {'agg_short_id': agg.short_id}
            return json_or_jsonp(request, data)

        return HttpResponseRedirect('/my-resources/')   # FIXME this will eventually need to change
Exemplo n.º 35
0
    def get_resource_list(self):
        params = utils.create_form(ResourceCRUD.GetResourceListForm,
                                   self.request)
        if params.is_valid():
            r = params.cleaned_data
            if r['dc']:
                r['dc'] = json.loads(r['dc'])
            else:
                r['dc'] = {}

            ret = []
            resource_table = hydroshare.get_resource_list(**r)
            originator = get_user(self.request)

            for resources in resource_table:
                for r in filter(
                        lambda x: authorize(
                            self.request, x.short_id, view=True), resources):
                    ret.append(r.short_id)

            return json_or_jsonp(self.request, ret)
        else:
            raise exceptions.ValidationError(params.errors)
Exemplo n.º 36
0
    def get_resource_list(self):
        params = GetResourceList.GetResourceListForm(self.request.REQUEST)
        if params.is_valid():
            r = params.cleaned_data
            if r['dc']:
                r['dc'] = json.loads(r['dc'])
            else:
                r['dc'] = {}

            ret = []
            resource_table = hydroshare.get_resource_list(**r)
            originator = get_user(self.request)

            for resources in resource_table:
                for r in filter(lambda x:
                                x.public or
                                x.view_users.filter(pk=originator.pk).exists() or
                                x.view_groups.filter(pk__in=[g.pk for g in originator.groups.all()]), resources):
                    ret.append(r.short_id)

            return json_or_jsonp(self.request, ret)
        else:
            raise exceptions.ValidationError('invalid request')
Exemplo n.º 37
0
def time_series_from_service(request):
    f = GetTSValuesForm(request.GET)
    if f.is_valid():
        params = f.cleaned_data
        ref_type = params['ref_type']
        url = params['service_url']
        site = params.get('site')
        variable = params.get('variable')
        global ts
        if ref_type == 'rest':
            ts = ts_utils.time_series_from_service(url, ref_type)
            site = ts.get('site_code')
            variable = ts.get('variable_code')
        else:
            ts = ts_utils.time_series_from_service(url, ref_type, site_name_or_code=site, variable_code=variable)
        for_graph = ts['for_graph']
        units = ts['units']
        variable_name = ts['variable_name']
        noDataValue = ts.get('noDataValue', None)
        ts['url'] = url
        ts['ref_type'] = ref_type
        vis_file = ts_utils.create_vis("theme/static/img/", site, for_graph, 'Date', variable_name, units, noDataValue)
        vis_file_name = os.path.basename(vis_file.name)
        return json_or_jsonp(request, {'vis_file_name': vis_file_name})
Exemplo n.º 38
0
    def get_revisions(self, pk):
        authorize(self.request, pk, view=True)

        js = {arrow.get(bag.timestamp).isoformat(): bag.bag.url for bag in hydroshare.get_revisions(pk) }
        return json_or_jsonp(self.request, js)
Exemplo n.º 39
0
    def get_resource_map(self, pk):
        authorize(self.request, pk, view=True)

        res = hydroshare.get_science_metadata(pk)
        return json_or_jsonp(self.request,
                             hydroshare.utils.serialize_resource_map(res))
Exemplo n.º 40
0
    def get_resource_map(self, pk):
        authorize(self.request, pk, view=True)

        res = hydroshare.get_capabilities(pk)
        return json_or_jsonp(self.request, res)
Exemplo n.º 41
0
    def get_revisions(self, pk):
        authorize(self.request, pk, view=True)

        js = {arrow.get(bag.timestamp).isoformat(): hydroshare.utils.current_site_url() + AbstractResource.bag_url(pk) for bag in hydroshare.get_revisions(pk) }
        return json_or_jsonp(self.request, js)
Exemplo n.º 42
0
def generate_files(request, shortkey, *args, **kwargs):
    res = hydroshare.get_resource_by_shortkey(shortkey)
    ts, csv_link, csv_size, xml_link, xml_size = {}, '', '', '', ''
    try:
        if res.reference_type == 'rest':
            ts = ts_utils.time_series_from_service(res.url, res.reference_type)
        else:
            ts = ts_utils.time_series_from_service(res.url,
                                               res.reference_type,
                                               site_name_or_code=res.data_site_code,
                                               variable_code=res.variable_code)

        vals = ts['values']
        version = ts['wml_version']
        d = datetime.date.today()
        date = '{0}_{1}_{2}'.format(d.month, d.day, d.year)
        file_base = '{0}-{1}'.format(res.title.replace(" ", ""), date)
        csv_name = '{0}.{1}'.format(file_base, 'csv')
        if version == '1':
            xml_end = 'wml_1'
            xml_name = '{0}-{1}.xml'.format(file_base, xml_end)
        elif version == '2.0':
            xml_end = 'wml_2_0'
            xml_name = '{0}-{1}.xml'.format(file_base, xml_end)
        for_csv = []
        for k, v in vals.items():
            t = (k, v)
            for_csv.append(t)
        ResourceFile.objects.filter(object_id=res.pk).delete()
        with open(csv_name, 'wb') as csv_file:
            w = csv.writer(csv_file)
            w.writerow([res.title])
            var = '{0}({1})'.format(ts['variable_name'], ts['units'])
            w.writerow(['time', var])
            for r in for_csv:
                w.writerow(r)
        with open(xml_name, 'wb') as xml_file:
            xml_file.write(ts['time_series'])
        csv_file = open(csv_name, 'r')
        xml_file = open(xml_name, 'r')
        files = [csv_file, xml_file]
        hydroshare.add_resource_files(res.short_id, csv_file, xml_file)
        create_bag(res)
        os.remove(csv_name)
        os.remove(xml_name)
        files = ResourceFile.objects.filter(object_id=res.pk)
        for f in files:
            if str(f.resource_file).endswith('.csv'):
                csv_link = f.resource_file.url
                csv_size = f.resource_file.size
            if xml_end in str(f.resource_file):
                xml_link = f.resource_file.url
                xml_size = f.resource_file.size
        status_code = 200
        data = {'for_graph': ts.get('for_graph'),
                'values': ts.get('values'),
                'units': ts.get('units'),
                'site_name': ts.get('site_name'),
                'variable_name': ts.get('variable_name'),
                'status_code': status_code,
                'csv_name': csv_name,
                'xml_name': xml_name,
                'csv_link': csv_link,
                'csv_size': csv_size,
                'xml_link': xml_link,
                'xml_size': xml_size}
        return json_or_jsonp(request, data)  # successfully generated new files
    except Exception:  # most likely because the server is unreachable
        files = ResourceFile.objects.filter(object_id=res.pk)
        xml_file = None
        for f in files:
            if str(f.resource_file).endswith('.csv'):
                csv_link = f.resource_file.url
                csv_size = f.resource_file.size
            if str(f.resource_file).endswith('.xml'):
                xml_link = f.resource_file.url
                xml_size = f.resource_file.size
                xml_file = f.resource_file
        if xml_file is None:
            status_code = 404
            data = {'for_graph': ts.get('for_graph'),
                    'values': ts.get('values'),
                    'units': ts.get('units'),
                    'site_name': ts.get('site_name'),
                    'variable_name': ts.get('variable_name'),
                    'status_code': status_code,
                    'csv_link': csv_link,
                    'csv_size': csv_size,
                    'xml_link': xml_link,
                    'xml_size': xml_size}
            return json_or_jsonp(request, data)  # did not generate new files, did not find old ones
        xml_doc = open(str(xml_file), 'r').read()
        root = etree.XML(xml_doc)
        os.remove(str(xml_file))
        version = ts_utils.get_version(root)
        if version == '1':
            ts = ts_utils.parse_1_0_and_1_1(root)
            status_code = 200
        elif version =='2.0':
            ts = ts_utils.parse_2_0(root)
            status_code = 200
        else:
            status_code = 503
        data = {'for_graph': ts.get('for_graph'),
                'values': ts.get('values'),
                'units': ts.get('units'),
                'site_name': ts.get('site_name'),
                'variable_name': ts.get('variable_name'),
                'status_code': status_code,
                'csv_link': csv_link,
                'csv_size': csv_size,
                'xml_link': xml_link,
                'xml_size': xml_size}
        return json_or_jsonp(request, data) # did not generate new files, return old ones
Exemplo n.º 43
0
def update_files(request, shortkey, *args, **kwargs):

    ts_utils.generate_files(shortkey, ts=None)
    status_code = 200
    return json_or_jsonp(request, status_code)  # successfully generated new files
Exemplo n.º 44
0
 def get_user_info(self, pk):
     return json_or_jsonp(self.request, hydroshare.get_user_info(pk))
Exemplo n.º 45
0
 def get_user_info(self, pk):
     return json_or_jsonp(self.request, hydroshare.get_user_info(pk))
Exemplo n.º 46
0
    def get_resource_map(self, pk):
        authorize(self.request, pk, view=True)

        res = hydroshare.get_capabilities(pk)
        return json_or_jsonp(self.request, res)
Exemplo n.º 47
0
    def get_resource_map(self, pk):
        authorize(self.request, pk, view=True)

        res = hydroshare.get_science_metadata(pk)
        return json_or_jsonp(
            self.request, hydroshare.utils.serialize_resource_map(res))
Exemplo n.º 48
0
def autocomplete(request):
    term = request.GET.get('term')
    resp = []

    types = [t for t in get_resource_types() if term.lower() in t.__name__.lower()]
    resp += [{'label': 'type', 'value': t.__name__, 'id': t.__name__} for t in types]

    # Party calculations are expensive and complicated. Deferring to focus on lower hanging fruit
    #
    parties = []
    def get_party_type(party):
        if Contributor.objects.filter(id=party.id).exists():
            return 'Contributor'
        elif Creator.objects.filter(id=party.id).exists():
            return 'Author'
        else:
            return None
    seen = set()
    filter_types = {
        'name': 'name__istartswith',
        'email': 'email__iexact',
    }
    for model in (Creator, Contributor):
        for filter_type in filter_types:
            for party in model.objects.filter(**{filter_types[filter_type]: term}):
                party_type = get_party_type(party)
                if party_type:
                    name = model.__name__
                    if model is Creator:
                        name = "Author"
                    if (name, party.name) not in seen:
                        seen.add((name, party.name))
                        resp.append({
                            'label': name,
                            'type': 'party',
                            'id': getattr(party, filter_type, 'id'),
                            'value': party.name,
                        })

    owners = User.objects.filter(username__istartswith=term)
    for owner in owners:
        if owner.first_name and owner.last_name:
            name = "%s %s (%s)" % (owner.first_name, owner.last_name, owner.username)
        elif owner.first_name:
            name = "%s (%s)" % (owner.first_name, owner.username)
        elif owner.last_name:
            name = "%s (%s)" % (owner.last_name, owner.username)
        else:
            name = owner.username
        resp.append({
            'label': 'Owner',
            'type': 'owner',
            'id': owner.username,
            'value': name,
        })

    subjects = Subject.objects.filter(value__istartswith=term)
    for subject in subjects:
        if ('subject', subject.value) not in seen:
            seen.add(('subject', subject.value))
            resp.append({
                'label': 'Subject',
                'type': 'subject',
                'id': subject.value,
                'value': subject.value,
            })

    # resources = get_resource_list(
    #     full_text_search=term,
    # )

    # todo: users
    # todo: groups
    # todo: other conditions?

    return json_or_jsonp(request, resp)