From 4d24f4c005af5f26c7fededaf3fa9e42340ff9be Mon Sep 17 00:00:00 2001 From: adeelehsan Date: Fri, 5 Mar 2021 19:08:35 +0500 Subject: [PATCH] datetimestamp added for account activation event VAN-390 --- .../0041_registration_activation_timestamp.py | 18 ++++++++++++++++++ common/djangoapps/student/models.py | 3 +++ .../student/tests/test_activate_account.py | 11 +++++++++++ common/djangoapps/student/views/management.py | 1 + 4 files changed, 33 insertions(+) create mode 100644 common/djangoapps/student/migrations/0041_registration_activation_timestamp.py diff --git a/common/djangoapps/student/migrations/0041_registration_activation_timestamp.py b/common/djangoapps/student/migrations/0041_registration_activation_timestamp.py new file mode 100644 index 0000000000..6bb0787381 --- /dev/null +++ b/common/djangoapps/student/migrations/0041_registration_activation_timestamp.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.19 on 2021-03-05 13:25 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('student', '0040_usercelebration'), + ] + + operations = [ + migrations.AddField( + model_name='registration', + name='activation_timestamp', + field=models.DateTimeField(blank=True, default=None, null=True), + ), + ] diff --git a/common/djangoapps/student/models.py b/common/djangoapps/student/models.py index 02001948b8..3cb38adea0 100644 --- a/common/djangoapps/student/models.py +++ b/common/djangoapps/student/models.py @@ -905,6 +905,7 @@ class Registration(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) activation_key = models.CharField((u'activation key'), max_length=32, unique=True, db_index=True) + activation_timestamp = models.DateTimeField(default=None, null=True, blank=True) def register(self, user): # MINOR TODO: Switch to crypto-secure key @@ -915,6 +916,8 @@ class Registration(models.Model): def activate(self): self.user.is_active = True self.user.save(update_fields=['is_active']) + self.activation_timestamp = datetime.utcnow() + self.save() USER_ACCOUNT_ACTIVATED.send_robust(self.__class__, user=self.user) log.info(u'User %s (%s) account is successfully activated.', self.user.username, self.user.email) diff --git a/common/djangoapps/student/tests/test_activate_account.py b/common/djangoapps/student/tests/test_activate_account.py index a75661eac3..ddad694912 100644 --- a/common/djangoapps/student/tests/test_activate_account.py +++ b/common/djangoapps/student/tests/test_activate_account.py @@ -3,6 +3,7 @@ import unittest from uuid import uuid4 +from datetime import datetime from django.conf import settings from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user @@ -87,6 +88,16 @@ class TestActivateAccount(TestCase): assert self.user.is_active, 'Sanity check for .activate()' mock_signal.send_robust.assert_called_once_with(Registration, user=self.user) # Ensure the signal is emitted + def test_activation_timestamp(self): + """ Assert that activate sets the flag but does not call segment. """ + # Ensure that the user starts inactive + assert not self.user.is_active + # Until you explicitly activate it + timestamp_before_activation = datetime.utcnow() + self.registration.activate() + assert self.user.is_active + assert self.registration.activation_timestamp > timestamp_before_activation + def test_account_activation_message(self): """ Verify that account correct activation message is displayed. diff --git a/common/djangoapps/student/views/management.py b/common/djangoapps/student/views/management.py index 5e5f4c1f2f..1f873df1f7 100644 --- a/common/djangoapps/student/views/management.py +++ b/common/djangoapps/student/views/management.py @@ -534,6 +534,7 @@ def activate_account(request, key): USER_ACCOUNT_ACTIVATED, { "user_id": registration.user.id, + "activation_timestamp": registration.activation_timestamp } )