Accepting a Friend Request Next Lecture
updated Oct. 14, 2020

Code changes:

  1. friend/views.py
  2. friend/urls.py
  3. friend/snippets/accept_friend_request.html
  4. friend/friend_requests.html
  5. account/account.html


Update friend/views.py file.


# TODO: ADD THIS METHOD.

def accept_friend_request(request, *args, **kwargs):
	user = request.user
	payload = {}
	if request.method == "GET" and user.is_authenticated:
		friend_request_id = kwargs.get("friend_request_id")
		if friend_request_id:
			friend_request = FriendRequest.objects.get(pk=friend_request_id)
			# confirm that is the correct request
			if friend_request.receiver == user:
				if friend_request: 
					# found the request. Now accept it
					updated_notification = friend_request.accept()
					payload['response'] = "Friend request accepted."

				else:
					payload['response'] = "Something went wrong."
			else:
				payload['response'] = "That is not your request to accept."
		else:
			payload['response'] = "Unable to accept that friend request."
	else:
		# should never happen
		payload['response'] = "You must be authenticated to accept a friend request."
	return HttpResponse(json.dumps(payload), content_type="application/json")



Update friend/urls.py file.


from friend.views import(
	send_friend_request,
	friend_requests,
	accept_friend_request, # TODO: ADD THIS LINE.

)

app_name = 'friend'

urlpatterns = [
    path('friend_request/', send_friend_request, name='friend-request'),
    path('friend_requests/<user_id>/', friend_requests, name='friend-requests'),
    path('friend_request_accept/<friend_request_id>/', accept_friend_request, name='friend-request-accept'), # TODO: ADD THIS LINE.
] 



Create a friend/templates/friend/snippets/accept_friend_request.html file.



Update account/templates/account/account.html file.

Few additions and deletions of code. Mitch uncommented a few code lines.





Author


Mitch Tabian

codingwithmitch.com

Software Dev


Lectures
  • Course Demo
  • Part 1: Project Setup
  • Project Setup
    FREE
  • Templates Setup
    FREE
  • Bootstrap and Static Files Setup
    FREE
  • Header and Footer Bootstrap Style Guide
    FREE
  • Part 2 - User Management
  • Custom User Model
    FREE
  • Register New Users
    FREE
  • Login and Logout
    FREE
  • Password Reset and Password Change
    FREE
  • User Account Screen
    FREE
  • User Search
    FREE
  • Editing User Account Properties
    FREE
  • Uploading an Image
    FREE
  • Cropping an Image with Javascript and Python
    FREE
  • Part 3: Friend System
  • Friend System
    FREE
  • Account Screen with Friend System
    FREE
  • Sending a Friend Request
    FREE
  • Querying Friend Requests
    FREE
  • Accepting a Friend Request
    FREE
  • Removing a Friend
    FREE
  • Decline a Friend Request
    FREE
  • Cancel a Friend Request
    FREE
  • Querying Friends
    FREE
  • Part 4: Public Chat
  • Public Chat App
    FREE
  • Installing Django Channels 2
    FREE
  • Public Chat Consumer
  • Sending a Payload to the Consumer
  • Formatting Chat Messages
  • Handling Consumer Errors
  • Django Humanize for Timestamp Formatting
  • Channel Layers for Broadcasting Messages
  • Saving Chat Messages to the Database
  • Querying Chatroom Messages
  • insertBefore and appendChild
  • Progress Indicator for Pagination
  • Display Connected Users
  • Public Chat Cleanup
  • Asynchronously Loading Images with Javascript
  • Security Risk and Markdown
  • Part 5: Private Chat
  • Private Chat App
  • Private Chatroom View
  • Creating Private Chatrooms
  • Post Save Receivers
  • Display Friends in Room
  • Private Chat Consumer
  • Private Chat WebSocket
  • Dynamically Connected a WebSocket
  • Get User Info Dynamically
  • Error Handling in Private Chat
  • Sending Private Chat Messages (1/2)
  • Sending Private Chat Messages (2/2)
  • Joining and Leaving a Chatroom
  • Querying Private Chatroom Messages
  • Private Chat Messages Payload to UI
  • Private Chat Pagination
  • Loading Spinner Private Chat
  • Highlighting the Selected Chatroom
  • Starting a Private Chat
  • Part 6: Notifications
  • Django Generic Relations (Notification Model)
  • Notification Admin
  • Notifications for Friend Actions
  • Notification Consumer
  • "General" Notification Payloads
  • Fetch FriendRequest & FriendList Notifications
  • Display FriendRequest & FriendList Notifications
  • Accepting a Friend Request from Notification
  • Declining a Friend Request from Notification
  • Pagination for General Notifications
  • Updating Notifications in Real-time
  • New General Notifications in Real-time
  • Unread Notifications Count
  • Private Chat Notification System
  • Tracking if Users are Connected to a Chat
  • Fetching Chatroom Message Notifications
  • Handling Chatroom Notification Payloads
  • Fetching New Chat Notifications in Real-time
  • Pagination for Chat Notifications
  • Displaying Count for Chat Notifications
  • Part 7: Push to Production
  • Push to Production



Comments


Lectures
  • Course Demo
  • Part 1: Project Setup
  • Project Setup
    FREE
  • Templates Setup
    FREE
  • Bootstrap and Static Files Setup
    FREE
  • Header and Footer Bootstrap Style Guide
    FREE
  • Part 2 - User Management
  • Custom User Model
    FREE
  • Register New Users
    FREE
  • Login and Logout
    FREE
  • Password Reset and Password Change
    FREE
  • User Account Screen
    FREE
  • User Search
    FREE
  • Editing User Account Properties
    FREE
  • Uploading an Image
    FREE
  • Cropping an Image with Javascript and Python
    FREE
  • Part 3: Friend System
  • Friend System
    FREE
  • Account Screen with Friend System
    FREE
  • Sending a Friend Request
    FREE
  • Querying Friend Requests
    FREE
  • Accepting a Friend Request
    FREE
  • Removing a Friend
    FREE
  • Decline a Friend Request
    FREE
  • Cancel a Friend Request
    FREE
  • Querying Friends
    FREE
  • Part 4: Public Chat
  • Public Chat App
    FREE
  • Installing Django Channels 2
    FREE
  • Public Chat Consumer
  • Sending a Payload to the Consumer
  • Formatting Chat Messages
  • Handling Consumer Errors
  • Django Humanize for Timestamp Formatting
  • Channel Layers for Broadcasting Messages
  • Saving Chat Messages to the Database
  • Querying Chatroom Messages
  • insertBefore and appendChild
  • Progress Indicator for Pagination
  • Display Connected Users
  • Public Chat Cleanup
  • Asynchronously Loading Images with Javascript
  • Security Risk and Markdown
  • Part 5: Private Chat
  • Private Chat App
  • Private Chatroom View
  • Creating Private Chatrooms
  • Post Save Receivers
  • Display Friends in Room
  • Private Chat Consumer
  • Private Chat WebSocket
  • Dynamically Connected a WebSocket
  • Get User Info Dynamically
  • Error Handling in Private Chat
  • Sending Private Chat Messages (1/2)
  • Sending Private Chat Messages (2/2)
  • Joining and Leaving a Chatroom
  • Querying Private Chatroom Messages
  • Private Chat Messages Payload to UI
  • Private Chat Pagination
  • Loading Spinner Private Chat
  • Highlighting the Selected Chatroom
  • Starting a Private Chat
  • Part 6: Notifications
  • Django Generic Relations (Notification Model)
  • Notification Admin
  • Notifications for Friend Actions
  • Notification Consumer
  • "General" Notification Payloads
  • Fetch FriendRequest & FriendList Notifications
  • Display FriendRequest & FriendList Notifications
  • Accepting a Friend Request from Notification
  • Declining a Friend Request from Notification
  • Pagination for General Notifications
  • Updating Notifications in Real-time
  • New General Notifications in Real-time
  • Unread Notifications Count
  • Private Chat Notification System
  • Tracking if Users are Connected to a Chat
  • Fetching Chatroom Message Notifications
  • Handling Chatroom Notification Payloads
  • Fetching New Chat Notifications in Real-time
  • Pagination for Chat Notifications
  • Displaying Count for Chat Notifications
  • Part 7: Push to Production
  • Push to Production