Ejemplo n.º 1
0
    def _getPPData(self,
                   startDatetime,
                   endDatetime,
                   platform,
                   xParm,
                   yParm,
                   pvDict={},
                   returnIDs=False,
                   sampleFlag=True):
        '''
        Get Parameter-Parameter data regardless if Parameters are 'Sampled' or 'Measured in situ'
        '''
        # Use same query builder that the STOQS UI uses
        request = HttpRequest
        request.META = {'dbAlias': self.args.database}
        pq = PQuery(request)
        pq.logger.setLevel(logging.ERROR)
        if self.args.verbose > 2:
            pq.logger.setLevel(logging.DEBUG)

        args = ()
        kwargs = {
            'time': (startDatetime.strftime('%Y-%m-%d %H:%M:%S'),
                     endDatetime.strftime('%Y-%m-%d %H:%M:%S')),
            'platforms': (platform, ),
            'parameterparameter': [
                Parameter.objects.using(self.args.database).get(name=xParm).id,
                Parameter.objects.using(self.args.database).get(name=yParm).id
            ],
            'parametervalues': [pvDict]
        }

        px, py = kwargs['parameterparameter']

        pq.buildPQuerySet(*args, **kwargs)

        pp = ParameterParameter(request, {'x': px, 'y': py}, None, pq, {})
        pp.logger.setLevel(logging.ERROR)
        if self.args.verbose > 2:
            pp.logger.setLevel(logging.DEBUG)

        points = []
        try:
            pp._getXYCData(strideFlag=False,
                           latlonFlag=True,
                           returnIDs=returnIDs,
                           sampleFlag=sampleFlag)
        except PPDatabaseException, e:
            if platform or startDatetime or endDatetime:
                raise NoPPDataException(
                    "No (%s, %s) data from (%s) between %s and %s" %
                    (xParm, yParm, platform, startDatetime, endDatetime))
            else:
                raise NoPPDataException("No (%s, %s) data returned" %
                                        (xParm, yParm))
Ejemplo n.º 2
0
    def _getPPData(self, startDatetime, endDatetime, platform, xParm, yParm, pvDict={}, returnIDs=False, sampleFlag=True):
        '''
        Get Parameter-Parameter data regardless if Parameters are 'Sampled' or 'Measured in situ'
        '''
        # Use same query builder that the STOQS UI uses
        request = HttpRequest
        request.META = {'dbAlias': self.args.database}
        pq = PQuery(request)
        pq.logger.setLevel(logging.ERROR)
        if self.args.verbose > 2:
            pq.logger.setLevel(logging.DEBUG)

        args = ()
        kwargs = {  'parameterparameter': [ Parameter.objects.using(self.args.database).get(name=xParm).id,
                                            Parameter.objects.using(self.args.database).get(name=yParm).id ],
                    'parametervalues': [pvDict]
                 }

        if startDatetime and endDatetime:
            kwargs['time'] = (startDatetime.strftime('%Y-%m-%d %H:%M:%S'), endDatetime.strftime('%Y-%m-%d %H:%M:%S'))
        if platform is not None:
            kwargs['platforms'] = (platform,)

        px, py  = kwargs['parameterparameter']

        pq.buildPQuerySet(*args, **kwargs)

        pp = ParameterParameter(kwargs, request, {'x': px, 'y': py}, None, pq, {})
        pp.logger.setLevel(logging.ERROR)
        if self.args.verbose > 2:
            pp.logger.setLevel(logging.DEBUG)

        points = []
        try:
            pp._getXYCData(strideFlag=False, latlonFlag=True, returnIDs=returnIDs, sampleFlag=sampleFlag)
        except PPDatabaseException:
            if platform or startDatetime or endDatetime:
                raise NoPPDataException("No (%s, %s) data from (%s) between %s and %s" % (xParm, yParm, platform, startDatetime, endDatetime))
            else:
                raise NoPPDataException("No (%s, %s) data returned" % (xParm, yParm))

        for lon, lat in zip(pp.lon, pp.lat):
            points.append(Point(lon, lat))

        if returnIDs:
            return pp.x_id, pp.y_id, pp.x, pp.y, points
        else:
            return pp.x, pp.y, points
Ejemplo n.º 3
0
    def process_request(self):
        '''
        Default request processing: Apply any query parameters and get fields for the values.  Respond with requested format.
        '''
        fields = self.getFields()
        geomFields = self.getGeomFields()
        self.assign_qs()

        if self.stoqs_object_name == 'measured_parameter':
            # Check if the query contains parametervalue constraints, in which case we need to wrap RawQuerySet in an MPQuerySet
            pvConstraints = self.parameterValueConstraints()
            if pvConstraints:
                mpq = MPQuery(self.request)
                pq = PQuery(self.request)
                sql = postgresifySQL(str(self.qs.query))
                sql = pq.addParameterValuesSelfJoins(
                    sql, pvConstraints, select_items=MPQuery.rest_select_items)
                self.qs = MPQuerySet(sql, MPQuerySet.rest_columns)
            else:
                self.qs = MPQuerySet(None,
                                     MPQuerySet.rest_columns,
                                     qs_mp=self.qs)

        # Process request based on format requested
        if self.format == 'csv' or self.format == 'tsv':
            response = HttpResponse()
            if self.format == 'tsv':
                response['Content-type'] = 'text/tab-separated-values'
                response[
                    'Content-Disposition'] = 'attachment; filename=%s.tsv' % self.stoqs_object_name
                writer = csv.writer(response, delimiter='\t')
            else:
                response['Content-type'] = 'text/csv'
                response[
                    'Content-Disposition'] = 'attachment; filename=%s.csv' % self.stoqs_object_name
                writer = csv.writer(response)
            writer.writerow(fields)
            for obj in self.qs:
                writer.writerow([obj[f] for f in fields])

            return response
        elif self.format == 'xml':
            return HttpResponse(serializers.serialize('xml', self.query_set),
                                'application/xml')

        elif self.format == 'json':
            return HttpResponse(
                simplejson.dumps(self.qs, cls=encoders.STOQSJSONEncoder),
                'application/json')

        elif self.format == 'kml':
            kml = KML(self.request, self.qs, self.qparams,
                      self.stoqs_object_name)
            return kml.kmlResponse()

        elif self.format == 'kmln':
            kml = KML(self.request,
                      self.qs,
                      self.qparams,
                      self.stoqs_object_name,
                      withTimeStamps=False,
                      withLineStrings=False,
                      withFullIconURL=False)
            return kml.kmlResponse()

        elif self.format == 'count':
            count = self.qs.count()
            logger.debug('count = %d', count)
            return HttpResponse('%d' % count, mimetype='text/plain')

        elif self.format == 'help':
            helpText = 'Fields: %s\n\nField Lookups: %s' % (fields,
                                                            self.fieldLookups)
            if geomFields:
                helpText += '\n\nSpatial and distance Lookups that may be appended to: %s\n\n%s\n\n%s' % (
                    geomFields, self.distanceLookups, self.spatialLookups)
            helpText += '\n\nResponses: %s' % (self.responses, )
            response = HttpResponse(helpText, mimetype="text/plain")
            return response

        else:
            self.build_html_template()
            response = render_to_response(
                self.html_tmpl_file, {
                    'cols': fields,
                    'google_analytics_code': settings.GOOGLE_ANALYTICS_CODE
                },
                context_instance=RequestContext(self.request))
            fh = open(self.html_tmpl_path, 'w')
            for line in response:
                fh.write(line)
            fh.close()
            return render_to_response(self.html_tmpl_path, {'list': self.qs})
Ejemplo n.º 4
0
    def process_request(self):
        '''
        Default request processing: Apply any query parameters and get fields for the values.  Respond with requested format.
        '''
        fields = self.getFields()
        geomFields = self.getGeomFields()
        try:
            self.assign_qs()
        except EmptyQuerySetException:
            raise Http404

        if self.stoqs_object_name == 'measured_parameter':
            # Check if the query contains parametervalue constraints, in which case we need to wrap RawQuerySet in an MPQuerySet
            pvConstraints = self.parameterValueConstraints()
            if pvConstraints:
                pq = PQuery(self.request)
                sql = postgresifySQL(str(self.qs.query))
                sql = pq.addParameterValuesSelfJoins(sql, pvConstraints, select_items=MPQuery.rest_select_items)
                self.qs = MPQuerySet(self.request.META['dbAlias'], sql, MPQuerySet.rest_columns)
            else:
                self.qs = MPQuerySet(self.request.META['dbAlias'], None, MPQuerySet.rest_columns, qs_mp=self.qs)

        # Process request based on format requested
        if self.format == 'csv' or self.format == 'tsv':
            response = HttpResponse()
            if self.format == 'tsv':
                response['Content-type'] = 'text/tab-separated-values'
                response['Content-Disposition'] = 'attachment; filename=%s.tsv' % self.stoqs_object_name
                writer = csv.writer(response, delimiter='\t')
            else:
                response['Content-type'] = 'text/csv'
                response['Content-Disposition'] = 'attachment; filename=%s.csv' % self.stoqs_object_name
                writer = csv.writer(response)
            writer.writerow(fields)
            for obj in self.qs:
                writer.writerow([obj[f] for f in fields])

            return response
        elif self.format == 'xml':
            return HttpResponse(serializers.serialize('xml', self.query_set), 'application/xml')

        elif self.format == 'json':
            return HttpResponse(json.dumps(self.qs, cls=encoders.STOQSJSONEncoder), 'application/json')

        elif self.format == 'kml':
            kml = KML(self.request, self.qs, self.qparams, self.stoqs_object_name)
            return kml.kmlResponse()

        elif self.format == 'kmln':
            kml = KML(self.request, self.qs, self.qparams, self.stoqs_object_name, withTimeStamps=False, withLineStrings=False, withFullIconURL=False)
            return kml.kmlResponse()

        elif self.format == 'count':
            count = self.qs.count()
            logger.debug('count = %d', count)
            return HttpResponse('%d' % count, content_type='text/plain')

        elif self.format == 'help':
            helpText = 'Fields: %s\n\nField Lookups: %s' % (fields, self.fieldLookups)
            if geomFields:
                helpText += '\n\nSpatial and distance Lookups that may be appended to: %s\n\n%s\n\n%s' % (geomFields, 
                            self.distanceLookups, self.spatialLookups)
            helpText += '\n\nResponses: %s' % (self.responses,)
            response = HttpResponse(helpText, content_type="text/plain")
            return response

        else:
            self.build_html_template()
            try:
                response = render_to_response(self.html_tmpl_file, {'cols': fields, 
                                                'google_analytics_code': settings.GOOGLE_ANALYTICS_CODE },
                                                context_instance = RequestContext(self.request))
            except AttributeError:
                response = render_to_response(self.html_tmpl_file, {'cols': fields}, 
                                                context_instance = RequestContext(self.request))
            fh = open(self.html_tmpl_path, 'w')
            for line in response:
                fh.write(line)
            fh.close()
            return render_to_response(self.html_tmpl_path, {'list': self.qs})
Ejemplo n.º 5
0
    def process_request(self):
        """
        Default request processing: Apply any query parameters and get fields for the values.  Respond with requested format.
        """
        fields = self.getFields()
        geomFields = self.getGeomFields()
        try:
            self.assign_qs()
        except EmptyQuerySetException:
            raise Http404

        if self.stoqs_object_name == "measured_parameter":
            # Check if the query contains parametervalue constraints, in which case we need to wrap RawQuerySet in an MPQuerySet
            pvConstraints = self.parameterValueConstraints()
            if pvConstraints:
                mpq = MPQuery(self.request)
                pq = PQuery(self.request)
                sql = postgresifySQL(str(self.qs.query))
                sql = pq.addParameterValuesSelfJoins(sql, pvConstraints, select_items=MPQuery.rest_select_items)
                self.qs = MPQuerySet(self.request.META["dbAlias"], sql, MPQuerySet.rest_columns)
            else:
                self.qs = MPQuerySet(self.request.META["dbAlias"], None, MPQuerySet.rest_columns, qs_mp=self.qs)

        # Process request based on format requested
        if self.format == "csv" or self.format == "tsv":
            response = HttpResponse()
            if self.format == "tsv":
                response["Content-type"] = "text/tab-separated-values"
                response["Content-Disposition"] = "attachment; filename=%s.tsv" % self.stoqs_object_name
                writer = csv.writer(response, delimiter="\t")
            else:
                response["Content-type"] = "text/csv"
                response["Content-Disposition"] = "attachment; filename=%s.csv" % self.stoqs_object_name
                writer = csv.writer(response)
            writer.writerow(fields)
            for obj in self.qs:
                writer.writerow([obj[f] for f in fields])

            return response
        elif self.format == "xml":
            return HttpResponse(serializers.serialize("xml", self.query_set), "application/xml")

        elif self.format == "json":
            return HttpResponse(json.dumps(self.qs, cls=encoders.STOQSJSONEncoder), "application/json")

        elif self.format == "kml":
            kml = KML(self.request, self.qs, self.qparams, self.stoqs_object_name)
            return kml.kmlResponse()

        elif self.format == "kmln":
            kml = KML(
                self.request,
                self.qs,
                self.qparams,
                self.stoqs_object_name,
                withTimeStamps=False,
                withLineStrings=False,
                withFullIconURL=False,
            )
            return kml.kmlResponse()

        elif self.format == "count":
            count = self.qs.count()
            logger.debug("count = %d", count)
            return HttpResponse("%d" % count, content_type="text/plain")

        elif self.format == "help":
            helpText = "Fields: %s\n\nField Lookups: %s" % (fields, self.fieldLookups)
            if geomFields:
                helpText += "\n\nSpatial and distance Lookups that may be appended to: %s\n\n%s\n\n%s" % (
                    geomFields,
                    self.distanceLookups,
                    self.spatialLookups,
                )
            helpText += "\n\nResponses: %s" % (self.responses,)
            response = HttpResponse(helpText, content_type="text/plain")
            return response

        else:
            self.build_html_template()
            response = render_to_response(
                self.html_tmpl_file,
                {"cols": fields, "google_analytics_code": settings.GOOGLE_ANALYTICS_CODE},
                context_instance=RequestContext(self.request),
            )
            fh = open(self.html_tmpl_path, "w")
            for line in response:
                fh.write(line)
            fh.close()
            return render_to_response(self.html_tmpl_path, {"list": self.qs})
Ejemplo n.º 6
0
    def process_request(self):
        '''
        Default request processing: Apply any query parameters and get fields for the values.  Respond with requested format.
        '''
        fields = self.getFields()
        geomFields = self.getGeomFields()
        try:
            self.assign_qs()
        except EmptyQuerySetException:
            raise Http404

        # A terrible hack to add latitude and longitude columns to the response - must call following assign_qs()
        fields = self.add_lon_lat_cols()

        if self.stoqs_object_name == 'measured_parameter':
            # Check if the query contains parametervalue constraints, in which case we need to wrap RawQuerySet in an MPQuerySet
            pvConstraints = self.parameterValueConstraints()
            if pvConstraints:
                pq = PQuery(self.request)
                sql = postgresifySQL(str(self.qs.query))
                sql = pq.addParameterValuesSelfJoins(
                    sql, pvConstraints, select_items=MPQuery.rest_select_items)
                self.qs = MPQuerySet(self.request.META['dbAlias'], sql,
                                     MPQuerySet.rest_columns)
            else:
                self.qs = MPQuerySet(self.request.META['dbAlias'],
                                     None,
                                     MPQuerySet.rest_columns,
                                     qs_mp=self.qs)

        # Process request based on format requested
        if self.format == 'csv' or self.format == 'tsv':
            response = HttpResponse()
            if self.format == 'tsv':
                response['Content-type'] = 'text/tab-separated-values'
                response[
                    'Content-Disposition'] = 'attachment; filename=%s.tsv' % self.stoqs_object_name
                writer = csv.writer(response, delimiter='\t')
            else:
                response['Content-type'] = 'text/csv'
                response[
                    'Content-Disposition'] = 'attachment; filename=%s.csv' % self.stoqs_object_name
                writer = csv.writer(response)
            writer.writerow(fields)
            for obj in self.qs:
                writer.writerow(self.row_of_fields(obj))

            return response
        elif self.format == 'xml':
            return HttpResponse(serializers.serialize('xml', self.query_set),
                                'application/xml')

        elif self.format == 'json':
            return HttpResponse(
                json.dumps(self.qs, cls=encoders.STOQSJSONEncoder),
                'application/json')

        elif self.format == 'kml':
            kml = KML(self.request, self.qs, self.qparams,
                      self.stoqs_object_name)
            return kml.kmlResponse()

        elif self.format == 'kmln':
            kml = KML(self.request,
                      self.qs,
                      self.qparams,
                      self.stoqs_object_name,
                      withTimeStamps=False,
                      withLineStrings=False,
                      withFullIconURL=False)
            return kml.kmlResponse()

        elif self.format == 'count':
            count = self.qs.count()
            logger.debug('count = %d', count)
            return HttpResponse('%d' % count, content_type='text/plain')

        elif self.format == 'help':
            helpText = 'Fields: %s\n\nField Lookups: %s' % (fields,
                                                            self.fieldLookups)
            if geomFields:
                helpText += '\n\nSpatial and distance Lookups that may be appended to: %s\n\n%s\n\n%s' % (
                    geomFields, self.distanceLookups, self.spatialLookups)
            helpText += '\n\nResponses: %s' % (self.responses, )
            response = HttpResponse(helpText, content_type="text/plain")
            return response

        else:
            self.build_html_template()
            try:
                response = render(self.request,
                                  self.html_tmpl_file,
                                  context={
                                      'cols':
                                      fields,
                                      'google_analytics_code':
                                      settings.GOOGLE_ANALYTICS_CODE
                                  })
            except AttributeError:
                response = render(self.request,
                                  self.html_tmpl_file,
                                  context={'cols': fields})
            fh = open(self.html_tmpl_path, 'w')
            for line in response:
                # Override Django's default datetime formatting with ISO 8601 format that includes seconds
                # STOQS model field names ending in 'timevalue' and 'date' are convertable datetimes
                # See: https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#date
                line = line.decode("utf-8").replace('timevalue }',
                                                    'timevalue|date:"c" }')
                line = line.replace('date }', 'date|date:"c" }')
                fh.write(line)
            fh.close()
            return render(self.request,
                          self.html_tmpl_path,
                          context={'list': self.qs})