Refactor code + fix a small bug

This commit is contained in:
cuom1999 2021-11-22 21:54:48 -06:00
parent d0dbb3a887
commit e1f19fb794
3 changed files with 14 additions and 12 deletions

View file

@ -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):

View file

@ -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}))

View file

@ -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):