class RecipientFactory(DjangoModelFactory): name = LazyAttribute(lambda o: faker.name()) email_address = LazyAttribute(lambda o: unicodedata.normalize( 'NFD', faker.safe_email()).encode('ascii', 'ignore')) class Meta: model = Recipient
class SocialMediaFactory(DjangoModelFactory): name = LazyAttribute(lambda o: random.choice(social_media.keys())) # Only currently supported social media username = LazyAttribute(lambda o: unicodedata.normalize('NFD', faker.user_name()).encode('ascii', 'ignore')) profile_url = LazyAttribute(lambda o: social_media[o.name] % o.username) class Meta: model = SocialMedia
class AccountFactory(DjangoModelFactory): tenant = SubFactory(TenantFactory) name = LazyAttribute(lambda o: faker.company()) description = LazyAttribute(lambda o: faker.bs()) status = SubFactory(AccountStatusFactory, tenant=SelfAttribute('..tenant')) @factory.post_generation def phone_numbers(self, create, extracted, **kwargs): phone_str = faker.phone_number() if create: phone_number = PhoneNumberFactory(tenant=self.tenant, number=phone_str) self.phone_numbers.add(phone_number) @factory.post_generation def websites(self, create, extracted, **kwargs): if not create: return size = extracted.get('size', 1) if extracted else 1 WebsiteFactory.create_batch(size=size, account=self, tenant=self.tenant) class Meta: model = Account
class ContactFactory(DjangoModelFactory): tenant = SubFactory(TenantFactory) first_name = LazyAttribute(lambda o: faker.first_name()) last_name = LazyAttribute(lambda o: faker.last_name()) class Meta: model = Contact
class DealFactory(DjangoModelFactory): tenant = SubFactory(TenantFactory) account = SubFactory(AccountFactory, tenant=SelfAttribute('..tenant')) contact = SubFactory(ContactFactory, tenant=SelfAttribute('..tenant')) amount_once = FuzzyDecimal(42.7) amount_recurring = FuzzyDecimal(42.7) assigned_to = SubFactory(LilyUserFactory, tenant=SelfAttribute('..tenant')) card_sent = FuzzyChoice([True, False]) contacted_by = SubFactory(DealContactedByFactory, tenant=SelfAttribute('..tenant')) currency = FuzzyChoice(dict(CURRENCIES).keys()) found_through = SubFactory(DealFoundThroughFactory, tenant=SelfAttribute('..tenant')) is_checked = FuzzyChoice([True, False]) name = LazyAttribute(lambda o: faker.word()) new_business = FuzzyChoice([True, False]) next_step = SubFactory(DealNextStepFactory, tenant=SelfAttribute('..tenant')) next_step_date = FuzzyDate(past_date, future_date) status = SubFactory(DealStatusFactory, tenant=SelfAttribute('..tenant')) twitter_checked = FuzzyChoice([True, False]) why_customer = SubFactory(DealWhyCustomerFactory, tenant=SelfAttribute('..tenant')) why_lost = SubFactory(DealWhyLostFactory, tenant=SelfAttribute('..tenant')) closed_date = LazyAttribute( lambda o: faker.date_time_between_dates(past_date, current_date, utc) if o.status.is_won or o.status.is_lost else None) class Meta: model = Deal
class EmailMessageFactory(DjangoModelFactory): subject = LazyAttribute(lambda o: faker.word()) sender = SubFactory(RecipientFactory) body_text = LazyAttribute(lambda o: faker.text()) sent_date = LazyAttribute( lambda o: faker.date_time_between_dates(past_date, current_date, utc)) account = SubFactory(EmailAccountFactory) message_id = FuzzyText() @post_generation def received_by(self, create, extracted, **kwargs): if not create: # Simple build, do nothing. return if extracted: if isinstance(extracted, Recipient): # A single team was passed in, use that. self.received_by.add(extracted) else: # A list of teams were passed in, use them. for recipient in extracted: self.received_by.add(recipient) class Meta: model = EmailMessage
class LilyUserFactory(DjangoModelFactory): tenant = SubFactory(TenantFactory) password = make_password('admin') first_name = LazyAttribute(lambda o: faker.first_name()) last_name = LazyAttribute(lambda o: faker.last_name()) email = LazyAttribute(lambda o: unicodedata.normalize( 'NFD', faker.safe_email()).encode('ascii', 'ignore')) is_active = LazyAttribute(lambda o: bool(randint(0, 1))) phone_number = LazyAttribute(lambda o: faker.phone_number()) language = FuzzyChoice(dict(LANGUAGES).keys()) timezone = FuzzyChoice(dict(TimeZoneField.CHOICES).values()) @post_generation def teams(self, create, extracted, **kwargs): if not create: # Simple build, do nothing. return if extracted: if isinstance(extracted, Team): # A single team was passed in, use that. self.teams.add(extracted) else: # A list of teams were passed in, use them. for team in extracted: self.teams.add(team) class Meta: model = LilyUser
class CallParticipantFactory(DjangoModelFactory): tenant = SubFactory(TenantFactory) number = LazyAttribute(lambda o: faker.phone_number()) name = LazyAttribute(lambda o: faker.name()) internal_number = FuzzyInteger(200, 999) class Meta: model = CallParticipant
class SocialMediaFactory(DjangoModelFactory): name = LazyAttribute( lambda o: random.choice(SocialMedia.SOCIAL_NAME_CHOICES)[0]) username = LazyAttribute(lambda o: faker.user_name()) profile_url = LazyAttribute(lambda o: faker.uri()) class Meta: model = SocialMedia
class AddressFactory(DjangoModelFactory): address = LazyAttribute(lambda o: faker.street_address()) postal_code = LazyAttribute(lambda o: faker.postcode()) city = LazyAttribute(lambda o: faker.city()) state_province = LazyAttribute(lambda o: faker.province()) country = FuzzyChoice(dict(COUNTRIES).keys()) type = FuzzyChoice(dict(Address.ADDRESS_TYPE_CHOICES).keys()) class Meta: model = Address
class CallFactory(DjangoModelFactory): unique_id = FuzzyText() called_number = LazyAttribute(lambda o: faker.phone_number()) caller_number = LazyAttribute(lambda o: faker.phone_number()) internal_number = FuzzyInteger(200, 999) status = FuzzyChoice(dict(Call.CALL_STATUS_CHOICES).keys()) type = FuzzyChoice(dict(Call.CALL_TYPE_CHOICES).keys()) class Meta: model = Call
class ContactFactory(DjangoModelFactory): tenant = SubFactory(TenantFactory) first_name = LazyAttribute(lambda o: faker.first_name()) last_name = LazyAttribute(lambda o: faker.last_name()) gender = FuzzyChoice(dict(Contact.CONTACT_GENDER_CHOICES).keys()) title = LazyAttribute(lambda o: faker.word()) description = LazyAttribute(lambda o: faker.text()) salutation = FuzzyChoice(dict(Contact.SALUTATION_CHOICES).keys()) class Meta: model = Contact
class EmailAccountFactory(DjangoModelFactory): tenant = SubFactory(TenantFactory) owner = SubFactory(LilyUserFactory, tenant=SelfAttribute('..tenant')) email_address = email_address from_name = LazyAttribute(lambda o: faker.name()) label = LazyAttribute(lambda o: faker.word()) is_authorized = True privacy = FuzzyChoice(dict(EmailAccount.PRIVACY_CHOICES).keys()) class Meta: model = EmailAccount
class EmailAccountFactory(DjangoModelFactory): tenant = SubFactory(TenantFactory) owner = SubFactory(LilyUserFactory) email_address = LazyAttribute(lambda o: unicodedata.normalize( 'NFD', faker.safe_email()).encode('ascii', 'ignore')) from_name = LazyAttribute(lambda o: faker.name()) label = LazyAttribute(lambda o: faker.word()) is_authorized = True privacy = FuzzyChoice(dict(EmailAccount.PRIVACY_CHOICES).keys()) class Meta: model = EmailAccount
class BeneficiaryGrant18Factory(BaseFactory): class Meta: model = pcapi.core.users.models.User email = factory.Sequence("jeanne.doux{}@example.com".format) address = factory.Sequence("{} rue des machines".format) city = "Paris" dateCreated = LazyAttribute(lambda _: datetime.utcnow()) dateOfBirth = LazyAttribute( # LazyAttribute to allow freez_time overrides lambda _: datetime.combine(date.today(), time(0, 0)) - relativedelta( years=users_constants.ELIGIBILITY_AGE_18, months=1)) departementCode = "75" firstName = "Jeanne" lastName = "Doux" isEmailValidated = True isAdmin = False roles = [pcapi.core.users.models.UserRole.BENEFICIARY] hasSeenProTutorials = True @classmethod def _create(cls, model_class, *args, **kwargs): password = kwargs.get("password", DEFAULT_PASSWORD) kwargs["password"] = crypto.hash_password(password) if "publicName" not in kwargs and kwargs["firstName"] and kwargs[ "lastName"]: kwargs["publicName"] = "%s %s" % (kwargs["firstName"], kwargs["lastName"]) instance = super()._create(model_class, *args, **kwargs) instance.clearTextPassword = DEFAULT_PASSWORD return instance @classmethod def _build(cls, model_class, *args, **kwargs): password = kwargs.get("password", DEFAULT_PASSWORD) kwargs["password"] = crypto.hash_password(password) if "publicName" not in kwargs and kwargs["firstName"] and kwargs[ "lastName"]: kwargs["publicName"] = "%s %s" % (kwargs["firstName"], kwargs["lastName"]) instance = super()._build(model_class, *args, **kwargs) instance.clearTextPassword = DEFAULT_PASSWORD return instance @factory.post_generation def deposit(obj, create, extracted, **kwargs): # pylint: disable=no-self-argument if not create: return None if "dateCreated" not in kwargs: kwargs["dateCreated"] = obj.dateCreated return DepositGrantFactory(user=obj, **kwargs)
class ExamAttemptFactory(DjangoModelFactory): class Meta: model = ExamAttempt student = SubFactory(StudentFactory) quiz = SubFactory(ExamFactory) score = 0 guess1 = LazyAttribute(lambda o: o.quiz.answer1) guess2 = LazyAttribute(lambda o: o.quiz.answer2) guess3 = LazyAttribute(lambda o: o.quiz.answer3) guess4 = LazyAttribute(lambda o: o.quiz.answer4) guess5 = LazyAttribute(lambda o: o.quiz.answer5)
class NummeraanduidingFactory(DjangoModelFactory): class Meta: model = models.Nummeraanduiding id = fuzzy.FuzzyText(length=14, chars=string.digits) landelijk_id = fuzzy.FuzzyText(length=16, chars=string.digits) huisnummer = LazyAttribute(lambda o: int(faker_instance.building_number())) openbare_ruimte = SubFactory(OpenbareRuimteFactory) verblijfsobject = SubFactory(VerblijfsobjectFactory) type = '01' # default verblijfsobject postcode = '1000AN' # default postcode.. _openbare_ruimte_naam = LazyAttribute(lambda o: o.openbare_ruimte.naam)
class AccountFactory(DjangoModelFactory): tenant = factory.SubFactory(TenantFactory) name = LazyAttribute(lambda o: faker.company()) description = LazyAttribute(lambda o: faker.bs()) @factory.post_generation def phone_numbers(self, create, extracted, **kwargs): phone_str = faker.phone_number() if create: phone_number = PhoneNumberFactory(tenant=self.tenant, raw_input=phone_str) self.phone_numbers.add(phone_number) class Meta: model = Account
class EmailDraftFactory(DjangoModelFactory): tenant = SubFactory(TenantFactory) send_from = SubFactory(EmailAccountFactory, tenant=SelfAttribute('..tenant')) to = email_addresses cc = email_addresses bcc = email_addresses subject = LazyAttribute(lambda o: faker.word()) body = LazyAttribute(lambda o: faker.text()) mapped_attachments = FuzzyInteger(0, 5) class Meta: model = EmailDraft
class CallRecordFactory(DjangoModelFactory): tenant = SubFactory(TenantFactory) call_id = FuzzyText(length=40) start = LazyAttribute(lambda o: faker.date_time(tzinfo=UTC)) end = LazyAttribute(lambda o: faker.date_time_between( start_date="now", end_date="+15m", tzinfo=UTC)) status = FuzzyChoice(dict(CallRecord.CALL_STATUS_CHOICES).keys()) direction = FuzzyChoice(dict(CallRecord.CALL_DIRECTION_CHOICES).keys()) caller = SubFactory(CallParticipantFactory, tenant=SelfAttribute('..tenant')) destination = SubFactory(CallParticipantFactory, tenant=SelfAttribute('..tenant')) class Meta: model = CallRecord
class EmailAddressFactory(DjangoModelFactory): email_address = LazyAttribute(lambda o: unicodedata.normalize( 'NFD', faker.safe_email()).encode('ascii', 'ignore')) status = EmailAddress.PRIMARY_STATUS class Meta: model = EmailAddress
class CaseFactory(DjangoModelFactory): tenant = SubFactory(TenantFactory) status = SubFactory(CaseStatusFactory, tenant=SelfAttribute('..tenant')) priority = FuzzyChoice(dict(Case.PRIORITY_CHOICES).keys()) subject = LazyAttribute(lambda o: faker.word()) account = SubFactory(AccountFactory, tenant=SelfAttribute('..tenant')) expires = FuzzyDate(datetime.date(2015, 1, 1), datetime.date(2016, 1, 1)) assigned_to = SubFactory(LilyUserFactory, tenant=SelfAttribute('..tenant')) type = SubFactory(CaseTypeFactory, tenant=SelfAttribute('..tenant')) created_by = SelfAttribute('.assigned_to') @factory.post_generation def teams(self, create, extracted, **kwargs): if not create: # Simple build, do nothing. return if extracted: if isinstance(extracted, Team): # A single team was passed in, use that. self.assigned_to_teams.add(extracted) else: # A list of teams were passed in, use them. for team in extracted: self.assigned_to_teams.add(team) class Meta: model = Case
class JouveContentFactory(factory.Factory): class Meta: model = models.JouveContent activity = random.choice(["Etudiant"]) address = "25 rue du moulin vert" birthDateTxt = LazyAttribute(lambda _: (datetime.utcnow() - relativedelta( years=18)).strftime("%d/%m/%Y")) birthLocationCtrl = random.choice(JOUVE_CTRL_VALUES) bodyBirthDateCtrl = random.choice(JOUVE_CTRL_VALUES) bodyBirthDateLevel = factory.Faker("pyint", max_value=100) bodyFirstnameCtrl = random.choice(JOUVE_CTRL_VALUES) bodyFirstnameLevel = factory.Faker("pyint", max_value=100) bodyNameLevel = factory.Faker("pyint", max_value=100) bodyNameCtrl = random.choice(JOUVE_CTRL_VALUES) bodyPieceNumber = factory.Faker("pyint") bodyPieceNumberCtrl = random.choice(JOUVE_CTRL_VALUES) bodyPieceNumberLevel = factory.Faker("pyint", max_value=100) city = "Paris" creatorCtrl = random.choice(JOUVE_CTRL_VALUES) id = factory.Faker("pyint") email = factory.Sequence("jeanne.doux{}@example.com".format) firstName = factory.Sequence("Jeanne{}".format) gender = random.choice(["Male", "Female"]) initialNumberCtrl = factory.Faker("pyint") initialSizeCtrl = random.choice(JOUVE_CTRL_VALUES) lastName = factory.Sequence("doux{}".format) phoneNumber = factory.Sequence("+3361212121{}".format) postalCode = "75008" posteCodeCtrl = "75" serviceCodeCtrl = factory.Faker("pystr")
class GuessFactory(DjangoModelFactory): class Meta: model = Guess user = SubFactory(UserFactory) market = SubFactory(MarketFactory) value = FuzzyDecimal(1, 10000) score = LazyAttribute(lambda o: o.get_score())
class WebsiteFactory(DjangoModelFactory): tenant = SubFactory(TenantFactory) account = SubFactory(AccountFactory, tenant=SelfAttribute('..tenant')) website = LazyAttribute(lambda o: faker.url()) is_primary = FuzzyChoice([True, False]) class Meta: model = Website
class DealWhyLostFactory(DjangoModelFactory): position = Sequence(int) name = LazyAttribute(lambda o: faker.word()) tenant = SubFactory(TenantFactory) class Meta: model = DealWhyLost django_get_or_create = ('tenant', 'name')
class UploadedFileFactory(DjangoModelFactory): class Meta: model = UploadedFile benefactor = SubFactory(StudentFactory) owner = LazyAttribute(lambda o: o.benefactor.user) category = 'psets' content = FileField(filename='pset.txt') unit = SubFactory(UnitFactory)
class CallTransferFactory(DjangoModelFactory): tenant = SubFactory(TenantFactory) timestamp = LazyAttribute(lambda o: faker.date_time_between( start_date="now", end_date="+1m", tzinfo=UTC)) destination = SubFactory(CallParticipantFactory, tenant=SelfAttribute('..tenant')) class Meta: model = CallTransfer
class NoteFactory(DjangoModelFactory): content = LazyAttribute(lambda o: faker.text()) author = SubFactory(LilyUserFactory, tenant=SelfAttribute('..tenant')) @factory.lazy_attribute def subject(self): SubjectFactory = random.choice([AccountFactory, ContactFactory]) return SubjectFactory(tenant=self.tenant) class Meta: model = Note
class PSetFactory(DjangoModelFactory): class Meta: model = PSet student = SubFactory(StudentFactory) unit = SubFactory(UnitFactory) upload = LazyAttribute( lambda o: UploadedFileFactory.create(benefactor=o.student, unit=o.unit) ) next_unit_to_unlock = SubFactory(UnitFactory) approved = True