Exemple #1
0
    def execute(self):
        method = self.query['method']

        
        if isinstance(self.query['search_limits'],list):
            if self.query['method'] not in ["json","return_json"]:
                self.query['search_limits'] = self.query['search_limits'][0]
            else:
                return self.multi_execute()
        
        if method=="return_json" or method=="json":
            frame = self.data()
            return self.return_json()

        if method=="return_tsv" or method=="tsv":
            import csv
            frame = self.data()
            return frame.to_csv(sep="\t",encoding="utf8",index=False,quoting=csv.QUOTE_NONE,escapechar="\\")

        if method=="return_pickle" or method=="DataFrame":
            frame = self.data()
            from cPickle import dumps as pickleDumps
            return pickleDumps(frame,protocol=-1)

        # Temporary catch-all pushes to the old methods:
        if method in ["returnPossibleFields","search_results","return_books"]:
            query = userquery(self.query)
            if method=="return_books":
                return query.execute()
            return json.dumps(query.execute())
    def execute(self):

        method = self.query['method']
        fmt = self.query['format'] if 'format' in self.query else False
        version = 2 if method == 'data' else 1

        # What to do with multiple search_limits
        if isinstance(self.query['search_limits'], list):
            if version == 2 and fmt == "json":
                return self.multi_execute(version=version)
            elif version == 1 and method in ["json", "return_json"]:
                return self.multi_execute(version=version)
            else:
                # Only return first search limit if not return in json
                self.query['search_limits'] = self.query['search_limits'][0]

        if version == 1:
            form = method[7:] if method[:6] == 'return' else method
            logging.warn("method == \"%s\" is deprecated. Use method=\"data\" "
                         "with format=\"%s\" instead." % (method, form))

            if method == "return_json" or method == "json":
                return self.return_json(version=1)

            elif method == "return_tsv" or method == "tsv":
                import csv
                frame = self.data()
                return frame.to_csv(sep="\t",
                                    encoding="utf8",
                                    index=False,
                                    quoting=csv.QUOTE_NONE,
                                    escapechar="\\")

            elif method == "return_pickle" or method == "DataFrame":
                frame = self.data()
                from cPickle import dumps as pickleDumps
                return pickleDumps(frame, protocol=-1)

        elif version == 2:
            if fmt == "json":
                return self.return_json(version=2)
            else:
                err = dict(status="error",
                           code="200",
                           message="Only format=json currently supported")
                return json.dumps(err)

        # Temporary catch-all pushes to the old methods:
        if method in [
                "returnPossibleFields", "search_results", "return_books"
        ]:
            query = userquery(self.query)
            if method == "return_books":
                return query.execute()
            return json.dumps(query.execute())
Exemple #3
0
    def generate_pandas_frame(self, call):
        """

        This is good example of the query that actually fetches the results.
        It creates some SQL, runs it, and returns it as a pandas DataFrame.
        
        The actual SQL production is handled by the userquery class, which uses more
        legacy code.

        """
        con = DbConnect(prefs, self.query['database'])
        q = userquery(call).query()
        df = read_sql(q, con.db)
        return df
    def generate_pandas_frame(self, call):
        """

        This is good example of the query that actually fetches the results.
        It creates some SQL, runs it, and returns it as a pandas DataFrame.

        The actual SQL production is handled by the userquery class, which uses
        more legacy code.

        """
        con = DbConnect(prefs, self.query["database"])
        q = userquery(call).query()
        df = read_sql(q, con.db)
        return df
    def execute(self):

        method = self.query['method']
        fmt = self.query['format'] if 'format' in self.query else False
        version = 2 if method == 'data' else 1

        # What to do with multiple search_limits
        if isinstance(self.query['search_limits'], list):
            if version == 2 and fmt == "json":
                return self.multi_execute(version=version)
            elif version == 1 and method in ["json", "return_json"]:
                return self.multi_execute(version=version)
            else:
                # Only return first search limit if not return in json
                self.query['search_limits'] = self.query['search_limits'][0]

        if version == 1:
            form = method[7:] if method[:6] == 'return' else method
            logging.warn("method == \"%s\" is deprecated. Use method=\"data\" "
                         "with format=\"%s\" instead." % (method, form))

            if method == "return_json" or method == "json":
                return self.return_json(version=1)

            elif method == "return_tsv" or method == "tsv":
                import csv
                frame = self.data()
                return frame.to_csv(sep="\t", encoding="utf8", index=False,
                                    quoting=csv.QUOTE_NONE, escapechar="\\")

            elif method == "return_pickle" or method == "DataFrame":
                frame = self.data()
                from cPickle import dumps as pickleDumps
                return pickleDumps(frame, protocol=-1)

        elif version == 2:
            if fmt == "json":
                return self.return_json(version=2)
            else:
                err = dict(status="error", code="200",
                           message="Only format=json currently supported")
                return json.dumps(err)

        # Temporary catch-all pushes to the old methods:
        if method in ["returnPossibleFields", "search_results",
                      "return_books"]:
            query = userquery(self.query)
            if method == "return_books":
                return query.execute()
            return json.dumps(query.execute())
Exemple #6
0
    def execute(self):

        method = self.query['method']

        if isinstance(self.query['search_limits'], list):
            if self.query['method'] not in ["json", "return_json"]:
                self.query['search_limits'] = self.query['search_limits'][0]
            else:
                return self.multi_execute()

        if method == "return_json" or method == "json":
            frame = self.data()
            return self.return_json()

        if method == "return_tsv" or method == "tsv":
            import csv
            frame = self.data()
            return frame.to_csv(sep="\t",
                                encoding="utf8",
                                index=False,
                                quoting=csv.QUOTE_NONE,
                                escapechar="\\")

        if method == "return_pickle" or method == "DataFrame":
            frame = self.data()
            from cPickle import dumps as pickleDumps
            return pickleDumps(frame, protocol=-1)

        # Temporary catch-all pushes to the old methods:
        if method in [
                "returnPossibleFields", "search_results", "return_books"
        ]:
            query = userquery(self.query)
            if method == "return_books":
                return query.execute()
            return json.dumps(query.execute())
    def execute(self):

        method = self.query["method"]
        fmt = self.query["format"] if "format" in self.query else False
        version = 2 if method == "data" else 1

        if version == 1:
            # What to do with multiple search_limits
            if isinstance(self.query["search_limits"], list):
                if method in ["json", "return_json"]:
                    return self.multi_execute(version=version)
                else:
                    # Only return first search limit if not return in json
                    self.query["search_limits"] = self.query["search_limits"][0]

            form = method[7:] if method[:6] == "return" else method
            logging.warn(
                'method == "%s" is deprecated. Use method="data" ' 'with format="%s" instead.' % (method, form)
            )

            if method == "return_json" or method == "json":
                return self.return_json(version=1)

            elif method == "return_tsv" or method == "tsv":
                import csv

                frame = self.data()
                return frame.to_csv(sep="\t", encoding="utf8", index=False, quoting=csv.QUOTE_NONE, escapechar="\\")

            elif method == "return_pickle" or method == "DataFrame":
                frame = self.data()
                from cPickle import dumps as pickleDumps

                return pickleDumps(frame, protocol=-1)

        elif version == 2:
            try:
                # What to do with multiple search_limits
                if isinstance(self.query["search_limits"], list):
                    if fmt == "json":
                        return self.multi_execute(version=version)
                    else:
                        # Only return first search limit if not return in json
                        self.query["search_limits"] = self.query["search_limits"][0]

                if fmt == "json":
                    return self.return_json(version=2)
                else:
                    err = dict(status="error", code=200, message="Only format=json currently supported")
                    return json.dumps(err)
            except BookwormException as e:
                # Error status codes are HTTP codes
                # http://www.restapitutorial.com/httpstatuscodes.html
                err = e.args[0]
                err["status"] = "error"
                return json.dumps(err)
            except:
                # General Uncaught error.
                logging.exception("Database error")
                return json.dumps({"status": "error", "message": "Database error. " "Try checking field names."})

        # Temporary catch-all pushes to the old methods:
        if method in ["returnPossibleFields", "search_results", "return_books"]:
            try:
                query = userquery(self.query)
                if method == "return_books":
                    return query.execute()
                return json.dumps(query.execute())
            except:
                return "General error"
Exemple #8
0
    def execute(self):

        method = self.query['method']
        fmt = self.query['format'] if 'format' in self.query else False
        version = 2 if method == 'data' else 1

        if version == 1:
            # What to do with multiple search_limits
            if isinstance(self.query['search_limits'], list):
                if method in ["json", "return_json"]:
                    return self.multi_execute(version=version)
                else:
                    # Only return first search limit if not return in json
                    self.query['search_limits'] = self.query['search_limits'][0]

            form = method[7:] if method[:6] == 'return' else method
            logging.warn("method == \"%s\" is deprecated. Use method=\"data\" "
                         "with format=\"%s\" instead." % (method, form))

            if method == "return_json" or method == "json":
                return self.return_json(version=1)

            elif method == "return_tsv" or method == "tsv":
                import csv
                frame = self.data()
                return frame.to_csv(sep="\t", encoding="utf8", index=False,
                                    quoting=csv.QUOTE_NONE, escapechar="\\")

            elif method == "return_pickle" or method == "DataFrame":
                frame = self.data()
                from cPickle import dumps as pickleDumps
                return pickleDumps(frame, protocol=-1)

        elif version == 2:
            try:
                # What to do with multiple search_limits
                if isinstance(self.query['search_limits'], list):
                    if fmt == "json":
                        return self.multi_execute(version=version)
                    else:
                        # Only return first search limit if not return in json
                        self.query['search_limits'] = self.query['search_limits'][0]

                if fmt == "json":
                    return self.return_json(version=2)
                else:
                    err = dict(status="error", code=200,
                               message="Only format=json currently supported")
                    return json.dumps(err)
            except BookwormException as e:
                # Error status codes are HTTP codes
                # http://www.restapitutorial.com/httpstatuscodes.html
                err = e.args[0]
                err['status'] = "error"
                return json.dumps(err)
            except:
                # General Uncaught error.
                logging.exception("Database error")
                return json.dumps({"status": "error", "message": "Database error. "
                               "Try checking field names."})

        # Temporary catch-all pushes to the old methods:
        if method in ["returnPossibleFields", "search_results",
                      "return_books"]:
                try:
                    query = userquery(self.query)
                    if method == "return_books":
                        return query.execute()
                    return json.dumps(query.execute())
                except Exception, e:
                    if len(str(e)) > 1 and e[1].startswith("Unknown database"):
                        return "No such bookworm {}".format(e[1].replace("Unknown database",""))
                except: