Exemplo n.º 1
0
    def create_full_object(self, model):
        kwargs = {}
        for f in model._meta.fields:
            if isinstance(f, models.fields.related.ForeignKey) and f.null:
                model_name = camel_to_underscore(f.rel.to.__name__)
                method_name = 'create_{}'.format(model_name)
                kwargs[f.name] = getattr(self, method_name)()

        method_name = 'create_{}'.format(camel_to_underscore(model.__name__))

        return getattr(self, method_name)(**kwargs), kwargs
Exemplo n.º 2
0
    def create_full_object(self, model):
        kwargs = {}
        for f in model._meta.fields:
            if isinstance(f, models.fields.related.ForeignKey) and f.null:
                model_name = camel_to_underscore(f.rel.to.__name__)
                method_name = 'create_{}'.format(model_name)
                kwargs[f.name] = getattr(self, method_name)()

        method_name = 'create_{}'.format(camel_to_underscore(model.__name__))

        return getattr(self, method_name)(**kwargs), kwargs
Exemplo n.º 3
0
    def setUp(self):
        super(UrlsTest, self).setUp()

        # we are going to send parameters, so one thing we'll do is to send
        # tie id 1
        self.user.delete()
        self.user.id = 1

        # give the user all the permissions, so we test every page
        self.user.is_superuser = True
        self.user.status = self.user.STATUS_ACCEPTED

        self.user.save()
        self.login()

        self.default_params = {}

        for model in get_our_models():
            model_name = camel_to_underscore(model.__name__)
            method_name = 'create_{}'.format(model_name)
            param_name = '{}_id'.format(model_name)
            param_name2 = '{}_pk'.format(model_name)

            if hasattr(self, method_name):
                obj = getattr(self, method_name)()
            else:
                obj = mommy.make(model)

            self.assertIsNotNone(obj, '{} returns None'.format(method_name))

            self.default_params[param_name] = obj.id
            self.default_params[param_name2] = obj.id
Exemplo n.º 4
0
    def setUp(self):
        super(UrlsTest, self).setUp()

        # we are going to send parameters, so one thing we'll do is to send
        # tie id 1
        self.user.delete()
        self.user.id = 1

        # give the user all the permissions, so we test every page
        self.user.is_superuser = True

        self.user.save()
        self.login()

        self.default_params = {}

        for model in get_our_models():
            model_name = camel_to_underscore(model.__name__)
            method_name = 'create_{}'.format(model_name)
            param_name = '{}_id'.format(model_name)

            obj = getattr(self, method_name)()

            self.assertIsNotNone(obj, '{} returns None'.format(method_name))

            self.default_params[param_name] = obj.id
Exemplo n.º 5
0
    def get_context_data(self, **kwargs):
        context = super(BaseSubModelCreateView, self).get_context_data(
            **kwargs
        )
        model_underscore_name = camel_to_underscore(self.parent_model.__name__)

        context[model_underscore_name] = self.parent

        return context
    def get_form_kwargs(self):
        model_underscore_name = camel_to_underscore(self.parent_model.__name__)

        obj = get_object_or_404(
            self.parent_model,
            pk=self.kwargs['{}_id'.format(model_underscore_name)])

        self.object = self.model(**{model_underscore_name: obj})

        return super(BaseSubModelCreateView, self).get_form_kwargs()
Exemplo n.º 7
0
    def get_form_kwargs(self):
        model_underscore_name = camel_to_underscore(self.parent_model.__name__)

        obj = get_object_or_404(
            self.parent_model,
            pk=self.kwargs['{}_id'.format(model_underscore_name)]
        )

        self.object = self.model(**{model_underscore_name: obj})

        return super(BaseSubModelCreateView, self).get_form_kwargs()
    def get_context_data(self, **kwargs):
        context = super(BaseSubModelCreateView,
                        self).get_context_data(**kwargs)
        model_underscore_name = camel_to_underscore(self.parent_model.__name__)

        obj = get_object_or_404(
            self.parent_model,
            pk=self.kwargs['{}_id'.format(model_underscore_name)])

        context[model_underscore_name] = obj

        return context
Exemplo n.º 9
0
    def get_context_data(self, **kwargs):
        context = super(BaseSubModelCreateView, self).get_context_data(
            **kwargs
        )
        model_underscore_name = camel_to_underscore(self.parent_model.__name__)

        obj = get_object_or_404(
            self.parent_model,
            pk=self.kwargs['{}_id'.format(model_underscore_name)]
        )

        context[model_underscore_name] = obj

        return context
Exemplo n.º 10
0
    def get_context_data(self, **kwargs):
        context = super(BaseSubModelCreateView,
                        self).get_context_data(**kwargs)
        model_underscore_name = camel_to_underscore(self.parent_model.__name__)

        obj = get_object_or_404(
            self.parent_model,
            pk=self.kwargs['{}_id'.format(model_underscore_name)])

        context[model_underscore_name] = obj
        context['title'] = _('Create %s') % self.model._meta.verbose_name
        context['cancel_url'] = obj.get_absolute_url()

        return context
def add_get_or_create(cls, model):
    model_name = camel_to_underscore(model.__name__)

    def get_or_create(self, **kwargs):
        try:
            return model.objects.get(**kwargs), False
        except model.DoesNotExist:
            pass

        method_name = 'create_{}'.format(model_name)
        return getattr(cls, method_name)(self, **kwargs), True

    get_or_create.__doc__ = "Get or create for {}".format(model_name)
    get_or_create.__name__ = "get_or_create_{}".format(model_name)
    setattr(cls, get_or_create.__name__, get_or_create)
Exemplo n.º 12
0
def add_get_or_create(cls, model):
    model_name = camel_to_underscore(model.__name__)

    def get_or_create(self, **kwargs):
        try:
            return model.objects.get(**kwargs), False
        except model.DoesNotExist:
            pass

        method_name = 'create_{}'.format(model_name)
        return getattr(cls, method_name)(self, **kwargs), True

    get_or_create.__doc__ = "Get or create for {}".format(model_name)
    get_or_create.__name__ = "get_or_create_{}".format(model_name)
    setattr(cls, get_or_create.__name__, get_or_create)
Exemplo n.º 13
0
 def get_form_prefix(self, form_class):
     return camel_to_underscore(form_class.__name__)