Beispiel #1
0
def job_new(request):
    if request.method == 'POST':
        form = JobForm(request.POST)
        if form.is_valid():
            job = form.save()
            return redirect('/client-manager/job/edit/{0}'.format(job.id))
    else:
        form = JobForm()
        
    data = {'form': form}
    return render_page(request, 'clientmanager/job_new.html', data)
Beispiel #2
0
def ajax_job_edit(request, job_id):
    job = get_object_or_404(Job, pk=job_id)

    json = simplejson.loads(request.POST['json'])
    job = get_object_or_404(Job, pk=job_id)
    job_form = JobForm(json['basic'], instance=job)
    job = job_form.save()

    existing = dict( (s.id, s) for s in job.shifts.all() )
    
    errors = {}
    
    for shift_json in json['shifts']:
        shift_form = ShiftForm(shift_json['info'])
        if not shift_form.is_valid():
            title = shift_form.data['title']
            errors[title] = ['{0}: {1}'.format(f, e[0]) for f,e in shift_form.errors.items()]
            break
            
        shift = shift_form.save(commit=False)
        shift.job = job
        
        id = shift_form.cleaned_data['id']
        if id in existing:
            shift.id = id
            del existing[id]
            
        shift.save()

        filter_errors = shift.update_filters(shift_json['attrs'])

        if len(filter_errors) > 0:
            errors[shift.title] = filter_errors
            
    Shift.objects.filter(id__in=existing.keys()).delete()
        
    if len(errors) == 0:
        return json_response({'status': 'ok'})
    else:
        return json_response({'status': 'error',
                              'errors': errors})