from django.shortcuts import render
from django.views import View
from .models import Form
from django.http import HttpResponse
import sqlite3
# Create your views here.


class Index(View):
    def get(self, request):
        return render(request, "hello/index.html")


class SignUp(View):
    def get(self, request):
        return render(request, "hello/signup.html")
    def post(self, request):

        form = Form(request.POST)

        if form.is_valid():
            db = sqlite3.connect("accounts.db")
            cur = db.cursor()
            cur.execute("CREATE TABLE IF NOT EXISTS accounts(username, password);")
            cur.execute(f"INSERT INTO accounts(username, password) VALUES('{form.cleaned_data.get('username')}', '{form.cleaned_data.get('password')}');")
            db.commit()
            db.close()
            return HttpResponse('''<h1 style='text-align:center;font-family:Arial;'>
                                    Account Creation Succeeded!</h1>
                                   <a href="/" style='font-family:Arial;'><p style='text-align:center;color:blue;'>-->  Back to Home Page</p></a>''')
        else:
            return HttpResponse(f"{form}")


class Login(View):
    def get(self, request):
        return render(request, "hello/login.html")
    def post(self, request):
        return HttpResponse("<h1></h1>")


class ViewAccounts(View):
    def get(self, request):
        con = sqlite3.connect("accounts.db")
        cur = con.cursor()
        cur.execute("SELECT * FROM accounts")
        select = cur.fetchall()
        con.close()
        return render(request, "hello/accounts.html", context={'select': select})

from django.shortcuts import render
from django.views import View
from .models import Form
from django.http import HttpResponse
import sqlite3
# Create your views here.


class Index(View):
    def get(self, request):
        return render(request, "hello/index.html")


class SignUp(View):
    def get(self, request):
        return render(request, "hello/signup.html")
    def post(self, request):

        form = Form(request.POST)

        if form.is_valid():
            db = sqlite3.connect("accounts.db")
            cur = db.cursor()
            cur.execute("CREATE TABLE IF NOT EXISTS accounts(username, password);")
            cur.execute(f"INSERT INTO accounts(username, password) VALUES('{form.cleaned_data.get('username')}', '{form.cleaned_data.get('password')}');")
            db.commit()
            db.close()
            return HttpResponse('''<h1 style='text-align:center;font-family:Arial;'>
                                    Account Creation Succeeded!</h1>
                                   <a href="/" style='font-family:Arial;'><p style='text-align:center;color:blue;'>-->  Back to Home Page</p></a>''')
        else:
            return HttpResponse(f"{form}")


class Login(View):
    def get(self, request):
        return render(request, "hello/login.html")
    def post(self, request):
        return HttpResponse("<h1></h1>")


class ViewAccounts(View):
    def get(self, request):
        con = sqlite3.connect("accounts.db")
        cur = con.cursor()
        cur.execute("SELECT * FROM accounts")
        select = cur.fetchall()
        con.close()
        return render(request, "hello/accounts.html", context={'select': select})

# Create your views here. <h1 style='text-align:center;font-family:Arial;'>                                     Account Creation Succeeded!</h1>                                    <a href="/" style='font-family:Arial;'><p style='text-align:center;color:blue;'>-->  Back to Home Page</p></a>