def send_team_request_state_email(team_request_id): team_request = UserTeamRequest.objects.get(id=team_request_id) team = team_request.team user = User.objects.get(id=team_request.user_id) if team_request.approved: TeamMember.objects.get_or_create( user=user, team=team ) email_subject = 'Your team request was successfully approved!' message = render_to_string('core/accepted_team_request.html', { 'user': user, 'team': team }) notification_message = UserNotification.get_notification_text( UserNotification.ACCEPTED_TEAM_REQUEST, username=user.username, team_name=team.name, ) UserNotification.objects.create( user=user, purpose=UserNotification.APPROVED_TEAM_REQUEST_PURPOSE, message=notification_message, notification_type=UserNotification.SUCCESS ) else: email_subject = 'Your team request was rejected.' message = render_to_string('core/rejected_team_request.html', { 'user': user, 'team': team }) notification_message = UserNotification.get_notification_text( UserNotification.REJECTED_TEAM_REQUEST, team_name=team.name, ) UserNotification.objects.create( user=user, purpose=UserNotification.REJECTED_TEAM_REQUEST_PURPOSE, message=notification_message, notification_type=UserNotification.WARNING ) email = EmailMessage( email_subject, message, from_email=settings.FROM_EMAIL, to=[user.email] ) email.content_subtype = 'html' email.send() team_request.email_was_send = True team_request.save()
def test_accepted_team_request_notification(self): """Checks that user notification creates successfully after accepting request""" send_team_request_state_email(self.team_request.id) notification = UserNotification.objects.last() self.assertEqual(notification.notification_type, UserNotification.SUCCESS) self.assertEqual(notification.user, self.user) notification_message = UserNotification.get_notification_text( UserNotification.ACCEPTED_TEAM_REQUEST, username=self.user.username, team_name=self.team.name, ) self.assertEqual(notification.message, notification_message)
def test_send_confirmation_email_notification(self): """Checks that user notification creates successfully after registration""" send_confirmation_email(self.user.id, 'http://127.0.0.1:8000', '12312123', self.user.email) notification = UserNotification.objects.last() self.assertEqual(notification.notification_type, UserNotification.WARNING) self.assertEqual(notification.user, self.user) notification_message = UserNotification.get_notification_text( UserNotification.CONFIRM_EMAIL, username=self.user.username, ) self.assertEqual(notification.message, notification_message)
def test_rejected_team_request_notification(self): """Checks that user notification creates successfully after rejecting request""" self.team_request.approved = False self.team_request.save() send_team_request_state_email(self.team_request.id) notification = UserNotification.objects.last() self.assertEqual(notification.notification_type, UserNotification.WARNING) self.assertEqual(notification.user, self.user) notification_message = UserNotification.get_notification_text( UserNotification.REJECTED_TEAM_REQUEST, team_name=self.team.name, ) self.assertEqual(notification.message, notification_message)
def calculate_coins_for_user(user, amount): """Calculates coins for user and add them to user""" coins_amount = constants.CENTS_COINS_DEPENDENCY.get(amount) user.coins += coins_amount user.save() notification_message = UserNotification.get_notification_text( UserNotification.SUCCESSFUL_COINS_PURCHASE, username=user.username, amount=coins_amount ) UserNotification.objects.create( user=user, message=notification_message, notification_type=UserNotification.SUCCESS )
def test_post_request_for_team(self): """Tests that user can create requests""" usual_user = UserFactory( username='******', email='*****@*****.**', ) token = Token.objects.get(user=usual_user) self.client.credentials(HTTP_AUTHORIZATION=f'Token {token.key}') data = {'team': self.team.id} response = self.client.post(reverse('api:user-team-requests-list'), data=data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) notification = UserNotification.objects.last() notification_message = UserNotification.get_notification_text( UserNotification.TEAM_REQUEST_WAS_SENT_WITH_DEACTIVATED_EMAIL, username=usual_user.username) self.assertEqual(notification.message, notification_message)
def create(self, request, *args, **kwargs): # noqa serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) _, is_created = serializer.save() if is_created: notification_message = UserNotification.TEAM_REQUEST_WAS_SENT_WITH_ACTIVATED_EMAIL\ if request.user.email_confirmed else UserNotification.TEAM_REQUEST_WAS_SENT_WITH_DEACTIVATED_EMAIL notification_message = UserNotification.get_notification_text( notification_message, username=request.user.username) UserNotification.objects.create( purpose=UserNotification.REQUEST_WAS_SENT_PURPOSE, user=request.user, message=notification_message, notification_type=UserNotification.INFO) response_status = 201 if is_created else forum_status.STATUS_222_USER_ALREADY_REQUESTED headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=response_status, headers=headers)
def test_registered_user_notification(self): """Checks that user notification creates successfully after registration""" user_data = { 'username': self.user.username + '2', 'password': '******', 'email': '*****@*****.**', 'game_nickname': self.user.game_nickname, 'gender': self.user.gender, } response = self.client.post(reverse('users:registration'), user_data) self.assertEqual(response.status_code, status.HTTP_200_OK) notification = UserNotification.objects.last() self.assertEqual(notification.notification_type, UserNotification.SUCCESS) notification_message = UserNotification.get_notification_text( UserNotification.SUCCESSFULLY_REGISTERED, username=self.user.username + '2', ) self.assertEqual(notification.message, notification_message)
def send_confirmation_email(user_pk, domain, token, user_email): email_subject = 'Activate your forum account.' message = render_to_string('users/email_confirmation.html', { 'domain': domain, 'uid': urlsafe_base64_encode(force_bytes(user_pk)), 'token': token, }) email = EmailMessage( email_subject, message, from_email=settings.FROM_EMAIL, to=[user_email] ) email.content_subtype = 'html' email.send() user = User.objects.get(pk=user_pk) notification_message = UserNotification.get_notification_text(UserNotification.CONFIRM_EMAIL, username=user.username) UserNotification.objects.create( user=user, notification_type=UserNotification.WARNING, message=notification_message, purpose=UserNotification.CONFIRM_EMAIL_PURPOSE )
def post(request, *args, **kwargs): # noqa data = request.data user_serializer = RegisterUserSerializer(data=data) user_serializer.is_valid(raise_exception=True) user = User.objects.create_user(**user_serializer.data) notification_message = UserNotification.get_notification_text( UserNotification.SUCCESSFULLY_REGISTERED, username=user.username) UserNotification.objects.create( user=user, notification_type=UserNotification.SUCCESS, message=notification_message, purpose=UserNotification.SUCCESSFULLY_REGISTERED_PURPOSE) token = Token.objects.get(user=user) email_token = account_activation_token.make_token(user) domain = get_current_site(request).domain send_confirmation_email.delay(user_pk=user.id, domain=domain, token=email_token, user_email=user.email) # settings.BASE_DIR ToDo: add default image (situated in static folder) return Response(data={'auth_token': token.key})
def get(self, request, uidb64, token, **kwargs): # noqa try: uid = force_text(urlsafe_base64_decode(uidb64)) user = User.objects.get(pk=uid) except (TypeError, ValueError, OverflowError, User.DoesNotExist): user = None if user is not None and account_activation_token.check_token( user, token): user.email_confirmed = True user.save() # ToDo: add notification to the user notification_message = UserNotification.get_notification_text( UserNotification.EMAIL_CONFIRMED, username=user.username) UserNotification.objects.filter( user=user, purpose=UserNotification.CONFIRM_EMAIL_PURPOSE).delete() UserNotification.objects.create( user=user, message=notification_message, purpose=UserNotification.EMAIL_WAS_CONFIRMED_PURPOSE, notification_type=UserNotification.SUCCESS) return render(request, 'api/home.html') return render(request, 'api/home.html')
def test_coins_purchase_functionality(self): """Checks that coins calculates properly""" calculate_coins_for_user(user=self.user, amount=constants.CENTS_FOR_500_COINS) self.assertEqual(self.user.coins, constants.COINS_500) self.user.coins = 0 self.user.save() notification_message = UserNotification.get_notification_text( UserNotification.SUCCESSFUL_COINS_PURCHASE, username=self.user.username, amount=constants.COINS_500) notification = UserNotification.objects.last() self.assertEqual(notification.message, notification_message) calculate_coins_for_user(user=self.user, amount=constants.CENTS_FOR_750_COINS) self.assertEqual(self.user.coins, constants.COINS_750) self.user.coins = 0 self.user.save() calculate_coins_for_user(user=self.user, amount=constants.CENTS_FOR_1000_COINS) self.assertEqual(self.user.coins, constants.COINS_1000) self.user.coins = 0 self.user.save() calculate_coins_for_user(user=self.user, amount=constants.CENTS_FOR_2000_COINS) self.assertEqual(self.user.coins, constants.COINS_2000) self.user.coins = 0 self.user.save() calculate_coins_for_user(user=self.user, amount=constants.CENTS_FOR_5000_COINS) self.assertEqual(self.user.coins, constants.COINS_5000) self.user.coins = 0 self.user.save() calculate_coins_for_user(user=self.user, amount=constants.CENTS_FOR_10000_COINS) self.assertEqual(self.user.coins, constants.COINS_10000) self.user.coins = 0 self.user.save()