Exemplo n.º 1
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})
Exemplo n.º 2
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})
Exemplo 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()
        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})
Exemplo 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

        # 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})