示例#1
0
文件: views.py 项目: reedboat/dcron
def create(request):
    if request.method == 'GET':
        form = TaskForm()
        form.helper.form_action = '/task/create'
        context = {'form':form}
        return render(request, 'task/new.tpl', context)

    if request.method != 'POST':
        return HttpResponse(status=400)

    form = TaskForm(request.POST)
    try:
        if form.is_valid():
            task = form.save(commit=False)
            task.run_time = form.cleaned_data['run_time']
            task.save()
        else:
            raise ValidationError(form.errors)
    except (ValidationError, ValueError):
        logger.exception('create task failed: validated error')
        return render(request, 'task/new.tpl', {"form":form})
    except Exception:
        logger.exception('create task failed')
        messages.error(request, '任务创建失败')
        return render(request, 'task/new.tpl', {"form":form})

    messages.success(request, '任务创建成功')
    return HttpResponseRedirect("/task/index")
示例#2
0
文件: views.py 项目: reedboat/dcron
def update(request):
    '''
    edit task
    '''
    if request.method == 'GET':
        task_id = int(request.GET['id'])
        result = '任务(id=%d)' % task_id
        try:
            task = Task.objects.get(pk = task_id)
            key = "run_time_"+task.type
            initial = {key : task.run_time}
            form = TaskForm(instance=task, initial=initial)
            form.helper.form_action = '/task/update?id=%d' % task_id 
        except ObjectDoesNotExist:
            logger.warn('edit task %d failed: task does not exist' % (task_id))
            messages.error(request, result + "不存在")
            return HttpResponseRedirect('/task/index')
        except Exception:
            logger.exception('edit task %d failed' % (task_id))
            messages.error(request, result + '编辑失败' )
            return HttpResponseRedirect('/task/index')

        context = {"task":task, 'form':form}
        return render(request, 'task/edit.tpl', context)

    elif request.method != 'POST':
        return HttpResponse(status=400)

    try:
        task_id = int(request.REQUEST.get('id', 0))
        result = '任务(id=%d)' % task_id
        task = Task.objects.get(pk=task_id)
        form = TaskForm(request.POST, instance=task)
        if form.is_valid():
            task = form.save(commit=False)
            task.run_time = form.cleaned_data['run_time']
            task.save()
        else:
            raise ValidationError()
    except ObjectDoesNotExist:
        logger.warn(request, 'update task (id=%d) failed: task not exist' % task_id)
        messages.warn(request, result+"不存在")
        return HttpResponseRedirect('/task/index')
    except ValidationError:
        logger.exception('update task(id=%d) failed: task validate failed' % task_id)
        messages.error(request, result+"验证失败")
        return render('task/edit', {'form':form})
    except Exception:
        logger.exception('update task(id=%d) failed' % (task_id))
        messages.error(request, result+"修改失败")
        return render('task/edit', {'form':form})

    messages.success(request, result+'修改成功')
    return HttpResponseRedirect('/task/show?id=%d' % task_id)