Tweaked the person factory to provide bio and photo on request.

- Legacy-Id: 11300
This commit is contained in:
Henrik Levkowetz 2016-06-09 21:51:46 +00:00
parent c80b654f18
commit f4d2f19ffb
2 changed files with 25 additions and 1 deletions

View file

@ -1,9 +1,12 @@
import os
import factory
import faker
import shutil
from unidecode import unidecode
from django.conf import settings
from django.contrib.auth.models import User
from ietf.person.models import Person, Alias, Email
fake = faker.Factory.create()
@ -30,6 +33,11 @@ class PersonFactory(factory.DjangoModelFactory):
name = factory.LazyAttribute(lambda p: '%s %s'%(p.user.first_name,p.user.last_name))
ascii = factory.LazyAttribute(lambda p: unidecode(p.name))
class Params:
with_bio = factory.Trait(
biography = u"\n\n".join(fake.paragraphs()),
)
@factory.post_generation
def default_aliases(self, create, extracted, **kwargs):
make_alias = getattr(AliasFactory, 'create' if create else 'build')
@ -41,6 +49,22 @@ class PersonFactory(factory.DjangoModelFactory):
make_email = getattr(EmailFactory, 'create' if create else 'build')
make_email(person=self,address=self.user.email)
@factory.post_generation
def default_photo(self, create, extracted, **kwargs):
import atexit
if self.biography:
photo_name = self.photo_name()
media_name = u"%s/%s.jpg" % (settings.PHOTOS_DIRNAME, photo_name)
self.photo = media_name
self.photo_thumb = media_name
photosrc = os.path.join(settings.TEST_DATA_DIR, "profile-default.jpg")
photodst = os.path.join(settings.PHOTOS_DIR, photo_name + '.jpg')
if not os.path.exists(photodst):
shutil.copy(photosrc, photodst)
def delete_file(file):
os.unlink(file)
atexit.register(delete_file, photodst)
class AliasFactory(factory.DjangoModelFactory):
class Meta:
model = Alias

Binary file not shown.

After

(image error) Size: 1.4 KiB