Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
CureLatte committed Apr 11, 2022
2 parents d6400a6 + 9a771aa commit 6a058c7
Show file tree
Hide file tree
Showing 24 changed files with 1,029 additions and 397 deletions.
8 changes: 7 additions & 1 deletion accounts/apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from django.apps import AppConfig

from gensim.models import word2vec

class AccountsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "accounts"


class W2V(AppConfig):
model_filename = 'accounts/model/kor_word2vec.model'

model = word2vec.Word2Vec.load(model_filename)
Binary file added accounts/model/kor_word2vec.model
Binary file not shown.
1 change: 1 addition & 0 deletions accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
path("friends/delete/<int:friend_id>/", views.remove_friend, name="remove_friend"),
path("friends/like", views.get_user, name="user_like"),
path("friend/keyword", views.save_like_keyword, name="save_like_keyword"),
path("friend/recommend/keyword/", views.friend_like_recommend, name="friend_like_recommend"),
path("mypage/public/setting/", views.like_public_setting, name='public_change')

]
79 changes: 79 additions & 0 deletions accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ def logout(request):
def friend_list(request):
user = request.user
friends = get_friend_list(user_id=user.id)
total_recommend_friend = recommend_friend(user)
context = {
"user": user,
"friends": friends,
"recommend_friend": total_recommend_friend,
}
return render(request, "accounts/user_list.html", context)

Expand Down Expand Up @@ -300,6 +302,83 @@ def save_like_keyword(request):
user.like_keyword = like_keyowrd
user.save()
return JsonResponse({"msg":"add like"})

import random
from accounts.apps import W2V


def recommend_friend(me):
user = me
all_user = CustomUser.objects.all().exclude(id=user.id)
print('나빼고 전체 유저', all_user)
friends = user.friends.all()
print('친구들', friends)
not_friends = []
recommend_friends = []
for x in all_user :
if x not in friends:
not_friends.append(x)

print('친구아닌 애들', not_friends)
simil_user = {}
user_keyword = user.like_keyword
if len(not_friends) < 6:
recommend_friends = not_friends
else :
if user_keyword == '':
recommend_friends = random.sample(not_friends,5)
else:
for friendx in not_friends:
try:
similarity = W2V.model.wv.similarity(user_keyword,friendx.like_keyword)
simil_user[friendx.id] = int(similarity * 100)
except:
simil_user[friendx.id] = 0

print('키워드 유사도',simil_user)
sorted_friend = sorted(simil_user.items(), key=lambda x:-x[1])
print('오름차순으로 나열된 친구', sorted_friend)
if len(sorted_friend) > 4:
for i in sorted_friend[:5]:
recommend = CustomUser.objects.get(id=i[0])
recommend_friends.append(recommend)
else:
for i in sorted_friend:
recommend = CustomUser.objects.get(id=i[0])
recommend_friends.append(recommend)

print('추천친구 목록',recommend_friends)
return recommend_friends


#친구 관심키워드
@csrf_exempt
@login_required
def friend_like_recommend(request):
friend_id = request.GET.get('friend_id')
print('친구아이디',friend_id)
friend = CustomUser.objects.get(id=friend_id)
friend_keyword = friend.like_keyword
friend_like_keywords = []
if friend_keyword == '':
friend_like_keywords.append('찜 없음')

else:
friend_like_keywords.append(friend_keyword)
try:
similar_word = W2V.model.wv.most_similar(friend_keyword)
for word in similar_word[:3]:
friend_like_keywords.append(word[0])
except:
print('친구 키워드',friend_like_keywords)
print('친구 키워드', friend_like_keywords)

context = {
'friend_keywords': friend_like_keywords
}
return HttpResponse(json.dumps(context), content_type="application/json")


##################################################################################################################


Expand Down
1 change: 1 addition & 0 deletions chat/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

websocket_urlpatterns = [
re_path(r"ws/chat/room/(?P<room_id>\w+)/$", consumers.ChatConsumer.as_asgi()),
re_path(r"ws/chat/list/(?P<partner_id>\w+)/$", consumers.ChatConsumer.as_asgi()),
]
4 changes: 4 additions & 0 deletions chat/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
path("chatlog/", views.chat_log_send, name="chat_log_send"),
path("morelist/", views.more_list, name="more_list"),
path("messageloader/", views.message_loader, name="massage_loader"),
path("latestmessage/", views.latest_message, name="latest_message"),
path("lastmessagelist/", views.last_message_list, name="last_message_list"),
path("latestmessagenotconnected/", views.latest_message_not_connected, name="latest_message_not_connected"),
path("delete/<int:room_id>/", views.delete_chat_room, name="delete_chat_room"),
]
103 changes: 86 additions & 17 deletions chat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
@login_required
def show_chat_list(request):
user = request.user
chatroom_list = ChatRoom.objects.filter(Q(participant1=user) | Q(participant2=user)).all()
chatroom_list = ChatRoom.objects.filter(Q(participant1=user) | Q(participant2=user)).all().order_by('-created_at')
all_message = ChatMessage.objects.all()
return render(request, "chat/chat_list.html", {"chatroom_list": chatroom_list, "all_message": all_message})
return render(request, "chat/chat_list.html", {'user_id': user.id, "chatroom_list": chatroom_list, "all_message": all_message})


# 채팅하기 버튼 클릭 시 채팅방 생성
Expand All @@ -44,15 +44,28 @@ def create_chat_room(request, id):
room_id = room.id
return redirect("/chat/room/" + str(room_id) + "/")
else:
chat_room = ChatRoom.objects.create(participant1=user, participant2=partner)
chat_room.save()
try:
chat_room = ChatRoom.objects.create(participant1=user, participant2=partner)
chat_room.save()

# ChatRoom에 있는 room id 로 redirect
room = ChatRoom.objects.get(participant1=user, participant2=partner)
room_id = room.id
return redirect("/chat/room/" + str(room_id) + "/")
except exist_room1.DoesNotExist or exist_room1.DoesNotExist:
return render(request, "chat/chat_room.html", {'error': '존재하지 않거나 사라진 채팅방입니다.'})

# ChatRoom에 있는 room id 로 redirect
room = ChatRoom.objects.get(participant1=user, participant2=partner)
room_id = room.id
return redirect("/chat/room/" + str(room_id) + "/")
else:
return render(request, "chat/chat_room.html", {'user': user})
return render(request, "chat/chat_room.html", {'error': '접근 불가한 채팅방 입니다.'})


# 웹소켓이 실행되면서 열린 chat/room/room_id html로 데이터 전달
@csrf_exempt
@login_required
def delete_chat_room(request, room_id):
target_room = ChatRoom.objects.get(id=room_id)
target_room.delete()
return redirect('/chat')


# 웹소켓이 실행되면서 열린 chat/room/room_id html로 데이터 전달
Expand All @@ -62,14 +75,10 @@ def post_data_to_chat_room(request, room_id):
if request.method == "GET":
user = request.user
chatroom = ChatRoom.objects.get(id=room_id)
chatroom_list = ChatRoom.objects.filter(Q(participant1=user) | Q(participant2=user)).all()
chatroom_list = ChatRoom.objects.filter(Q(participant1=user) | Q(participant2=user)).all().order_by('-created_at')
all_message = ChatMessage.objects.all()

last_messages = ChatMessage.objects.filter(chatroom_id=room_id).order_by("created_at")
limit = len(last_messages) - 40
if len(last_messages) > 40:
last_messages = last_messages[limit:]
# latest_created_message = ChatMessage.objects.filter(chatroom_id=room_id).order_by('-created_at')[0]
# latest_messages = ChatMessage.objects.filter(chatroom_id=room_id).order_by("-created_at")[0]

if chatroom.participant1.id == user.id:
participant = chatroom.participant2
Expand All @@ -95,8 +104,7 @@ def post_data_to_chat_room(request, room_id):
"participant2": chatroom.participant2,
"participant": participant,
"all_message": all_message,
"last_messages": last_messages,
# "latest_created_message": latest_created_message,
# "latest_messages": latest_messages,
"participant_like_youtube": participant_like_youtube,
"participant_like_news": participant_like_news,
"participant_like_book": participant_like_book,
Expand Down Expand Up @@ -173,3 +181,64 @@ def message_loader(request):
return HttpResponse(json.dumps(context), content_type="application/json")


@csrf_exempt
@login_required
def latest_message(request):
room_id = json.loads(request.body.decode('utf-8'))['room_id']
latest_message = ChatMessage.objects.filter(chatroom_id=room_id).order_by("-created_at")[0]
chatroom = ChatRoom.objects.get(id=room_id)
if request.user.id == chatroom.participant1_id:
partner_id = chatroom.participant2_id
else :
partner_id = chatroom.participant1_id
print(partner_id)
context = {
'message': latest_message.message,
'chatroom_id': latest_message.chatroom_id,
'author_id': latest_message.author_id,
'partner_id': partner_id,
}

return HttpResponse(json.dumps(context), content_type="application/json")


@csrf_exempt
@login_required
def last_message_list(request):
# chatroom_list = ChatRoom.objects.filter(Q(participant1=request.user) | Q(participant2=request.user)).all()
last_messages = ChatMessage.objects.all().order_by('created_at')

last_message_list = []
for message in last_messages:
last_message_list.append({'message': message.message, 'author_id': message.author_id, 'chatroom_id': message.chatroom_id})

context = {
'last_message_list': last_message_list
}

return HttpResponse(json.dumps(context), content_type="application/json")


@csrf_exempt
@login_required
def latest_message_not_connected(request):
partner_list = json.loads(request.body.decode('utf-8'))['partner_list']
print(partner_list)

latest_chat_list = []
for partner in partner_list:
print(partner)
chatter = CustomUser.objects.get(id=partner)
chatroom = ChatRoom.objects.get(Q(participant1=request.user, participant2_id=chatter) | Q(participant2=request.user, participant1=chatter))
print(chatroom)
latest_message_each_chatroom = ChatMessage.objects.filter(chatroom=chatroom).order_by("-created_at")
for message in latest_message_each_chatroom:
if message is latest_message_each_chatroom[0]:
print(message.message)
latest_chat_list.append({'partner': chatter.id, 'author_message': message.author_id ,'latest_message_each_chatroom': message.message})

context = {
'latest_chat_list': latest_chat_list,
}

return HttpResponse(json.dumps(context), content_type="application/json")
Loading

0 comments on commit 6a058c7

Please sign in to comment.