My asset_list html is like this
{% if user_role == 'admin' %}
{% include 'asset_mng/admin_sidebar.html' %}
{% elif user_role == 'asset_owner' %}
{% include 'asset_mng/asset_owner_sidebar.html' %}
{% endif %}
<div class="right">
<div class="search-container">
<input type="text" id="searchInput" placeholder="Search by AssetName..." aria-label="Search">
<button class="add-button" aria-label="Add Asset" id="addUserButton"><i class="fas fa-plus"></i> Add</button>
</div>
login database is like this , with roles admin and user
class UserDetails(models.Model):
username = models.CharField(max_length=100, unique=True)
password = models.CharField(max_length=100) # Ideally, this should be hashed
role = models.CharField(max_length=45)
login view.py
def asset_login(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(request,username=username,password=password)
try:
user = UserDetails.objects.get(username=username, password=password)
if user is not None:
request.session['username'] = user.username
request.session['role'] = user.role #pass the user's role to the template
if user.role == 'admin':
return redirect('admin_dashboard')
elif user.role == 'head':
return redirect('head_dashboard')
elif user.role == 'asset_owner':
return redirect('asset_owner_dashboard')
elif user.role == 'ciso':
return redirect('ciso_dashboard')
else:
messages.error(request, 'Unknown user role')
else:
messages.error(request, 'Invalid username or password')
except UserDetails.DoesNotExist:
messages.error(request, 'Invalid username or password')
return render(request, 'asset_mng/asset_mng.html')
views for asset list
def asset_list(request):
users = Asset_Table.objects.all()
return render(request, 'asset_mng/asset_pro.html', {'users': users})
database to set on and off privilege
class AssetOwner_Privilege(models.Model):
user = models.ForeignKey(UserDetails, on_delete=models.CASCADE)
add_asset = models.BooleanField(default=False)
views for setting on and off in database
def privilege_asset_owner(request):
username =request.session.get('username',None)
if not username:
return redirect('asset_login')
user = UserDetails.objects.get(username = username)
asset_owner, created = AssetOwner_Privilege.objects.get_or_create(user=user)
if request.method == 'POST':
add_asset = request.POST.get('add_asset') == 'on'
asset_owner.add_asset = add_asset
asset_owner.save()
return redirect('privilege_asset_owner')
return render(request, 'asset_mng/privilege_asset_owner.html', {
'username': username,
'add_asset': asset_owner.add_asset,
})