def _import_drupal_user(self, drupal_user): self.stdout.write(self.style.MIGRATE_LABEL( ' Start importing the Drupal user with ID: {0}'.format(drupal_user.uid)), ending='') # STEP 1: creates a user instance # -- try: legacy_profile = LegacyAccountProfile.objects \ .filter(origin=LegacyAccountProfile.DB_DRUPAL).get(legacy_id=str(drupal_user.uid)) user = legacy_profile.user except LegacyAccountProfile.DoesNotExist: user, _ = get_or_create_legacy_user( username=drupal_user.name, email=drupal_user.mail, hashed_password='******' + drupal_user.pass_field) LegacyAccountProfile.objects.create( user=user, legacy_id=str(drupal_user.uid), origin=LegacyAccountProfile.DB_DRUPAL) # STEP 2: associates the user to the proper Journal instances # -- # roles = Role.objects.all() # roles_dict = {r.rid: r for r in roles} # noqa ; should we use this? journals = Journal.objects.filter(collection__code='erudit') for journal in journals: db_id = 'drupalerudit_' + journal.code try: drupal_user_roles = list( UsersRoles.objects.using(db_id).filter(uid=drupal_user.uid) .values_list('rid')) except OperationalError: self.stdout.write(self.style.WARNING(' inexistant DB: "{}"'.format(db_id))) if drupal_user_roles: # Add the user to the members of the considered Journal journal.members.add(user) self.stdout.write(self.style.SUCCESS(' [OK]'))
def _import_drupal_user(self, drupal_user): self.stdout.write(self.style.MIGRATE_LABEL( ' Start importing the Drupal user with ID: {0}'.format(drupal_user.uid)), ending='') # STEP 1: creates a user instance # -- try: legacy_profile = LegacyAccountProfile.objects \ .filter(origin=LegacyAccountProfile.DB_DRUPAL).get(legacy_id=str(drupal_user.uid)) user = legacy_profile.user except LegacyAccountProfile.DoesNotExist: user = get_or_create_legacy_user( username=drupal_user.name, email=drupal_user.mail, hashed_password='******' + drupal_user.pass_field) LegacyAccountProfile.objects.create( user=user, legacy_id=str(drupal_user.uid), origin=LegacyAccountProfile.DB_DRUPAL) # STEP 2: associates the user to the proper Journal instances # -- # roles = Role.objects.all() # roles_dict = {r.rid: r for r in roles} # noqa ; should we use this? journals = Journal.objects.filter(collection__code='erudit') for journal in journals: db_id = 'drupalerudit_' + journal.code try: drupal_user_roles = list( UsersRoles.objects.using(db_id).filter(uid=drupal_user.uid) .values_list('rid')) except OperationalError: self.stdout.write(self.style.WARNING(' inexistant DB: "{}"'.format(db_id))) if drupal_user_roles: # Add the user to the members of the considered Journal journal.members.add(user) self.stdout.write(self.style.MIGRATE_SUCCESS(' [OK]'))
def import_abonnes(self): if LegacyAccountProfile.objects \ .filter(origin=LegacyAccountProfile.DB_ABONNEMENTS).count() > 0: self.stdout.write("Some accounts are already present on destination \ table. Importation canceled.") return for old_abonne in Abonneindividus.objects.all(): try: LegacyAccountProfile.objects.get( origin=LegacyAccountProfile.DB_ABONNEMENTS, legacy_id=str(old_abonne.abonneindividusid)) except LegacyAccountProfile.DoesNotExist: hasher = PBKDF2WrappedAbonnementsSHA1PasswordHasher() user = get_or_create_legacy_user( username='******'.format(old_abonne.abonneindividusid), email=old_abonne.courriel, hashed_password=hasher.encode_sha1_hash(old_abonne.password, hasher.salt())) user.first_name = old_abonne.prenom user.last_name = old_abonne.nom user.save() LegacyAccountProfile.objects.create( origin=LegacyAccountProfile.DB_ABONNEMENTS, user=user, legacy_id=str(old_abonne.abonneindividusid))
def _import_restriction_subscription(self, restriction_subscription): logger.info('Process subscription with ID: {0}'.format( restriction_subscription.id)) # Fetches the subscriber try: restriction_subscriber = Abonne.objects.get( pk=restriction_subscription.abonneid) except Abonne.DoesNotExist: logger.error( 'Unable to retrieve the "Abonne" instance with ID: {0}'.format( restriction_subscription.abonneid)) raise ImportException # Fetch the related journal try: restriction_journal = Revue.objects.get( revueid=restriction_subscription.revueid) except Revue.DoesNotExist: logger.error( 'Unable to retrieve the "Revue" instance with ID: {0}'.format( restriction_subscription.revueid)) raise ImportException # STEP 1: gets or creates the RestrictionProfile instance # -- try: restriction_profile = LegacyAccountProfile.objects \ .filter(origin=LegacyAccountProfile.DB_RESTRICTION) \ .get(legacy_id=str(restriction_subscriber.pk)) except LegacyAccountProfile.DoesNotExist: username = '******'.format(restriction_subscriber.pk) user, created = get_or_create_legacy_user( username=username, email=restriction_subscriber.courriel) if created: created_objects['user'] += 1 logger.debug( "User created={}, pk={}, username={}, email={}".format( created, user.pk, username, restriction_subscriber.courriel)) organisation, created = Organisation.objects.get_or_create( name=restriction_subscriber.abonne[:120]) if created: logger.debug( "Organisation created=True, pk={}, name={}".format( organisation.pk, organisation.name)) restriction_profile = LegacyAccountProfile.objects.create( origin=LegacyAccountProfile.DB_RESTRICTION, legacy_id=str(restriction_subscriber.pk), user=user, organisation=organisation) if restriction_subscriber.icone: f = open( op.join(restriction_settings.ABONNE_ICONS_PATH, restriction_subscriber.icone), 'rb') image_file = File(f) organisation.badge.save(restriction_subscriber.icone, image_file, save=True) organisation.save() f.close() # STEP 2: gets or creates a JournalAccessSubscription instance # -- try: journal_code = restriction_journal.titrerevabr.lower() journal = Journal.legacy_objects.get_by_id(journal_code) except Journal.DoesNotExist: logger.error( 'Unable to retrieve the "Journal" instance with code: {0}'. format(restriction_journal.titrerevabr)) raise ImportException subscription, created = JournalAccessSubscription.objects.get_or_create( organisation=restriction_profile.organisation) if created: created_objects['subscription'] += 1 logger.debug("subscription created={}, pk={}".format( created, subscription.pk)) if not subscription.journals.filter(pk=journal.pk): subscription.journals.add(journal) logger.debug("subscription pk={} add journal pk={}".format( subscription.pk, journal.pk)) # STEP 3: creates the subscription period # -- start_date = dt.date(restriction_subscription.anneeabonnement, 2, 1) end_date = dt.date(restriction_subscription.anneeabonnement + 1, 2, 1) subscription_period, created = JournalAccessSubscriptionPeriod.objects.get_or_create( subscription=subscription, start=start_date, end=end_date) if created: created_objects['period'] += 1 logger.debug( "period created={}, pk={}, subscription_pk={}, start={}, end={}" .format(created, subscription_period.pk, subscription.pk, start_date, end_date)) try: subscription_period.clean() except ValidationError as ve: # We are saving multiple periods for multiple journals under the same subscription # instance so period validation errors can happen. logger.error( 'Cannot save the subscription period : {0}'.format(ve)) raise else: subscription_period.save() # STEP 5: create the subscription referer # -- if restriction_subscriber.referer: referer, created = InstitutionReferer.objects.get_or_create( subscription=subscription, referer=restriction_subscriber.referer) if created: logger.debug( "Referer created=True, subscription_pk={}, referer={}". format(subscription.pk, restriction_subscriber.referer)) # STEP 4: creates the IP whitelist associated with the subscription # -- restriction_subscriber_ips_set1 = Ipabonne.objects.filter( abonneid=str(restriction_subscriber.pk)) for ip in restriction_subscriber_ips_set1: ip_start, ip_end = self._get_ip_range_from_ip(ip.ip) ip_range, created = InstitutionIPAddressRange.objects.get_or_create( subscription=subscription, ip_start=ip_start, ip_end=ip_end) if created: created_objects['iprange'] += 1 logger.debug( "Ipabonne created={}, pk={}, ip_start={}, ip_end={}". format(created, ip_range.pk, ip_start, ip_end)) restriction_subscriber_ips_set2 = Adressesip.objects.filter( abonneid=restriction_subscriber.pk) for ip in restriction_subscriber_ips_set2: ip_start, ip_end = self._get_ip_range_from_ip(ip.ip) ip_range, created = InstitutionIPAddressRange.objects.get_or_create( subscription=subscription, ip_start=ip_start, ip_end=ip_end) if created: created_objects['iprange'] += 1 logger.debug( "Ipabonne created={}, pk={}, ip_start={}, ip_end={}". format(created, ip_range.pk, ip_start, ip_end)) restriction_subscriber_ips_ranges = Ipabonneinterval.objects.filter( abonneid=restriction_subscriber.pk) for ip_range in restriction_subscriber_ips_ranges: ip_start = self._get_ip(ip_range.debutinterval, repl='0') ip_end = self._get_ip(ip_range.fininterval, repl='255') ip_range, created = InstitutionIPAddressRange.objects.get_or_create( subscription=subscription, ip_start=ip_start, ip_end=ip_end) if created: created_objects['iprange'] += 1 logger.debug( "Ipabonneinterval created={}, pk={}, ip_start={}, ip_end={}" .format(created, ip_range.pk, ip_start, ip_end))
def _import_restriction_subscription(self, restriction_subscription): self.stdout.write(self.style.MIGRATE_LABEL( ' Start importing the subscription with ID: {0}'.format( restriction_subscription.id)), ending='') # Fetches the subscriber try: restriction_subscriber = Abonne.objects.get(pk=restriction_subscription.abonneid) except Abonne.DoesNotExist: self.stdout.write(self.style.ERROR( ' Unable to retrieve the "Abonne" instance with ID: {0}'.format( restriction_subscription.abonneid))) raise ImportException # Fetch the related journal try: restriction_journal = Revue.objects.get(revueid=restriction_subscription.revueid) except Revue.DoesNotExist: self.stdout.write(self.style.ERROR( ' Unable to retrieve the "Revue" instance with ID: {0}'.format( restriction_subscription.revueid))) raise ImportException # STEP 1: gets or creates the RestrictionProfile instance # -- try: restriction_profile = LegacyAccountProfile.objects \ .filter(origin=LegacyAccountProfile.DB_RESTRICTION) \ .get(legacy_id=str(restriction_subscriber.pk)) except LegacyAccountProfile.DoesNotExist: user = get_or_create_legacy_user( username='******'.format(restriction_subscriber.pk), email=restriction_subscriber.courriel) organisation = Organisation.objects.create(name=restriction_subscriber.abonne[:120]) restriction_profile = LegacyAccountProfile.objects.create( origin=LegacyAccountProfile.DB_RESTRICTION, legacy_id=str(restriction_subscriber.pk), user=user, organisation=organisation) if restriction_subscriber.icone: f = open( op.join(restriction_settings.ABONNE_ICONS_PATH, restriction_subscriber.icone), 'rb') image_file = File(f) organisation.badge.save(restriction_subscriber.icone, image_file, save=True) organisation.save() f.close() # STEP 2: gets or creates a JournalAccessSubscription instance # -- try: journal_code = restriction_journal.titrerevabr.lower() journal = Journal.objects.get(Q(localidentifier=journal_code) | Q(code=journal_code)) except Journal.DoesNotExist: self.stdout.write(self.style.ERROR( ' Unable to retrieve the "Journal" instance with code: {0}'.format( restriction_journal.titrerevabr))) raise ImportException subscription, _ = JournalAccessSubscription.objects.get_or_create( organisation=restriction_profile.organisation) subscription.journals.add(journal) # STEP 3: creates the subscription period # -- subscription_period, _ = JournalAccessSubscriptionPeriod.objects.get_or_create( subscription=subscription, start=dt.date(restriction_subscription.anneeabonnement, 2, 1), end=dt.date(restriction_subscription.anneeabonnement + 1, 2, 1)) try: subscription_period.clean() except ValidationError as ve: # We are saving multiple periods for multiple journals under the same subscription # instance so period validation errors can happen. self.stdout.write(self.style.ERROR( ' Cannot save the subscription period : {0}'.format( ve))) raise else: subscription_period.save() # STEP 4: creates the IP whitelist associated with the subscription # -- restriction_subscriber_ips_set1 = Ipabonne.objects.filter( abonneid=str(restriction_subscriber.pk)) for ip in restriction_subscriber_ips_set1: ip_start, ip_end = self._get_ip_range_from_ip(ip.ip) InstitutionIPAddressRange.objects.get_or_create( subscription=subscription, ip_start=ip_start, ip_end=ip_end) restriction_subscriber_ips_set2 = Adressesip.objects.filter( abonneid=restriction_subscriber.pk) for ip in restriction_subscriber_ips_set2: ip_start, ip_end = self._get_ip_range_from_ip(ip.ip) InstitutionIPAddressRange.objects.get_or_create( subscription=subscription, ip_start=ip_start, ip_end=ip_end) restriction_subscriber_ips_ranges = Ipabonneinterval.objects.filter( abonneid=restriction_subscriber.pk) for ip_range in restriction_subscriber_ips_ranges: ip_start = self._get_ip(ip_range.debutinterval, repl='0') ip_end = self._get_ip(ip_range.fininterval, repl='255') InstitutionIPAddressRange.objects.get_or_create( subscription=subscription, ip_start=ip_start, ip_end=ip_end) self.stdout.write(self.style.MIGRATE_SUCCESS(' [OK]'))