def save(self, *args, **kwargs): if self.pk is None: # Creating a new object # Freeze applicant contact information assert self.applicant_id is not None, u'Inforequest.applicant is mandatory' assert self.applicant_name == u'', u'Inforequest.applicant_name is read-only' assert self.applicant_street == u'', u'Inforequest.applicant_street is read-only' assert self.applicant_city == u'', u'Inforequest.applicant_city is read-only' assert self.applicant_zip == u'', u'Inforequest.applicant_zip is read-only' self.applicant_name = self.applicant.get_full_name() self.applicant_street = self.applicant.profile.street self.applicant_city = self.applicant.profile.city self.applicant_zip = self.applicant.profile.zip # Generate unique random email assert self.unique_email == u'', u'Inforequest.unique_email is read-only' length = 4 while True: token = random_readable_string(length) self.unique_email = settings.INFOREQUEST_UNIQUE_EMAIL.format(token=token) try: with transaction.atomic(): super(Inforequest, self).save(*args, **kwargs) except IntegrityError: length += 1 if length <= 10: continue self.unique_email = None raise # Give up return # object is already saved super(Inforequest, self).save(*args, **kwargs)
def save(self, *args, **kwargs): if self.pk is None: # Creating a new object # Freeze applicant contact information assert self.applicant_id is not None, u'Inforequest.applicant is mandatory' assert self.applicant_name == u'', u'Inforequest.applicant_name is read-only' assert self.applicant_street == u'', u'Inforequest.applicant_street is read-only' assert self.applicant_city == u'', u'Inforequest.applicant_city is read-only' assert self.applicant_zip == u'', u'Inforequest.applicant_zip is read-only' self.applicant_name = self.applicant.get_full_name() self.applicant_street = self.applicant.profile.street self.applicant_city = self.applicant.profile.city self.applicant_zip = self.applicant.profile.zip # Generate unique random email assert self.unique_email == u'', u'Inforequest.unique_email is read-only' length = 4 while True: token = random_readable_string(length) self.unique_email = settings.INFOREQUEST_UNIQUE_EMAIL.format( token=token) try: with transaction.atomic(): super(Inforequest, self).save(*args, **kwargs) except IntegrityError: length += 1 if length <= 10: continue self.unique_email = None raise # Give up return # object is already saved super(Inforequest, self).save(*args, **kwargs)
def _check(self, length, vowels, consonants): pattern = r'^[{vowels}]?([{consonants}][{vowels}])*[{consonants}]?$'.format( vowels=re.escape(vowels), consonants=re.escape(consonants)) res = random_readable_string(length, vowels, consonants) self.assertEqual(len(res), length) self.assertRegexpMatches(res, pattern)
def test_with_default_argumnts(self): for length in range(1, 20): res = random_readable_string(length) self.assertEqual(len(res), length)