User activity file includes failed logins matched by phone
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
ناهماهنگ نمیشود. تصمیمِ ادغامنکردنِ اینها در یک جدول: ستونهایشان فرق دارد.
|
ناهماهنگ نمیشود. تصمیمِ ادغامنکردنِ اینها در یک جدول: ستونهایشان فرق دارد.
|
||||||
آزموده نشده: کاربری با دهها هزار ردیف (همهی ردیفها در حافظه میآیند).
|
آزموده نشده: کاربری با دهها هزار ردیف (همهی ردیفها در حافظه میآیند).
|
||||||
"""
|
"""
|
||||||
|
from django.db.models import Q
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from payment.models import Order
|
from payment.models import Order
|
||||||
@@ -19,7 +20,9 @@ COLUMNS = ['زمان', 'نوع', 'شرح', 'مبلغ', 'آیپی']
|
|||||||
|
|
||||||
def user_activity(user: User) -> list[dict]:
|
def user_activity(user: User) -> list[dict]:
|
||||||
rows = [{'at': user.created_at, 'kind': 'ثبتنام', 'detail': '', 'amount': '', 'ip': ''}]
|
rows = [{'at': user.created_at, 'kind': 'ثبتنام', 'detail': '', 'amount': '', 'ip': ''}]
|
||||||
for e in LoginEvent.objects.filter(user_id=user.id):
|
# ورودِ ناموفق هنوز user_id ندارد (کاربر معلوم نیست)، پس با شماره هم میگیریم.
|
||||||
|
by_user = Q(user_id=user.id) | (Q(phone=user.phone) if user.phone else Q(pk__in=[]))
|
||||||
|
for e in LoginEvent.objects.filter(by_user):
|
||||||
rows.append({'at': e.created_at, 'kind': _EVENT_FA.get(e.event, e.event),
|
rows.append({'at': e.created_at, 'kind': _EVENT_FA.get(e.event, e.event),
|
||||||
'detail': e.user_agent[:80], 'amount': '', 'ip': e.ip or ''})
|
'detail': e.user_agent[:80], 'amount': '', 'ip': e.ip or ''})
|
||||||
orders = list(Order.objects.filter(user_id=user.id))
|
orders = list(Order.objects.filter(user_id=user.id))
|
||||||
|
|||||||
@@ -37,5 +37,10 @@ class UserActivityTests(TestCase):
|
|||||||
self.client.force_login(s)
|
self.client.force_login(s)
|
||||||
self.assertEqual(self.client.get(f'/admin/accounts/user/{self.user.id}/activity/').status_code, 403)
|
self.assertEqual(self.client.get(f'/admin/accounts/user/{self.user.id}/activity/').status_code, 403)
|
||||||
|
|
||||||
|
def test_failed_login_by_phone_is_included(self):
|
||||||
|
LoginEvent.objects.create(event='login_failed', phone=self.user.phone, ip='198.51.100.4')
|
||||||
|
r = self.client.get(f'/admin/accounts/user/{self.user.id}/activity/')
|
||||||
|
self.assertIn('ورود ناموفق', r.content.decode())
|
||||||
|
|
||||||
def test_unknown_user_is_404(self):
|
def test_unknown_user_is_404(self):
|
||||||
self.assertEqual(self.client.get('/admin/accounts/user/999999/activity/').status_code, 404)
|
self.assertEqual(self.client.get('/admin/accounts/user/999999/activity/').status_code, 404)
|
||||||
|
|||||||
Reference in New Issue
Block a user