def get_room(self, request): """ Use the util room creation function to create room for one/two user(s). This can be extended in the future to add multiple users in a group chat. """ user = request.user target_user = User.objects.get(pk=request.POST.get('target_user')) if user == target_user: room = create_room([user]) else: room = create_room([user, target_user]) return room
def test_create_room_with_valid_input(self): user1 = User.objects.get(username="******") user2 = User.objects.get(username="******") user3 = User.objects.get(username="******") room = create_room([user1, user2, user3]) room_in_db = Room.objects.all()[0] self.assertEqual(room_in_db, room)
def get(self, request, *args, **kwargs): rooms_list = Room.objects.filter( members=request.user).order_by('-date_modified') if rooms_list.exists(): latest_room_uuid = rooms_list[0].pk return HttpResponseRedirect( reverse('django_chatter:chatroom', args=[latest_room_uuid])) else: # create room with the user themselves room = create_room([self.request.user]) return HttpResponseRedirect( reverse('django_chatter:chatroom', args=[room.pk]))
def chat_context(request): context = {} if not request.user.is_authenticated: return context rooms_list = Room.objects.filter( members=request.user).order_by('-date_modified') if rooms_list.exists(): latest_room_uuid = rooms_list[0].id else: user = User.objects.get(username=request.user) latest_room_uuid = create_room([user]).id staff = User.objects.filter(groups__name='Students Help') room = create_room(staff) room.members.add(user) room.save() crv = ChatRoomView() crv.request = request context = crv.get_context_data(uuid=latest_room_uuid) context['head_message'] = "You can talk here {}".format(latest_room_uuid) return context
def test_creating_existing_room(self): user1 = User.objects.get(username="******") user2 = User.objects.get(username="******") room = create_room([user1, user2]) new_room = create_room([user1, user2]) self.assertEqual(new_room, room)
def test_create_room_with_empty_input(self): self.assertRaises(TypeError, lambda: create_room())
def test_create_room_with_invalid_input(self): self.assertRaises(TypeError, lambda: create_room([1, 2, 3])) self.assertRaises(TypeError, lambda: create_room(['1', '2', '2']))
def signup(request): if request.user.is_authenticated: return redirect("old-user") data = {} if request.method == 'POST': email = request.POST.get('email') password = request.POST.get('password') try: User.objects.get(email=email) #messages.info(request, 'This email is already registered') data = { 'status': 'error', 'msg': 'This email is already registered' } return JsonResponse(data) except ObjectDoesNotExist: uname = email.replace('@', '_') user = User(username=uname, email=email) user.set_password(password) user.save() staff = User.objects.filter(groups__name='Students Help') room = create_room(staff) room.members.add(user) room.save() user_detail = UserDetails(user=user, user_type="Student") user_detail.save() if request.session.get('session_key'): username = request.session['session_key'] unknown_user = User.objects.get(username=username) try: laborder = LabOrders.objects.get(user=unknown_user) laborder.user = user laborder.save() c = { 'user': user.username, 'order_id': laborder.order_id, 'subject': laborder.subject, 'deadline': laborder.deadline } order_id = laborder.order_id email_msg = render_to_string('order.txt', c) mail = EmailMessage( subject=f'Order Created - {order_id}', body=email_msg, from_email= 'TutorChamps Student Support <*****@*****.**>', to=[email, '*****@*****.**']) mail.send() except: order = Orders.objects.get(user=unknown_user) order.user = user order.save() c = { 'user': user.username, 'order_id': order.order_id, 'subject': order.subject, 'deadline': order.deadline } order_id = order.order_id print(order.deadline) email_msg = render_to_string('order.txt', c) mail = EmailMessage( subject=f'Order Created - {order_id}', body=email_msg, from_email= 'TutorChamps Student Support <*****@*****.**>', to=[email, '*****@*****.**']) mail.send() finally: unknown_user.delete() del request.session['session_key'] #messages.success(request, 'you have registered successfully') usr = authenticate(username=uname, password=password) login(request, usr) data = {'status': 'ok', 'msg': 'User created successfully'} print("Order successful, order id : {}".format( order.order_id)) send_message( request, "Order successful, order id : {}".format(order_id)) data['order'] = "YES" #return redirect('new-user-ordered', order="order-successful") return JsonResponse(data) else: #messages.success(request, 'you have registered successfully') usr = authenticate(username=uname, password=password) login(request, usr) data = {'status': 'ok', 'msg': 'User created successfully'} c = {'user': user.username} email_msg = render_to_string('signup.txt', c) mail = EmailMessage( subject='Welcome to TutorChamps', body=email_msg, from_email= 'TutorChamps Student Support <*****@*****.**>', to=[email]) mail.send() return JsonResponse(data) print(usr) print("here2") return render(request, 'signup.html', data)