示例#1
0
def _get_restaurant(params, else_value=None):
  tab = dynamodb.get_table('restaurant')
  name = params.get('name', '')
  id = str(hashlib.sha1(name).hexdigest())
  if not tab.has_item(id=id):
    return else_value
  item = tab.get_item(id=id)

  return {
    'name': item['name_en'],
    'path': item['path'],
  }
示例#2
0
def _restaurant_namelist(params):
  dishes = dynamodb.get_table('dishes')
  restaurant_name = params.get('keyword')
  if restaurant_name is None:
    return []

  ds = dishes.scan(restaurant__beginswith=restaurant_name)
  nameset = set()
  for item in ds:
    name = item.get('restaurant')
    if name is None or not name.startswith(restaurant_name):
      continue
    nameset.add(name)
  return list(nameset)
示例#3
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
示例#4
0
def _search_restaurantlist(names, page=None, limit=9):
  page = 1 if page is None else page
  starts = (page - 1) * limit
  ends = page * limit
  keys = map(lambda s: str(hashlib.sha1(s).hexdigest()), names[starts:ends])

  results = []
  tab = dynamodb.get_table('restaurant')
  for key in keys:
    if tab.has_item(id=key):
      results.append(tab.get_item(id=key))

  return {
    'page': page,
    'limit': limit,
    'restaurants': [ _item_to_restaurant_result(item) for item in results ],
  }
示例#5
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),
  }