Skip to content
Snippets Groups Projects
Commit 928a2e5e authored by a2-imeri's avatar a2-imeri
Browse files

Intigrate rules with Admin

parent ad277d8c
No related branches found
No related tags found
No related merge requests found
Showing
with 130 additions and 40 deletions
# Ignore all __pycache__ directories
__pycache__/ __pycache__/
**/migrations/*
!**/migrations/__init__.py # Ignore all migrations directories
registration migrations/
\ No newline at end of file
#Dockerfile
# Use the full Python runtime as a parent image # Use the full Python runtime as a parent image
FROM python:3.10 FROM python:3.10
......
...@@ -10,7 +10,10 @@ ...@@ -10,7 +10,10 @@
<h1>Admin Dashboard</h1> <h1>Admin Dashboard</h1>
<div class="list-group"> <div class="list-group">
<a href="{% url 'admin_users' %}" class="list-group-item list-group-item-action">Users Activity</a> <a href="{% url 'admin_users' %}" class="list-group-item list-group-item-action">Users Activity</a>
<!-- Add other admin links here --> <a href="{% url 'rules:list_rules' %}" class="list-group-item list-group-item-action">Manage Rules</a>
<a href="{% url 'rules:admin_add_location' %}" class="list-group-item list-group-item-action">Add
Location</a>
<a href="{% url 'rules:admin_add_item' %}" class="list-group-item list-group-item-action">Add Item</a>
</div> </div>
</div> </div>
</div> </div>
......
No preview for this file type
#docker-compose.yml
version: '3.7' version: '3.7'
services: services:
......
File deleted
File deleted
File deleted
No preview for this file type
No preview for this file type
File deleted
No preview for this file type
File deleted
No preview for this file type
from django import forms from django import forms
from .models import Rule from .models import Rule, Location, Item
class RuleForm(forms.ModelForm): class RuleForm(forms.ModelForm):
class Meta: class Meta:
model = Rule model = Rule
fields = ['name', 'condition', 'action'] # Include all the fields you want from the model fields = ['user', 'item', 'locations']
def clean_name(self): class LocationForm(forms.ModelForm):
name = self.cleaned_data.get('name') class Meta:
if not name: model = Location
raise forms.ValidationError("The name field cannot be left blank.") fields = ['name']
return name
def clean_condition(self):
condition = self.cleaned_data.get('condition')
if not condition:
raise forms.ValidationError("The condition field cannot be left blank.")
return condition
def clean_action(self): class ItemForm(forms.ModelForm):
action = self.cleaned_data.get('action') class Meta:
if not action: model = Item
raise forms.ValidationError("The action field cannot be left blank.") fields = ['name']
return action
# Generated by Django 3.2 on 2024-05-15 15:06 # Generated by Django 3.2 on 2024-05-15 20:43
from django.conf import settings
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration): class Migration(migrations.Migration):
...@@ -8,16 +10,31 @@ class Migration(migrations.Migration): ...@@ -8,16 +10,31 @@ class Migration(migrations.Migration):
initial = True initial = True
dependencies = [ dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
] ]
operations = [ operations = [
migrations.CreateModel( migrations.CreateModel(
name='Rule', name='Item',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255, unique=True)),
],
),
migrations.CreateModel(
name='Location',
fields=[ fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255, unique=True)), ('name', models.CharField(max_length=255, unique=True)),
('condition', models.TextField()), ],
('action', models.TextField()), ),
migrations.CreateModel(
name='Rule',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('item', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='rules.item')),
('locations', models.ManyToManyField(to='rules.Location')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
], ],
), ),
] ]
No preview for this file type
from django.db import models from django.db import models
from django.contrib.auth.models import User
class Rule(models.Model): class Location(models.Model):
name = models.CharField(max_length=255, unique=True)
def __str__(self):
return self.name
class Item(models.Model):
name = models.CharField(max_length=255, unique=True) name = models.CharField(max_length=255, unique=True)
condition = models.TextField()
action = models.TextField()
def __str__(self): def __str__(self):
return self.name return self.name
class Rule(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
item = models.ForeignKey(Item, on_delete=models.CASCADE)
locations = models.ManyToManyField(Location)
def __str__(self):
return f"{self.item.name} Rule"
{% extends 'core/base.html' %}
{% load static %}
{% block title %}Add Rule{% endblock %}
{% block content %}
<div class="container mt-5">
<h1>Add Rule</h1> <h1>Add Rule</h1>
<form method="post"> <form method="post">
{% csrf_token %} {% csrf_token %}
<label for="name">Name:</label> {{ form.as_p }}
<input type="text" id="name" name="name"><br> <button type="submit" class="btn btn-success">Save Rule</button>
<label for="condition">Condition:</label>
<textarea id="condition" name="condition"></textarea><br>
<label for="action">Action:</label>
<textarea id="action" name="action"></textarea><br>
<input type="submit" value="Submit">
</form> </form>
</div>
{% endblock %}
\ No newline at end of file
{% extends 'core/base.html' %}
{% load static %}
{% block title %}Add Item{% endblock %}
{% block content %}
<div class="container mt-5">
<h1>Add Item</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-success">Add Item</button>
</form>
<h2>Existing Items</h2>
<table class="table">
<thead>
<tr>
<th>Item Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for item in page_obj %}
<tr>
<td>{{ item.name }}</td>
<td>
<a href="{% url 'rules:edit_item' item.id %}" class="btn btn-warning btn-sm">Edit</a>
<a href="{% url 'rules:delete_item' item.id %}" class="btn btn-danger btn-sm"
onclick="return confirm('Are you sure you want to delete this item?');">Delete</a>
</td>
</tr>
{% empty %}
<tr>
<td colspan="2">No items found.</td>
</tr>
{% endfor %}
</tbody>
</table>
<!-- Pagination -->
<nav aria-label="Page navigation">
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a>
</li>
{% endif %}
{% for num in page_obj.paginator.page_range %}
<li class="page-item {% if page_obj.number == num %}active{% endif %}"><a class="page-link"
href="?page={{ num }}">{{ num }}</a></li>
{% endfor %}
{% if page_obj.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a></li>
{% endif %}
</ul>
</nav>
</div>
{% endblock %}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment