예제 #1
0
class AdminUserFactory(UserFactory):
    email = LazyAttributeSequence(lambda o, n: '*****@*****.**' % (o.name.split()[0].lower(), n))

    @classmethod
    def _create(cls, target_class, *args, **kwargs):
        manager = cls._get_manager(target_class)
        return manager.create_superuser(*args, **kwargs)
예제 #2
0
class ReleaseFactory(DjangoModelFactory):
    class Meta:
        model = Release

    url = Faker('url')
    tag_name = LazyAttributeSequence(
        lambda o, n: "{year}.{n}".format(year=o.date.year, n=n))
    date = Sequence(lambda n: datetime(2018, 1, 1, tzinfo=timezone.utc) +
                    timedelta(days=n * n))
예제 #3
0
class UserSocialAuthFactory(DjangoModelFactory):
    """
    Factory for UserSocialAuth records.
    """
    class Meta(object):
        model = UserSocialAuth

    user = SubFactory(UserFactory)
    uid = LazyAttributeSequence(lambda o, n: '%s:%d' % (o.slug, n))

    class Params(object):
        slug = 'gatech'
예제 #4
0
class UserFactory(DjangoModelFactory):
    class Meta:
        model = User

    email = LazyAttributeSequence(lambda o, n: '*****@*****.**' % (o.name.split()[0].lower(), n))
    name = Sequence(lambda n: 'User #%s' % n)
    password = '******'

    @classmethod
    def _create(cls, target_class, *args, **kwargs):
        manager = cls._get_manager(target_class)
        return manager.create_user(*args, **kwargs)
예제 #5
0
class FieldFactory(DjangoModelFactory):
    application_type = SubFactory(ApplicationTypeFactory)
    key = LazyAttributeSequence(
        lambda obj, num: u'{application_type}-{field_type}-{sequence}'.format(
            application_type=obj.application_type.key,
            field_type=obj.field_type.key,
            sequence=num,
        ))
    name = Sequence(lambda n: u'поле {0}'.format(n))
    field_type = SubFactory(FieldTypeFactory)

    class Meta:
        model = Field
예제 #6
0
class AuthUserFactory(DjangoModelFactory):
    id = LazyAttributeSequence(
        lambda _, counter: auth_user_model.objects.order_by('id').last().id +
        counter + 1 if auth_user_model.objects.exists() else counter + 1)
    first_name = LazyAttribute(lambda obj: fake.first_name())
    last_name = LazyAttribute(lambda obj: fake.last_name())
    username = LazyAttribute(lambda obj: fake.user_name())
    email = LazyAttribute(lambda obj: fake.email())
    is_staff = False
    is_superuser = False

    class Meta:
        # Get Django's user model without directly referencing the model,
        # according to Django's own recommendations, as the model may change.
        model = auth_user_model
        django_get_or_create = ("username", )
예제 #7
0
class LeafDescriptorFactory(Factory):
    """
    Factory to generate leaf XModuleDescriptors.
    """
    # pylint: disable=missing-docstring

    class Meta(object):
        model = XModuleDescriptor

    runtime = SubFactory(DescriptorSystemFactory)
    url_name = LazyAttributeSequence('{.block_type}_{}'.format)

    @lazy_attribute
    def location(self):
        return Location('org', 'course', 'run', 'category', self.url_name, None)

    @lazy_attribute
    def block_type(self):
        return self.descriptor_cls.__name__  # pylint: disable=no-member

    @lazy_attribute
    def definition_id(self):
        return self.location

    @lazy_attribute
    def usage_id(self):
        return self.location

    @classmethod
    def _build(cls, target_class, *args, **kwargs):  # pylint: disable=unused-argument
        runtime = kwargs.pop('runtime')
        desc_cls = kwargs.pop('descriptor_cls')
        block_type = kwargs.pop('block_type')
        def_id = kwargs.pop('definition_id')
        usage_id = kwargs.pop('usage_id')

        block = runtime.construct_xblock_from_class(
            desc_cls,
            ScopeIds(None, block_type, def_id, usage_id),
            DictFieldData(dict(**kwargs))
        )
        block.save()
        return block
예제 #8
0
class XModuleItemFactory(Factory):
    """
    Factory for XModule items.
    """

    ABSTRACT_FACTORY = True

    parent_location = 'i4x://MITx/999/course/Robot_Super_Course'
    category = 'problem'
    display_name = LazyAttributeSequence(
        lambda o, n: "{} {}".format(o.category, n))

    @staticmethod
    def location(parent, category, display_name):
        dest_name = display_name.replace(
            " ", "_") if display_name is not None else uuid4().hex
        return Location(parent).replace(category=category, name=dest_name)

    @classmethod
    def _create(cls, target_class, **kwargs):
        """
        Uses ``**kwargs``:

        :parent_location: (required): the location of the parent module
            (e.g. the parent course or section)

        :category: the category of the resulting item.

        :data: (optional): the data for the item
            (e.g. XML problem definition for a problem item)

        :display_name: (optional): the display name of the item

        :metadata: (optional): dictionary of metadata attributes

        :boilerplate: (optional) the boilerplate for overriding field values

        :target_class: is ignored
        """

        DETACHED_CATEGORIES = ['about', 'static_tab', 'course_info']
        # catch any old style users before they get into trouble
        assert not 'template' in kwargs
        parent_location = Location(kwargs.get('parent_location'))
        data = kwargs.get('data')
        category = kwargs.get('category')
        display_name = kwargs.get('display_name')
        metadata = kwargs.get('metadata', {})
        location = kwargs.get(
            'location',
            XModuleItemFactory.location(parent_location, category,
                                        display_name))
        assert location != parent_location
        if kwargs.get('boilerplate') is not None:
            template_id = kwargs.get('boilerplate')
            clz = XModuleDescriptor.load_class(category)
            template = clz.get_template(template_id)
            assert template is not None
            metadata.update(template.get('metadata', {}))
            if not isinstance(data, basestring):
                data.update(template.get('data'))

        store = editable_modulestore('direct')

        # This code was based off that in cms/djangoapps/contentstore/views.py
        parent = store.get_item(parent_location)

        # replace the display name with an optional parameter passed in from the caller
        if display_name is not None:
            metadata['display_name'] = display_name
        store.create_and_save_xmodule(location,
                                      metadata=metadata,
                                      definition_data=data)

        if location.category not in DETACHED_CATEGORIES:
            parent.children.append(location.url())
            store.update_children(parent_location, parent.children)

        return store.get_item(location)
예제 #9
0
class ProgrammingLanguageFactory(DjangoModelFactory):
    class Meta:
        model = models.ProgrammingLanguage

    ref = LazyAttributeSequence(lambda x: 'lang%s' % x)
    name = LazyAttributeSequence(lambda x: 'Lang-%s' % x)