Преглед изворни кода

Implement /search.json route (#27)

tags/v1.0^2
Ben Kurtovic пре 10 година
родитељ
комит
4dc83e913a
3 измењених фајлова са 32 додато и 18 уклоњено
  1. +27
    -11
      app.py
  2. +4
    -6
      bitshift/codelet.py
  3. +1
    -1
      bitshift/database/__init__.py

+ 27
- 11
app.py Прегледај датотеку

@@ -2,13 +2,13 @@
Module to contain all the project's Flask server plumbing. 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 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 = Flask(__name__)
app.config.from_object("bitshift.config") app.config.from_object("bitshift.config")
@@ -17,17 +17,33 @@ app_env = app.jinja_env
app_env.line_statement_prefix = "=" app_env.line_statement_prefix = "="
app_env.globals.update(assets=assets) app_env.globals.update(assets=assets)


# database = Database()
database = Database()


@app.route("/") @app.route("/")
def index(): def index():
return render_template("index.html", typeahead_languages=languages.LANGS) return render_template("index.html", typeahead_languages=languages.LANGS)


@app.route("/search/<query>")
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") @app.route("/about")
def about(): def about():


+ 4
- 6
bitshift/codelet.py Прегледај датотеку

@@ -1,5 +1,3 @@
import json

from .languages import LANGS from .languages import LANGS


__all__ = ["Codelet"] __all__ = ["Codelet"]
@@ -72,15 +70,15 @@ class Codelet(object):


def serialize(self): 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 :rtype: str
""" """
return json.dumps({
return {
"name": self.name, "code": self.code, "lang": LANGS[self.language], "name": self.name, "code": self.code, "lang": LANGS[self.language],
"authors": self.authors, "url": self.code_url, "authors": self.authors, "url": self.code_url,
"created": self.date_created.isoformat(), "created": self.date_created.isoformat(),
"modified": self.date_modified.isoformat(), "modified": self.date_modified.isoformat(),
"symbols": self.symbols, "origin": self.origin "symbols": self.symbols, "origin": self.origin
})
}

+ 1
- 1
bitshift/database/__init__.py Прегледај датотеку

@@ -63,7 +63,7 @@ class Database(object):
query, args = tree.build_query(page) query, args = tree.build_query(page)
cursor.execute(query, args) cursor.execute(query, args)
ids = [id for id, _ in cursor.fetchall()] ids = [id for id, _ in cursor.fetchall()]
num_results = 0 # TODO: NotImplemented
num_results = len(ids) # TODO: NotImplemented
return ids, num_results return ids, num_results


def _get_authors_for_codelet(self, cursor, codelet_id): def _get_authors_for_codelet(self, cursor, codelet_id):


Loading…
Откажи
Сачувај