Ejemplo n.º 1
0
    def edit(self, request, id):
        """Render a form to edit an object."""
        try:
            object = self.model.objects.get(id=id)
        except self.model.DoesNotExist:
            return render(
                request = request,
                template_name = '404.html',
                status = 404
            )

        form = (self.form or generate_form(self.model))(instance=object)

        # Add "_method" field to override request method to PUT
        form.fields['_method'] = CharField(required=True, initial='PUT', widget=HiddenInput)

        return self._render(
            request = request,
            template = 'edit',
            context = {
                self.model.__name__.lower(): object,
                'form': form
            },
            status = 200
        )
Ejemplo n.º 2
0
    def replace(self, request, id):
        """Replace an object."""
        try:
            object = self.model.objects.get(id=id)
        except self.model.DoesNotExist:
            return render(
                request = request,
                template_name = '404.html',
                status = 404
            )

        form = (self.form or generate_form(self.model))(request.PUT, instance=object)

        if form.is_valid():
            object = form.save()

            return self.show(request, id)
        else:
            return self._render(
                request = request,
                template = 'edit',
                context = {
                    'form': form
                },
                status = 400
            )
Ejemplo n.º 3
0
    def replace(self, request, id):
        """Replace an object."""
        try:
            object = self.model.objects.get(id=id)
        except self.model.DoesNotExist:
            return self._render(request=request,
                                template='404',
                                context={
                                    'error':
                                    'The %s could not be found.' %
                                    self.model.__name__.lower()
                                },
                                status=404,
                                prefix_template_path=False)

        form = (self.form or generate_form(self.model))(request.PUT,
                                                        instance=object)

        if form.is_valid():
            object = form.save()

            return self.show(request, id)
        else:
            return self._render(request=request,
                                template='edit',
                                context={'form': form},
                                status=400)
Ejemplo n.º 4
0
    def update(self, request, id):
        """Update an object."""
        try:
            object = self.model.objects.get(id=id)
        except self.model.DoesNotExist:
            return self._render(
                request=request,
                template="404",
                context={"error": "The %s could not be found." % self.model.__name__.lower()},
                status=404,
                prefix_template_path=False,
            )

        fields = []
        for field in request.PATCH:

            try:
                self.model._meta.get_field_by_name(field)
            except FieldDoesNotExist:
                continue
            else:
                fields.append(field)

        Form = generate_form(model=self.model, form=self.form, fields=fields)

        form = Form(request.PATCH, instance=object)
        if form.is_valid():
            object = form.save()

            return self.show(request, id)
        else:
            return self._render(request=request, template="edit", context={"form": form}, status=400)
Ejemplo n.º 5
0
    def update(self, request, id):
        """Update an object."""
        try:
            object = self.model.objects.get(id=id)
        except self.model.DoesNotExist:
            return render(
                request = request,
                template_name = '404.html',
                status = 404
            )

        data = request.PATCH.copy()
        for field in model_to_dict(object):
            data[field] = data.get(field) or getattr(object, field)

        form = (self.form or generate_form(self.model))(data, instance=object)

        if form.is_valid():
            object = form.save()

            return self.show(request, id)
        else:
            return self._render(
                request = request,
                template = 'edit',
                context = {
                    'form': form
                },
                status = 400
            )
Ejemplo n.º 6
0
    def create(self, request, gallery_id):
        """Create a new object."""
        try:
            gallery = Gallery.objects.get(id=gallery_id)
        except Gallery.DoesNotExist:
            return self._render(
                request=request,
                template='404',
                context={'error': 'The gallery could not be found.'},
                status=404,
                prefix_template_path=False)

        form = generate_form(self.model)(request.POST, request.FILES)

        if form.is_valid():
            image = form.save()
            # Update the gallery
            gallery.images.add(image)

            return self._render(request=request,
                                template='show',
                                context={'image': image.serialize()},
                                status=201)
            return HttpResponse("successfull")
        else:

            return self._render(request=request,
                                template='new',
                                context={'form': form},
                                status=400)
Ejemplo n.º 7
0
    def edit(self, request, id):
        """Render a form to edit an object."""
        try:
            object = self.model.objects.get(id=id)
        except self.model.DoesNotExist:
            return self._render(
                request = request,
                template = '404',
                context = {
                    'error': 'The %s could not be found.' % self.model.__name__.lower()
                },
                status = 404,
                prefix_template_path = False
            )

        form = (self.form or generate_form(self.model))(instance=object)

        # Add "_method" field to override request method to PUT
        form.fields['_method'] = CharField(required=True, initial='PUT', widget=HiddenInput)

        return self._render(
            request = request,
            template = 'edit',
            context = {
                cc2us(self.model.__name__): object,
                'form': form
            },
            status = 200
        )
Ejemplo n.º 8
0
    def replace(self, request, id):
        """Replace an object."""
        try:
            object = self.model.objects.get(id=id)
        except self.model.DoesNotExist:
            return self._render(
                request = request,
                template = '404',
                context = {
                    'error': 'The %s could not be found.' % self.model.__name__.lower()
                },
                status = 404,
                prefix_template_path = False
            )

        form = (self.form or generate_form(self.model))(request.PUT, instance=object)

        if form.is_valid():
            object = form.save()

            return self.show(request, id)
        else:
            return self._render(
                request = request,
                template = 'edit',
                context = {
                    'form': form
                },
                status = 400
            )
Ejemplo n.º 9
0
    def edit(self, request, id):
        """Render a form to edit an object."""
        try:
            object = self.model.objects.get(id=id)
        except self.model.DoesNotExist:
            return self._render(request=request,
                                template='404',
                                context={
                                    'error':
                                    'The %s could not be found.' %
                                    self.model.__name__.lower()
                                },
                                status=404,
                                prefix_template_path=False)

        form = (self.form or generate_form(self.model))(instance=object)

        # Add "_method" field to override request method to PUT
        form.fields['_method'] = CharField(required=True,
                                           initial='PUT',
                                           widget=HiddenInput)

        return self._render(request=request,
                            template='edit',
                            context={
                                self.model.__name__.lower(): object,
                                'form': form
                            },
                            status=200)
Ejemplo n.º 10
0
    def new(self, request, gallery_id):
        """Render a form to create a new object."""
        try:
            Gallery.objects.get(id=gallery_id)
        except Gallery.DoesNotExist:
            return self._render(
                request=request,
                template='404',
                context={
                    'error': 'The gallery could not be found.'
                },
                status=404,
                prefix_template_path=False
            )

        form = (self.form or generate_form(self.model))()

        return self._render(
            request=request,
            template='new',
            context={
                'form': form
            },
            status=200
        )
Ejemplo n.º 11
0
    def new(self, request):
        """Render a form to create a new object."""
        form = (self.form or generate_form(self.model))()

        return self._render(request=request,
                            template='new',
                            context={'form': form},
                            status=200)
Ejemplo n.º 12
0
def test_form_serialization():
    import django.forms

    form = generate_form(Article)()

    assert Serializer(form).preprocess() == {
        'fields': ['title', 'content', 'is_published']
    }
Ejemplo n.º 13
0
    def create(self, request):
        """Create a new object."""
        form = (self.form or generate_form(self.model))(request.POST)

        if form.is_valid():
            object = form.save()

            return self._render(
                request=request, template="show", context={self.model.__name__.lower(): object}, status=201
            )
        else:
            return self._render(request=request, template="new", context={"form": form}, status=400)
Ejemplo n.º 14
0
    def new(self, request):
        """Render a form to create a new object."""
        form = (self.form or generate_form(self.model))()

        return self._render(
            request = request,
            template = 'new',
            context = {
                'form': form
            },
            status = 200
        )
Ejemplo n.º 15
0
def test_form_serialization():
    """Verify that forms may be serialized."""
    import django.forms

    request = factory.get('/')

    form = generate_form(Article)()

    assert_equal(Serializer(form).serialize(request), {
        'fields': ['title', 'content', 'is_published', 'created_at', 'tags', 'author']
    })

    assert JSONSerializer(form).serialize(request)
    assert JSONPSerializer(form).serialize(request)
    assert XMLSerializer(form).serialize(request)
Ejemplo n.º 16
0
def test_form_serialization():
    """Verify that forms may be serialized."""
    import django.forms

    request = factory.get('/')

    form = generate_form(Article)()

    assert_equal(Serializer(form).serialize(request), {
        'fields': ['title', 'content', 'is_published', 'created_at', 'tags', 'author']
    })

    assert JSONSerializer(form).serialize(request)
    assert JSONPSerializer(form).serialize(request)
    assert XMLSerializer(form).serialize(request)
Ejemplo n.º 17
0
    def create(self, request):
        """Create a new object."""
        form = (self.form or generate_form(self.model))(request.POST)

        if form.is_valid():
            object = form.save()

            return self._render(request=request,
                                template='show',
                                context={self.model.__name__.lower(): object},
                                status=201)
        else:
            return self._render(request=request,
                                template='new',
                                context={'form': form},
                                status=400)
Ejemplo n.º 18
0
    def update(self, request, id):
        """Update an object."""
        try:
            object = self.model.objects.get(id=id)
        except self.model.DoesNotExist:
            return self._render(
                request = request,
                template = '404',
                context = {
                    'error': 'The %s could not be found.' % self.model.__name__.lower()
                },
                status = 404,
                prefix_template_path = False
            )

        fields = []
        for field in request.PATCH:

            try:
                self.model._meta.get_field_by_name(field)
            except FieldDoesNotExist:
                continue
            else:
                fields.append(field)

        Form = generate_form(
            model = self.model,
            form = self.form,
            fields = fields
        )

        form = Form(request.PATCH, instance=object)
        if form.is_valid():
            object = form.save()

            return self.show(request, id)
        else:
            return self._render(
                request = request,
                template = 'edit',
                context = {
                    'form': form
                },
                status = 400
            )
Ejemplo n.º 19
0
    def new(self, request, gallery_id):
        """Render a form to create a new object."""
        try:
            Gallery.objects.get(id=gallery_id)
        except Gallery.DoesNotExist:
            return self._render(
                request=request,
                template='404',
                context={'error': 'The gallery could not be found.'},
                status=404,
                prefix_template_path=False)

        form = (self.form or generate_form(self.model))()

        return self._render(request=request,
                            template='new',
                            context={'form': form},
                            status=200)
Ejemplo n.º 20
0
    def create(self, request, gallery_id):
        """Create a new object."""
        try:
            gallery = Gallery.objects.get(id=gallery_id)
        except Gallery.DoesNotExist:
            return self._render(
                request=request,
                template='404',
                context={
                    'error': 'The gallery could not be found.'
                },
                status=404,
                prefix_template_path=False
            )

        form = generate_form(self.model)(request.POST, request.FILES)

        if form.is_valid():
            image = form.save()
            # Update the gallery
            gallery.images.add(image)

            return self._render(
                request=request,
                template='show',
                context={
                    'image':  image.serialize()
                },
                status=201
            )
            return HttpResponse("successfull")
        else:

            return self._render(
                request=request,
                template='new',
                context={
                    'form': form
                },
                status=400
            )
Ejemplo n.º 21
0
    def edit(self, request, id):
        """Render a form to edit an object."""
        try:
            object = self.model.objects.get(id=id)
        except self.model.DoesNotExist:
            return self._render(
                request=request,
                template="404",
                context={"error": "The %s could not be found." % self.model.__name__.lower()},
                status=404,
                prefix_template_path=False,
            )

        form = (self.form or generate_form(self.model))(instance=object)

        # Add "_method" field to override request method to PUT
        form.fields["_method"] = CharField(required=True, initial="PUT", widget=HiddenInput)

        return self._render(
            request=request, template="edit", context={self.model.__name__.lower(): object, "form": form}, status=200
        )
Ejemplo n.º 22
0
    def update(self, request, id, p):
        """
        Update the basic informations of a project
        
        :Decorators: ``rest_login_required, must_be_owner``
        :Rest Types: ``PATCH``
        :URL: ``^(?P<id>[0-9]+)(?:/$|.(html|json)$)``
        """
        # Create form
        fields = []
        for field in request.PATCH:
            try:
                Project._meta.get_field_by_name(field)
                fields.append(field)
            except FieldDoesNotExist:
                continue

        Form = generate_form(model = Project, form = ProjectForm, fields = fields)
        form = Form(request.PATCH, instance=p)

        return self._form_validation(request, form, 400, "Update", messages="Project updated")
Ejemplo n.º 23
0
    def update(self, request, username):
        """
        Update the basic informations of a UserProfile
        
        :Decorators: ``rest_login_required, must_be_itself``
        :Rest Types: ``PATCH``
        :URL: ``^(?P<username>\w+)(?:/$|.(html|json)$)``
        """
        username = str(username)
        u = UserProfile.objects.get(user__username__iexact=username)
        fields = []
        for field in request.PATCH:
            try:
                UserProfile._meta.get_field_by_name(field)
                fields.append(field)
            except FieldDoesNotExist:
                continue

        Form = generate_form(model = UserProfile, form = UserProfileForm, fields = fields)
        form = Form(request.PATCH, instance=u)

        return self._form_validation(request, form, 400, messages="Profile updated")
Ejemplo n.º 24
0
    def create(self, request):
        """Create a new object."""    
        form = (self.form or generate_form(self.model))(request.POST)

        if form.is_valid():
            object = form.save()

            response = HttpResponse(status=303)
            response['Location'] = reverse(
                viewname = '%s_%s' % (self.model._meta.app_label, cc2us(self.model.__name__)),
                args = [object.id]
            )
            return response
        else:
            return self._render(
                request = request,
                template = 'new',
                context = {
                    'form': form
                },
                status = 400
            )
Ejemplo n.º 25
0
    def create(self, request):
        """Create a new object."""
        form = (self.form or generate_form(self.model))(request.POST)

        if form.is_valid():
            object = form.save()

            return self._render(
                request = request,
                template = 'show',
                context = {
                    cc2us(self.model.__name__): object
                },
                status = 201
            )
        else:
            return self._render(
                request = request,
                template = 'new',
                context = {
                    'form': form
                },
                status = 400
            )
Ejemplo n.º 26
0
def test_generate_form():
    from respite.utils import generate_form
    from .project.app.models import Article

    assert generate_form(Article)
Ejemplo n.º 27
0
def test_generate_form():
    from respite.utils import generate_form
    from news.models import Article

    assert generate_form(Article)
Ejemplo n.º 28
0
def test_generate_form():
    from respite.utils import generate_form
    from .project.app.models import Article

    assert generate_form(Article)