Refactor code + fix a small bug
This commit is contained in:
parent
d0dbb3a887
commit
e1f19fb794
3 changed files with 14 additions and 12 deletions
|
@ -57,7 +57,10 @@ class Ignore(models.Model):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_ignored_users(self, user):
|
def get_ignored_users(self, user):
|
||||||
|
try:
|
||||||
return self.objects.get(user=user).ignored_users.all()
|
return self.objects.get(user=user).ignored_users.all()
|
||||||
|
except Ignore.DoesNotExist:
|
||||||
|
return Profile.objects.none()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def add_ignore(self, current_user, friend):
|
def add_ignore(self, current_user, friend):
|
||||||
|
|
|
@ -276,10 +276,7 @@ def get_status_context(request, include_ignored=False):
|
||||||
ignored_users = Profile.objects.none()
|
ignored_users = Profile.objects.none()
|
||||||
queryset = Profile.objects
|
queryset = Profile.objects
|
||||||
else:
|
else:
|
||||||
try:
|
|
||||||
ignored_users = Ignore.get_ignored_users(request.profile)
|
ignored_users = Ignore.get_ignored_users(request.profile)
|
||||||
except:
|
|
||||||
ignored_users = Profile.objects.none()
|
|
||||||
queryset = Profile.objects.exclude(id__in=ignored_users)
|
queryset = Profile.objects.exclude(id__in=ignored_users)
|
||||||
|
|
||||||
last_two_minutes = timezone.now()-timezone.timedelta(minutes=2)
|
last_two_minutes = timezone.now()-timezone.timedelta(minutes=2)
|
||||||
|
@ -304,12 +301,9 @@ def get_status_context(request, include_ignored=False):
|
||||||
if joined_id:
|
if joined_id:
|
||||||
recent_list = Profile.objects.raw(
|
recent_list = Profile.objects.raw(
|
||||||
f'SELECT * from judge_profile where id in ({joined_id}) order by field(id,{joined_id})')
|
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)\
|
friend_list = Friend.get_friend_profiles(request.profile).exclude(id__in=recent_profile_id)\
|
||||||
.exclude(id__in=ignored_users)\
|
.exclude(id__in=ignored_users)\
|
||||||
.order_by('-last_access')
|
.order_by('-last_access')
|
||||||
except:
|
|
||||||
friend_list = Profile.objects.none()
|
|
||||||
admin_list = queryset.filter(display_rank='admin')\
|
admin_list = queryset.filter(display_rank='admin')\
|
||||||
.exclude(id__in=friend_list).exclude(id__in=recent_profile_id)
|
.exclude(id__in=friend_list).exclude(id__in=recent_profile_id)
|
||||||
all_user_status = queryset\
|
all_user_status = queryset\
|
||||||
|
@ -379,6 +373,11 @@ def get_or_create_room(request):
|
||||||
return HttpResponseBadRequest()
|
return HttpResponseBadRequest()
|
||||||
# TODO: each user can only create <= 300 rooms
|
# TODO: each user can only create <= 300 rooms
|
||||||
room = get_room(other_user, user)
|
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':
|
if request.method == 'GET':
|
||||||
return JsonResponse({'room': room.id, 'other_user_id': other_user.id})
|
return JsonResponse({'room': room.id, 'other_user_id': other_user.id})
|
||||||
return HttpResponseRedirect(reverse('chat', kwargs={'room_id': room.id}))
|
return HttpResponseRedirect(reverse('chat', kwargs={'room_id': room.id}))
|
||||||
|
|
|
@ -262,7 +262,7 @@ class Friend(models.Model):
|
||||||
try:
|
try:
|
||||||
ret = self.objects.get(current_user=current_user).users.all()
|
ret = self.objects.get(current_user=current_user).users.all()
|
||||||
except Friend.DoesNotExist:
|
except Friend.DoesNotExist:
|
||||||
ret = []
|
ret = Profile.objects.none()
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
Loading…
Reference in a new issue