Views.py file:
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
from .models import Record
from .forms import AddRecordForm
# Create your views here.
def home(request):
records = Record.objects.all()[:10]
print(records)
if request.method == 'POST':
username = request.POST['username'].lower().strip().removesuffix('@sanimabank.com')
password = request.POST['password']
print(f'Username is {username}')
print(f'Password is {password}')
user = authenticate(request, username=username, password = password)
print(f'User is {user}')
#user = check_login(username, password)
if user is not None:
login(request, user)
messages.success(request, "You Have Been Logged In")
return redirect('home')
else:
messages.success(request, "Error Logging in. Please try again with correct credentials.")
return redirect('home')
return render(request, 'home.html', {'records':records})
def customer_record(request,pk):
if request.user.is_authenticated:
#look up_records
customer_record = Record.objects.get(email=pk)
print(f'Customer record is {customer_record}')
return render(request,'record.html',{'customer_record':customer_record})
else:
messages.success(request, "Login First to view this page")
return redirect('home')
def logout_user(request):
logout(request)
messages.success(request, "You have been Logged Out....")
return redirect('home')
def update_record(request, pk):
if request.user.is_authenticated:
current_record = Record.objects.get(email=pk)
print(f'pk is {pk}')
print(f'current record is {current_record}')
print(f'current record email is {current_record.email}')
print(f'current users email is {request.user.email}')
print(f'current users username is {request.user.username}')
if (request.user.email == pk) or (request.user.username == 'admin'):
print('USER OK TO UPDATE')
return redirect('home')
else:
messages.success(request, "Not Allowed!! You can update only your own record.")
return redirect('home')
# =============================================================================
# print("NOT OK TO UPDATE")
# return redirect('record')
# =============================================================================
else:
messages.success(request, "You must be logged in first to make changes here")
return redirect('home')
def add_record(request):
form = AddRecordForm(request.POST or None)
if request.user.is_authenticated:
if request.method == "POST":
if form.is_valid():
add_record = form.save()
messages.success(request, "Record Addded Successfully")
return redirect('home')
return render(request,'add_record.html',{'form':form})
else:
messages.success(request, "YOU MUST BE LOGGED IN ")
return redirect('home')
home.html
{% extends 'base.html' %}
{% block content %}
{% if user.is_authenticated %}
<H1> AFTER LOGIN </H1>
<table class="table table-striped table-hover table-bordered">
<thead class ="table-dark">
<tr>
<th scope="col">NAME</th>
<th scope="col">Email_ID</th>
<th scope="col">SIP_Number</th>
</tr>
</thead>
<tbody>
{% if records %}
{% for record in records %}
<tr>
<td>{{record.first_name}} {{record.last_name}}</td>
<td><a href = "{% url 'record' record.email %}">{{record.email}}</a></td>
<td>{{record.SIP_number}}</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
</table>
{% else %}
<h1>LOGIN PAGE </h1>
<H1> Login to the SIP Directory </H1>
<br/>
<form method ="POST" action = "{% url 'home' %}">
{% csrf_token %}
<form>
<div class="mb-3">
<input type="text" class="form-control" name ="username", placeholder = "Username" required>
</div>
<div class="mb-3">
<input type="password" class="form-control" name ="password", placeholder = "Password" required>
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
</form>
{% endif %}
{% endblock %}