def create_username(cls, person): username = clean_username( # First try : lastname + initials u"%s%s" % (person.lastname, person.initiales(person.firstname))) nb = cls.objects.filter(username=username).count() if nb > 0: if person.birthday == None: # Arrghh, pas assez d'infos.. on rajout un nombre à la con cls.objects.filter(username=username).count() > 0 username = clean_username( # First try : lastname + initials u"%s%s%s" % (person.lastname, person.initiales(person.firstname), nb)) return username # Second try : lastname + initials + birthday username = clean_username( u"%s%s%s" % ( person.lastname, person.initiales(person.firstname), person.birthday.strftime("%m%d"))) if cls.objects.filter(username=username).count() > 0: # Third try : lastname + full initials + full birthday username = clean_username( u"%s%s%s" % ( person.lastname, person.initiales(" ".join(person.firstnames)), person.birthday.strftime("%Y%m%d"))) if cls.objects.filter(username=username).count() > 0: raise Exception("World record or bug : jumeaux absolus !!") return username
def create_user_from_person(cls, person): username = clean_username(u"%s%s%s" % (person.lastname, person.initiales(person.firstname), person.birthday.strftime("%m%d"))) if User.objects.filter(username=username).count() > 0: username = clean_username(u"%s%s%s" % (person.lastname, person.initiales(" ".join(person.firstnames)), person.birthday.strftime("%m%d"))) if User.objects.filter(username=username).count() > 0: username = clean_username(u"%s%s%s" % (person.lastname, " ".join(person.firstnames), person.birthday.strftime("%m%d"))) if User.objects.filter(username=username).count() > 0: raise Exception("World record or bug : jumeaux absolus !!") user = User(username=username, first_name=person.firstname, last_name=person.lastname, email="", is_active=True, is_staff=False, is_superuser=False) initialpass = create_password() user.set_password(initialpass) user.save() if cls == Teacher: create_teacher_profile(user, person, initialpass) elif cls == Student: create_student_profile(user, person, initialpass) return username, initialpass
def test_clean_username(self): """ Testing username cleaning """ self.assertEqual(clean_username(u"Bàékmük"), u"baekmuk") self.assertEqual(clean_username(u"$toto"), u"toto")