def update_likes(): # user_id = 5 user_id = current_user.get_id() if not user_id: user_id = 3 #default # content_id = 7 # content_id도 받아와야함! conn2 = sqlite3.connect('bongsa.db') cur2 = conn2.cursor() data = request.form['like_checked'] content_id = request.form['content_id'] if data == 'true': # db에 등록 execute_stm = "INSERT INTO likes VALUES (?, ?)" cur2.execute(execute_stm, (str(content_id), str(user_id))) conn2.commit() elif data == 'false': # db에서 빼기 execute_stm = "DELETE FROM likes WHERE user_id=" + str( user_id) + " and content_id=" + str(content_id) cur2.execute(execute_stm) conn2.commit() conn2.close() # db 연결 해제 if len(data) != 0: # db에 없을 때 javascript에 전달할 말 return json.dumps({'status': 'OK'}) else: # db에 있을 때 javascript에 전달할 말 return json.dumps({"status": 'error'})
def blog_detail(content_id): # db 연결 user_id = current_user.get_id() if not user_id: user_id = 3 #default conn = sqlite3.connect('bongsa.db') cur = conn.cursor() # query cur.execute("select * from volunteer where id=" + content_id) # content_id에 해당하는 컨텐츠 가져오기 # data fetch row = cur.fetchone() category_names, category_receivers, category_tags = classification( row[8:-5]) # 봉사대상, 봉사분류 if len(category_receivers) == 0: category_receivers = '전체시민' if len(category_names) == 0: category_names = '' recruit_date = row[5][4:6] + '/' + row[5][6:8] + '/' + row[5][:4] # if session.user.id , session.content.id가 일치하는 데이터가 db에 있으면, likes에 미리 checked! # + 좋아요 표시를 누르면 db에 toggle 적용 -> 있으면 빼주고, 없으면 넣어주고! # user_id = 3 # example (change later) # 처음화면에서 좋아요가 되어있는지 아닌지 나타내기 위함 likes_bool = already_liked(user_id, content_id) execute_stm = "select count(*) from likes where content_id=" + str( content_id) cur.execute(execute_stm) like_counts = cur.fetchone() conn.close() return render_template('blog-detail.html', contents=row, category_receiver=category_receivers, category_name=category_names, tags=category_tags, recruit_date=recruit_date, check_flag=likes_bool, like_counts=like_counts[0])
def mypage(): user_id = current_user.get_id() # print(type(user_id)) # user가 좋아요 한 Content ID가져오기 userLike = Like.query.all() temp_list = [] # volunteer Content id 임시저장 for i in range(len(userLike)): if userLike[i].user_id == int(user_id): temp_list.append(userLike[i].content_id) # volunteer content 가져오기 volunContents = Volunteer.query.all() volun_list = [] count = 1 for j in temp_list: volun_list.append([j, volunContents[j].title, count]) count += 1 # print(volun_list) return render_template('mypage.html', contentlike=volun_list)
def blog_detail(content_id): # db 연결 user_id = current_user.get_id() if not user_id: user_id = 0 #default # conn = sqlite3.connect('bongsa.db') # cur = conn.cursor() # query # cur.execute("select * from volunteer where id="+content_id) # content_id에 해당하는 컨텐츠 가져오기 # # # data fetch # row = cur.fetchone() # 봉사 컨텐츠 중 volun_id와 일치하는 컨텐츠 가져오기 volun = Volunteer.query.all() row = () for i in range(len(volun)): if volun[i].id == int(content_id): # data fetch row = volun[i].to_tuple() category_names, category_receivers, category_tags = classification( row[8:-5]) # 함수에 필요한 값만 보냄 # 봉사대상, 봉사분류 if len(category_receivers) == 0: category_receivers = '전체시민' if len(category_names) == 0: category_names = '' recruit_date = row[5][4:6] + '/' + row[5][6:8] + '/' + row[5][:4] # if session.user.id , session.content.id가 일치하는 데이터가 db에 있으면, likes에 미리 checked! # + 좋아요 표시를 누르면 db에 toggle 적용 -> 있으면 빼주고, 없으면 넣어주고! # user_id = 3 # example (change later) # 처음화면에서 좋아요가 되어있는지 아닌지 나타내기 위함 likes_bool = already_liked(user_id, content_id) # execute_stm = "select count(*) from " + str(content_id) # cur.execute(execute_stm) # like_counts = cur.fetchone() # # conn.close() likequery = Like.query.all() like_counts = 0 for i in range(len(likequery)): if likequery[i].content_id == int(content_id): # data fetch like_counts += 1 return render_template('blog-detail.html', contents=row, category_receiver=category_receivers, category_name=category_names, tags=category_tags, recruit_date=recruit_date, check_flag=likes_bool, like_counts=like_counts)
def update_likes(): # user_id = 5 user_id = current_user.get_id() if not user_id: user_id = 0 #default # content_id = 7 # content_id도 받아와야함! # conn2 = sqlite3.connect('bongsa.db') # cur2 = conn2.cursor() data = request.form['like_checked'] content_id = request.form['content_id'] likes = Like() ##### tempUser = User_category.query.all() for i in range(len(tempUser)): if tempUser[i].id == int(user_id): aa = list(tempUser[i].to_tuple()[1:]) print("User_Category", tempUser[i].to_tuple()[1:], len(tempUser[i].to_tuple()[1:])) print(aa) tempVolun = Volunteer.query.all() for i in range(len(tempVolun)): if tempVolun[i].id == int(content_id): bb = list(tempVolun[i].to_tuple()[8:-5]) print("Volun_Category", tempVolun[i].to_tuple()[8:-5], len(tempVolun[i].to_tuple()[8:-5])) print(bb) sum_list = [] subtract_list = [] if data == 'true': # db에 등록 likes.user_id = user_id likes.content_id = content_id db.session.add(likes) #####sum##### for i in range(len(aa)): c = aa[i] + float(bb[i]) sum_list.append(c) tempUser[int(user_id)].category_life = sum_list[0] tempUser[int(user_id)].category_world = sum_list[1] tempUser[int(user_id)].category_environment = sum_list[2] tempUser[int(user_id)].category_human = sum_list[3] tempUser[int(user_id)].category_disaster = sum_list[4] tempUser[int(user_id)].category_country = sum_list[5] tempUser[int(user_id)].category_home = sum_list[6] tempUser[int(user_id)].category_medic = sum_list[7] tempUser[int(user_id)].category_education = sum_list[8] tempUser[int(user_id)].category_administration = sum_list[9] tempUser[int(user_id)].category_consulting = sum_list[10] tempUser[int(user_id)].category_culture = sum_list[11] tempUser[int(user_id)].category_physical = sum_list[12] tempUser[int(user_id)].category_history = sum_list[13] tempUser[int(user_id)].category_circles = sum_list[14] tempUser[int(user_id)].category_travel = sum_list[15] tempUser[int(user_id)].category_marketing = sum_list[16] tempUser[int(user_id)].category_social = sum_list[17] tempUser[int(user_id)].category_plan = sum_list[18] tempUser[int(user_id)].category_language = sum_list[19] tempUser[int(user_id)].category_scene = sum_list[20] tempUser[int(user_id)].category_etc = sum_list[21] tempUser[int(user_id)].recruit_disabled = sum_list[22] tempUser[int(user_id)].recruit_oldman = sum_list[23] tempUser[int(user_id)].recruit_foreigner = sum_list[24] tempUser[int(user_id)].recruit_homeless = sum_list[25] tempUser[int(user_id)].recruit_multiculture = sum_list[26] tempUser[int(user_id)].recruit_worker = sum_list[27] tempUser[int(user_id)].recruit_baby = sum_list[28] tempUser[int(user_id)].recruit_women = sum_list[29] tempUser[int(user_id)].recruit_zzokbang = sum_list[30] tempUser[int(user_id)].recruit_teenager = sum_list[31] tempUser[int(user_id)].recruit_nation = sum_list[32] elif data == 'false': # db에서 빼기 temp_like = Like.query.all() for i in range(len(temp_like)): # temp_like[i].user_id는 int, current_user.get_id()--u_id는 str이다. 따라서 형변환을 해줘야함. if temp_like[i].user_id == int( user_id) and temp_like[i].content_id == int( request.form['content_id']): print(user_id) print(content_id) db.session.delete(temp_like[i]) for i in range(len(aa)): c = aa[i] - float(bb[i]) subtract_list.append(c) tempUser[int(user_id)].category_life = subtract_list[0] tempUser[int(user_id)].category_world = subtract_list[1] tempUser[int(user_id)].category_environment = subtract_list[2] tempUser[int(user_id)].category_human = subtract_list[3] tempUser[int(user_id)].category_disaster = subtract_list[4] tempUser[int(user_id)].category_country = subtract_list[5] tempUser[int(user_id)].category_home = subtract_list[6] tempUser[int(user_id)].category_medic = subtract_list[7] tempUser[int(user_id)].category_education = subtract_list[8] tempUser[int(user_id)].category_administration = subtract_list[9] tempUser[int(user_id)].category_consulting = subtract_list[10] tempUser[int(user_id)].category_culture = subtract_list[11] tempUser[int(user_id)].category_physical = subtract_list[12] tempUser[int(user_id)].category_history = subtract_list[13] tempUser[int(user_id)].category_circles = subtract_list[14] tempUser[int(user_id)].category_travel = subtract_list[15] tempUser[int(user_id)].category_marketing = subtract_list[16] tempUser[int(user_id)].category_social = subtract_list[17] tempUser[int(user_id)].category_plan = subtract_list[18] tempUser[int(user_id)].category_language = subtract_list[19] tempUser[int(user_id)].category_scene = subtract_list[20] tempUser[int(user_id)].category_etc = subtract_list[21] tempUser[int(user_id)].recruit_disabled = subtract_list[22] tempUser[int(user_id)].recruit_oldman = subtract_list[23] tempUser[int(user_id)].recruit_foreigner = subtract_list[24] tempUser[int(user_id)].recruit_homeless = subtract_list[25] tempUser[int(user_id)].recruit_multiculture = subtract_list[26] tempUser[int(user_id)].recruit_worker = subtract_list[27] tempUser[int(user_id)].recruit_baby = subtract_list[28] tempUser[int(user_id)].recruit_women = subtract_list[29] tempUser[int(user_id)].recruit_zzokbang = subtract_list[30] tempUser[int(user_id)].recruit_teenager = subtract_list[31] tempUser[int(user_id)].recruit_nation = subtract_list[32] db.session.commit() # execute_stm = "DELETE FROM likes WHERE user_id="+str(user_id)+" and content_id="+str(content_id) # cur2.execute(execute_stm) # conn2.commit() # # conn2.close() # db 연결 해제 if len(data) != 0: # db에 없을 때 javascript에 전달할 말 return json.dumps({'status': 'OK'}) else: # db에 있을 때 javascript에 전달할 말 return json.dumps({"status": 'error'})
def user_detail(): user_id = current_user.get_id() if not user_id: user_id = 3 #default else: user_id = int(user_id) #str->int major_recommend(user_id) get_add = GeoCoordinates('서울 중구 덕수궁길 15') # print(get_add['result']['items'][0]['point']['x'], get_add['result']['items'][0]['point']['y']) # user_id = 3 # temporary # db 연결 conn = sqlite3.connect('bongsa.db') cur = conn.cursor() # # 컨텐츠 카테고리 매트릭스 만들기 # vol_list = [] vol_content = [] # engine = sqlalchemy.create_engine('my connection string') # Session = scoped_session(sessionmaker(bind=engine)) # # s = Session() # # result = s.execute('SELECT * FROM my_table WHERE my_column = :val', {'val': 5}) # # vol_row = db.engine.execute("select * from volunteer") # vol_row = s.execute("select * from volunteer") # print('vol',vol_row[0]) cur.execute("select * from volunteer") vol_row = cur.fetchall() for i in range(len(vol_row)): row_list = list(vol_row[i][8:30]) row_content = list(vol_row[i][0:8]) vol_list.append(row_list) vol_content.append(row_content) # print(vol_content[:4]) vol_latlng = [] cur.execute("select id,latitude,longitude from volunteer") vol_row = cur.fetchall() for i in range(len(vol_row)): row_latlng = list(vol_row[i][0:3]) vol_latlng.append(row_latlng) # # 유저 카테고리 매트릭스 만들기 # cur.execute("select * from user_category") # content_id에 해당하는 컨텐츠 가져오기 user_row = cur.fetchall() user_list = [] # 유저카테고리 1:23 # 튜플인 데이터를 유저매트릭스에 저장하기 for i in range(len(user_row)): row_list = list(user_row[i][1:23]) user_list.append(row_list) # 유사도 별 갯수 similar_list = maxuser(int(user_id), user_list, vol_list, vol_latlng) # # 유저의 첫 번째 선호도 지역 받아오기 # cur.execute("select prefer_sloc1 from user where id=" + str(user_id)) first_city_row = cur.fetchone() first_city = first_city_row[0] cur.execute("select * from volunteer where city2='" + first_city + "'") first_city_row = cur.fetchall() first_city_list = [] for i in range(len(first_city_row)): row_list = list(first_city_row[i][44:]) first_city_list.append( [first_city_row[i][0], row_list[0], row_list[1]]) first_city4 = first_city_list[:8] total_list = [] for i in range(len(similar_list)): total_list.append(similar_list[i]) for i in range(len(first_city4)): total_list.append(first_city4[i]) print(total_list) # print('=======') # print(total_list) # print('=======') # print('similar',similar_list) """ 좋아요 부분 """ # 어느 카테고리 인지 categ # contents마다 user가 좋아요를 눌러놓은 게시물인지 아닌지 확인 similar_star_rate = [] category_names = [] likes_bool = [] for i in range(len(total_list)): likes_bool.append(already_liked(user_id, total_list[i][0])) cur.execute("select * from volunteer where id=" + str(total_list[i][0])) row = cur.fetchone() # print(row) # print('here',max_classification(row[8:-5])) category_names.append(max_classification(row[8:-5])) for i in range(len(similar_list)): similar_star_rate.append(similar_star(similar_list[i][3])) # print('star', similar_star_rate) return render_template('base_map.html', category_name=category_names, check_flag=likes_bool, vol_content=vol_content, similar_list=similar_list, star_rate=similar_star_rate, test_lng=vol_latlng, total_list=total_list, first_city=first_city_list, city_name=first_city)