commit 3b618dd791813013273f6bc0200ed67c3a3a95cd Author: Mohamad Hesham Jenbaz Date: Wed Feb 26 13:24:53 2025 +0100 first commit diff --git a/excel_mimic/__init__.py b/excel_mimic/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/excel_mimic/asgi.py b/excel_mimic/asgi.py new file mode 100644 index 0000000..ab15ba9 --- /dev/null +++ b/excel_mimic/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for excel_mimic project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'excel_mimic.settings') + +application = get_asgi_application() diff --git a/excel_mimic/settings.py b/excel_mimic/settings.py new file mode 100644 index 0000000..2f207b7 --- /dev/null +++ b/excel_mimic/settings.py @@ -0,0 +1,124 @@ +""" +Django settings for excel_mimic project. + +Generated by 'django-admin startproject' using Django 5.1.3. + +For more information on this file, see +https://docs.djangoproject.com/en/5.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/5.1/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-qsqw7#kmvlyc1ou5emdh8^_bqnvp+hui(4w#1yy4ui82+p=%p*' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'sheets.apps.SheetsConfig', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'excel_mimic.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'excel_mimic.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/5.1/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/5.1/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/5.1/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/excel_mimic/urls.py b/excel_mimic/urls.py new file mode 100644 index 0000000..6a61b93 --- /dev/null +++ b/excel_mimic/urls.py @@ -0,0 +1,23 @@ +""" +URL configuration for excel_mimic project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/5.1/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), # Admin site + path('', include('sheets.urls')), # Sheets app URLs +] \ No newline at end of file diff --git a/excel_mimic/wsgi.py b/excel_mimic/wsgi.py new file mode 100644 index 0000000..03f1921 --- /dev/null +++ b/excel_mimic/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for excel_mimic project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/5.1/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'excel_mimic.settings') + +application = get_wsgi_application() diff --git a/manage.py b/manage.py new file mode 100644 index 0000000..3d270b2 --- /dev/null +++ b/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'excel_mimic.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/sheets/__init__.py b/sheets/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sheets/admin.py b/sheets/admin.py new file mode 100644 index 0000000..c6fe108 --- /dev/null +++ b/sheets/admin.py @@ -0,0 +1,2 @@ +from django.contrib import admin + diff --git a/sheets/apps.py b/sheets/apps.py new file mode 100644 index 0000000..757cee8 --- /dev/null +++ b/sheets/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class SheetsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'sheets' diff --git a/sheets/forms.py b/sheets/forms.py new file mode 100644 index 0000000..5704562 --- /dev/null +++ b/sheets/forms.py @@ -0,0 +1,7 @@ +from django import forms +from .models import ExcelEntry + +class ExcelEntryForm(forms.ModelForm): + class Meta: + model = ExcelEntry + fields = ['name', 'age', 'email'] # Include only the fields you want users to input diff --git a/sheets/migrations/0001_initial.py b/sheets/migrations/0001_initial.py new file mode 100644 index 0000000..d91b967 --- /dev/null +++ b/sheets/migrations/0001_initial.py @@ -0,0 +1,21 @@ +# Generated by Django 5.1.3 on 2024-12-04 13:22 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Sheet', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=100)), + ], + ), + ] diff --git a/sheets/migrations/0002_maincolumn_row_subcolumn_cell.py b/sheets/migrations/0002_maincolumn_row_subcolumn_cell.py new file mode 100644 index 0000000..e12c666 --- /dev/null +++ b/sheets/migrations/0002_maincolumn_row_subcolumn_cell.py @@ -0,0 +1,46 @@ +# Generated by Django 5.1.3 on 2024-12-04 14:19 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('sheets', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='MainColumn', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=100)), + ('sheet', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='main_columns', to='sheets.sheet')), + ], + ), + migrations.CreateModel( + name='Row', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('sheet', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='rows', to='sheets.sheet')), + ], + ), + migrations.CreateModel( + name='SubColumn', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=100)), + ('main_column', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='sub_columns', to='sheets.maincolumn')), + ], + ), + migrations.CreateModel( + name='Cell', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('value', models.CharField(blank=True, max_length=255, null=True)), + ('row', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='cells', to='sheets.row')), + ('sub_column', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='cells', to='sheets.subcolumn')), + ], + ), + ] diff --git a/sheets/migrations/0003_remove_maincolumn_sheet_remove_subcolumn_main_column_and_more.py b/sheets/migrations/0003_remove_maincolumn_sheet_remove_subcolumn_main_column_and_more.py new file mode 100644 index 0000000..16449f2 --- /dev/null +++ b/sheets/migrations/0003_remove_maincolumn_sheet_remove_subcolumn_main_column_and_more.py @@ -0,0 +1,50 @@ +# Generated by Django 5.1.4 on 2024-12-10 07:27 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('sheets', '0002_maincolumn_row_subcolumn_cell'), + ] + + operations = [ + migrations.RemoveField( + model_name='maincolumn', + name='sheet', + ), + migrations.RemoveField( + model_name='subcolumn', + name='main_column', + ), + migrations.RemoveField( + model_name='row', + name='sheet', + ), + migrations.RenameModel( + old_name='Sheet', + new_name='Parent', + ), + migrations.CreateModel( + name='Child', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('description', models.CharField(max_length=200)), + ('parent', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='children', to='sheets.parent')), + ], + ), + migrations.DeleteModel( + name='Cell', + ), + migrations.DeleteModel( + name='MainColumn', + ), + migrations.DeleteModel( + name='SubColumn', + ), + migrations.DeleteModel( + name='Row', + ), + ] diff --git a/sheets/migrations/0004_excelentry_delete_child_delete_parent.py b/sheets/migrations/0004_excelentry_delete_child_delete_parent.py new file mode 100644 index 0000000..9bf73b2 --- /dev/null +++ b/sheets/migrations/0004_excelentry_delete_child_delete_parent.py @@ -0,0 +1,29 @@ +# Generated by Django 5.1.5 on 2025-01-22 13:05 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('sheets', '0003_remove_maincolumn_sheet_remove_subcolumn_main_column_and_more'), + ] + + operations = [ + migrations.CreateModel( + name='ExcelEntry', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=100)), + ('age', models.IntegerField()), + ('email', models.EmailField(max_length=254)), + ('date_joined', models.DateField(auto_now_add=True)), + ], + ), + migrations.DeleteModel( + name='Child', + ), + migrations.DeleteModel( + name='Parent', + ), + ] diff --git a/sheets/migrations/0005_secondtableentry.py b/sheets/migrations/0005_secondtableentry.py new file mode 100644 index 0000000..0d2fbd5 --- /dev/null +++ b/sheets/migrations/0005_secondtableentry.py @@ -0,0 +1,23 @@ +# Generated by Django 5.1.5 on 2025-02-17 10:24 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('sheets', '0004_excelentry_delete_child_delete_parent'), + ] + + operations = [ + migrations.CreateModel( + name='SecondTableEntry', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=100)), + ('age', models.IntegerField()), + ('email', models.EmailField(max_length=254)), + ('date_joined', models.DateField(auto_now_add=True)), + ], + ), + ] diff --git a/sheets/migrations/__init__.py b/sheets/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sheets/models.py b/sheets/models.py new file mode 100644 index 0000000..c855fea --- /dev/null +++ b/sheets/models.py @@ -0,0 +1,19 @@ +from django.db import models + +class ExcelEntry(models.Model): + name = models.CharField(max_length=100) + age = models.IntegerField() + email = models.EmailField() + date_joined = models.DateField(auto_now_add=True) + + def __str__(self): + return self.name + +class SecondTableEntry(models.Model): + name = models.CharField(max_length=100) + age = models.IntegerField() + email = models.EmailField() + date_joined = models.DateField(auto_now_add=True) + + def __str__(self): + return self.name \ No newline at end of file diff --git a/sheets/templates/excel_table.html b/sheets/templates/excel_table.html new file mode 100644 index 0000000..433a372 --- /dev/null +++ b/sheets/templates/excel_table.html @@ -0,0 +1,344 @@ + + + + + + Excel-like Table + + + + + + + +

Excel-like Table

+ +
+ +
+ + + + + + + + + + + + + + {% for entry in entries_table1 %} + + + + + + + + + {% endfor %} + +
IDNameAgeEmailDate JoinedActions
{{ entry.id }}{{ entry.name }}{{ entry.age }}{{ entry.email }}{{ entry.date_joined }} + + +
+
+ + +
+ + + + + + + + + + + + + + {% for entry in entries_table2 %} + + + + + + + + + {% endfor %} + +
IDNameAgeEmailDate JoinedActions
{{ entry.id }}{{ entry.name }}{{ entry.age }}{{ entry.email }}{{ entry.date_joined }} + + +
+
+
+ + + + + + + + + + + \ No newline at end of file diff --git a/sheets/tests.py b/sheets/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/sheets/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/sheets/urls.py b/sheets/urls.py new file mode 100644 index 0000000..c583839 --- /dev/null +++ b/sheets/urls.py @@ -0,0 +1,9 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path('', views.excel_table_view, name='excel_table'), + path('add-entry//', views.add_entry, name='add_entry'), + path('update-entry//', views.update_entry, name='update_entry'), + path('delete-entry//', views.delete_entry, name='delete_entry'), +] \ No newline at end of file diff --git a/sheets/views.py b/sheets/views.py new file mode 100644 index 0000000..b838c93 --- /dev/null +++ b/sheets/views.py @@ -0,0 +1,111 @@ +from django.shortcuts import render +from django.http import JsonResponse +from django.apps import apps +from datetime import date + +# View to render the table page +def excel_table_view(request): + # Fetch existing entries from both tables + entries_table1 = apps.get_model('sheets', 'ExcelEntry').objects.all() + entries_table2 = apps.get_model('sheets', 'SecondTableEntry').objects.all() + return render(request, 'excel_table.html', { + 'entries_table1': entries_table1, + 'entries_table2': entries_table2, + }) + +# Generic view to add a new row to any table +def add_entry(request, model_name): + if request.method == 'POST': + try: + # Dynamically get the model + model = apps.get_model('sheets', model_name) + + # Get data from the request + name = request.POST.get('name', 'New Name') + age = int(request.POST.get('age', 0)) + email = request.POST.get('email', 'example@email.com') + + # Create a new entry + entry = model.objects.create( + name=name, + age=age, + email=email, + date_joined=date.today() + ) + + # Return the new entry as JSON response + return JsonResponse({ + 'id': entry.id, + 'name': entry.name, + 'age': entry.age, + 'email': entry.email, + 'date_joined': entry.date_joined.strftime('%Y-%m-%d'), + }) + + except Exception as e: + return JsonResponse({'status': 'error', 'message': str(e)}, status=400) + + return JsonResponse({'status': 'error', 'message': 'Invalid request'}, status=400) + +# Generic view to update an entry in any table +def update_entry(request, model_name): + if request.method == 'POST': + try: + # Dynamically get the model + model = apps.get_model('sheets', model_name) + + # Get data from the request + entry_id = int(request.POST.get('id')) + name = request.POST.get('name') + age = int(request.POST.get('age')) + email = request.POST.get('email') + + # Fetch the entry to be updated + entry = model.objects.get(id=entry_id) + + # Update the entry with new data + entry.name = name + entry.age = age + entry.email = email + entry.save() + + # Return the updated entry as a JSON response + return JsonResponse({ + 'status': 'success', + 'id': entry.id, + 'name': entry.name, + 'age': entry.age, + 'email': entry.email, + 'date_joined': entry.date_joined.strftime('%Y-%m-%d'), + }) + + except model.DoesNotExist: + return JsonResponse({'status': 'error', 'message': 'Entry not found'}, status=404) + except Exception as e: + return JsonResponse({'status': 'error', 'message': str(e)}, status=400) + + return JsonResponse({'status': 'error', 'message': 'Invalid request'}, status=400) + +# Generic view to delete an entry from any table +def delete_entry(request, model_name): + if request.method == 'POST': + try: + # Dynamically get the model + model = apps.get_model('sheets', model_name) + + # Get the entry ID from the request + entry_id = request.POST.get('id') + + # Find the entry by its ID and delete it + entry = model.objects.get(id=entry_id) + entry.delete() + + # Return success response + return JsonResponse({'status': 'success', 'message': 'Entry deleted'}) + + except model.DoesNotExist: + return JsonResponse({'status': 'error', 'message': 'Entry not found'}, status=404) + except Exception as e: + return JsonResponse({'status': 'error', 'message': str(e)}, status=400) + + return JsonResponse({'status': 'error', 'message': 'Invalid request'}, status=400) \ No newline at end of file