# Path: osf/models/registration_bulk_upload_job.py
# class JobState(IntEnum):
#     """Defines the six states of registration bulk upload jobs.
#     """
# 
#     PENDING = 0  # Database preparation is in progress
#     INITIALIZED = 1  # Database preparation has been finished and is ready to be picked up
#     PICKED_UP = 2  # Registration creation is in progress
#     DONE_FULL = 3  # All (draft) registrations have been successfully created
#     DONE_PARTIAL = 4  # Some (draft) registrations have failed the creation creation process
#     DONE_ERROR = 5  # All (draft) registrations have failed
# 
# Path: osf/models/registration_bulk_upload_row.py
# class RegistrationBulkUploadContributors:
#     """A helper class of which an instance contains parsed data about contributors per registration row.
#     """
# 
#     def __init__(self, admin_set, read_only_set, read_write_set, author_set, contributor_list):
#         self.contributor_list = contributor_list
#         self.admin_set = admin_set
#         self.read_write_set = read_write_set
#         self.read_only_set = read_only_set
#         self.author_set = author_set
# 
#     def is_bibliographic(self, email):
#         return email in self.author_set
# 
#     def get_permission(self, email):
#         if email in self.admin_set:
#             return ADMIN
#         elif email in self.read_write_set:
#             return WRITE
#         elif email in self.read_only_set:
#             return READ
#         else:
#             raise RegistrationBulkCreationContributorError(error=f'{email} does not have a permission')


import logging
from django.core.exceptions import ValidationError
from django.db.utils import IntegrityError
from django.utils import timezone
from framework import sentry
from framework.auth import Auth
from framework.celery_tasks import app as celery_app
from osf.exceptions import (
    NodeStateError,
    RegistrationBulkCreationContributorError,
    RegistrationBulkCreationRowError,
    UserNotAffiliatedError,
    UserStateError,
    ValidationValueError,
)
from osf.models import (
    AbstractNode,
    Contributor,
    DraftRegistration,
    Institution,
    OSFUser,
    RegistrationBulkUploadJob,
    RegistrationBulkUploadRow,
    RegistrationProvider,
    RegistrationSchema,
    Subject,
)
from osf.models.licenses import NodeLicense
from osf.models.registration_bulk_upload_job import JobState
from osf.models.registration_bulk_upload_row import RegistrationBulkUploadContributors
from osf.registrations.utils import get_registration_provider_submissions_url
from osf.utils.permissions import ADMIN
from website import mails, settings
        upload.delete()
        return handle_internal_error(initiator=initiator, provider=provider, message=repr(e), dry_run=dry_run)

    # Cancel the preparation task if duplicates are found in the CSV and/or in DB
    if draft_error_list:
        upload.delete()
        mails.send_mail(
            to_addr=initiator.username,
            mail=mails.REGISTRATION_BULK_UPLOAD_FAILURE_DUPLICATES,
            fullname=initiator.fullname,
            count=initial_row_count,
            draft_errors=draft_error_list,
            osf_support_email=settings.OSF_SUPPORT_EMAIL,
        )
        return

    if dry_run:
        logger.info('Dry run: complete. Bulk creation did not run and emails are not sent.')
        return

    try:
        logger.info(f'Bulk creating [{len(bulk_upload_rows)}] registration rows ...')
        created_objects = RegistrationBulkUploadRow.objects.bulk_create(bulk_upload_rows)
    except (ValueError, IntegrityError):
        upload.delete()
        sentry.log_exception()
        message = 'Bulk upload preparation failure: failed to create the rows.'
        return handle_internal_error(initiator=initiator, provider=provider, message=message, dry_run=dry_run)
    logger.info(f'[{len(created_objects)}] rows successfully prepared.')


# Path: osf/models/registration_bulk_upload_job.py
# class JobState(IntEnum):
#     """Defines the six states of registration bulk upload jobs.
#     """
# 
#     PENDING = 0  # Database preparation is in progress
#     INITIALIZED = 1  # Database preparation has been finished and is ready to be picked up
#     PICKED_UP = 2  # Registration creation is in progress
#     DONE_FULL = 3  # All (draft) registrations have been successfully created
#     DONE_PARTIAL = 4  # Some (draft) registrations have failed the creation creation process
#     DONE_ERROR = 5  # All (draft) registrations have failed
# 
# Path: osf/models/registration_bulk_upload_row.py
# class RegistrationBulkUploadContributors:
#     """A helper class of which an instance contains parsed data about contributors per registration row.
#     """
# 
#     def __init__(self, admin_set, read_only_set, read_write_set, author_set, contributor_list):
#         self.contributor_list = contributor_list
#         self.admin_set = admin_set
#         self.read_write_set = read_write_set
#         self.read_only_set = read_only_set
#         self.author_set = author_set
# 
#     def is_bibliographic(self, email):
#         return email in self.author_set
# 
#     def get_permission(self, email):
#         if email in self.admin_set:
#             return ADMIN
#         elif email in self.read_write_set:
#             return WRITE
#         elif email in self.read_only_set:
#             return READ
#         else:
#             raise RegistrationBulkCreationContributorError(error=f'{email} does not have a permission')


# Path: api/providers/tasks.py
import logging
from django.core.exceptions import ValidationError
from django.db.utils import IntegrityError
from django.utils import timezone
from framework import sentry
from framework.auth import Auth
from framework.celery_tasks import app as celery_app
from osf.exceptions import (
    NodeStateError,
    RegistrationBulkCreationContributorError,
    RegistrationBulkCreationRowError,
    UserNotAffiliatedError,
    UserStateError,
    ValidationValueError,
)
from osf.models import (
    AbstractNode,
    Contributor,
    DraftRegistration,
    Institution,
    OSFUser,
    RegistrationBulkUploadJob,
    RegistrationBulkUploadRow,
    RegistrationProvider,
    RegistrationSchema,
    Subject,
)
from osf.models.licenses import NodeLicense
from osf.models.registration_bulk_upload_job import JobState
from osf.models.registration_bulk_upload_row import RegistrationBulkUploadContributors
from osf.registrations.utils import get_registration_provider_submissions_url
from osf.utils.permissions import ADMIN
from website import mails, settings

        upload.delete()
        return handle_internal_error(initiator=initiator, provider=provider, message=repr(e), dry_run=dry_run)

    # Cancel the preparation task if duplicates are found in the CSV and/or in DB
    if draft_error_list:
        upload.delete()
        mails.send_mail(
            to_addr=initiator.username,
            mail=mails.REGISTRATION_BULK_UPLOAD_FAILURE_DUPLICATES,
            fullname=initiator.fullname,
            count=initial_row_count,
            draft_errors=draft_error_list,
            osf_support_email=settings.OSF_SUPPORT_EMAIL,
        )
        return

    if dry_run:
        logger.info('Dry run: complete. Bulk creation did not run and emails are not sent.')
        return

    try:
        logger.info(f'Bulk creating [{len(bulk_upload_rows)}] registration rows ...')
        created_objects = RegistrationBulkUploadRow.objects.bulk_create(bulk_upload_rows)
    except (ValueError, IntegrityError):
        upload.delete()
        sentry.log_exception()
        message = 'Bulk upload preparation failure: failed to create the rows.'
        return handle_internal_error(initiator=initiator, provider=provider, message=message, dry_run=dry_run)
    logger.info(f'[{len(created_objects)}] rows successfully prepared.')


    upload.state = JobState.INITIALIZED