Skip to content

Commit f4d2f19

Browse files
committed
Tweaked the person factory to provide bio and photo on request.
- Legacy-Id: 11300
1 parent c80b654 commit f4d2f19

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

ietf/person/factories.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import os
12
import factory
23
import faker
3-
4+
import shutil
45
from unidecode import unidecode
56

7+
from django.conf import settings
68
from django.contrib.auth.models import User
9+
710
from ietf.person.models import Person, Alias, Email
811

912
fake = faker.Factory.create()
@@ -30,6 +33,11 @@ class Meta:
3033
name = factory.LazyAttribute(lambda p: '%s %s'%(p.user.first_name,p.user.last_name))
3134
ascii = factory.LazyAttribute(lambda p: unidecode(p.name))
3235

36+
class Params:
37+
with_bio = factory.Trait(
38+
biography = u"\n\n".join(fake.paragraphs()),
39+
)
40+
3341
@factory.post_generation
3442
def default_aliases(self, create, extracted, **kwargs):
3543
make_alias = getattr(AliasFactory, 'create' if create else 'build')
@@ -41,6 +49,22 @@ def default_emails(self, create, extracted, **kwargs):
4149
make_email = getattr(EmailFactory, 'create' if create else 'build')
4250
make_email(person=self,address=self.user.email)
4351

52+
@factory.post_generation
53+
def default_photo(self, create, extracted, **kwargs):
54+
import atexit
55+
if self.biography:
56+
photo_name = self.photo_name()
57+
media_name = u"%s/%s.jpg" % (settings.PHOTOS_DIRNAME, photo_name)
58+
self.photo = media_name
59+
self.photo_thumb = media_name
60+
photosrc = os.path.join(settings.TEST_DATA_DIR, "profile-default.jpg")
61+
photodst = os.path.join(settings.PHOTOS_DIR, photo_name + '.jpg')
62+
if not os.path.exists(photodst):
63+
shutil.copy(photosrc, photodst)
64+
def delete_file(file):
65+
os.unlink(file)
66+
atexit.register(delete_file, photodst)
67+
4468
class AliasFactory(factory.DjangoModelFactory):
4569
class Meta:
4670
model = Alias

test/data/profile-default.jpg

1.36 KB
Loading

0 commit comments

Comments
 (0)