Exemplo n.º 1
0
def ChildAdd(request):
    if request.method == "POST":
        form = ChildAddForm(request.POST)

        print "child's parent is {0}".format(request.POST)
        child = form.save(commit=False)
        parent = Parent.objects.get(parent_id=str(request.POST['parent']))
        child.parent_of_child = parent
        child.save()
        return redirect('childview', pk = child.pk)
    else:
        form = ChildAddForm()
        return render(request, 'login/childadd.html', {'form':form})
Exemplo n.º 2
0
def add_child(request):
    key = request.META.get('HTTP_AUTHORIZATION')
    try:
        user = get_user_by_token(token_key=key)
    except Token.DoesNotExist:
        user = None
    if request.method == "POST" and user:
        form = ChildAddForm(request.POST, request.FILES)
        if form.is_valid():
            child = form.save(commit=False)
            child.mother = user
            child.save()
            serializer = ChildSerializer(child)
            return JSONResponse(serializer.data)
        else:
            errors = [{k: v[0]} for k, v in form.errors.items()]
            return JSONResponse({'errors': errors}, status_code=412)
    else:
        return HttpResponse(status=401)
Exemplo n.º 3
0
def edit_child(request, id):
    key = request.META.get('HTTP_AUTHORIZATION')
    try:
        user = get_user_by_token(token_key=key)
        child = Child.objects.get(id=id)
    except Token.DoesNotExist:
        user = None
    except Child.DoesNotExist:
        child = None
    if user and child and request.method == 'POST':
        form = ChildAddForm(request.POST, request.FILES, instance=child)
        if form.is_valid():
            child = form.save(commit=False)
            if not request.FILES.get('photo'):
                child.photo = None
            child.save()
            serializer = ChildSerializer(child)
            return JSONResponse(serializer.data)
        else:
            errors = [{k: v[0]} for k, v in form.errors.items()]
            return JSONResponse({'errors': errors}, status_code=412)
    else:
        return HttpResponse(status=401)