def autocomplete(): db_vals = [] search = request.args.get('search[term]') #app.logger.debug(search) db_vals += db_session.query(Recs).filter(Recs.artist.ilike('%' + search + '%')).all() db_vals += db_session.query(Recs).filter(Recs.title.ilike('%' + search + '%')).all() results = [] for v in db_vals[:LIMIT]: results.append({"value": "%s, %s" %(v.artist, v.title), "id" : v.track_id, "cluster" : v.cluster, "cluster_new" : v.cluster_new}) return Response(json.dumps(results))
def get_recs(cluster_inputs, page, sort, ascdesc): if len(cluster_inputs) < 1: app.logger.debug("No selected songs") results = [{"value": "No songs selected"}] return results else: lim = int(LIMIT/len(cluster_inputs)) db_vals = [] for c in cluster_inputs: c_old = c[0] c_new = c[1] init_len = len(db_vals) db_vals += db_session.query(Recs).filter(Recs.cluster==c_old, Recs.cluster_new==c_new, Recs.recommend==1) \ .order_by(getattr(getattr(Recs, sort), ascdesc)()) \ .slice(page*lim, page*lim+lim) mid_len = len(db_vals) #if not enough double match recs: if (mid_len - init_len) < lim: new_lim = lim - (mid_len - init_len) db_vals += db_session.query(Recs).filter(Recs.cluster!=c_old, Recs.cluster_new==c_new, Recs.recommend==1) \ .order_by(getattr(getattr(Recs, sort), ascdesc)()) \ .slice(page*new_lim, (page*new_lim+new_lim)) #if not enough new_rec recs: if (len(db_vals) - init_len) < lim: new_lim = lim - (len(db_vals) - init_len) db_vals += db_session.query(Recs).filter(Recs.cluster==c_old, Recs.cluster_new!=c_new, Recs.recommend==1) \ .order_by(getattr(getattr(Recs, sort), ascdesc)()) \ .slice(page*new_lim, (page*new_lim+new_lim)) #if not enough of either, repeat recs, but with random: if (len(db_vals) - init_len) < lim: new_lim = lim - (len(db_vals) - init_len) db_vals += db_session.query(Recs).filter((Recs.cluster==c_old) | (Recs.cluster_new==c_new), Recs.recommend==1) \ .order_by(func.random()).limit(new_lim) results = [] for v in db_vals: results.append({"value": "%s, %s" %(v.artist, v.title), "id" : v.track_id, "cluster" : v.cluster, "cluster_new": v.cluster_new}) return results