Exemplo n.º 1
0
def add_def():
  word, pos = parse_request()
  data = storage.get_def(word)
  if not data:
    data = []
  d = json.loads(request.data)
  if not d or "def" not in d:
    return json.dumps({
      "status": "error",
      "message":"Incorrect or no Data sent"
    })
  def_data = {
    "id": str(uuid.uuid4()),
    "def": d["def"].strip(),
    "html": markdown.markdown(d["def"], safe_mode='escape'),
    "last_touch": datetime.now().isoformat()
    }
  if pos is None or pos == "":
    data.append(def_data)
  else:
    def_data["id"] = pos
    found = False
    for d, i in zip(data, range(len(data))):
      if d["id"] == pos:
        data[i] = def_data
        found = True
        break
    if not found:
      return json.dumps({
        "status": "error",
        "message": "the def '%s' was not found" % pos 
      })

  storage.set_def(word, data)
  return json.dumps({"status": "worked"})
Exemplo n.º 2
0
def single_word_def():
  word, pos = parse_request()
  data = storage.get_def(word)
  if not data:
    logging.debug("LOOK UP FAILED: %s" % word)
    storage.failed_lookup(word)
    return json.dumps({
      "status": "error",
      "message": "Coudn't find '%s'" % (word)
    })
  if not pos:
    logging.debug("LOOK UP WORKED: %s" % word)
    return json.dumps({
      "status": "worked",
      "word": word,
      "defs": data
    });
  result = None
  for d in data:
    if d["id"] == pos:
      result = d
      break
  if not result:
    logging.debug("LOOK UP WITH POS FAILED: %s (%s)" % (word, pos))
    return json.dumps({
      "status": "error",
      "message": "Coudn't find '%s' with def '%s'" % (word, pos)
    })
  logging.debug("LOOK UP WORKED: %s (%s)" % (word, pos))
  return json.dumps({
    "status": "worked",
    "word": word,
    "def": result
  });
Exemplo n.º 3
0
def add_def():
    word, pos = parse_request()
    data = storage.get_def(word)
    if not data:
        data = []
    d = json.loads(request.data)
    if not d or "def" not in d:
        return json.dumps({
            "status": "error",
            "message": "Incorrect or no Data sent"
        })
    def_data = {
        "id": str(uuid.uuid4()),
        "def": d["def"].strip(),
        "html": markdown.markdown(d["def"], safe_mode='escape'),
        "last_touch": datetime.now().isoformat()
    }
    if pos is None or pos == "":
        data.append(def_data)
    else:
        def_data["id"] = pos
        found = False
        for d, i in zip(data, range(len(data))):
            if d["id"] == pos:
                data[i] = def_data
                found = True
                break
        if not found:
            return json.dumps({
                "status": "error",
                "message": "the def '%s' was not found" % pos
            })

    storage.set_def(word, data)
    return json.dumps({"status": "worked"})
Exemplo n.º 4
0
def del_def():
  word, pos = parse_request()
  data = storage.get_def(word)
  if not data:
    return json.dumps({
      "status": "error",
      "message":"word is already deleted"
    })
  for i in range(len(data)):
    d = data[i]
    if d["id"] == pos:
      del data[i]
      break
  if len(data) == 0:
    storage.del_def(word)
  else:
    storage.set_def(word, data)
  return json.dumps({"status": "worked"})
Exemplo n.º 5
0
def del_def():
    word, pos = parse_request()
    data = storage.get_def(word)
    if not data:
        return json.dumps({
            "status": "error",
            "message": "word is already deleted"
        })
    for i in range(len(data)):
        d = data[i]
        if d["id"] == pos:
            del data[i]
            break
    if len(data) == 0:
        storage.del_def(word)
    else:
        storage.set_def(word, data)
    return json.dumps({"status": "worked"})
Exemplo n.º 6
0
def single_word_def():
    word, pos = parse_request()
    data = storage.get_def(word)
    if not data:
        logging.debug("LOOK UP FAILED: %s" % word)
        storage.failed_lookup(word)
        return json.dumps({
            "status": "error",
            "message": "Coudn't find '%s'" % (word)
        })
    if not pos:
        logging.debug("LOOK UP WORKED: %s" % word)
        return json.dumps({
            "status": "worked",
            "word": word,
            "defs": data
        })
    result = None
    for d in data:
        if d["id"] == pos:
            result = d
            break
    if not result:
        logging.debug("LOOK UP WITH POS FAILED: %s (%s)" % (word, pos))
        return json.dumps({
            "status":
            "error",
            "message":
            "Coudn't find '%s' with def '%s'" % (word, pos)
        })
    logging.debug("LOOK UP WORKED: %s (%s)" % (word, pos))
    return json.dumps({
        "status": "worked",
        "word": word,
        "def": result
    })