o
    nѪh                     @   sH   d dl Z d dlZd dlmZ d dlmZ d dlmZ G dd deZdS )    N)BaseCommand)settings)ICICTRegistrantc                   @   s    e Zd ZdZdd Zdd ZdS )Commandz(Import ICICT registrations from CSV filec                 C   s&   |j dtddd |j dddd d S )	Nz--filez"icict_attendance/registrations.csvz-Path to the CSV file relative to project root)typedefaulthelpz	--dry-run
store_truez6Show what would be imported without actually importing)actionr   )add_argumentstr)selfparser r   ^/var/www/html/smartRegister/icict_attendance/management/commands/import_icict_registrations.pyadd_arguments   s   
zCommand.add_argumentsc                 O   sx  t jtj|d }|d }t j|s#| j| j	d|  d S d}d}d}t
|dddY}t|}	t|	dD ]E\}
}d	}d
|v rId
}||rU|| sVq<zt|| }|d  }|d  }|dd prd }|dd p|d }|dd pd }|dd  }|dkrd}nd}|r| jd| d| d|  |d7 }W q<tjj|||||||dd\}}|r|d7 }| j| jd| d| d nbd}|j|kr||_d}|j|kr||_d}|j|kr||_d}|j|kr||_d}|j|kr||_d}|j|kr||_d}|r8|  |d7 }| j| jd| d| d n|d7 }W q< ty` } z| j| j	d| d|  W Y d }~q<d }~w ty } z| j| j	d | d|  W Y d }~q<d }~ww W d    n	1 sw   Y  |r| j| jd!| d" d S | j| jd#| d$| d%| d& d S )'Nfiledry_runzCSV file not found: r   rzutf-8)encoding   IDu   ﻿IDNamezEmail AddressAffiliation zPhone NumberAddresszRegistration state	completedawaiting_paymentzWould import: z (ID: z) - )nameemailaffiliationphone_numberaddresspayment_status)registration_iddefaultsz
Imported: )FTz	Updated: zError processing row z: z Unexpected error processing row z!
Dry run completed. Would import z registrants.z
Import completed:
- Imported: z new registrants
- Updated: z! existing registrants
- Skipped: z unchanged registrants) ospathjoinr   BASE_DIRexistsstdoutwritestyleERRORopencsv
DictReader	enumerategetstripintlowerr   objectsget_or_createSUCCESSr   r   r#   r    r!   r"   saveWARNING
ValueError	Exception)r   argsoptions	file_pathr   imported_countupdated_countskipped_countcsvfilereaderrow_numrowid_fieldr$   r   r   r    r!   r"   payment_status_rawr#   
registrantcreatedupdateder   r   r   handle   s   




`zCommand.handleN)__name__
__module____qualname__r   r   rO   r   r   r   r   r      s    r   )	r1   r'   django.core.management.baser   django.confr   icict_attendance.modelsr   r   r   r   r   r   <module>   s    