Esempio n. 1
0
def index():
  # bypassing the Knowl objects to speed things up
  from knowl import get_knowls
  get_knowls().ensure_index('_keywords')
  keyword = request.args.get("search", "").lower()
  keywords = filter(lambda _:len(_) >= 3, keyword.split(" "))
  #logger.debug("keywords: %s" % keywords)
  keyword_q = {'_keywords' : { "$all" : keywords}}

  cur_cat = request.args.get("category", "")
  
  qualities = []
  defaults = "filter" not in request.args
  searchmode = "search" in request.args
  categorymode = "category" in request.args

  from knowl import knowl_qualities
  # TODO wrap this into a loop:
  reviewed = request.args.get("reviewed", "") == "on" or defaults or searchmode
  ok       = request.args.get("ok", "") == "on"       or defaults or searchmode
  beta     = request.args.get("beta", "") == "on"     or defaults or searchmode

  if reviewed: qualities.append("reviewed")
  if ok:       qualities.append("ok")
  if beta:     qualities.append("beta")

  quality_q = { '$in' : qualities }

  s_query = {}
  s_query['title'] = { "$exists" : True }
  if searchmode:
    s_query['quality'] = quality_q

  if categorymode:
    s_query['_id'] = { "$regex" : r"^%s\..+" % cur_cat }

  logger.debug("search query: %s" % s_query)
  knowls = get_knowls().find(s_query, fields=['title'])

  def first_char(k):
    t = k['title']
    if len(t) == 0: return "?"
    if t[0] not in string.ascii_letters: return "?"
    return t[0].upper()

  # way to additionally narrow down the search
  # def incl(knwl):
  #   if keyword in knwl['_id'].lower():   return True
  #   if keyword in knwl['title'].lower(): return True
  #   return False
  # if keyword: knowls = filter(incl, knowls)
  
  from knowl import get_categories 
  cats = get_categories()

  knowls = sorted(knowls, key = lambda x : x['title'].lower())
  from itertools import groupby
  knowls = groupby(knowls, first_char)
  return render_template("knowl-index.html", 
         title  = "Knowledge Database",
         bread  = get_bread(),
         knowls = knowls,
         search = keyword,
         navi_raw = searchbox(request.args.get("search", ""), searchmode),
         knowl_qualities = knowl_qualities,
         searchmode = searchmode,
         filters = (beta, ok, reviewed),
         categories = cats,
         cur_cat = cur_cat,
         categorymode = categorymode)
Esempio n. 2
0
def index():
    # bypassing the Knowl objects to speed things up
    from knowl import get_knowls
    get_knowls().ensure_index('_keywords')
    keyword = request.args.get("search", "").lower()
    keywords = filter(lambda _: len(_) >= 3, keyword.split(" "))
    #logger.debug("keywords: %s" % keywords)
    keyword_q = {'_keywords': {"$all": keywords}}

    cur_cat = request.args.get("category", "")

    qualities = []
    defaults = "filter" not in request.args
    searchmode = "search" in request.args
    categorymode = "category" in request.args

    from knowl import knowl_qualities
    # TODO wrap this into a loop:
    reviewed = request.args.get("reviewed",
                                "") == "on" or defaults or searchmode
    ok = request.args.get("ok", "") == "on" or defaults or searchmode
    beta = request.args.get("beta", "") == "on" or defaults or searchmode

    if reviewed: qualities.append("reviewed")
    if ok: qualities.append("ok")
    if beta: qualities.append("beta")

    quality_q = {'$in': qualities}

    s_query = {}
    s_query['title'] = {"$exists": True}
    if searchmode:
        s_query['quality'] = quality_q

    if categorymode:
        s_query['_id'] = {"$regex": r"^%s\..+" % cur_cat}

    logger.debug("search query: %s" % s_query)
    knowls = get_knowls().find(s_query, fields=['title'])

    def first_char(k):
        t = k['title']
        if len(t) == 0: return "?"
        if t[0] not in string.ascii_letters: return "?"
        return t[0].upper()

    # way to additionally narrow down the search
    # def incl(knwl):
    #   if keyword in knwl['_id'].lower():   return True
    #   if keyword in knwl['title'].lower(): return True
    #   return False
    # if keyword: knowls = filter(incl, knowls)

    from knowl import get_categories
    cats = get_categories()

    knowls = sorted(knowls, key=lambda x: x['title'].lower())
    from itertools import groupby
    knowls = groupby(knowls, first_char)
    return render_template("knowl-index.html",
                           title="Knowledge Database",
                           bread=get_bread(),
                           knowls=knowls,
                           search=keyword,
                           navi_raw=searchbox(request.args.get("search", ""),
                                              searchmode),
                           knowl_qualities=knowl_qualities,
                           searchmode=searchmode,
                           filters=(beta, ok, reviewed),
                           categories=cats,
                           cur_cat=cur_cat,
                           categorymode=categorymode)
Esempio n. 3
0
def render(ID, footer=None, kwargs = None):
  """
  this method renders the given Knowl (ID) to insert it
  dynamically in a website. It is intended to be used 
  by an AJAX call, but should do a similar job server-side
  only, too.

  Note, that the used knowl-render.html template is *not*
  based on any globally defined website and just creates
  a small and simple html snippet!
  """
  k = Knowl(ID)

  #logger.debug("kwargs: %s", request.args)
  kwargs = kwargs or dict(((k, v) for k,v in request.args.iteritems()))
  #logger.debug("kwargs: %s" , kwargs)

  #this is a very simple template based on no other template to render one single Knowl
  #for inserting into a website via AJAX or for server-side operations.
  if request.method == "POST":
    con = request.form['content']
    foot = footer or request.form['footer']
  elif request.method == "GET":
    con = request.args.get("content", k.content)
    foot = footer or request.args.get("footer", "1") 

  #authors = []
  #for a in k.author_links():
  #  authors.append("<a href='%s'>%s</a>" % 
  #    (url_for('users.profile', userid=a['_id']), a['full_name'] or a['_id'] ))
  #authors = ', '.join(authors)

  render_me = u"""\
  {%% include "knowl-defs.html" %%}
  {%% from "knowl-defs.html" import KNOWL with context %%}
  {%% from "knowl-defs.html" import KNOWL_LINK with context %%}
  {%% from "knowl-defs.html" import KNOWL_INC with context %%}
  {%% from "knowl-defs.html" import TEXT_DATA with context %%}

  <div class="knowl">
  <div class="knowl-content">%(content)s</div>"""
  if foot == "1": 
    render_me += """\
  <div class="knowl-footer">
    <a href="{{ url_for('.show', ID='%(ID)s') }}">permalink</a> 
    {%% if user_is_authenticated %%}
      &middot;
      <a href="{{ url_for('.edit', ID='%(ID)s') }}">edit</a> 
    {%% endif %%}
  </div>"""
  # """ &middot; Authors: %(authors)s """
  render_me += "</div>"
  # render_me = render_me % {'content' : con, 'ID' : k.id }
  # markdown enabled
  render_me = render_me % {'content' : md.convert(con), 'ID' : k.id } #, 'authors' : authors }
  # Pass the text on to markdown.  Note, backslashes need to be escaped for this, but not for the javascript markdown parser

  #logger.debug("rendering template string:\n%s" % render_me)

  # TODO wrap this string-rendering into a try/catch and return a proper error message
  # so that the user has a clue. Most likely, the {{ KNOWL('...') }} has the wrong syntax!
  logger.debug("kwargs: %s" % k.template_kwargs)
  return render_template_string(render_me, k = k, **kwargs)
Esempio n. 4
0
def render(ID, footer=None, kwargs=None):
    """
  this method renders the given Knowl (ID) to insert it
  dynamically in a website. It is intended to be used 
  by an AJAX call, but should do a similar job server-side
  only, too.

  Note, that the used knowl-render.html template is *not*
  based on any globally defined website and just creates
  a small and simple html snippet!
  """
    k = Knowl(ID)

    #logger.debug("kwargs: %s", request.args)
    kwargs = kwargs or dict(((k, v) for k, v in request.args.iteritems()))
    #logger.debug("kwargs: %s" , kwargs)

    #this is a very simple template based on no other template to render one single Knowl
    #for inserting into a website via AJAX or for server-side operations.
    if request.method == "POST":
        con = request.form['content']
        foot = footer or request.form['footer']
    elif request.method == "GET":
        con = request.args.get("content", k.content)
        foot = footer or request.args.get("footer", "1")

    #authors = []
    #for a in k.author_links():
    #  authors.append("<a href='%s'>%s</a>" %
    #    (url_for('users.profile', userid=a['_id']), a['full_name'] or a['_id'] ))
    #authors = ', '.join(authors)

    render_me = u"""\
  {%% include "knowl-defs.html" %%}
  {%% from "knowl-defs.html" import KNOWL with context %%}
  {%% from "knowl-defs.html" import KNOWL_LINK with context %%}
  {%% from "knowl-defs.html" import KNOWL_INC with context %%}
  {%% from "knowl-defs.html" import TEXT_DATA with context %%}

  <div class="knowl">
  <div class="knowl-content">%(content)s</div>"""
    if foot == "1":
        render_me += """\
  <div class="knowl-footer">
    <a href="{{ url_for('.show', ID='%(ID)s') }}">permalink</a> 
    {%% if user_is_authenticated %%}
      &middot;
      <a href="{{ url_for('.edit', ID='%(ID)s') }}">edit</a> 
    {%% endif %%}
  </div>"""
    # """ &middot; Authors: %(authors)s """
    render_me += "</div>"
    # render_me = render_me % {'content' : con, 'ID' : k.id }
    # markdown enabled
    render_me = render_me % {
        'content': md.convert(con),
        'ID': k.id
    }  #, 'authors' : authors }
    # Pass the text on to markdown.  Note, backslashes need to be escaped for this, but not for the javascript markdown parser

    #logger.debug("rendering template string:\n%s" % render_me)

    # TODO wrap this string-rendering into a try/catch and return a proper error message
    # so that the user has a clue. Most likely, the {{ KNOWL('...') }} has the wrong syntax!
    logger.debug("kwargs: %s" % k.template_kwargs)
    return render_template_string(render_me, k=k, **kwargs)