SHARE
TWEET

Untitled

a guest Feb 20th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import factory
  2. from faker import Faker
  3. from phonenumber_field.modelfields import PhoneNumberField
  4.  
  5. from routing.tests.factories import IterCountryFactory, FAKER_LOCALE
  6. from sender.models import List, Contact, Sender, Message, MESSAGE_MAX_LENGTH, \
  7.     SendBatch
  8. from users.tests.factories import AdminUserFactory
  9.  
  10.  
  11. class ContactListFactory(factory.django.DjangoModelFactory):
  12.     class Meta:
  13.         model = List
  14.         django_get_or_create = ('name', 'created_by')
  15.  
  16.     name = name_faker(Meta.model)
  17.     created_by = factory.SubFactory(AdminUserFactory)
  18.  
  19.  
  20. class ContactFactory(factory.django.DjangoModelFactory):
  21.     class Meta:
  22.         model = Contact
  23.         django_get_or_create = ('list', 'phone')
  24.  
  25.     list = factory.SubFactory(ContactListFactory)
  26.     created_by = factory.SubFactory(AdminUserFactory)
  27.     country = factory.SubFactory(IterCountryFactory)
  28.     enabled = True
  29.  
  30.     @factory.lazy_attribute
  31.     def phone(self):
  32.         locale = FAKER_LOCALE.get(self.country.code)
  33.         if self.country.code in ('RU', 'IL'):
  34.             fake = Faker(locale, ['common.faker_providers.phone_number'])
  35.         else:
  36.             fake = Faker(locale)
  37.         phone_number = fake.phone_number()
  38.  
  39.         return PhoneNumberField().get_prep_value(phone_number)
  40.  
  41.  
  42. class SenderFactory(factory.django.DjangoModelFactory):
  43.     class Meta:
  44.         model = Sender
  45.         django_get_or_create = ('name', 'created_by')
  46.  
  47.     name = name_faker(Meta.model)
  48.     created_by = factory.SubFactory(AdminUserFactory)
  49.  
  50.  
  51. class MessageFactory(factory.django.DjangoModelFactory):
  52.     class Meta:
  53.         model = Message
  54.         django_get_or_create = ('name', 'created_by')
  55.  
  56.     name = name_faker(Meta.model)
  57.     contents = factory.Faker('text', max_nb_chars=MESSAGE_MAX_LENGTH)
  58.     sender = factory.SubFactory(SenderFactory)
  59.     created_by = factory.SubFactory(AdminUserFactory)
  60.  
  61.  
  62. class SendBatchFactory(factory.django.DjangoModelFactory):
  63.     class Meta:
  64.         model = SendBatch
  65.  
  66.     list = factory.SubFactory(ContactListFactory)
  67.     message = factory.SubFactory(MessageFactory)
  68.     created_by = factory.SubFactory(AdminUserFactory)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top