def insertednewuser(request):
    if 'useradmin' in request.COOKIES and request.method == 'POST':
        user = tb_user()
        user.connectToDB('localhost', 'root', '')
        uname = request.POST['uname']
        gender = request.POST['gender']
        email = request.POST['email']
        mobile = request.POST['mobile']
        password = request.POST['password']
        address = request.POST['address']
        imgData = request.FILES['photo']
        # imgData = imgData.read().decode()
        filePath = "/user/{}.jpg".format(uname)
        f = open("ecom_app/static/images{}".format(filePath), 'wb')
        for i in imgData:
            f.write(i)
        f.close()
        # sourceEncoding = "ISO-8859-1"
        # targetEncoding = "utf-8"
        # source = open("ecom_app/static/temp/0.jpg")
        # target = open("ecom_app/static/temp/0.jpg", "w")
        # target.write(source.read())
        # source.close()
        # target.close()
        # f = open("ecom_app/static/temp/0.jpg", 'rb')
        # imgData = f.read().decode()
        # f.close()
        user.insertUser(uname, gender, email, mobile, password, address,
                        filePath)
        user.closeConnection()
        return redirect(userhome)
    else:
        return redirect(touserhome)
def useractive(request):
    if 'useradmin' in request.COOKIES:
        stuObj = tb_user()
        stuObj.connectToDB('localhost', 'root', '')
        ID = request.GET['activeThis']
        status = request.GET['toggle1']
        stuObj.updateActive(ID, status)
        stuObj.closeConnection()
        return redirect(userhome)
    else:
        return redirect(touserhome)
def editvendor(request):
    if 'useradmin' in request.COOKIES:
        if request.method == 'POST' and request.POST['operation'] == 'edit':
            user_id = request.POST['editThis']
            user = tb_user()
            user.connectToDB('localhost', 'root', '')
            users = user.getData(user_id)
            user.closeConnection()
            return render(request, 'vendor/edit.html', {'users': users})
        else:
            return redirect(touserhome)
    else:
        return redirect(touserhome)
def fpass(request):
    if request.method == 'GET':
        return render(request, 'myfpass.html', {})
    if request.method == 'POST':
        user = request.POST['email']
        admin = tb_user()
        admin.connectToDB('localhost', 'root', '')
        if admin.fprocess(user):
            admin.closeConnection()
            response = redirect(mylogin)
            return response
        else:
            admin.closeConnection()
            return redirect(mylogin)
def mylogin(request):
    if request.method == 'GET':
        return render(request, 'mylogin.html', {})
    if request.method == 'POST':
        user = request.POST['email']
        password = request.POST['password']
        admin = tb_user()
        admin.connectToDB('localhost', 'root', '')
        if admin.authenticate(user, password):
            admin.closeConnection()
            response = redirect(home)
            response.set_cookie('useradmin', user)
            return response
        else:
            admin.closeConnection()
            return redirect(mylogin)
def userhome(request):
    if 'useradmin' in request.COOKIES:
        user = tb_user()
        user.connectToDB('localhost', 'root', '')
        if request.method == "POST":
            if request.POST['operation'] == 'delete':
                user.deleteUser(request.POST['delThis'])
                user.closeConnection()
                return redirect(userhome)
        users = user.getAllData()
        user.closeConnection()
        return render(request, 'user/home.html', {
            'users': users,
            'total': len(users)
        })
    else:
        return redirect(touserhome)
def editedvendor(request):
    if 'useradmin' in request.COOKIES:
        if request.method == 'POST':
            user = tb_user()
            user.connectToDB('localhost', 'root', '')
            user_id = request.POST['id']
            user_name = request.POST['uname']
            gender = request.POST['gender']
            email = request.POST['email']
            mobile = request.POST['mobile']

            address = request.POST['address']

            from os import rename
            rename(
                "ecom_app/static/images/user/{}.jpg".format(
                    user.getData(user_id)[1]),
                "ecom_app/static/images{}".format(filePath))
            user.editUserWithPhoto(user_id, user_name, gender, email, mobile,
                                   password, address, filePath)
            try:
                photo = request.FILES['photo']
                f = open(
                    "ecom_app/static/images/user/{}.jpg".format(user_name),
                    'wb')
                for i in photo:
                    f.write(i)
                f.close()
            except:
                pass
            user.closeConnection()
            return redirect(userhome)
        else:
            return redirect(touserhome)
    else:
        return redirect(touserhome)