Tweaked the person factory to provide bio and photo on request.
- Legacy-Id: 11300
This commit is contained in:
parent
c80b654f18
commit
f4d2f19ffb
|
@ -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
|
||||
|
|
BIN
test/data/profile-default.jpg
Normal file
BIN
test/data/profile-default.jpg
Normal file
Binary file not shown.
After ![]() (image error) Size: 1.4 KiB |
Loading…
Reference in a new issue