Hey there! This problem might be a bit unpredictable, but give it a go and let us know how you do!
Create a Django management command that generates a daily report of user activity, including new user registrations, total logins, and the top 5 most active users. The command should accept an optional date parameter.Custom management commands
Daily digital detective dossier
Report a typo
Reorder lines using drag or arrows. Adjust indentation with left buttons
def add_arguments(self, parser):
class Command(BaseCommand):
parser.add_argument('--date', type=str, default=datetime.now().strftime('%Y-%m-%d'))
self.stdout.write(f"Report for {date.strftime('%Y-%m-%d')}:\nNew Users: {new_users}\nTotal Logins: {logins}\nMost Active Users: {', '.join([u.username for u in active_users])}")
new_users = User.objects.filter(date_joined__date=date).count()
logins = LoginLog.objects.filter(login_time__date=date).count()
date = datetime.strptime(options['date'], '%Y-%m-%d')
active_users = User.objects.annotate(login_count=Count('loginlog', filter=Q(loginlog__login_time__date=date))).order_by('-login_count')[:5]
def handle(self, *args, **options):
help = 'Generate daily user activity report'
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.