Skip to content

Commit

Permalink
Add find users script and write headers into csv exports
Browse files Browse the repository at this point in the history
  • Loading branch information
triole committed Dec 26, 2021
1 parent 871649e commit c0fcf31
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 11 deletions.
29 changes: 18 additions & 11 deletions rdmo/core/management/commands/find_spam_users.py
@@ -1,5 +1,4 @@
import csv
import os

from django.contrib.auth.models import User
from django.core.management.base import BaseCommand
Expand All @@ -9,10 +8,6 @@

class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument(
'-o', '--output_file', default='potential_spam_users.csv',
help='output file, default is \'potential_spam_users.csv\''
)
parser.add_argument(
'-t', '--timespan', default=2, type=int,
help='timespan in seconds between two joining users, ' +
Expand All @@ -25,19 +20,29 @@ def add_arguments(self, parser):
'violations at which users are put into the ' +
'potential spam users list, default is 3'
)

def rm(self, filename):
if os.path.exists(filename):
os.remove(filename)
parser.add_argument(
'-p', '--print', action='store_true',
help='print found users, don\'t save them to csv'
)
parser.add_argument(
'-o', '--output_file', default='potential_spam_users.csv',
help='output file, default is \'potential_spam_users.csv\''
)

def save_csv(self, data, filename):
self.rm(filename)
data_file = open(filename, 'w')
csv_writer = csv.writer(data_file)
csv_writer.writerow(list(data[0].keys()))
for user in data:
csv_writer.writerow(user.values())
print('List written to ' + filename)

def print_file(self, filename):
f = open(filename, 'r')
content = f.read()
print(content)
f.close()

def get_users_having_projects(self):
arr = []
memberships = Membership.objects.all().values_list('user')
Expand Down Expand Up @@ -133,7 +138,7 @@ def handle(self, *args, **options):
)

print(
'''Potential spam users: %d %.2f%% / of which have at least one project %d'''
'Potential spam users: %d %.2f%% / of which have at least one project %d'
% (
len(potential_spam_users),
(100/no_total_users)*len(potential_spam_users),
Expand All @@ -142,3 +147,5 @@ def handle(self, *args, **options):
)

self.save_csv(potential_spam_users, options['output_file'])
if options['print'] is True:
self.print_file(options['output_file'])
114 changes: 114 additions & 0 deletions rdmo/core/management/commands/find_users.py
@@ -0,0 +1,114 @@
import csv
import re

from django.contrib.auth.models import User
from django.core.management.base import BaseCommand

from rdmo.projects.models import Membership


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument(
'--id', default='.*',
help='find users by id'
)
parser.add_argument(
'--email', default='.*',
help='find users by email'
)
parser.add_argument(
'--username', default='.*',
help='find users by username'
)
parser.add_argument(
'--first_name', default='.*',
help='find users by first name'
)
parser.add_argument(
'--last_name', default='.*',
help='find users by last name'
)
parser.add_argument(
'-p', '--print', action='store_true',
help='print found users, don\'t save them to csv'
)
parser.add_argument(
'-o', '--output_file', default='found_users.csv',
help='output file, default is \'found_users.csv\''
)

def save_csv(self, data, filename):
data_file = open(filename, 'w')
csv_writer = csv.writer(data_file)
csv_writer.writerow(list(data[0].keys()))
for user in data:
csv_writer.writerow(user.values())
print('List written to ' + filename)

def print_file(self, filename):
f = open(filename, 'r')
content = f.read()
print(content)
f.close()

def get_users_having_projects(self):
arr = []
memberships = Membership.objects.all().values_list('user')
for mem in memberships:
arr.append(
User.objects.filter(id=mem[0]).values('id')[0]['id']
)
return arr

def rx_match(self, regex, s):
return bool(re.search(regex, str(s)))

def check_match(self, user, options):
if self.rx_match(options['id'], user.id) is False:
return False
if self.rx_match(options['email'], user.email) is False:
return False
if self.rx_match(options['username'], user.username) is False:
return False
if self.rx_match(options['last_name'], user.last_name) is False:
return False
if self.rx_match(options['first_name'], user.first_name) is False:
return False
return True

def find_users(self, options):
found_users = []
for _, user in enumerate(User.objects.all().order_by('date_joined')):
m = self.check_match(user, options)
if m is True:
found_users.append(
{
'id': user.id,
'username': user.username,
'first_name': user.first_name,
'last_name': user.last_name,
'date_joined': user.date_joined,
'unix_joined': user.date_joined.timestamp(),
'email': user.email,
'last_login': user.last_login,
}
)
return found_users

def handle(self, *args, **options):
no_total_users = User.objects.all().count()
print('Total no of users: %d' % (no_total_users))
found_users = self.find_users(options)

print(
'Matching filter the: %d %.2f%%'
% (
len(found_users),
(100/no_total_users)*len(found_users)
)
)

self.save_csv(found_users, options['output_file'])
if options['print'] is True:
self.print_file(options['output_file'])

0 comments on commit c0fcf31

Please sign in to comment.