Python django.views.generic.CreateView() Examples

The following are 15 code examples of django.views.generic.CreateView(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.views.generic , or try the search function .
Example #1
Source File: auth.py    From pasportaservo with GNU Affero General Public License v3.0 6 votes vote down vote up
def dispatch(self, request, *args, **kwargs):
        """
        Permission check for create and general views.
        The context is determined according to the parent object, which is expected
        to be already retrieved by previous dispatch() methods, and stored in the
        auth_base keyword argument.
        """
        if getattr(self, 'exact_role', None) == ANONYMOUS or self.minimum_role == ANONYMOUS:
            self.allow_anonymous = True
        if not request.user.is_authenticated and not self.allow_anonymous:
            self.role = VISITOR
            return self.handle_no_permission()  # authorization implies a logged-in user
        if 'auth_base' in kwargs:
            object = kwargs['auth_base']
            self._auth_verify(object, context_omitted=object is None)
        elif isinstance(self, generic.CreateView):
            raise ImproperlyConfigured(
                "Creation base not found. Make sure {}'s auth_base is accessible by "
                "AuthMixin as a dispatch kwarg.".format(self.__class__.__name__)
            )
        return super().dispatch(request, *args, **kwargs) 
Example #2
Source File: mixins.py    From adhocracy4 with GNU Affero General Public License v3.0 6 votes vote down vote up
def _get_object(self, cls, attr):
        # CreateView supplies a defect get_object method and has to be excluded
        if hasattr(self, 'get_object') \
                and not isinstance(self, generic.CreateView):
            try:
                object = self.get_object()
                if isinstance(object, cls):
                    return object

                if hasattr(object, attr):
                    return getattr(object, attr)
            except Http404:
                return None
            except AttributeError:
                return None

        return None 
Example #3
Source File: views.py    From django-generic-scaffold with MIT License 6 votes vote down vote up
def get_create_class_view(self):
        name = '{0}_{1}'.format(self.get_name(), 'CreateView')
        options_dict = {
            'kind': 'create',
            'model': self.model,
            'fields': '__all__',
        }
        if hasattr(self, 'form_template_name') and self.form_template_name:
            options_dict['template_name'] = self.form_template_name

        if hasattr(self, 'form_class') and self.form_class:
            options_dict['form_class'] = self.form_class
            options_dict['fields'] = None

        parent_classes_list = [FallbackTemplateMixin]
        parent_classes_list.extend(self.create_mixins)
        parent_classes_list.append(self.create_view_class)

        klazz = type(name, tuple(parent_classes_list), options_dict )
        klazz.get_context_data = self.get_get_context_data(klazz)
        return klazz 
Example #4
Source File: views.py    From pythonjobs.ie with GNU General Public License v2.0 5 votes vote down vote up
def get_context_data(self, **kwargs):
        context = super(generic.CreateView, self).get_context_data(**kwargs)

        context['cities'] = Job.cities
        context['categories'] = Job.categories

        return context 
Example #5
Source File: abstract.py    From django-flexible-subscriptions with GNU General Public License v3.0 5 votes vote down vote up
def get_context_data(self, **kwargs):
        """Overriding get_context_data to add additional context."""
        context = super(CreateView, self).get_context_data(**kwargs)

        # Provides the base template to extend from
        context['template_extends'] = self.template_extends

        return context 
Example #6
Source File: views.py    From InvenTree with MIT License 5 votes vote down vote up
def get(self, request, *args, **kwargs):
        """ Creates form with initial data, and renders JSON response """

        super(CreateView, self).get(request, *args, **kwargs)

        self.request = request
        form = self.get_form()
        return self.renderJsonResponse(request, form) 
Example #7
Source File: views.py    From zing with GNU General Public License v3.0 5 votes vote down vote up
def get_success_url(self):
        # XXX: This is unused but enforced by `CreateView`
        return reverse("pootle-user-stats", kwargs=self.kwargs) 
Example #8
Source File: views.py    From kmanga with GNU General Public License v3.0 5 votes vote down vote up
def post(self, request, *args, **kwargs):
        try:
            self.object = self.get_object()
            success_url = self.get_success_url()
            self.object.deleted = False
            self.object.save()
            return HttpResponseRedirect(success_url)
        except:
            pass
        return super(CreateView, self).post(request, *args, **kwargs) 
Example #9
Source File: tests.py    From django-generic-scaffold with MIT License 5 votes vote down vote up
def test_views_have_correct_parent_class(self):
        self.assertEquals(self.list_view.__bases__[-1].__name__, "ListView")
        self.assertEquals(self.create_view.__bases__[-1].__name__, "CreateView")
        self.assertEquals(self.update_view.__bases__[-1].__name__, "UpdateView")
        self.assertEquals(self.delete_view.__bases__[-1].__name__, "DeleteView")
        self.assertEquals(self.detail_view.__bases__[-1].__name__, "DetailView") 
Example #10
Source File: tests.py    From django-generic-scaffold with MIT License 5 votes vote down vote up
def test_views_have_correct_parent_class(self):
        self.assertEquals(self.list_view.__bases__[-1].__name__, "ListView")
        self.assertEquals(self.create_view.__bases__[-1].__name__, "CreateView")
        self.assertEquals(self.update_view.__bases__[-1].__name__, "UpdateView")
        self.assertEquals(self.delete_view.__bases__[-1].__name__, "DeleteView")
        self.assertEquals(self.detail_view.__bases__[-1].__name__, "DetailView") 
Example #11
Source File: test_views.py    From django-rules with MIT License 5 votes vote down vote up
def test_predefined_view_type(self):
        class TestView(AutoPermissionRequiredMixin, CreateView):
            model = self.model
            fields = ()

        self.assertEqual(TestView.as_view()(self.req).status_code, 200) 
Example #12
Source File: test_views.py    From django-rules with MIT License 5 votes vote down vote up
def test_custom_view_type(self):
        class CustomViewMixin:
            pass

        class TestView(AutoPermissionRequiredMixin, CustomViewMixin, CreateView):
            model = self.model
            fields = ()
            permission_type_map = [
                (CustomViewMixin, "unknown_perm")
            ] + AutoPermissionRequiredMixin.permission_type_map
            raise_exception = True

        with self.assertRaises(PermissionDenied):
            TestView.as_view()(self.req) 
Example #13
Source File: test_views.py    From django-rules with MIT License 5 votes vote down vote up
def test_overwrite_perm_type(self):
        class TestView(AutoPermissionRequiredMixin, CreateView):
            model = self.model
            fields = ()
            permission_type = "unknown"
            raise_exception = True

        with self.assertRaises(PermissionDenied):
            TestView.as_view()(self.req) 
Example #14
Source File: test_views.py    From django-rules with MIT License 5 votes vote down vote up
def test_disable_perm_checking(self):
        class TestView(AutoPermissionRequiredMixin, CreateView):
            model = self.model
            fields = ()
            permission_type = None

        self.assertEqual(TestView.as_view()(self.req).status_code, 200) 
Example #15
Source File: test_views.py    From django-rules with MIT License 5 votes vote down vote up
def test_permission_required_passthrough(self):
        class TestView(AutoPermissionRequiredMixin, CreateView):
            model = self.model
            fields = ()
            permission_required = "testapp.unknown_perm"
            raise_exception = True

        with self.assertRaises(PermissionDenied):
            TestView.as_view()(self.req)