Esempio n. 1
0
def _item_to_restaurant_result(item):
  d = dynamodb.item_to_dict(item)
  imagepath = '{0}{1}/{2}'.format(
      dynamodb.get_env('IMAGE_SERVER_PREFIX'),
      dynamodb.get_env('IMAGE_SERVER'),
      item.get('path'))
  return {
    'name': d.get('name_en', ''),
    'path': imagepath if d.has_key('path') else '', 
  }
Esempio n. 2
0
def _suggest_result(suggest_type, param):
  ''' return suggest json structure '''
  if not param.has_key('prefix'):
    return []
  prefix = param['prefix']
  # search here

  tab = dynamodb.get_table(suggest_type)
  items = tab.scan(name_en__beginswith=prefix) 
  scanned = [ dynamodb.item_to_dict(item) for item in items ]

  candidates = sorted(scanned, cmp=lambda a,b: cmp(a['name_en'], b['name_en']))[0:5]
  cand_list = []
  cand_list = [ c['name_en'] for c in candidates ]

  return cand_list
Esempio n. 3
0
def _search_dish(params):
  tab = dynamodb.get_table('dishes')
  keywords = params.get('keyword', '')
  itemfrom = int(params.get('itemfrom', '0'))
  itemto = int(params.get('itemto', '10'))

  ks = filter(lambda s: s not in ['', None], keywords.split(','))

  allitems = [ dynamodb.item_to_dict(item) for item in tab.scan() ]


  def _to_cmp(d, else_value=None):
    # match names
    name = d.get('name_en', '') 
    if name in ks:
      return 0
    # complete equal: tags
    ts = d.get('tags', '').split(',')
    count = 0
    for keyword in ks:
      if keyword in ts:
        count = count + 1
    if count > 0:
      return 1 + 50 - int(float(count) / float(len(ts)) * 50)

   # imcomplete equals 
    count = 0
    for keyword in ks:
      if name.find(keyword) >= 0:
        return 100
      for tag in ts:
        if tag.find(keyword) >= 0:
          count = count + 1
    if count > 0:
      return 100 + len(ts) - count
    # not matched
    return else_value
  
  results = []
  for dish in allitems:
    cmpvalue = _to_cmp(dish)
    if cmpvalue is None:
      continue
    dish['_cmp'] = cmpvalue
    results.append(dish)
  
  cmpfunc = lambda a,b: cmp( (a['_cmp'], a.get('name_en')), (b['_cmp'], b.get('name_en')))
  ordered = []
  setnames = set()
  for item in sorted(results, cmp=cmpfunc):
    if item.get('name_en') in setnames:
      continue
    name = item.get('name_en', '')
    if name is '':
      continue
    ordered.append(item)
    setnames.add(name)
  
  return {
    'result': [_dish_to_json_dict(itemfrom + idx, item) for (idx, item) in enumerate(ordered[itemfrom:itemto]) ],
    'count': len(ordered[itemfrom:itemto]),
    'beginindex': itemfrom,
    'total': len(ordered),
  }