diff --git a/app.py b/app.py index 92949bd..20d8997 100644 --- a/app.py +++ b/app.py @@ -2,13 +2,13 @@ Module to contain all the project's Flask server plumbing. """ -from flask import Flask -from flask import render_template, session +from json import dumps + +from flask import Flask, make_response, render_template, request from bitshift import assets -from bitshift import languages -# from bitshift.database import Database -# from bitshift.query import parse_query +from bitshift.database import Database +from bitshift.query import parse_query, QueryParseException app = Flask(__name__) app.config.from_object("bitshift.config") @@ -17,17 +17,33 @@ app_env = app.jinja_env app_env.line_statement_prefix = "=" app_env.globals.update(assets=assets) -# database = Database() +database = Database() @app.route("/") def index(): return render_template("index.html", typeahead_languages=languages.LANGS) -@app.route("/search/") -def search(query): - # tree = parse_query(query) - # database.search(tree) - pass +@app.route("/search.json") +def search(): + def reply(json): + resp = make_response(dumps(json)) + resp.mimetype = "application/json" + return resp + + query, page = request.args.get("q"), request.args.get("p", 1) + if not query: + return reply({"error": "No query given"}) + try: + tree = parse_query(query) + except QueryParseException as exc: + return reply({"error": exc.args[0]}) + try: + page = int(page) + except ValueError: + return reply({"error": u"Invalid page number: %s" % page}) + count, codelets = database.search(tree, page) + results = [clt.serialize() for clt in codelets] + return reply({"count": count, "results": results}) @app.route("/about") def about(): diff --git a/bitshift/codelet.py b/bitshift/codelet.py index 8897249..865ae52 100644 --- a/bitshift/codelet.py +++ b/bitshift/codelet.py @@ -1,5 +1,3 @@ -import json - from .languages import LANGS __all__ = ["Codelet"] @@ -72,15 +70,15 @@ class Codelet(object): def serialize(self): """ - Convert the codelet into a JSON string representation for the frontend. + Convert the codelet into a dictionary that can be sent as JSON. - :return: The JSON codelet representation. + :return: The codelet as a dictionary. :rtype: str """ - return json.dumps({ + return { "name": self.name, "code": self.code, "lang": LANGS[self.language], "authors": self.authors, "url": self.code_url, "created": self.date_created.isoformat(), "modified": self.date_modified.isoformat(), "symbols": self.symbols, "origin": self.origin - }) + } diff --git a/bitshift/database/__init__.py b/bitshift/database/__init__.py index e4fa430..1e49a8d 100644 --- a/bitshift/database/__init__.py +++ b/bitshift/database/__init__.py @@ -63,7 +63,7 @@ class Database(object): query, args = tree.build_query(page) cursor.execute(query, args) ids = [id for id, _ in cursor.fetchall()] - num_results = 0 # TODO: NotImplemented + num_results = len(ids) # TODO: NotImplemented return ids, num_results def _get_authors_for_codelet(self, cursor, codelet_id):