Kaynağa Gözat

Implement /search.json route (#27)

tags/v1.0^2
Ben Kurtovic 10 yıl önce
ebeveyn
işleme
4dc83e913a
3 değiştirilmiş dosya ile 32 ekleme ve 18 silme
  1. +27
    -11
      app.py
  2. +4
    -6
      bitshift/codelet.py
  3. +1
    -1
      bitshift/database/__init__.py

+ 27
- 11
app.py Dosyayı Görüntüle

@@ -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/<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")
def about():


+ 4
- 6
bitshift/codelet.py Dosyayı Görüntüle

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

+ 1
- 1
bitshift/database/__init__.py Dosyayı Görüntüle

@@ -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):


Yükleniyor…
İptal
Kaydet