def choose_project(): sesion = create_session() if current_user.is_anonymous: last_prjct_id = request.cookies.get('last_project_id', None) if last_prjct_id: last_prjct_id = int(last_prjct_id) + 1 else: last_prjct_id = 1 else: last_prjct_id = request.cookies.get('last_project_id', None) if last_prjct_id: last_prjct_id = int(last_prjct_id) + 1 else: last_prjct_id = 1 project = sesion.query(Projects).get(last_prjct_id) try: max_id = sesion.query(func.max(Projects.id))[0][0] except: max_id = 2 s = select([ranked_table ]).where(ranked_table.c.user_id == current_user.id) conn = create_coon() ranked_projects = [row[1] for row in conn.execute(s)] while (not project or project.id in ranked_projects) and last_prjct_id <= max_id: last_prjct_id += 1 project = sesion.query(Projects).get(last_prjct_id) if not project: return None return (project, last_prjct_id)
def write_new_likes(): current_date = datetime.date.today() sesion = db_session.create_session() conn = db_session.create_coon() for project in sesion.query(Projects).all(): values = { 'rates_' + str(i): getattr(project, 'rates_' + str(i)) for i in range(1, 6) } print('values', values) values.update({ 'project_id': project.id, 'avg_rate': project.avg_rate, 'date': current_date }) ins = likes_in_day_table.insert().values(**values) conn.execute(ins) conn.close()
def add_tags_to_project(prj_id, tags): sesion = db_session.create_session() for tag_name in tags: tag = sesion.query(Tags).filter(Tags.interest == tag_name).first() if not tag: new_tag = Tags(interest=tag_name) sesion.add(new_tag) sesion.commit() last_id = sesion.query(func.max(Tags.id)).one()[0] last_id = last_id if last_id else 1 print('last tag id', last_id) else: last_id = tag.id print('Ths', prj_id, last_id) conn = db_session.create_coon() ins = project_tag_table.insert().values(project_id=prj_id, tag_id=last_id) conn.execute(ins) return
def add_to_already_ranked(id, rank): if current_user.is_authenticated: conn = create_coon() ins = ranked_table.insert().values(project_id=id, user_id=current_user.id, rank=rank) conn.execute(ins) sesion = create_session() c_prjct = sesion.query(Projects).get(id) if not c_prjct: return {'response': 404} rank_int = int(rank[-1]) rts = getattr(c_prjct, f'rates_{rank_int}') setattr(c_prjct, f'rates_{rank_int}', int(rts) + 1) if c_prjct.avg_rate == 0: c_prjct.avg_rate = rank_int else: c_prjct.avg_rate = (c_prjct.avg_rate * c_prjct.num_rates + rank_int) / ( 1 + c_prjct.num_rates) c_prjct.num_rates += 1 sesion.commit() return {'response': 200} return {'response': 403}