Esempio n. 1
0
def content_action(request, response):
    # 二次确认提交才有此值
    action_form_data = request.POST.get('__action_form_data')
    action_form_post = QueryDict(
        action_form_data) if action_form_data else request.POST

    # 判断批量选择数据是否合法
    action_form = ActionForm(action_form_post)
    if not action_form.is_valid():
        raise Alert(Alert.ERROR, action_form.errors)

    # 权限检查
    has_perm(request, 'cms.action.%s' % action_form.cleaned_data.get('action'))

    # 选定数据检查
    selected = action_form_post.getlist('pk')
    if not selected:
        raise Alert(Alert.ERROR, u'您没有勾选需要操作的内容')
    if len(selected) > CONTENT_ACTION_MAX_RESULTS:
        raise Alert(Alert.ERROR, u'超过最大选择条数: %d' % CONTENT_ACTION_MAX_RESULTS)

    # 为二次确认提交准备
    if not action_form_data:
        action_form_data = action_form_post.urlencode()

    back = action_form.cleaned_data.get('back')
    Model = action_form.get_model_object()
    Form = action_form.get_form_object()
    action_name = action_form.get_action_name()
    content_set = Model.objects.filter(pk__in=selected).select_related(
        'column', 'update_user')

    if request.POST.get('__confirm') == 'yes':
        form = Form(content_set, request.POST, request.FILES)
        if form.is_valid():
            form.execute()
            msg = u'批量执行: ' + action_name
            log.success(
                request, u'%s\nID: %s\n表单数据: %s' %
                (msg, ','.join(selected), form.cleaned_data), form.log_action)
            messages.success(request, u'已完成' + msg)
            if back:
                return HttpResponseRedirect(back)
            return redirect('admin:cms:content')
    else:
        form = Form(content_set)
    return locals()
Esempio n. 2
0
def recycle(request, response):
    content_set = ContentBase.objects.filter(is_deleted=True).select_related(
        'path', 'update_user')
    if request.method == 'POST':
        action = request.POST.get('action')
        selected = request.POST.getlist('pk')
        if not selected:
            raise Alert(Alert.ERROR, u'您没有勾选需要操作的内容')
        if action == 'fire':
            has_perm(request, 'cms.recycle.fire')
            content_set.filter(pk__in=selected).delete()
            msg = u'回收站批量删除'
            log.success(request, u'%s\nID: %s' % (msg, ','.join(selected)),
                        log.DELETE)
            messages.success(request, u'已完成' + msg)
    content_set = Paging(content_set, request, CONTENT_PER_PAGE).page()
    return locals()
Esempio n. 3
0
def recycle(request, response, model=None):
    models = CONTENT_MODELS
    selected_model = models.get(model)
    # 无效的 model
    if model and selected_model is None:
        return redirect('admin:cms:recycle')

    if selected_model:
        Model = get_model_object(model, 'model')
        content_set = Model.objects.filter(is_deleted=True).select_related(
            'column', 'update_user')

        if request.method == 'POST':
            action = request.POST.get('action')
            selected = request.POST.getlist('pk')
            if not selected:
                raise Alert(Alert.ERROR, u'您没有勾选需要操作的内容')
            if action == 'fire':
                has_perm(request, 'cms.recycle.fire')
                content_set.filter(pk__in=selected).delete()
                msg = u'回收站批量删除: ' + selected_model.get('name')
                log.success(request, u'%s\nID: %s' % (msg, ','.join(selected)),
                            log.DELETE)
                messages.success(request, u'已完成' + msg)

        content_set = Paging(content_set, request, CONTENT_PER_PAGE).page()
    else:
        # 没有选择任何模型,则调用所有模型删除统计
        stats = {}
        total_count = 0
        for m, i in models.items():
            M = get_model_object(m, 'model')
            if not M:
                break
            count = M.objects.filter(is_deleted=True).count()
            total_count += count
            stats[m] = {'name': i.get('name'), 'count': count}
    return locals()
Esempio n. 4
0
def content_edit(request, response, column_select, content_id=None):
    user = get_request_user(request)
    is_content_edit = True
    column = column_select.selected
    
    Model = column.get_model_object('model')
    Form = column.get_model_object('form')
    if Model is None or Form is None:
        raise Alert(Alert.ERROR, '此栏目 %s 不可添加内容' % column)
    
    content = Model.objects.get(column=column, pk=content_id) if content_id else None
    model_log = ModelLogger(content)
    
    # 权限检查
    if not content:
        has_perm(request, 'cms.content.create')
    elif content.create_user == user:
        has_perm(request, 'cms.content.update.self')
    else:
        has_perm(request, 'cms.content.update.other')
    
    # 删除操作
    delete_id = request.GET.get('delete')
    if content and not content.is_deleted and content_id == delete_id:
        has_perm(request, 'cms.action.delete')
        update_instance(content, is_deleted=True)
        log.success(request, model_log.message(content), log.DELETE)
        messages.success(request, '删除内容: %s 成功' % str(content))
        return redirect('admin:cms:content-path', column.path)
    
    # 删除恢复
    recover_id = request.GET.get('recover')
    if content and content.is_deleted and content_id == recover_id:
        has_perm(request, 'cms.content.recover')
        update_instance(content, is_deleted=False)
        log.success(request, model_log.message(content), log.UPDATE)
        messages.success(request, '恢复删除内容: %s 成功' % str(content))
        return redirect('admin:cms:content-path', column.path)
    
    if request.method == 'POST':
        form = Form(request.POST, request.FILES, instance=content)
        if form.is_valid():
            form.instance.column = column
            form.instance.column_path = column.path
            form.instance.column_depth = column.depth
            if not form.instance.create_user:
                form.instance.create_user = user
            form.instance.update_user = user
            obj = form.save()
            
            log.success(request, model_log.message(obj), log.CREATE)
            messages.success(request, (content and '修改' or '创建') + '内容: ' + str(obj) + ' 成功')
            if request.GET.get('next'):
                return HttpResponseRedirect(request.GET.get('next'))
            return redirect('admin:cms:content-path', column.path)
    else:
        form = Form(instance=content)
    
    # 表单字段分离
    main_fields = []
    meta_fields = []
    meta_fields_name = ['is_hidden']
    _t = column.get_model_object('form_meta_fields')
    if _t:
        meta_fields_name.extend(_t)
    
    for i in form.visible_fields():
        if i.name in meta_fields_name:
            meta_fields.append(i)
        else:
            main_fields.append(i)
    
    return 'content', locals()
Esempio n. 5
0
def _content_edit(request, response, current_path=None, content_id=None):
    user = get_request_user(request)
    is_content_edit = True

    if content_id:
        content_base = ContentBase.objects.get(pk=content_id)
        Model = content_base.model_class
        Form = content_base.form_class
        path = content_base.path
        content = Model.objects.get(pk=content_id)
        current_path = get_current_path(path.path)
    elif current_path:
        path = current_path.selected
        Model = path.model_class
        Form = path.form_class
        content = None
    else:
        raise Alert(Alert.ERROR, u'缺少必要参数')

    if Form is None:
        raise Alert(Alert.ERROR, u'此栏目 %s 不可添加内容' % path)

    model_log = ModelLogger(content)

    # 权限检查
    if not content:
        has_perm(request, 'cms.content.create')
    elif content.create_user == user:
        has_perm(request, 'cms.content.update.self')
    else:
        has_perm(request, 'cms.content.update.other')

    # 删除操作
    delete_id = request.GET.get('delete')
    if content and not content.is_deleted and content_id == delete_id:
        has_perm(request, 'cms.action.delete')
        update_instance(content, is_deleted=True)
        log.success(request, model_log.message(content), log.DELETE)
        messages.success(request, u'删除内容: %s 成功' % unicode(content))
        return redirect('admin:cms:content-path', path.path)

    # 删除恢复
    recover_id = request.GET.get('recover')
    if content and content.is_deleted and content_id == recover_id:
        has_perm(request, 'cms.content.recover')
        update_instance(content, is_deleted=False)
        log.success(request, model_log.message(content), log.UPDATE)
        messages.success(request, u'恢复删除内容: %s 成功' % unicode(content))
        return redirect('admin:cms:content-path', path.path)

    if request.method == 'POST':
        form = Form(request.POST, request.FILES, instance=content)
        if form.is_valid():
            form.instance.path = path
            form.instance.path_value = path.path
            form.instance.path_depth = path.depth
            if not form.instance.model:
                form.instance.model = path.model
            if not form.instance.create_user:
                form.instance.create_user = user
            form.instance.update_user = user
            obj = form.save()

            log.success(request, model_log.message(obj), log.CREATE)
            messages.success(request, (content and u'修改' or u'创建') + u'内容: ' +
                             unicode(obj) + u' 成功')
            if request.GET.get('next'):
                return HttpResponseRedirect(request.GET.get('next'))
            return redirect('admin:cms:content-path', path.path)
    else:
        form = Form(instance=content)

    # 表单字段分离
    main_fields = []
    meta_fields = []
    meta_fields_name = ['flag', 'collections', 'is_hidden']
    _t = path.model_config.get('form_meta_fields')
    if _t:
        meta_fields_name.extend(_t)

    for i in form.visible_fields():
        if i.name in meta_fields_name:
            meta_fields.append(i)
        else:
            main_fields.append(i)

    return 'content_edit', locals()