Skip to content

Commit 18966d9

Browse files
committed
command to set auth ip
1 parent ee9dc6b commit 18966d9

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import json
2+
from django.core.management.base import BaseCommand
3+
4+
from judge.models import Profile
5+
6+
7+
class Command(BaseCommand):
8+
help = 'Set IP authentication for users from a JSON file'
9+
10+
def add_arguments(self, parser):
11+
parser.add_argument('file', help='Path to JSON file containing username and vpnIpAddress')
12+
13+
def handle(self, *args, **options):
14+
file_path = options['file']
15+
16+
data = json.load(open(file_path, 'r'))
17+
18+
updated_count = 0
19+
error_count = 0
20+
not_found_count = 0
21+
22+
for entry in data:
23+
username = entry.get('username')
24+
ip_address = entry.get('vpnIpAddress')
25+
26+
if not username or not ip_address:
27+
self.stderr.write(self.style.WARNING(
28+
f'Skipping entry with missing username or vpnIpAddress: {entry}'
29+
))
30+
error_count += 1
31+
continue
32+
33+
try:
34+
profile = Profile.objects.get(user__username=username)
35+
profile.ip_auth = ip_address
36+
profile.save(update_fields=['ip_auth'])
37+
updated_count += 1
38+
except Profile.DoesNotExist:
39+
self.stderr.write(self.style.WARNING(
40+
f'User not found: {username}'
41+
))
42+
not_found_count += 1
43+
except Exception as e:
44+
self.stderr.write(self.style.ERROR(
45+
f'Error updating {username}: {e}'
46+
))
47+
error_count += 1
48+
49+
self.stdout.write(self.style.SUCCESS(f'Total entries processed: {len(data)}'))
50+
self.stdout.write(self.style.SUCCESS(f'Successfully updated: {updated_count}'))
51+
if not_found_count > 0:
52+
self.stdout.write(self.style.WARNING(f'Users not found: {not_found_count}'))
53+
if error_count > 0:
54+
self.stdout.write(self.style.ERROR(f'Errors: {error_count}'))

0 commit comments

Comments
 (0)