Skip to content

Commit

Permalink
Fix importing staff list command and add an option tally id
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnMwashuma committed Nov 14, 2024
1 parent e69dc24 commit 66f9442
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions tally_ho/apps/tally/management/commands/import_staff_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.core.management.base import BaseCommand
from django.utils.translation import gettext_lazy

from tally_ho.apps.tally.models.tally import Tally
from tally_ho.libs.permissions import groups
from tally_ho.apps.tally.models.user_profile import UserProfile

Expand Down Expand Up @@ -41,42 +42,51 @@


def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs):
# csv.py doesn't do Unicode; encode temporarily as UTF-8:
csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),
dialect=dialect, **kwargs)
csv_reader = csv.reader(unicode_csv_data, dialect=dialect, **kwargs)
for row in csv_reader:
# decode UTF-8 back to Unicode, cell by cell:
yield [str(cell, 'utf-8') for cell in row]
yield row


def utf_8_encoder(unicode_csv_data):
for line in unicode_csv_data:
yield line.encode('utf-8')


def add_row(command, name, username, role, admin=None):
def add_row(command, name, username, role, admin=None, tally_id=None):
try:
# Parse the name into first and last name
first_name, last_name = assign_names(name)
user = create_user(first_name, last_name, username)

tally = None
if tally_id:
try:
tally = Tally.objects.get(id=tally_id)
except Tally.DoesNotExist:
command.stdout.write(command.style.ERROR(
f"Tally with id '{tally_id}' does not exist."
))
return # Exit function if the Tally is not found

user = create_user(first_name, last_name, username, tally)

permission = True if admin == 'Yes' else False
user.is_superuser = user.is_staff = permission
user.save()

except Exception as e:
command.stdout.write(command.style.ERROR(
"User '%s' not created! '%s'" % (username, e)))
f"User '{username}' not created! '{e}'"
))
else:
system_role = STAFF_ROLE_DICT.get(role.upper().strip())

if system_role:
group = Group.objects.get_or_create(
name=system_role)[0]
group = Group.objects.get_or_create(name=system_role)[0]
user.groups.add(group)
else:
command.stdout.write(command.style.ERROR(
"Unable to add user %s to unknown group '%s'."
% (username, role)))
f"Unable to add user {username} to unknown group '{role}'."
))


def assign_names(name):
Expand All @@ -91,22 +101,27 @@ def assign_names(name):
return first_name, last_name


def create_user(first_name, last_name, username):
def create_user(first_name, last_name, username, tally=None):
try:
return UserProfile.objects.get(username=username)
except UserProfile.DoesNotExist:
return UserProfile.objects.create_user(
username, password=username,
user = UserProfile.objects.create_user(
username=username,
password=username,
first_name=first_name,
last_name=last_name)
last_name=last_name
)
if tally:
user.tally = tally
user.save()
return user


class Command(BaseCommand):
help = gettext_lazy("Import staff list.")

def handle(self, *args, **kwargs):
self.import_staff_list()
self.import_user_list()

def import_staff_list(self):
with codecs.open(STAFF_LIST_PATH, encoding='utf-8') as f:
Expand All @@ -116,12 +131,15 @@ def import_staff_list(self):
for row in reader:
try:
name, username, role, admin = row[0:4]
tally_id =\
row[4].strip()\
if len(row) > 4 and row[4].strip() else None
except Exception as e:
self.stdout.write(self.style.ERROR(
'Unable to add user in row: %s. Exception %s.' %
(row, e)))
f'Unable to add user in row: {row}. Exception: {e}.'
))
else:
add_row(self, name, username, role, admin)
add_row(self, name, username, role, admin, tally_id)

def import_user_list(self):
with codecs.open(USER_LIST_PATH, encoding='utf-8') as f:
Expand Down

0 comments on commit 66f9442

Please sign in to comment.