diff --git a/.gitignore b/.gitignore index 06e58eb..f7dfdf6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /.venv __pycache__ /project/settings_local.py +/db.sqlite3 diff --git a/db.sqlite3 b/db.template.sqlite3 similarity index 95% rename from db.sqlite3 rename to db.template.sqlite3 index 8506228..eb2d571 100644 Binary files a/db.sqlite3 and b/db.template.sqlite3 differ diff --git a/project/main/apps.py b/project/main/apps.py index bf4f436..0928127 100644 --- a/project/main/apps.py +++ b/project/main/apps.py @@ -1,6 +1,21 @@ +import os +import shutil + from django.apps import AppConfig +from django.conf import settings class MainConfig(AppConfig): default_auto_field = "django.db.models.BigAutoField" name = "project.main" + + def ready(self): + """copy template database if db not exists""" + + db_fname = settings.DATABASES["default"]["NAME"] + template_db_fname = settings.BASE_DIR / "db.template.sqlite3" + + if os.path.exists(db_fname): + return + + shutil.copyfile(template_db_fname, db_fname) diff --git a/project/main/migrations/0003_person.py b/project/main/migrations/0003_person.py new file mode 100644 index 0000000..9945c5e --- /dev/null +++ b/project/main/migrations/0003_person.py @@ -0,0 +1,60 @@ +from django.db import migrations, models + + +def create_persons_data(apps, schema_editor): + Person = apps.get_model("main", "Person") + + bulk = [ + Person( + name="Pero", + address="Perina ulica 15", + city="Zagreb", + ), + Person( + name="Mirko", + address="Mirkova ulica 17", + city="Zagreb", + ), + Person( + name="Šime", + address="Šimina ulica 19", + city="Split", + ), + Person( + name="Furio", + address="Furiozna ulica 21", + city="Pula", + ), + ] + + Person.objects.bulk_create(bulk) + + +class Migration(migrations.Migration): + dependencies = [ + ("main", "0002_example_data"), + ] + + operations = [ + migrations.CreateModel( + name="Person", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(max_length=100)), + ("address", models.CharField(max_length=100)), + ("city", models.CharField(max_length=100)), + ], + options={ + "db_table": "persons", + }, + ), + migrations.RunPython(create_persons_data), + ] diff --git a/project/main/models/__init__.py b/project/main/models/__init__.py index 191fd62..888879d 100644 --- a/project/main/models/__init__.py +++ b/project/main/models/__init__.py @@ -1 +1,2 @@ from .cat_breed import CatBreed +from .person import Person diff --git a/project/main/models/person.py b/project/main/models/person.py new file mode 100644 index 0000000..73b71e0 --- /dev/null +++ b/project/main/models/person.py @@ -0,0 +1,10 @@ +from django.db import models + + +class Person(models.Model): + name = models.CharField(max_length=100) + address = models.CharField(max_length=100) + city = models.CharField(max_length=100) + + class Meta: + db_table = "persons" diff --git a/project/main/templates/main/components/inline_table_row.html b/project/main/templates/main/components/inline_table_row.html new file mode 100644 index 0000000..1960443 --- /dev/null +++ b/project/main/templates/main/components/inline_table_row.html @@ -0,0 +1,43 @@ +{% macro inline_table_row(person, is_editing) %} + + {% if is_editing %} + + + + + + + + + + + + + + {% else %} + {{ person.name }} + {{ person.address }} + {{ person.city }} + + + + {% endif %} + +{% endmacro %} diff --git a/project/main/templates/main/filter_list.html b/project/main/templates/main/filter_list.html index bf0842d..a17ea7c 100644 --- a/project/main/templates/main/filter_list.html +++ b/project/main/templates/main/filter_list.html @@ -50,6 +50,5 @@ - {% include "main/filter_list_content.html" %} {% endblock %} diff --git a/project/main/templates/main/table_inline_edit.html b/project/main/templates/main/table_inline_edit.html index 5eccfae..93d4d24 100644 --- a/project/main/templates/main/table_inline_edit.html +++ b/project/main/templates/main/table_inline_edit.html @@ -1,7 +1,24 @@ {% extends "main/base/layout.html" %} -{% from "main/components/js_alert.html" import js_alert_work_in_progress %} +{% from "main/components/js_alert.html" import no_js_alert %} +{% from "main/components/inline_table_row.html" import inline_table_row %} {% block content %} - {{ js_alert_work_in_progress() }} + {{ no_js_alert() }} + + + + + + + + + + + + {% for person in persons %} + {{ inline_table_row(person, is_editing=False) }} + {% endfor %} + +
NameAddressCity 
{% endblock %} diff --git a/project/main/templates/main/table_inline_table_row.html b/project/main/templates/main/table_inline_table_row.html new file mode 100644 index 0000000..9264292 --- /dev/null +++ b/project/main/templates/main/table_inline_table_row.html @@ -0,0 +1,3 @@ +{% from "main/components/inline_table_row.html" import inline_table_row %} + +{{ inline_table_row(person, is_editing=is_editing) }} diff --git a/project/main/views/__init__.py b/project/main/views/__init__.py index a75755b..2deca66 100644 --- a/project/main/views/__init__.py +++ b/project/main/views/__init__.py @@ -8,4 +8,4 @@ from .filter_list import FilterListFilterView, FilterListView from .form_validation import FormValidationView from .home import HomeView from .swap import SwapView -from .table_inline_edit import TableInlineEditView +from .table_inline_edit import TableInlineEditView, TableInlineEditRowView diff --git a/project/main/views/table_inline_edit.py b/project/main/views/table_inline_edit.py index 3c4486d..2171d36 100644 --- a/project/main/views/table_inline_edit.py +++ b/project/main/views/table_inline_edit.py @@ -1,3 +1,8 @@ +from typing import Any + +from django.http import Http404 + +from project.main.models import Person from project.main.views.demo_view_base import DemoViewBase @@ -5,3 +10,37 @@ class TableInlineEditView(DemoViewBase): template_name = "main/table_inline_edit.html" active_section = "table-inline-edit" title = "Table Inline Edit" + + def get_context_data(self, **kwargs) -> dict[str, Any]: + context_data = super().get_context_data(**kwargs) + + persons = Person.objects.all() + + context_data.update( + { + "persons": persons, + } + ) + return context_data + + +class TableInlineEditRowView(DemoViewBase): + template_name = "main/table_inline_table_row.html" + + def get_context_data(self, **kwargs) -> dict[str, Any]: + context_data = super().get_context_data(**kwargs) + + try: + person = Person.objects.get(pk=kwargs.get("pk")) + except Person.DoesNotExist: + raise Http404("Person not found") + + action = self.request.GET.get("action") + + context_data.update( + { + "person": person, + "is_editing": action != "cancel", + } + ) + return context_data diff --git a/project/urls.py b/project/urls.py index 10f49bc..87448b1 100644 --- a/project/urls.py +++ b/project/urls.py @@ -13,4 +13,5 @@ urlpatterns = [ path("complex-form/handle/reports", views.ReportsHandleView.as_view(), name="complex-form-handle-reports"), path("complex-form/handle/warehouse-management", views.WarehouseManagementHandleView.as_view(), name="complex-form-handle-warehouse-management"), path("table-inline-edit", views.TableInlineEditView.as_view(), name="table-inline-edit"), + path("table-inline-edit/edit/", views.TableInlineEditRowView.as_view(), name="table-inline-edit-row"), ]