from django.conf import settings
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
from .forms import *
from .models import *
from django.db import connection
from django.http import JsonResponse
from django.template.loader import render_to_string
from django.forms.models import model_to_dict
from django_user_agents.utils import get_user_agent


# what is in this file is accessible in all template without triggering a specific view in view.py by adding a url in urls.py first, see config in app_settings/settings.py TEMPLATES section
# see https://stackoverflow.com/questions/77032864/django-count-number-of-records-with-raw-mysql-query-and-use-result-in-html-templ/77033298?noredirect=1#comment135801831_77033298


def number_of_rec(request):
    with connection.cursor() as cursor:
        cursor.execute('SELECT count(*) FROM table_contacts')
        number_of_rec = cursor.fetchone()[0]
        number_of_rec = number_of_rec-1 #because of the contact 0 "add new contact"

    return {'number_of_rec': number_of_rec}

'''
or 

def number_of_rec(request):
    number_of_rec = models_contacts.objects.count()
    return {'number_of_rec': number_of_rec}
    '''