Пример #1
0
def update_object(request, method='json', *args, **kwargs):
    if method == 'json' and request.method == 'POST':
        login_required = kwargs.get('login_required')
        model = kwargs.get('model')
        form_class = kwargs.get('form_class')
        object_id = kwargs.get('object_id')
        slug = kwargs.get('slug')
        slug_field = kwargs.get('slug_field', 'slug')
        if login_required and not request.user.is_authenticated():
            return JsonResponse({'status': 'denied'})
        model, form_class = create_update.get_model_and_form_class(model, form_class)
        obj = create_update.lookup_object(model, object_id, slug, slug_field)
        if request.method == 'POST':
            if issubclass(form_class, ModelFormWithRequest):
                form = form_class(request, request.POST, request.FILES, instance=obj)
            else:
                form = form_class(request.POST, request.FILES, instance=obj)
            if form.is_valid():
                obj = form.save()
                #msg = ugettext("The %(verbose_name)s was updated successfully.") %\
                #                    {"verbose_name": model._meta.verbose_name}
                #messages.success(request, msg, fail_silently=True)
                return JsonResponse({'status': 'ok', 'instance': get_instance_json(obj)})
            else:
                errors = [unicode(striptags("%s: %s" % (k, v))) for k, v in form.errors.iteritems()]
                return JsonResponse({'status': 'failed', 'errors': errors})
    else:
        return create_update.update_object(request, *args, **kwargs)
Пример #2
0
def delete_object(request, method='json', *args, **kwargs):
    if method == 'json' and request.method == 'POST':
        login_required = kwargs.get('login_required')
        model = kwargs['model']
        post_delete_redirect = kwargs['post_delete_redirect']
        object_id = kwargs.get('object_id')
        slug = kwargs.get('slug')
        slug_field = kwargs.get('slug_field', 'slug')
        if login_required and not request.user.is_authenticated():
            return JsonResponse({'status': 'denied'})
        obj = create_update.lookup_object(model, object_id, slug, slug_field)
        if request.method == 'POST':
            if isinstance(obj, ModelWithRequest):
                obj.delete(request)
            else:
                obj.delete()
            #msg = ugettext("The %(verbose_name)s was deleted.") %\
            #                        {"verbose_name": model._meta.verbose_name}
            #messages.success(request, msg, fail_silently=True)
            return JsonResponse({'status': 'ok', 'post_delete_redirect': u"%s"%post_delete_redirect})
    else:
        return create_update.delete_object(request, *args, **kwargs)