From e1f19fb79497b42c90ae32336cf6279f6eff6190 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Mon, 22 Nov 2021 21:54:48 -0600 Subject: [PATCH] Refactor code + fix a small bug --- chat_box/models.py | 5 ++++- chat_box/views.py | 19 +++++++++---------- judge/models/profile.py | 2 +- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/chat_box/models.py b/chat_box/models.py index f867299..5958347 100644 --- a/chat_box/models.py +++ b/chat_box/models.py @@ -57,7 +57,10 @@ class Ignore(models.Model): @classmethod def get_ignored_users(self, user): - return self.objects.get(user=user).ignored_users.all() + try: + return self.objects.get(user=user).ignored_users.all() + except Ignore.DoesNotExist: + return Profile.objects.none() @classmethod def add_ignore(self, current_user, friend): diff --git a/chat_box/views.py b/chat_box/views.py index 1e7a3c8..fd527f1 100644 --- a/chat_box/views.py +++ b/chat_box/views.py @@ -276,10 +276,7 @@ def get_status_context(request, include_ignored=False): ignored_users = Profile.objects.none() queryset = Profile.objects else: - try: - ignored_users = Ignore.get_ignored_users(request.profile) - except: - ignored_users = Profile.objects.none() + ignored_users = Ignore.get_ignored_users(request.profile) queryset = Profile.objects.exclude(id__in=ignored_users) last_two_minutes = timezone.now()-timezone.timedelta(minutes=2) @@ -304,12 +301,9 @@ def get_status_context(request, include_ignored=False): if joined_id: recent_list = Profile.objects.raw( f'SELECT * from judge_profile where id in ({joined_id}) order by field(id,{joined_id})') - try: - friend_list = Friend.get_friend_profiles(request.profile).exclude(id__in=recent_profile_id)\ - .exclude(id__in=ignored_users)\ - .order_by('-last_access') - except: - friend_list = Profile.objects.none() + friend_list = Friend.get_friend_profiles(request.profile).exclude(id__in=recent_profile_id)\ + .exclude(id__in=ignored_users)\ + .order_by('-last_access') admin_list = queryset.filter(display_rank='admin')\ .exclude(id__in=friend_list).exclude(id__in=recent_profile_id) all_user_status = queryset\ @@ -379,6 +373,11 @@ def get_or_create_room(request): return HttpResponseBadRequest() # TODO: each user can only create <= 300 rooms room = get_room(other_user, user) + for u in [other_user, user]: + user_room, _ = UserRoom.objects.get_or_create(user=u, room=room) + user_room.last_seen = timezone.now() + user_room.save() + if request.method == 'GET': return JsonResponse({'room': room.id, 'other_user_id': other_user.id}) return HttpResponseRedirect(reverse('chat', kwargs={'room_id': room.id})) diff --git a/judge/models/profile.py b/judge/models/profile.py index e5e2129..b6111b7 100644 --- a/judge/models/profile.py +++ b/judge/models/profile.py @@ -262,7 +262,7 @@ class Friend(models.Model): try: ret = self.objects.get(current_user=current_user).users.all() except Friend.DoesNotExist: - ret = [] + ret = Profile.objects.none() return ret def __str__(self):