def test_valid_registration_sibel(self): nt.assert_equal(len(self.user.django_user.groups.all()), 2) personal_group = self.user.django_user.groups.all()[0] public_group = self.user.django_user.groups.all()[1] public_groups = get_public_groups() nt.assert_equal("*****@*****.**", personal_group.name) nt.assert_in(public_group, public_groups)
def main(): """Imports metpetdb's various tables into Django for auth purposes. BEFORE RUNNING THIS SCRIPT, execute the following SQL: ALTER TABLE users ADD COLUMN django_user_id int UNIQUE REFERENCES auth_user(id); This function is idempotent, but shouldn't need to be run multiple times. """ for metpet_user in MetpetUser.objects.filter(django_user=None): logger.info("Transitioning %s.", metpet_user.name) email = metpet_user.email logger.debug("Email = %s", email) # Use the email for the username, but strip out disallowed characters # and cap total length at 30 characters to comply with Django's # requirements: username = ''.join(c for c in email if c.isalnum() or c in ['_', '@', '+', '.', '-'])[:30] logger.debug("Username = %s", username) password = translate(bytearray(metpet_user.password)) if password == 'sha1$$': logger.warning("%s (%s) has an unusable password and won't be "+ "able to log in.", metpet_user.name, email) logger.debug("Password hash = %s", password) result = AuthUser(username=username, password=password, email=email, is_staff=False, is_active=True, is_superuser=False) result.save() ApiKey.objects.create(user=result) metpet_user.django_user = result metpet_user.password = password metpet_user.save() if metpet_user.enabled.upper() == 'Y': # Add user to public group(s), so (s)he can read public things logger.info("Adding %s to public group.", metpet_user.name) metpet_user.auto_verify(None) # Pass None to skip code check if metpet_user.contributor_enabled.upper() == 'Y': # Add user to personal group, so (s)he can create things logger.info("Adding %s to personal group.", metpet_user.name) metpet_user.manual_verify() metpet_user.save() models_with_owners = [Sample, Image] models_with_public_data = [Sample, Image, Subsample, ChemicalAnalyses, Grid] public_groups = get_public_groups() for Model in models_with_owners: ctype = ContentType.objects.get_for_model(Model) for item in Model.objects.all(): owner = item.user owner_django = owner.django_user try: owner_group = Group.objects.get(groupextra__owner=owner_django, groupextra__group_type='u_uid') except Group.DoesNotExist: logger.warning("Skipping item %s, owner %s doesn't have a group.", item, owner) continue # skip this item if GroupAccess.objects.filter(group=owner_group, content_type=ctype, object_id=item.pk).exists(): continue # Already been done, skip it GroupAccess(group=owner_group, read_access=True, write_access=True, content_type=ctype, object_id=item.pk).save() for Model in models_with_public_data: ctype = ContentType.objects.get_for_model(Model) for item in Model.objects.filter(public_data__iexact='y'): for group in public_groups: if GroupAccess.objects.filter(group=group, content_type=ctype, object_id=item.pk).exists(): continue # Object is already in this group GroupAccess(group=group, content_type=ctype, object_id=item.pk, read_access=True, write_access=False).save()
def main(): """Imports metpetdb's various tables into Django for auth purposes. BEFORE RUNNING THIS SCRIPT, execute the following SQL: ALTER TABLE users ADD COLUMN django_user_id int UNIQUE REFERENCES auth_user(id); This function is idempotent, but shouldn't need to be run multiple times. """ for metpet_user in MetpetUser.objects.filter(django_user=None): logger.info("Transitioning %s.", metpet_user.name) email = metpet_user.email logger.debug("Email = %s", email) # Use the email for the username, but strip out disallowed characters # and cap total length at 30 characters to comply with Django's # requirements: username = ''.join( c for c in email if c.isalnum() or c in ['_', '@', '+', '.', '-'])[:30] logger.debug("Username = %s", username) password = translate(bytearray(metpet_user.password)) if password == 'sha1$$': logger.warning( "%s (%s) has an unusable password and won't be " + "able to log in.", metpet_user.name, email) logger.debug("Password hash = %s", password) result = AuthUser(username=username, password=password, email=email, is_staff=False, is_active=True, is_superuser=False) result.save() ApiKey.objects.create(user=result) metpet_user.django_user = result metpet_user.password = password metpet_user.save() if metpet_user.enabled.upper() == 'Y': # Add user to public group(s), so (s)he can read public things logger.info("Adding %s to public group.", metpet_user.name) metpet_user.auto_verify(None) # Pass None to skip code check if metpet_user.contributor_enabled.upper() == 'Y': # Add user to personal group, so (s)he can create things logger.info("Adding %s to personal group.", metpet_user.name) metpet_user.manual_verify() metpet_user.save() models_with_owners = [Sample, Image] models_with_public_data = [ Sample, Image, Subsample, ChemicalAnalyses, Grid ] public_groups = get_public_groups() for Model in models_with_owners: ctype = ContentType.objects.get_for_model(Model) for item in Model.objects.all(): owner = item.user owner_django = owner.django_user try: owner_group = Group.objects.get(groupextra__owner=owner_django, groupextra__group_type='u_uid') except Group.DoesNotExist: logger.warning( "Skipping item %s, owner %s doesn't have a group.", item, owner) continue # skip this item if GroupAccess.objects.filter(group=owner_group, content_type=ctype, object_id=item.pk).exists(): continue # Already been done, skip it GroupAccess(group=owner_group, read_access=True, write_access=True, content_type=ctype, object_id=item.pk).save() for Model in models_with_public_data: ctype = ContentType.objects.get_for_model(Model) for item in Model.objects.filter(public_data__iexact='y'): for group in public_groups: if GroupAccess.objects.filter(group=group, content_type=ctype, object_id=item.pk).exists(): continue # Object is already in this group GroupAccess(group=group, content_type=ctype, object_id=item.pk, read_access=True, write_access=False).save()