Able to Send Friend requests successfully but responding to the requests are an issue. When you press Accept to Add, the button is removed but the Friend isn't added or when you press Cancel to Decline, nothing happens.
Tried adding a forms
class Add_Friend(forms.ModelForm):
model = UserProfile
def add_friend(request, user_profile):
request.notification_set.get(type=Notification.FRIEND_REQUEST, sender=user_profile.user.username).delete()
request.friends.add(user_profile)
user_profile.friends.add(self)
request.friend_requests.remove(user_profile)
noti = Notification.objects.create(owner=user_profile, type=Notification.ACCEPTED_FRIEND_REQUEST, sender=self.user.username)
user_profile.notification_set.add(noti)
return self.friends.count()
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
first_name = models.CharField(blank=True, max_length=128)
friends = models.ManyToManyField('self', blank=True, related_name='friends')
friend_requests = models.ManyToManyField('self', blank=True, related_name='friend_requests')
def send_friend_request(self, user_profile):
self.friend_requests.add(user_profile)
noti = Notification.objects.create(owner=self, type=Notification.FRIEND_REQUEST, sender=user_profile.user.username)
self.notification_set.add(noti)
return self.friend_requests.count()
def add_friend(self, user_profile):
self.friend_requests.remove(user_profile)
self.notification_set.get(type=Notification.FRIEND_REQUEST, sender=user_profile.user.username).delete()
self.friends.add(user_profile)
user_profile.friends.add(self)
noti = Notification.objects.create(owner=user_profile, type=Notification.ACCEPTED_FRIEND_REQUEST, sender=self.user.username)
user_profile.notification_set.add(noti)
return self.friends.count()
def cancel_friend_request(self, user_profile):
self.friend_requests.remove(user_profile)
self.notification_set.get(type=Notification.FRIEND_REQUEST, sender=user_profile.user.username).delete()
noti = Notification.objects.create(owner=user_profile, type=Notification.DECLINED_FRIEND_REQUEST, sender=self.user.username)
user_profile.notification_set.add(noti)
return self.friend_requests.count()
def __str__(self):
return self.get_first_name()
#Takes you to the userprofile page
def get_absolute_url(self):
return "/users/{}".format(self.id)
@method_decorator(login_required, name='dispatch')
class SendFriendRequestView(View):
def get(self, request, *args, **kwargs):
profile_id = request.GET.get('profile_id')
requester_id = request.GET.get('requester_id')
target = UserProfile.objects.get(id=profile_id)
requester = UserProfile.objects.get(id=requester_id)
target.send_friend_request(requester)
message = 'Friend request to {} sent!'.format(target.visible_name)
messages.info(request, message)
return redirect('profile', username=target.user.username)
@method_decorator(login_required, name='dispatch')
class CancelFriendRequestView(View):
def cancel_friend_request(request, id):
if request.user.is_authenticated():
user = get_object_or_404(User, id=id)
frequest, created = FriendRequest.objects.filter(
from_user=request.user,
to_user=user).first()
frequest.delete()
return HttpResponseRedirect('/users')
@method_decorator(login_required, name='dispatch')
class AddFriendView(View):
def get(self, request, *args, **kwargs):
try:
profile_id = request.GET.get('profile_id')
requester_id = request.GET.get('requester_id')
target = UserProfile.objects.get(id=profile_id)
requester = UserProfile.objects.get(id=requester_id)
target.add_friend(requester)
message = 'Added friend {}!'.format(target.visible_name)
messages.info(request, message)
return redirect('friends', username=target.user.username)
except Exception as e:
print('Error: {}'.format(e))
url(r'^friend-request/send/(?P<id>[\w\-]+)/$', send_friend_request),
url(r'^friend-request/cancel/(?P<id>[\w\-]+)/$', cancel_friend_request),
url(r'^friend-request/accept/(?P<id>[\w\-]+)/$', accept_friend_request),
{% if user.userprofile in userprofile.friends.all %}
<form>
<button id="remove_friend" data-requesterid="{{user.userprofile.id}}" data-profileid="{{userprofile.id}}">
Remove friend
</button>
</form>
{% elif user.userprofile in userprofile.friend_requests.all %}
Friend request pending...
<form>
<button id="cancel_friend_request " data-requesterid="{{user.userprofile.id}}" data-profileid="{{userprofile.id}}">
Cancel
</button>
</form>
{% else %}
<form>
<button id="send_friend_request" data-requesterid="{{user.userprofile.id}}" data-profileid="{{userprofile.id}}">
Send friend request
</button>
</form>
{% endif %}
I'd like the User to be able to Accept/Decline Friend Requests.