
| Current Path : /var/www/wsgi/www/api/core/management/commands/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/wsgi/www/api/core/management/commands/import_employees.py |
from django.core.management.base import BaseCommand
import csv
from core.models import User, EmployeeProfile, Company
from django.contrib.auth.hashers import make_password
class Command(BaseCommand):
help = 'Import employees from CSV'
def add_arguments(self, parser):
parser.add_argument('company_id', type=int)
parser.add_argument('csv_file', type=str)
def handle(self, *args, **options):
company = Company.objects.get(pk=options['company_id'])
with open(options['csv_file']) as f:
reader = csv.DictReader(f)
for row in reader:
user = User.objects.create(
username=row['username'],
email=row['email'],
first_name=row['first_name'],
last_name=row['last_name'],
password=make_password(row['password']),
role='EMPLOYEE',
company=company
)
EmployeeProfile.objects.create(
user=user,
matricule=row['matricule'],
salary=row['salary'],
hire_date=row['hire_date']
)