<s> from __future__ import absolute_import <EOL> import factory <EOL> from django . contrib . auth . models import Group , Permission , User <EOL> from . models import Article <EOL> def _get_perm ( perm_name ) : <EOL> """<STR_LIT>""" <EOL> app_label , codename = perm_name . split ( '<STR_LIT:.>' ) <EOL> return Permission . objects . get ( <EOL> content_type__app_label = app_label , codename = codename ) <EOL> class ArticleFactory ( factory . django . DjangoModelFactory ) : <EOL> title = factory . Sequence ( lambda n : '<STR_LIT>' . format ( n ) ) <EOL> body = factory . Sequence ( lambda n : '<STR_LIT>' . format ( n ) ) <EOL> class Meta : <EOL> model = Article <EOL> abstract = False <EOL> class GroupFactory ( factory . django . DjangoModelFactory ) : <EOL> name = factory . Sequence ( lambda n : '<STR_LIT>' . format ( n ) ) <EOL> class Meta : <EOL> model = Group <EOL> abstract = False <EOL> class UserFactory ( factory . django . DjangoModelFactory ) : <EOL> username = factory . Sequence ( lambda n : '<STR_LIT>' . format ( n ) ) <EOL> first_name = factory . Sequence ( lambda n : '<STR_LIT>' . format ( n ) ) <EOL> last_name = factory . Sequence ( lambda n : '<STR_LIT>' . format ( n ) ) <EOL> email = factory . Sequence ( lambda n : '<STR_LIT>' . format ( n ) ) <EOL> password = '<STR_LIT>' <EOL> class Meta : <EOL> model = User <EOL> abstract = False <EOL> @ classmethod <EOL> def _prepare ( cls , create , ** kwargs ) : <EOL> password = kwargs . pop ( '<STR_LIT:password>' , None ) <EOL> user = super ( UserFactory , cls ) . _prepare ( create , ** kwargs ) <EOL> if password : <EOL> user . set_password ( password ) <EOL> if create : <EOL> user . save ( ) <EOL> return user <EOL> @ factory . post_generation <EOL> def permissions ( self , create , extracted , ** kwargs ) : <EOL> if create and extracted : <EOL> self . user_permissions . add ( * [ _get_perm ( pn ) for pn in extracted ] ) </s>
