Skip to content

Commit

Permalink
add render-block
Browse files Browse the repository at this point in the history
  • Loading branch information
ed-p-may committed Dec 12, 2024
1 parent 4c5670b commit ec74674
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 49 deletions.
1 change: 1 addition & 0 deletions PH_Materials/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"django_htmx",
"template_partials",
"import_export",
"render_block",
# ---------------------
# -- Apps
"webportal",
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ django-htmx==1.21.0
django-import-export==4.3.1
django-template-partials==24.4
django-widget-tweaks==1.5.0
django-render-block==0.10
factory-boy==3.3.1
faker==33.1.0
gunicorn==23.0.0
Expand Down
17 changes: 17 additions & 0 deletions webportal/management/commands/delete_assemblies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.core.management.base import BaseCommand

from webportal.models import Assembly


class Command(BaseCommand):
help = """
Delete all the assemblies from the database.
To run: 'python manage.py delete_assemblies'
"""

def handle(self, *args, **kwargs):
num_assemblies = Assembly.objects.count()
Assembly.objects.all().delete()
self.stdout.write(
self.style.SUCCESS(f"{num_assemblies} assemblies deleted successfully.")
)
18 changes: 18 additions & 0 deletions webportal/migrations/0008_alter_material_unique_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.3 on 2024-12-11 22:24

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("webportal", "0007_assembly_user_alter_material_unique_id"),
]

operations = [
migrations.AlterField(
model_name="material",
name="unique_id",
field=models.CharField(default="41573d", max_length=6, unique=True),
),
]
18 changes: 18 additions & 0 deletions webportal/migrations/0009_alter_material_unique_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.3 on 2024-12-11 22:28

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("webportal", "0008_alter_material_unique_id"),
]

operations = [
migrations.AlterField(
model_name="material",
name="unique_id",
field=models.CharField(default="4e3bd0", max_length=6, unique=True),
),
]
9 changes: 9 additions & 0 deletions webportal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ class Assembly(models.Model):
name = models.CharField(max_length=100, default="assembly")
created_at = models.DateTimeField(auto_now_add=True)

def save(self, *args, **kwargs):
super().save(*args, **kwargs)
cells = Cell.objects.filter(container=self)
if not cells.exists():
Cell.objects.create(container=self, column_number=1, row_number=1)


class Cell(models.Model):
container = models.ForeignKey(
Expand All @@ -123,3 +129,6 @@ class Cell(models.Model):
column_number = models.IntegerField()
row_number = models.IntegerField()
value = models.TextField(blank=True, null=True)

def __str__(self):
return f"{self.container.name} Cell: [{self.row_number}:{self.column_number}]"
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
<div id="assembly-detail-view">

<h1>assembly-detail-view {{ assembly.name }}</h1>
<div id="assembly-cells">
{% for row in row_range %}
<div class="row">
{% for col in col_range %}
<div class="cell">
<input class="text-black" value="Test" />
{% comment %}
{% for cell in cells %}
{% if cell.x == col and cell.y == row %}
{% endif %}
{% endfor %}
{% endcomment %}
</div>
{% endfor %}
</div>
{% endfor %}
</div>

<div id="grid-control-buttons">
<button class="btn" hx-post="{% url 'add-row' assembly.pk %}" hx-target="#assembly-cells" hx-swap="beforeend">Add Row</button>
<button class="btn" hx-post="{% url 'add-column' assembly.pk %}" hx-target="#assembly-cells">Add Column</button>
</div>

</div>

This file was deleted.

23 changes: 22 additions & 1 deletion webportal/templates/webportal/partials/assemblies/assembly.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
<div id="assembly" class="col-span-4">
{% if assembly %}
{% include "webportal/partials/assemblies/assembly-name.html" %}

{% block assembly-name %}
<div id="assembly-name">
<form
hx-post="{% url 'update-assembly-name' assembly.id %}"
hx-target="#assembly-name"
hx-swap="outerHTML"
>
<input
type="text"
name="name"
class="text-black"
value="{{ assembly.name }}"
hx-trigger="changed delay:500ms"
style="border: 1px solid #939799; padding: 8px; border-radius: 4px; background: transparent; color: white;"
onfocus="this.style.borderColor='#007bff'; this.style.boxShadow='0 0 5px rgba(0, 123, 255, 0.5)'; this.style.outline='none'; this.style.background='white'; this.style.color='black';"
onblur="this.style.borderColor='#ccc'; this.style.boxShadow='none'; this.style.background='transparent'; this.style.color='white';"
/>
</form>
</div>
{% endblock %}

{% include "webportal/partials/assemblies/assembly-detail.html" %}
{% endif %}
</div>
3 changes: 0 additions & 3 deletions webportal/templates/webportal/partials/assemblies/column.html

This file was deleted.

11 changes: 8 additions & 3 deletions webportal/templates/webportal/partials/assemblies/row.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{% for cell in cells %}
<div class="cell">{{ cell.value }}</div>
{% endfor %}
<div class="row grid grid-cols-[200px_minmax(900px,_1fr)_100px]">
{% for cell in cells %}
{% comment %} <div class="cell">{{ cell.value }}</div> {% endcomment %}
<div class="cell">
<input class="text-black" value="Test" />
</div>
{% endfor %}
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{% endif %}
>
<a
id="delete-assembly-{{ assembly.pk }}"
hx-delete="{% url 'delete-assembly' assembly.pk %}"
hx-target="#assembly"
hx-swap="outerHTML"
Expand All @@ -27,6 +28,7 @@
</svg>
</a>
<a
id="assembly-name-{{ assembly.pk }}"
hx-get="{% url 'assembly' assembly.pk %}"
hx-target="#assembly"
hx-swap="outerHTML"
Expand Down
76 changes: 53 additions & 23 deletions webportal/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
from django.core.handlers.wsgi import WSGIRequest
from django.core.paginator import Paginator
from django.http import HttpResponse, JsonResponse
from django.db.models import Q
from django.db.models import Q, Max
from django.db.models.functions import Lower
from django.template.loader import render_to_string
from django.template import Context
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
from django.views.decorators.http import require_http_methods, require_POST
from django_htmx.http import retarget
from tablib import Dataset
from render_block import render_block_to_string

from webportal.filters import MaterialCategoryFilter
from webportal.forms import MaterialForm
Expand Down Expand Up @@ -241,10 +243,30 @@ def assembly(request: WSGIRequest, pk: int) -> HttpResponse:
"""
this_assembly = get_object_or_404(Assembly, pk=pk)

# Render the new assembly item
num_rows = this_assembly.cells.aggregate(mx=Max("row_number"))["mx"]
num_cols = this_assembly.cells.aggregate(mx=Max("column_number"))["mx"]

assembly_html = render_to_string(
"webportal/partials/assemblies/assembly.html", {"assembly": this_assembly}
"webportal/partials/assemblies/assembly.html",
{
"assembly": this_assembly,
"cells": this_assembly.cells.all(),
"row_range": range(num_rows),
"col_range": range(num_cols),
},
)

# sidebar_html = render_block_to_string(
# "webportal/partials/assemblies/sidebar_list.html",
# block_name="",
# context=Context(
# {
# "assemblies": Assembly.objects.filter(user=request.user),
# "active_assembly_id": pk,
# }
# ),
# request=request,
# )
sidebar_html = render_to_string(
"webportal/partials/assemblies/sidebar_list.html",
{
Expand All @@ -262,20 +284,19 @@ def assembly(request: WSGIRequest, pk: int) -> HttpResponse:
@login_required
@require_POST
def update_assembly_name(request: WSGIRequest, pk: int) -> HttpResponse:
"""Update the name of the Assembly.
Returns:
The updated 'Assembly' view.
"""

this_assembly = get_object_or_404(Assembly, pk=pk)

if request.method == "POST":
if name := request.POST.get("name"):
this_assembly = get_object_or_404(Assembly, pk=pk)
this_assembly.name = name
this_assembly.save()

return assembly(request, pk)
template_name = "webportal/partials/assemblies/assembly.html"
context = {"assembly": this_assembly}
detail_view_name = render_block_to_string(
template_name, block_name="assembly-name", context=context, request=request
)
sidebar_name = f"<div id='assembly-name-{this_assembly.pk}' hx-swap-oob='true'>{this_assembly.name}</div>"
return HttpResponse(content=detail_view_name + sidebar_name)


@login_required
Expand Down Expand Up @@ -320,31 +341,40 @@ def assembly_detail(request, pk):

def add_row(request, pk):
container = get_object_or_404(Assembly, id=pk)
max_y = container.cells.aggregate(max_y=models.Max("y"))["max_y"] or 0
new_y = max_y + 1
max_x = container.cells.aggregate(max_x=models.Max("x"))["max_x"] or 0
max_row_num = container.cells.aggregate(mx=Max("row_number")).get("mx", 0)
new_row_num = max_row_num + 1
max_col_num = container.cells.aggregate(mx=Max("column_number")).get("mx", 0)

# Add new row
new_cells = []
for x in range(max_x + 1):
cell = Cell(container=container, x=x, y=new_y, value="")
for col_num in range(max_col_num):
cell = Cell(
container=container,
column_number=col_num + 1,
row_number=new_row_num,
value="",
)
new_cells.append(cell)
Cell.objects.bulk_create(new_cells)

# Render just the new row
return JsonResponse({"html": render_to_string("row.html", {"cells": new_cells})})
return HttpResponse(
render_to_string("webportal/partials/assemblies/row.html", {"cells": new_cells})
)


def add_column(request, pk):
container = get_object_or_404(Assembly, id=container_id)
max_x = container.cells.aggregate(max_x=models.Max("x"))["max_x"] or 0
new_x = max_x + 1
max_y = container.cells.aggregate(max_y=models.Max("y"))["max_y"] or 0
max_row_num = container.cells.aggregate(mx=Max("row_number")).get("mx", 0)
max_col_num = container.cells.aggregate(mx=Max("column_number")).get("mx", 0)
new_col_num = max_col_num + 1

# Add new column
new_cells = []
for y in range(max_y + 1):
cell = Cell(container=container, x=new_x, y=y, value="")
for row_num in range(max_row_num + 1):
cell = Cell(
container=container, column_number=new_col_num, row_number=row_num, value=""
)
new_cells.append(cell)
Cell.objects.bulk_create(new_cells)

Expand Down

0 comments on commit ec74674

Please sign in to comment.