from django.shortcuts import render,redirect
from django.template.loader import render_to_string
from django.http import HttpResponse
from django.views import View
from form.forms import BasicForm

from pprint import pprint
import json

# Create your views here.
def example(request) :
    form = BasicForm()
    return HttpResponse(form.as_table())

class DumpPostView(View):  # Reusable bit...
    def post(self, request) :
        js = json.dumps(request.POST, sort_keys=True, indent=4)        
        ctx = {'title': 'request.POST', 'dump': js}
        return render(request, 'dump.html', ctx)

class SimpleCreate(DumpPostView): 
    def get(self, request) :
        form = BasicForm()
        ctx = {'form' : form}
        return render(request, 'form.html', ctx)

class SimpleUpdate(DumpPostView):
    def get(self, request) :
        old_data = {
            'title': 'SakaiCar', 
            'mileage' : 42, 
            'purchase_date': '2018-08-14'
        }
        form = BasicForm(old_data)
        ctx = {'form' : form}
        return render(request, 'form.html', ctx)

class Validate(DumpPostView):
    def get(self, request) :
        old_data = {
            'title': 'SakaiCar', 
            'mileage' : 42, 
            'purchase_date': '2018-08-14'
        }
        form = BasicForm(initial=old_data)
        ctx = {'form' : form}
        return render(request, 'form.html', ctx)

    def post(self, request) :
        form = BasicForm(request.POST)
        if not form.is_valid() :
            ctx = {'form' : form}
            return render(request, 'form.html', ctx)
        # Save the Data
        return redirect('/form/success')

def success(request) :
    return HttpResponse('Thank you!')


# References

# https://stackoverflow.com/questions/383944/what-is-a-python-equivalent-of-phps-var-dump

from django.shortcuts import render,redirect
from django.template.loader import render_to_string
from django.http import HttpResponse
from django.views import View
from form.forms import BasicForm

from pprint import pprint
import json

# Create your views here.
def example(request) :
    form = BasicForm()
    return HttpResponse(form.as_table())

class DumpPostView(View):  # Reusable bit...
    def post(self, request) :
        js = json.dumps(request.POST, sort_keys=True, indent=4)        
        ctx = {'title': 'request.POST', 'dump': js}
        return render(request, 'dump.html', ctx)

class SimpleCreate(DumpPostView): 
    def get(self, request) :
        form = BasicForm()
        ctx = {'form' : form}
        return render(request, 'form.html', ctx)

class SimpleUpdate(DumpPostView):
    def get(self, request) :
        old_data = {
            'title': 'SakaiCar', 
            'mileage' : 42, 
            'purchase_date': '2018-08-14'
        }
        form = BasicForm(old_data)
        ctx = {'form' : form}
        return render(request, 'form.html', ctx)

class Validate(DumpPostView):
    def get(self, request) :
        old_data = {
            'title': 'SakaiCar', 
            'mileage' : 42, 
            'purchase_date': '2018-08-14'
        }
        form = BasicForm(initial=old_data)
        ctx = {'form' : form}
        return render(request, 'form.html', ctx)

    def post(self, request) :
        form = BasicForm(request.POST)
        if not form.is_valid() :
            ctx = {'form' : form}
            return render(request, 'form.html', ctx)
        # Save the Data
        return redirect('/form/success')

def success(request) :
    return HttpResponse('Thank you!')


# References

# https://stackoverflow.com/questions/383944/what-is-a-python-equivalent-of-phps-var-dump

# Create your views here. # Reusable bit... # Save the Data # References # https://stackoverflow.com/questions/383944/what-is-a-python-equivalent-of-phps-var-dump