From 400cc781b5b6e23ada144d3d8da2c386cad25c59 Mon Sep 17 00:00:00 2001 From: himanshumahajan138 Date: Wed, 11 Dec 2024 19:40:14 +0530 Subject: [PATCH 01/10] [WIP] First Test Phase --- example/package.mill | 1 + .../pythonlib/web/1-hello-flask/build.mill | 32 +++++ .../web/1-hello-flask/foo/src/foo.py | 10 ++ .../web/1-hello-flask/foo/test/src/test.py | 17 +++ example/pythonlib/web/2-todo-flask/build.mill | 24 ++++ .../web/2-todo-flask/todo/src/app.py | 74 +++++++++++ .../web/2-todo-flask/todo/src/forms.py | 10 ++ .../web/2-todo-flask/todo/src/models.py | 14 ++ .../web/2-todo-flask/todo/static/style.css | 21 +++ .../web/2-todo-flask/todo/templates/base.html | 31 +++++ .../2-todo-flask/todo/templates/index.html | 30 +++++ .../web/2-todo-flask/todo/templates/task.html | 24 ++++ .../web/2-todo-flask/todo/test/src/test.py | 1 + .../pythonlib/web/3-hello-django/build.mill | 31 +++++ .../3-hello-django/foo/src/app/__init__.py | 0 .../web/3-hello-django/foo/src/app/asgi.py | 16 +++ .../3-hello-django/foo/src/app/settings.py | 124 ++++++++++++++++++ .../web/3-hello-django/foo/src/app/urls.py | 24 ++++ .../web/3-hello-django/foo/src/app/wsgi.py | 16 +++ .../3-hello-django/foo/src/main/__init__.py | 0 .../web/3-hello-django/foo/src/main/admin.py | 3 + .../web/3-hello-django/foo/src/main/apps.py | 6 + .../foo/src/main/migrations/__init__.py | 0 .../web/3-hello-django/foo/src/main/models.py | 3 + .../web/3-hello-django/foo/src/main/tests.py | 12 ++ .../web/3-hello-django/foo/src/main/views.py | 5 + .../web/3-hello-django/foo/src/manage.py | 22 ++++ 27 files changed, 551 insertions(+) create mode 100644 example/pythonlib/web/1-hello-flask/build.mill create mode 100644 example/pythonlib/web/1-hello-flask/foo/src/foo.py create mode 100644 example/pythonlib/web/1-hello-flask/foo/test/src/test.py create mode 100644 example/pythonlib/web/2-todo-flask/build.mill create mode 100644 example/pythonlib/web/2-todo-flask/todo/src/app.py create mode 100644 example/pythonlib/web/2-todo-flask/todo/src/forms.py create mode 100644 example/pythonlib/web/2-todo-flask/todo/src/models.py create mode 100644 example/pythonlib/web/2-todo-flask/todo/static/style.css create mode 100644 example/pythonlib/web/2-todo-flask/todo/templates/base.html create mode 100644 example/pythonlib/web/2-todo-flask/todo/templates/index.html create mode 100644 example/pythonlib/web/2-todo-flask/todo/templates/task.html create mode 100644 example/pythonlib/web/2-todo-flask/todo/test/src/test.py create mode 100644 example/pythonlib/web/3-hello-django/build.mill create mode 100644 example/pythonlib/web/3-hello-django/foo/src/app/__init__.py create mode 100644 example/pythonlib/web/3-hello-django/foo/src/app/asgi.py create mode 100644 example/pythonlib/web/3-hello-django/foo/src/app/settings.py create mode 100644 example/pythonlib/web/3-hello-django/foo/src/app/urls.py create mode 100644 example/pythonlib/web/3-hello-django/foo/src/app/wsgi.py create mode 100644 example/pythonlib/web/3-hello-django/foo/src/main/__init__.py create mode 100644 example/pythonlib/web/3-hello-django/foo/src/main/admin.py create mode 100644 example/pythonlib/web/3-hello-django/foo/src/main/apps.py create mode 100644 example/pythonlib/web/3-hello-django/foo/src/main/migrations/__init__.py create mode 100644 example/pythonlib/web/3-hello-django/foo/src/main/models.py create mode 100644 example/pythonlib/web/3-hello-django/foo/src/main/tests.py create mode 100644 example/pythonlib/web/3-hello-django/foo/src/main/views.py create mode 100755 example/pythonlib/web/3-hello-django/foo/src/manage.py diff --git a/example/package.mill b/example/package.mill index 1616927e8ca..1b5f8421d0b 100644 --- a/example/package.mill +++ b/example/package.mill @@ -69,6 +69,7 @@ object `package` extends RootModule with Module { object dependencies extends Cross[ExampleCrossModule](build.listIn(millSourcePath / "dependencies")) object publishing extends Cross[ExampleCrossModule](build.listIn(millSourcePath / "publishing")) object module extends Cross[ExampleCrossModule](build.listIn(millSourcePath / "module")) + object web extends Cross[ExampleCrossModule](build.listIn(millSourcePath / "web")) } object cli extends Module{ diff --git a/example/pythonlib/web/1-hello-flask/build.mill b/example/pythonlib/web/1-hello-flask/build.mill new file mode 100644 index 00000000000..2fc70becb84 --- /dev/null +++ b/example/pythonlib/web/1-hello-flask/build.mill @@ -0,0 +1,32 @@ +package build +import mill._, pythonlib._ + +object foo extends PythonModule { + + def mainScript = Task.Source { millSourcePath / "src" / "foo.py" } + + def pythonDeps = Seq("flask==3.1.0") + + object test extends PythonTests with TestModule.Unittest + +} + +/** Usage + +> ./mill foo.test +... +test_hello_flask (test.TestScript...) +Test the '/' endpoint. ... ok +... +Ran 1 test... +OK +... + +> ./mill foo.runBackground + +> curl http://localhost:5000 +...

Hello, Mill!

... + +> ./mill clean foo.runBackground + +*/ diff --git a/example/pythonlib/web/1-hello-flask/foo/src/foo.py b/example/pythonlib/web/1-hello-flask/foo/src/foo.py new file mode 100644 index 00000000000..ca92ec70e52 --- /dev/null +++ b/example/pythonlib/web/1-hello-flask/foo/src/foo.py @@ -0,0 +1,10 @@ +from flask import Flask + +app = Flask(__name__) + +@app.route("/") +def hello_world(): + return "

Hello, Mill!

" + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file diff --git a/example/pythonlib/web/1-hello-flask/foo/test/src/test.py b/example/pythonlib/web/1-hello-flask/foo/test/src/test.py new file mode 100644 index 00000000000..982e222e474 --- /dev/null +++ b/example/pythonlib/web/1-hello-flask/foo/test/src/test.py @@ -0,0 +1,17 @@ +import unittest +from foo import app # type: ignore + +class TestScript(unittest.TestCase): + def setUp(self): + """Set up the test client before each test.""" + self.app = app.test_client() # Initialize the test client + self.app.testing = True # Enable testing mode for better error handling + + def test_hello_flask(self): + """Test the '/' endpoint.""" + response = self.app.get('/') # Simulate a GET request to the root endpoint + self.assertEqual(response.status_code, 200) # Check the HTTP status code + self.assertIn(b"Hello, Mill!", response.data) # Check if the response contains the expected text + +if __name__ == '__main__': + unittest.main() diff --git a/example/pythonlib/web/2-todo-flask/build.mill b/example/pythonlib/web/2-todo-flask/build.mill new file mode 100644 index 00000000000..c3523519099 --- /dev/null +++ b/example/pythonlib/web/2-todo-flask/build.mill @@ -0,0 +1,24 @@ +package build +import mill._, pythonlib._ + +object todo extends PythonModule { + + def mainScript = Task.Source { millSourcePath / "src" / "app.py" } + + def pythonDeps = Seq("flask==3.1.0", "Flask-SQLAlchemy==3.1.1", "Flask-WTF==1.2.2") + + object test extends PythonTests with TestModule.Unittest + +} + +// TODO: Testing will be added soon... + +/** Usage + +> ./mill todo.runBackground + +> curl http://localhost:5001 + +> ./mill clean todo.runBackground + +*/ diff --git a/example/pythonlib/web/2-todo-flask/todo/src/app.py b/example/pythonlib/web/2-todo-flask/todo/src/app.py new file mode 100644 index 00000000000..0cebf1afb94 --- /dev/null +++ b/example/pythonlib/web/2-todo-flask/todo/src/app.py @@ -0,0 +1,74 @@ +from flask import Flask, render_template, redirect, url_for, flash, request +from flask_sqlalchemy import SQLAlchemy +from flask_wtf import FlaskForm +from wtforms import StringField, TextAreaField, SelectField, DateField, SubmitField +from wtforms.validators import DataRequired, Length + +# Initialize Flask App and Database +app = Flask(__name__, static_folder="../static", template_folder="../templates") +app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///todo.db" +app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False +app.config["SECRET_KEY"] = "your_secret_key" + +# Import models +from models import Task, db + +# Import forms +from forms import TaskForm + +db.init_app(app) + + +# Routes +@app.route("/") +def index(): + tasks = Task.query.all() + return render_template("index.html", tasks=tasks) + + +@app.route("/add", methods=["GET", "POST"]) +def add_task(): + form = TaskForm() + if form.validate_on_submit(): + new_task = Task( + title=form.title.data, + description=form.description.data, + status=form.status.data, + deadline=form.deadline.data, + ) + db.session.add(new_task) + db.session.commit() + flash("Task added successfully!", "success") + return redirect(url_for("index")) + return render_template("task.html", form=form, title="Add Task") + + +@app.route("/edit/", methods=["GET", "POST"]) +def edit_task(task_id): + task = Task.query.get_or_404(task_id) + form = TaskForm(obj=task) + if form.validate_on_submit(): + task.title = form.title.data + task.description = form.description.data + task.status = form.status.data + task.deadline = form.deadline.data + db.session.commit() + flash("Task updated successfully!", "success") + return redirect(url_for("index")) + return render_template("task.html", form=form, title="Edit Task") + + +@app.route("/delete/") +def delete_task(task_id): + task = Task.query.get_or_404(task_id) + db.session.delete(task) + db.session.commit() + flash("Task deleted successfully!", "success") + return redirect(url_for("index")) + + +# Create database tables and run the app +if __name__ == "__main__": + with app.app_context(): + db.create_all() + app.run(debug=True, port=5001) diff --git a/example/pythonlib/web/2-todo-flask/todo/src/forms.py b/example/pythonlib/web/2-todo-flask/todo/src/forms.py new file mode 100644 index 00000000000..be10adcff0f --- /dev/null +++ b/example/pythonlib/web/2-todo-flask/todo/src/forms.py @@ -0,0 +1,10 @@ +from flask_wtf import FlaskForm +from wtforms import StringField, TextAreaField, SelectField, DateField, SubmitField +from wtforms.validators import DataRequired, Length + +class TaskForm(FlaskForm): + title = StringField('Title', validators=[DataRequired(), Length(max=100)]) + description = TextAreaField('Description') + status = SelectField('Status', choices=[('Pending', 'Pending'), ('Completed', 'Completed')]) + deadline = DateField('Deadline', format='%Y-%m-%d', validators=[DataRequired()]) + submit = SubmitField('Save') diff --git a/example/pythonlib/web/2-todo-flask/todo/src/models.py b/example/pythonlib/web/2-todo-flask/todo/src/models.py new file mode 100644 index 00000000000..93bd7c82bf4 --- /dev/null +++ b/example/pythonlib/web/2-todo-flask/todo/src/models.py @@ -0,0 +1,14 @@ +from flask_sqlalchemy import SQLAlchemy + +db = SQLAlchemy() + +class Task(db.Model): + id = db.Column(db.Integer, primary_key=True) + title = db.Column(db.String(100), nullable=False) + description = db.Column(db.Text, nullable=True) + status = db.Column(db.String(20), default='Pending') # Options: Pending, Completed + created_at = db.Column(db.DateTime, default=db.func.current_timestamp()) + deadline = db.Column(db.Date) + + def __repr__(self): + return f'' diff --git a/example/pythonlib/web/2-todo-flask/todo/static/style.css b/example/pythonlib/web/2-todo-flask/todo/static/style.css new file mode 100644 index 00000000000..54f07a91f48 --- /dev/null +++ b/example/pythonlib/web/2-todo-flask/todo/static/style.css @@ -0,0 +1,21 @@ +body { + background-color: #f8f9fa; +} + +.navbar-brand { + font-weight: bold; +} + +.table th, +.table td { + text-align: center; + vertical-align: middle; +} + +.btn { + margin-right: 5px; +} + +.container { + max-width: 900px; +} diff --git a/example/pythonlib/web/2-todo-flask/todo/templates/base.html b/example/pythonlib/web/2-todo-flask/todo/templates/base.html new file mode 100644 index 00000000000..7beef670609 --- /dev/null +++ b/example/pythonlib/web/2-todo-flask/todo/templates/base.html @@ -0,0 +1,31 @@ + + + + + + Flask To-Do App + + + + + +
+ {% with messages = get_flashed_messages(with_categories=True) %} + {% if messages %} + {% for category, message in messages %} + + {% endfor %} + {% endif %} + {% endwith %} + {% block content %}{% endblock %} +
+ + + diff --git a/example/pythonlib/web/2-todo-flask/todo/templates/index.html b/example/pythonlib/web/2-todo-flask/todo/templates/index.html new file mode 100644 index 00000000000..8d1619a4758 --- /dev/null +++ b/example/pythonlib/web/2-todo-flask/todo/templates/index.html @@ -0,0 +1,30 @@ +{% extends 'base.html' %} +{% block content %} +

Task List

+Add Task + + + + + + + + + + + + {% for task in tasks %} + + + + + + + + {% endfor %} + +
#TitleStatusDeadlineActions
{{ loop.index }}{{ task.title }}{{ task.status }}{{ task.deadline }} + Edit + Delete +
+{% endblock %} diff --git a/example/pythonlib/web/2-todo-flask/todo/templates/task.html b/example/pythonlib/web/2-todo-flask/todo/templates/task.html new file mode 100644 index 00000000000..672696454a9 --- /dev/null +++ b/example/pythonlib/web/2-todo-flask/todo/templates/task.html @@ -0,0 +1,24 @@ +{% extends 'base.html' %} +{% block content %} +

{{ title }}

+
+ {{ form.hidden_tag() }} +
+ {{ form.title.label }}
+ {{ form.title(class="form-control") }} +
+
+ {{ form.description.label }}
+ {{ form.description(class="form-control") }} +
+
+ {{ form.status.label }}
+ {{ form.status(class="form-control") }} +
+
+ {{ form.deadline.label }}
+ {{ form.deadline(class="form-control") }} +
+ +
+{% endblock %} diff --git a/example/pythonlib/web/2-todo-flask/todo/test/src/test.py b/example/pythonlib/web/2-todo-flask/todo/test/src/test.py new file mode 100644 index 00000000000..ab5258cae2a --- /dev/null +++ b/example/pythonlib/web/2-todo-flask/todo/test/src/test.py @@ -0,0 +1 @@ +# Work in Progress... \ No newline at end of file diff --git a/example/pythonlib/web/3-hello-django/build.mill b/example/pythonlib/web/3-hello-django/build.mill new file mode 100644 index 00000000000..fb5158d0f70 --- /dev/null +++ b/example/pythonlib/web/3-hello-django/build.mill @@ -0,0 +1,31 @@ +package build +import mill._, pythonlib._ + +object foo extends PythonModule { + + def mainScript = Task.Source { millSourcePath / "src" / "manage.py" } + + def pythonDeps = Seq("django==5.1.4") + +} + +/** Usage + +> ./mill foo.run test main -v 2 # using inbuilt `django test`, `main` is the app name, `-v 2` is verbosity level 2 +... +System check identified no issues (0 silenced). +test_index_view (main.tests.TestScript...) +Test that the index view returns a 200 status code ... ok +... +Ran 1 test... +OK +... + +> ./mill foo.runBackground runserver + +> curl http://localhost:8000 +...

Hello, Mill!

... + +> ./mill clean foo.runBackground + +*/ diff --git a/example/pythonlib/web/3-hello-django/foo/src/app/__init__.py b/example/pythonlib/web/3-hello-django/foo/src/app/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/example/pythonlib/web/3-hello-django/foo/src/app/asgi.py b/example/pythonlib/web/3-hello-django/foo/src/app/asgi.py new file mode 100644 index 00000000000..df7e978063c --- /dev/null +++ b/example/pythonlib/web/3-hello-django/foo/src/app/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for app 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', 'app.settings') + +application = get_asgi_application() diff --git a/example/pythonlib/web/3-hello-django/foo/src/app/settings.py b/example/pythonlib/web/3-hello-django/foo/src/app/settings.py new file mode 100644 index 00000000000..3c40a0f9649 --- /dev/null +++ b/example/pythonlib/web/3-hello-django/foo/src/app/settings.py @@ -0,0 +1,124 @@ +""" +Django settings for app project. + +Generated by 'django-admin startproject' using Django 5.1.4. + +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-7@1hg!(c00%z)t82=^_mu02sxa$nlex_xc!7j++18z5w4dc(iu' + +# 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', + 'main', +] + +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 = 'app.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 = 'app.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/example/pythonlib/web/3-hello-django/foo/src/app/urls.py b/example/pythonlib/web/3-hello-django/foo/src/app/urls.py new file mode 100644 index 00000000000..879e54d05df --- /dev/null +++ b/example/pythonlib/web/3-hello-django/foo/src/app/urls.py @@ -0,0 +1,24 @@ +""" +URL configuration for app 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 +from main import views + +urlpatterns = [ + path('admin/', admin.site.urls), + path('', views.index, name="homepage") +] diff --git a/example/pythonlib/web/3-hello-django/foo/src/app/wsgi.py b/example/pythonlib/web/3-hello-django/foo/src/app/wsgi.py new file mode 100644 index 00000000000..829fcc707bb --- /dev/null +++ b/example/pythonlib/web/3-hello-django/foo/src/app/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for app 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', 'app.settings') + +application = get_wsgi_application() diff --git a/example/pythonlib/web/3-hello-django/foo/src/main/__init__.py b/example/pythonlib/web/3-hello-django/foo/src/main/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/example/pythonlib/web/3-hello-django/foo/src/main/admin.py b/example/pythonlib/web/3-hello-django/foo/src/main/admin.py new file mode 100644 index 00000000000..8c38f3f3dad --- /dev/null +++ b/example/pythonlib/web/3-hello-django/foo/src/main/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/example/pythonlib/web/3-hello-django/foo/src/main/apps.py b/example/pythonlib/web/3-hello-django/foo/src/main/apps.py new file mode 100644 index 00000000000..167f04426e4 --- /dev/null +++ b/example/pythonlib/web/3-hello-django/foo/src/main/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class MainConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'main' diff --git a/example/pythonlib/web/3-hello-django/foo/src/main/migrations/__init__.py b/example/pythonlib/web/3-hello-django/foo/src/main/migrations/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/example/pythonlib/web/3-hello-django/foo/src/main/models.py b/example/pythonlib/web/3-hello-django/foo/src/main/models.py new file mode 100644 index 00000000000..71a83623907 --- /dev/null +++ b/example/pythonlib/web/3-hello-django/foo/src/main/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/example/pythonlib/web/3-hello-django/foo/src/main/tests.py b/example/pythonlib/web/3-hello-django/foo/src/main/tests.py new file mode 100644 index 00000000000..aa6ec79bd6a --- /dev/null +++ b/example/pythonlib/web/3-hello-django/foo/src/main/tests.py @@ -0,0 +1,12 @@ +from django.test import TestCase +from django.urls import reverse + +class TestScript(TestCase): + def test_index_view(self): + """ + Test that the index view returns a 200 status code + and the expected HTML content. + """ + response = self.client.get(reverse('homepage')) + self.assertEqual(response.status_code, 200) + self.assertContains(response, '

Hello, Mill!

') diff --git a/example/pythonlib/web/3-hello-django/foo/src/main/views.py b/example/pythonlib/web/3-hello-django/foo/src/main/views.py new file mode 100644 index 00000000000..05b8dc7a8f6 --- /dev/null +++ b/example/pythonlib/web/3-hello-django/foo/src/main/views.py @@ -0,0 +1,5 @@ +from django.shortcuts import render +from django.http import HttpResponse + +def index(request): + return HttpResponse('

Hello, Mill!

') \ No newline at end of file diff --git a/example/pythonlib/web/3-hello-django/foo/src/manage.py b/example/pythonlib/web/3-hello-django/foo/src/manage.py new file mode 100755 index 00000000000..49313893cbd --- /dev/null +++ b/example/pythonlib/web/3-hello-django/foo/src/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', 'app.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() From a3ece202328b861983c325d9f5a563c7c58c1cf1 Mon Sep 17 00:00:00 2001 From: himanshumahajan138 Date: Thu, 12 Dec 2024 01:04:32 +0530 Subject: [PATCH 02/10] Added Test Todo-Flask --- .../web/1-hello-flask/foo/src/foo.py | 6 +- .../web/1-hello-flask/foo/test/src/test.py | 12 +- example/pythonlib/web/2-todo-flask/build.mill | 29 ++++- .../integrationTest/src/test_integration.py | 116 ++++++++++++++++++ .../web/2-todo-flask/todo/src/app.py | 14 ++- .../web/2-todo-flask/todo/src/forms.py | 13 +- .../web/2-todo-flask/todo/src/models.py | 5 +- .../2-todo-flask/todo/templates/index.html | 5 + .../web/2-todo-flask/todo/test/src/test.py | 1 - .../todo/unitTest/src/test_unit.py | 23 ++++ .../web/3-hello-django/foo/src/manage.py | 4 +- 11 files changed, 206 insertions(+), 22 deletions(-) create mode 100644 example/pythonlib/web/2-todo-flask/todo/integrationTest/src/test_integration.py delete mode 100644 example/pythonlib/web/2-todo-flask/todo/test/src/test.py create mode 100644 example/pythonlib/web/2-todo-flask/todo/unitTest/src/test_unit.py diff --git a/example/pythonlib/web/1-hello-flask/foo/src/foo.py b/example/pythonlib/web/1-hello-flask/foo/src/foo.py index ca92ec70e52..cc0d2bcc146 100644 --- a/example/pythonlib/web/1-hello-flask/foo/src/foo.py +++ b/example/pythonlib/web/1-hello-flask/foo/src/foo.py @@ -2,9 +2,11 @@ app = Flask(__name__) + @app.route("/") def hello_world(): return "

Hello, Mill!

" -if __name__ == '__main__': - app.run(debug=True) \ No newline at end of file + +if __name__ == "__main__": + app.run(debug=True) diff --git a/example/pythonlib/web/1-hello-flask/foo/test/src/test.py b/example/pythonlib/web/1-hello-flask/foo/test/src/test.py index 982e222e474..eb7a184cb21 100644 --- a/example/pythonlib/web/1-hello-flask/foo/test/src/test.py +++ b/example/pythonlib/web/1-hello-flask/foo/test/src/test.py @@ -1,17 +1,21 @@ import unittest from foo import app # type: ignore + class TestScript(unittest.TestCase): def setUp(self): """Set up the test client before each test.""" self.app = app.test_client() # Initialize the test client - self.app.testing = True # Enable testing mode for better error handling + self.app.testing = True # Enable testing mode for better error handling def test_hello_flask(self): """Test the '/' endpoint.""" - response = self.app.get('/') # Simulate a GET request to the root endpoint + response = self.app.get("/") # Simulate a GET request to the root endpoint self.assertEqual(response.status_code, 200) # Check the HTTP status code - self.assertIn(b"Hello, Mill!", response.data) # Check if the response contains the expected text + self.assertIn( + b"Hello, Mill!", response.data + ) # Check if the response contains the expected text + -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/example/pythonlib/web/2-todo-flask/build.mill b/example/pythonlib/web/2-todo-flask/build.mill index c3523519099..c77b6b542ea 100644 --- a/example/pythonlib/web/2-todo-flask/build.mill +++ b/example/pythonlib/web/2-todo-flask/build.mill @@ -7,17 +7,40 @@ object todo extends PythonModule { def pythonDeps = Seq("flask==3.1.0", "Flask-SQLAlchemy==3.1.1", "Flask-WTF==1.2.2") - object test extends PythonTests with TestModule.Unittest + object unitTest extends PythonTests with TestModule.Unittest + object integrationTest extends PythonTests with TestModule.Unittest } -// TODO: Testing will be added soon... - /** Usage +> ./mill todo.unitTest +... +test_task_creation (test_unit.UnitTest...) ... ok +test_task_status_update (test_unit.UnitTest...) ... ok +... +Ran 2 tests... +OK +... + +> ./mill todo.integrationTest +... +test_add_task (test_integration.IntegrationTest...) +Test adding a task through the /add route. ... ok +test_delete_task (test_integration.IntegrationTest...) ... ok +test_edit_task (test_integration.IntegrationTest...) +Test editing a task through the /edit/ route. ... ok +test_index_empty (test_integration.IntegrationTest...) +Test the index route with no tasks. ... ok +... +Ran 4 tests... +OK +... + > ./mill todo.runBackground > curl http://localhost:5001 +...To-Do App Using Mill Build Tool... > ./mill clean todo.runBackground diff --git a/example/pythonlib/web/2-todo-flask/todo/integrationTest/src/test_integration.py b/example/pythonlib/web/2-todo-flask/todo/integrationTest/src/test_integration.py new file mode 100644 index 00000000000..a37267fdc96 --- /dev/null +++ b/example/pythonlib/web/2-todo-flask/todo/integrationTest/src/test_integration.py @@ -0,0 +1,116 @@ +import unittest +from app import app, db, Task +from datetime import date + +class IntegrationTest(unittest.TestCase): + def setUp(self): + """Set up the test client and initialize the in-memory database.""" + app.config['TESTING'] = True + app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///:memory:" + # Disable CSRF for testing + app.config['WTF_CSRF_ENABLED'] = False + self.client = app.test_client() + with app.app_context(): + db.create_all() + + def tearDown(self): + """Clean up the database after each test.""" + with app.app_context(): + db.session.remove() + db.drop_all() + + def test_index_empty(self): + """Test the index route with no tasks.""" + response = self.client.get("/") + self.assertEqual(response.status_code, 200) + self.assertIn(b"No tasks found", response.data) + + def test_add_task(self): + """Test adding a task through the /add route.""" + + # Simulate a POST request with valid form data + response = self.client.post("/add", data={ + "title": "Test Task", + "description": "This is a test task description.", + "status": "Pending", + "deadline": "2024-12-31" + }, follow_redirects=True) + + # Check if the response is successful + self.assertEqual(response.status_code, 200) + self.assertIn(b"Task added successfully!", response.data) + + # Verify the task was added to the database + with app.app_context(): + task = Task.query.first() # Retrieve the first task in the database + self.assertIsNotNone(task, "Task was not added to the database.") + self.assertEqual(task.title, "Test Task") + self.assertEqual(task.description, "This is a test task description.") + self.assertEqual(task.status, "Pending") + self.assertEqual(str(task.deadline), "2024-12-31") + + def test_edit_task(self): + """Test editing a task through the /edit/ route.""" + + # Prepopulate the database with a task + with app.app_context(): + new_task = Task( + title="Original Task", + description="Original description.", + status="Pending", + deadline=date(2024, 12, 31) + ) + db.session.add(new_task) + db.session.commit() + task_id = new_task.id # Get the ID of the newly added task + + # Simulate a POST request to edit the task + response = self.client.post(f"/edit/{task_id}", data={ + "title": "Updated Task", + "description": "Updated description.", + "status": "Completed", + "deadline": "2025-01-15" + }, follow_redirects=True) + + # Check if the response is successful + self.assertEqual(response.status_code, 200) + self.assertIn(b"Task updated successfully!", response.data) + + # Verify the task was updated in the database + with app.app_context(): + task = db.session.get(Task, task_id) + self.assertIsNotNone(task, "Task not found in the database after edit.") + self.assertEqual(task.title, "Updated Task") + self.assertEqual(task.description, "Updated description.") + self.assertEqual(task.status, "Completed") + self.assertEqual(str(task.deadline), "2025-01-15") + + # Test editing a non-existent task + non_existent_id = 9999 + response = self.client.get(f"/edit/{non_existent_id}", follow_redirects=True) + self.assertEqual(response.status_code, 200) + self.assertIn(b"Task not found.", response.data) + + def test_delete_task(self): + with app.app_context(): # Ensure application context is active + # Add a task to delete + task = Task(title="Test Task") + db.session.add(task) + db.session.commit() + task_id = task.id + + # Test deleting an existing task + response = self.client.get(f"/delete/{task_id}", follow_redirects=True) + self.assertEqual(response.status_code, 200) + self.assertIn(b"Task deleted successfully!", response.data) + self.assertIsNone(db.session.get(Task, task_id)) + + # Test deleting a non-existent task + non_existent_id = 9999 + response = self.client.get(f"/delete/{non_existent_id}", follow_redirects=True) + self.assertEqual(response.status_code, 200) + self.assertIn(b"Task not found.", response.data) + + +if __name__ == "__main__": + unittest.main() diff --git a/example/pythonlib/web/2-todo-flask/todo/src/app.py b/example/pythonlib/web/2-todo-flask/todo/src/app.py index 0cebf1afb94..d6de93cd6a4 100644 --- a/example/pythonlib/web/2-todo-flask/todo/src/app.py +++ b/example/pythonlib/web/2-todo-flask/todo/src/app.py @@ -8,7 +8,9 @@ app = Flask(__name__, static_folder="../static", template_folder="../templates") app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///todo.db" app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False -app.config["SECRET_KEY"] = "your_secret_key" +app.config["SECRET_KEY"] = ( + "8f41b7124eec1c73f2fbe77e6e76c54602a40c44c842da93b09f48b79c023c88" +) # Import models from models import Task, db @@ -45,7 +47,10 @@ def add_task(): @app.route("/edit/", methods=["GET", "POST"]) def edit_task(task_id): - task = Task.query.get_or_404(task_id) + task = db.session.get(Task, task_id) + if not task: # Handle case where task doesn't exist + flash("Task not found.", "error") + return redirect(url_for("index")) form = TaskForm(obj=task) if form.validate_on_submit(): task.title = form.title.data @@ -60,7 +65,10 @@ def edit_task(task_id): @app.route("/delete/") def delete_task(task_id): - task = Task.query.get_or_404(task_id) + task = db.session.get(Task, task_id) + if not task: # Handle case where task doesn't exist + flash("Task not found.", "error") + return redirect(url_for("index")) db.session.delete(task) db.session.commit() flash("Task deleted successfully!", "success") diff --git a/example/pythonlib/web/2-todo-flask/todo/src/forms.py b/example/pythonlib/web/2-todo-flask/todo/src/forms.py index be10adcff0f..e790d73c2fb 100644 --- a/example/pythonlib/web/2-todo-flask/todo/src/forms.py +++ b/example/pythonlib/web/2-todo-flask/todo/src/forms.py @@ -2,9 +2,12 @@ from wtforms import StringField, TextAreaField, SelectField, DateField, SubmitField from wtforms.validators import DataRequired, Length + class TaskForm(FlaskForm): - title = StringField('Title', validators=[DataRequired(), Length(max=100)]) - description = TextAreaField('Description') - status = SelectField('Status', choices=[('Pending', 'Pending'), ('Completed', 'Completed')]) - deadline = DateField('Deadline', format='%Y-%m-%d', validators=[DataRequired()]) - submit = SubmitField('Save') + title = StringField("Title", validators=[DataRequired(), Length(max=100)]) + description = TextAreaField("Description") + status = SelectField( + "Status", choices=[("Pending", "Pending"), ("Completed", "Completed")] + ) + deadline = DateField("Deadline", format="%Y-%m-%d", validators=[DataRequired()]) + submit = SubmitField("Save") diff --git a/example/pythonlib/web/2-todo-flask/todo/src/models.py b/example/pythonlib/web/2-todo-flask/todo/src/models.py index 93bd7c82bf4..22606eae456 100644 --- a/example/pythonlib/web/2-todo-flask/todo/src/models.py +++ b/example/pythonlib/web/2-todo-flask/todo/src/models.py @@ -2,13 +2,14 @@ db = SQLAlchemy() + class Task(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) description = db.Column(db.Text, nullable=True) - status = db.Column(db.String(20), default='Pending') # Options: Pending, Completed + status = db.Column(db.String(20), default="Pending") # Options: Pending, Completed created_at = db.Column(db.DateTime, default=db.func.current_timestamp()) deadline = db.Column(db.Date) def __repr__(self): - return f'' + return f"" diff --git a/example/pythonlib/web/2-todo-flask/todo/templates/index.html b/example/pythonlib/web/2-todo-flask/todo/templates/index.html index 8d1619a4758..bd0b59624b3 100644 --- a/example/pythonlib/web/2-todo-flask/todo/templates/index.html +++ b/example/pythonlib/web/2-todo-flask/todo/templates/index.html @@ -2,6 +2,8 @@ {% block content %}

Task List

Add Task + +{% if tasks %} @@ -27,4 +29,7 @@

Task List

{% endfor %}
+{% else %} +

No tasks found

+{% endif %} {% endblock %} diff --git a/example/pythonlib/web/2-todo-flask/todo/test/src/test.py b/example/pythonlib/web/2-todo-flask/todo/test/src/test.py deleted file mode 100644 index ab5258cae2a..00000000000 --- a/example/pythonlib/web/2-todo-flask/todo/test/src/test.py +++ /dev/null @@ -1 +0,0 @@ -# Work in Progress... \ No newline at end of file diff --git a/example/pythonlib/web/2-todo-flask/todo/unitTest/src/test_unit.py b/example/pythonlib/web/2-todo-flask/todo/unitTest/src/test_unit.py new file mode 100644 index 00000000000..1e4954daacc --- /dev/null +++ b/example/pythonlib/web/2-todo-flask/todo/unitTest/src/test_unit.py @@ -0,0 +1,23 @@ +import unittest +from app import Task # type: ignore + + +class UnitTest(unittest.TestCase): + def test_task_creation(self): + task = Task( + title="Sample Task", description="Task Description", status="Pending" + ) + self.assertEqual(task.title, "Sample Task") + self.assertEqual(task.description, "Task Description") + self.assertEqual(task.status, "Pending") + + def test_task_status_update(self): + task = Task( + title="Sample Task", description="Task Description", status="Pending" + ) + task.status = "Completed" + self.assertEqual(task.status, "Completed") + + +if __name__ == "__main__": + unittest.main() diff --git a/example/pythonlib/web/3-hello-django/foo/src/manage.py b/example/pythonlib/web/3-hello-django/foo/src/manage.py index 49313893cbd..1a64b14a0e1 100755 --- a/example/pythonlib/web/3-hello-django/foo/src/manage.py +++ b/example/pythonlib/web/3-hello-django/foo/src/manage.py @@ -6,7 +6,7 @@ def main(): """Run administrative tasks.""" - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings') + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings") try: from django.core.management import execute_from_command_line except ImportError as exc: @@ -18,5 +18,5 @@ def main(): execute_from_command_line(sys.argv) -if __name__ == '__main__': +if __name__ == "__main__": main() From 61d3fd5c517e92eae5030754468ee45415411f72 Mon Sep 17 00:00:00 2001 From: himanshumahajan138 Date: Fri, 13 Dec 2024 01:19:59 +0530 Subject: [PATCH 03/10] Final Code Updated --- example/pythonlib/web/2-todo-flask/build.mill | 2 +- .../web/2-todo-flask/todo/templates/base.html | 2 +- .../pythonlib/web/3-hello-django/build.mill | 2 +- .../pythonlib/web/4-todo-django/build.mill | 52 ++++++ .../4-todo-django/todo/src/app/__init__.py | 0 .../web/4-todo-django/todo/src/app/asgi.py | 16 ++ .../4-todo-django/todo/src/app/settings.py | 124 ++++++++++++++ .../web/4-todo-django/todo/src/app/urls.py | 24 +++ .../web/4-todo-django/todo/src/app/wsgi.py | 16 ++ .../4-todo-django/todo/src/main/__init__.py | 0 .../web/4-todo-django/todo/src/main/admin.py | 3 + .../web/4-todo-django/todo/src/main/apps.py | 6 + .../web/4-todo-django/todo/src/main/forms.py | 11 ++ .../todo/src/main/migrations/__init__.py | 0 .../web/4-todo-django/todo/src/main/models.py | 17 ++ .../todo/src/main/static/styles.css | 21 +++ .../todo/src/main/templates/base.html | 30 ++++ .../todo/src/main/templates/index.html | 45 +++++ .../todo/src/main/templates/task.html | 82 +++++++++ .../web/4-todo-django/todo/src/main/tests.py | 156 ++++++++++++++++++ .../web/4-todo-django/todo/src/main/urls.py | 9 + .../web/4-todo-django/todo/src/main/views.py | 45 +++++ .../web/4-todo-django/todo/src/manage.py | 22 +++ 23 files changed, 682 insertions(+), 3 deletions(-) create mode 100644 example/pythonlib/web/4-todo-django/build.mill create mode 100644 example/pythonlib/web/4-todo-django/todo/src/app/__init__.py create mode 100644 example/pythonlib/web/4-todo-django/todo/src/app/asgi.py create mode 100644 example/pythonlib/web/4-todo-django/todo/src/app/settings.py create mode 100644 example/pythonlib/web/4-todo-django/todo/src/app/urls.py create mode 100644 example/pythonlib/web/4-todo-django/todo/src/app/wsgi.py create mode 100644 example/pythonlib/web/4-todo-django/todo/src/main/__init__.py create mode 100644 example/pythonlib/web/4-todo-django/todo/src/main/admin.py create mode 100644 example/pythonlib/web/4-todo-django/todo/src/main/apps.py create mode 100644 example/pythonlib/web/4-todo-django/todo/src/main/forms.py create mode 100644 example/pythonlib/web/4-todo-django/todo/src/main/migrations/__init__.py create mode 100644 example/pythonlib/web/4-todo-django/todo/src/main/models.py create mode 100644 example/pythonlib/web/4-todo-django/todo/src/main/static/styles.css create mode 100644 example/pythonlib/web/4-todo-django/todo/src/main/templates/base.html create mode 100644 example/pythonlib/web/4-todo-django/todo/src/main/templates/index.html create mode 100644 example/pythonlib/web/4-todo-django/todo/src/main/templates/task.html create mode 100644 example/pythonlib/web/4-todo-django/todo/src/main/tests.py create mode 100644 example/pythonlib/web/4-todo-django/todo/src/main/urls.py create mode 100644 example/pythonlib/web/4-todo-django/todo/src/main/views.py create mode 100755 example/pythonlib/web/4-todo-django/todo/src/manage.py diff --git a/example/pythonlib/web/2-todo-flask/build.mill b/example/pythonlib/web/2-todo-flask/build.mill index c77b6b542ea..fd184ad3df0 100644 --- a/example/pythonlib/web/2-todo-flask/build.mill +++ b/example/pythonlib/web/2-todo-flask/build.mill @@ -40,7 +40,7 @@ OK > ./mill todo.runBackground > curl http://localhost:5001 -...To-Do App Using Mill Build Tool... +...To-Do Flask App Using Mill Build Tool... > ./mill clean todo.runBackground diff --git a/example/pythonlib/web/2-todo-flask/todo/templates/base.html b/example/pythonlib/web/2-todo-flask/todo/templates/base.html index 7beef670609..f0ef14f688d 100644 --- a/example/pythonlib/web/2-todo-flask/todo/templates/base.html +++ b/example/pythonlib/web/2-todo-flask/todo/templates/base.html @@ -10,7 +10,7 @@
diff --git a/example/pythonlib/web/3-hello-django/build.mill b/example/pythonlib/web/3-hello-django/build.mill index fb5158d0f70..d4f9fd5d433 100644 --- a/example/pythonlib/web/3-hello-django/build.mill +++ b/example/pythonlib/web/3-hello-django/build.mill @@ -21,7 +21,7 @@ Ran 1 test... OK ... -> ./mill foo.runBackground runserver +> ./mill foo.runBackground runserver 8000 > curl http://localhost:8000 ...

Hello, Mill!

... diff --git a/example/pythonlib/web/4-todo-django/build.mill b/example/pythonlib/web/4-todo-django/build.mill new file mode 100644 index 00000000000..9e9e993e657 --- /dev/null +++ b/example/pythonlib/web/4-todo-django/build.mill @@ -0,0 +1,52 @@ +package build +import mill._, pythonlib._ + +object todo extends PythonModule { + + def mainScript = Task.Source { millSourcePath / "src" / "manage.py" } + + def pythonDeps = Seq("django==5.1.4") + +} + +/** Usage + +> ./mill todo.run makemigrations + +> ./mill todo.run migrate + +> ./mill todo.run test main -v 2 # using inbuilt `django test`, `main` is the app name, `-v 2` is verbosity level 2 +... +test_add_task_message (main.tests.TestScript...) +Test that a success message appears when a task is successfully added. ... ok +test_add_task_view_get (main.tests.TestScript...) +Test that the 'Add Task' page loads successfully with an empty form. ... ok +test_add_task_view_post_invalid_data (main.tests.TestScript...) +Test that submitting the form with invalid data shows errors. ... ok +test_add_task_view_post_valid_data (main.tests.TestScript...) +Test that submitting the form with valid data creates a new task. ... ok +test_default_status (main.tests.TestScript...) +Test that the default status of a task is 'Pending'. ... ok +test_delete_task_message (main.tests.TestScript...) +Test that a success message appears when a task is deleted. ... ok +test_delete_task_view (main.tests.TestScript...) +Test that deleting a task works and redirects to the task list. ... ok +test_edit_task_view_post_valid_data (main.tests.TestScript...) +Test that editing a task updates its details. ... ok +test_str_method (main.tests.TestScript...) +Test the __str__ method of the Task model. ... ok +test_url_resolves_to_correct_view (main.tests.TestScript...) +Test that the correct view function is mapped to each URL. ... ok +... +...Ran 10 tests... +OK +... + +> ./mill todo.runBackground runserver 8001 + +> curl http://localhost:8001 +...To-Do Django App Using Mill Build Tool... + +> ./mill clean todo.runBackground + +*/ diff --git a/example/pythonlib/web/4-todo-django/todo/src/app/__init__.py b/example/pythonlib/web/4-todo-django/todo/src/app/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/example/pythonlib/web/4-todo-django/todo/src/app/asgi.py b/example/pythonlib/web/4-todo-django/todo/src/app/asgi.py new file mode 100644 index 00000000000..0c9af490b03 --- /dev/null +++ b/example/pythonlib/web/4-todo-django/todo/src/app/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for app 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", "app.settings") + +application = get_asgi_application() diff --git a/example/pythonlib/web/4-todo-django/todo/src/app/settings.py b/example/pythonlib/web/4-todo-django/todo/src/app/settings.py new file mode 100644 index 00000000000..02216e12238 --- /dev/null +++ b/example/pythonlib/web/4-todo-django/todo/src/app/settings.py @@ -0,0 +1,124 @@ +""" +Django settings for app project. + +Generated by 'django-admin startproject' using Django 5.1.4. + +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-on9yu8ulr@rvkw&hv(hfi35j!z#ij0ljbzmz#469*@5$25e@(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", + "main", +] + +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 = "app.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 = "app.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/example/pythonlib/web/4-todo-django/todo/src/app/urls.py b/example/pythonlib/web/4-todo-django/todo/src/app/urls.py new file mode 100644 index 00000000000..463b2df9c25 --- /dev/null +++ b/example/pythonlib/web/4-todo-django/todo/src/app/urls.py @@ -0,0 +1,24 @@ +""" +URL configuration for app 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), + path("", include("main.urls")), +] diff --git a/example/pythonlib/web/4-todo-django/todo/src/app/wsgi.py b/example/pythonlib/web/4-todo-django/todo/src/app/wsgi.py new file mode 100644 index 00000000000..3cba99e8cde --- /dev/null +++ b/example/pythonlib/web/4-todo-django/todo/src/app/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for app 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", "app.settings") + +application = get_wsgi_application() diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/__init__.py b/example/pythonlib/web/4-todo-django/todo/src/main/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/admin.py b/example/pythonlib/web/4-todo-django/todo/src/main/admin.py new file mode 100644 index 00000000000..8c38f3f3dad --- /dev/null +++ b/example/pythonlib/web/4-todo-django/todo/src/main/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/apps.py b/example/pythonlib/web/4-todo-django/todo/src/main/apps.py new file mode 100644 index 00000000000..34f1451f2e2 --- /dev/null +++ b/example/pythonlib/web/4-todo-django/todo/src/main/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class MainConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "main" diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/forms.py b/example/pythonlib/web/4-todo-django/todo/src/main/forms.py new file mode 100644 index 00000000000..dcd31c6f466 --- /dev/null +++ b/example/pythonlib/web/4-todo-django/todo/src/main/forms.py @@ -0,0 +1,11 @@ +from django import forms +from .models import Task + + +class TaskForm(forms.ModelForm): + class Meta: + model = Task + fields = ["title", "description", "status", "deadline"] + widgets = { + "deadline": forms.DateInput(attrs={"type": "date"}), + } diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/migrations/__init__.py b/example/pythonlib/web/4-todo-django/todo/src/main/migrations/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/models.py b/example/pythonlib/web/4-todo-django/todo/src/main/models.py new file mode 100644 index 00000000000..bd652e5c8c4 --- /dev/null +++ b/example/pythonlib/web/4-todo-django/todo/src/main/models.py @@ -0,0 +1,17 @@ +from django.db import models + + +class Task(models.Model): + STATUS_CHOICES = [ + ("Pending", "Pending"), + ("In Progress", "In Progress"), + ("Completed", "Completed"), + ] + + title = models.CharField(max_length=100) + description = models.TextField(blank=True, null=True) + status = models.CharField(max_length=20, choices=STATUS_CHOICES, default="Pending") + deadline = models.DateField(null=True, blank=True) + + def __str__(self): + return self.title diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/static/styles.css b/example/pythonlib/web/4-todo-django/todo/src/main/static/styles.css new file mode 100644 index 00000000000..54f07a91f48 --- /dev/null +++ b/example/pythonlib/web/4-todo-django/todo/src/main/static/styles.css @@ -0,0 +1,21 @@ +body { + background-color: #f8f9fa; +} + +.navbar-brand { + font-weight: bold; +} + +.table th, +.table td { + text-align: center; + vertical-align: middle; +} + +.btn { + margin-right: 5px; +} + +.container { + max-width: 900px; +} diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/templates/base.html b/example/pythonlib/web/4-todo-django/todo/src/main/templates/base.html new file mode 100644 index 00000000000..4d6aad45c84 --- /dev/null +++ b/example/pythonlib/web/4-todo-django/todo/src/main/templates/base.html @@ -0,0 +1,30 @@ + + + + {% load static %} + + + Django To-Do App + + + + + +
+ {% if messages %} + {% for message in messages %} + + {% endfor %} + {% endif %} + {% block content %}{% endblock %} +
+ + + diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/templates/index.html b/example/pythonlib/web/4-todo-django/todo/src/main/templates/index.html new file mode 100644 index 00000000000..ab56f014146 --- /dev/null +++ b/example/pythonlib/web/4-todo-django/todo/src/main/templates/index.html @@ -0,0 +1,45 @@ +{% extends 'base.html' %} + +{% block content %} +
+

Task List

+
+ Total Tasks: {{ tasks|length }} + Add Task +
+ + {% if tasks %} + + + + + + + + + + + + {% for task in tasks %} + + + + + + + + {% endfor %} + +
#TitleStatusDeadlineActions
{{ forloop.counter }}{{ task.title }} + + {{ task.status }} + + {{ task.deadline|date:"M d, Y" }} + Edit + Delete +
+ {% else %} +

No tasks found. Start by adding a new task!

+ {% endif %} +
+{% endblock %} diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/templates/task.html b/example/pythonlib/web/4-todo-django/todo/src/main/templates/task.html new file mode 100644 index 00000000000..cff6cb8fd38 --- /dev/null +++ b/example/pythonlib/web/4-todo-django/todo/src/main/templates/task.html @@ -0,0 +1,82 @@ +{% extends 'base.html' %} + +{% block content %} +
+

{{ title }}

+
+ {% csrf_token %} + {{ form.hidden_tag }} + + +
+ + + + {% if form.title.errors %} +
+ {% for error in form.title.errors %} +

{{ error }}

+ {% endfor %} +
+ {% endif %} +
+ + +
+ + + + {% if form.description.errors %} +
+ {% for error in form.description.errors %} +

{{ error }}

+ {% endfor %} +
+ {% endif %} +
+ + +
+ + + + {% if form.status.errors %} +
+ {% for error in form.status.errors %} +

{{ error }}

+ {% endfor %} +
+ {% endif %} +
+ + +
+ + + + {% if form.deadline.errors %} +
+ {% for error in form.deadline.errors %} +

{{ error }}

+ {% endfor %} +
+ {% endif %} +
+ +
+ +
+
+
+{% endblock %} diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/tests.py b/example/pythonlib/web/4-todo-django/todo/src/main/tests.py new file mode 100644 index 00000000000..628d5be1f72 --- /dev/null +++ b/example/pythonlib/web/4-todo-django/todo/src/main/tests.py @@ -0,0 +1,156 @@ +from django.test import TestCase +from django.urls import reverse, resolve +from django.contrib.messages import get_messages +from .models import Task +from .views import index, add_task, edit_task, delete_task + + +class TestScript(TestCase): + """ + Unit and Integration tests. + """ + + def test_str_method(self): + """ + Test the __str__ method of the Task model. + """ + task = Task.objects.create( + title="Buy groceries", description="Get milk, eggs, and bread" + ) + self.assertEqual(str(task), "Buy groceries") + + def test_default_status(self): + """ + Test that the default status of a task is 'Pending'. + """ + task = Task.objects.create(title="Finish project") + self.assertEqual(task.status, "Pending") + + def test_add_task_view_get(self): + """ + Test that the 'Add Task' page loads successfully with an empty form. + """ + url = reverse("add_task") + response = self.client.get(url) + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, "task.html") + self.assertContains(response, "Add Task") + + def test_add_task_view_post_valid_data(self): + """ + Test that submitting the form with valid data creates a new task. + """ + url = reverse("add_task") + data = { + "title": "Test Task", + "description": "This is a test task description.", + "status": "Pending", + "deadline": "2024-12-31", + } + response = self.client.post(url, data) + + # Check that the task was added and the user is redirected + self.assertEqual(response.status_code, 302) + self.assertTrue(Task.objects.filter(title="Test Task").exists()) + + def test_add_task_view_post_invalid_data(self): + """ + Test that submitting the form with invalid data shows errors. + """ + url = reverse("add_task") + data = { + "title": "", + "description": "No title task", + "status": "Pending", + "deadline": "2024-12-31", + } # Invalid title + response = self.client.post(url, data) + messages = list(get_messages(response.wsgi_request)) + + # Ensure that the form is not saved and the page reloads with errors + self.assertEqual(response.status_code, 200) + self.assertEqual(str(messages[0]), "Please Add Required Fields!") + + def test_edit_task_view_post_valid_data(self): + """ + Test that editing a task updates its details. + """ + task = Task.objects.create(title="Old Task", description="Old description") + url = reverse("edit_task", args=[task.id]) + data = { + "title": "Updated Task", + "description": "Updated description", + "status": "Completed", + "deadline": "2024-12-31", + } + + response = self.client.post(url, data) + + # Verify that the task was updated and the user is redirected + self.assertEqual(response.status_code, 302) + task.refresh_from_db() + self.assertEqual(task.title, "Updated Task") + self.assertEqual(task.description, "Updated description") + + def test_delete_task_view(self): + """ + Test that deleting a task works and redirects to the task list. + """ + task = Task.objects.create( + title="Task to delete", description="This task will be deleted" + ) + url = reverse("delete_task", args=[task.id]) + + response = self.client.post(url) + + # Ensure the task is deleted and the user is redirected + self.assertEqual(response.status_code, 302) + self.assertFalse(Task.objects.filter(title="Task to delete").exists()) + + def test_url_resolves_to_correct_view(self): + """ + Test that the correct view function is mapped to each URL. + """ + # Mapping URLs to their respective views + url_resolutions = { + reverse("index"): index, + reverse("add_task"): add_task, + reverse("edit_task", args=[1]): edit_task, + reverse("delete_task", args=[1]): delete_task, + } + + for url, view in url_resolutions.items(): + with self.subTest(url=url): + resolved = resolve(url) + self.assertEqual(resolved.func, view) + + def test_add_task_message(self): + """ + Test that a success message appears when a task is successfully added. + """ + url = reverse("add_task") + data = { + "title": "Complete Assignment", + "description": "Finish Django tutorial", + "status": "Pending", + "deadline": "2024-12-31", + } + response = self.client.post(url, data) + + # Check if the success message appears in the response + messages = list(get_messages(response.wsgi_request)) + self.assertEqual(str(messages[0]), "Task added successfully!") + + def test_delete_task_message(self): + """ + Test that a success message appears when a task is deleted. + """ + task = Task.objects.create( + title="Task to delete", description="Delete this task" + ) + url = reverse("delete_task", args=[task.id]) + response = self.client.post(url) + + # Check if the success message appears + messages = list(get_messages(response.wsgi_request)) + self.assertEqual(str(messages[0]), "Task deleted successfully!") diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/urls.py b/example/pythonlib/web/4-todo-django/todo/src/main/urls.py new file mode 100644 index 00000000000..d0e2256c95d --- /dev/null +++ b/example/pythonlib/web/4-todo-django/todo/src/main/urls.py @@ -0,0 +1,9 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path("", views.index, name="index"), + path("add/", views.add_task, name="add_task"), + path("edit//", views.edit_task, name="edit_task"), + path("delete//", views.delete_task, name="delete_task"), +] diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/views.py b/example/pythonlib/web/4-todo-django/todo/src/main/views.py new file mode 100644 index 00000000000..ba1b4e4bdca --- /dev/null +++ b/example/pythonlib/web/4-todo-django/todo/src/main/views.py @@ -0,0 +1,45 @@ +from django.shortcuts import render, redirect, get_object_or_404 +from django.contrib import messages +from .models import Task +from .forms import TaskForm + + +def index(request): + tasks = Task.objects.all() + return render(request, "index.html", {"tasks": tasks}) + + +def add_task(request): + if request.method == "POST": + form = TaskForm(request.POST) + if form.is_valid(): + form.save() + messages.success(request, "Task added successfully!") + return redirect("index") + else: + # If the form is not valid, pass the form back to the template with errors + messages.warning(request, "Please Add Required Fields!") + return render(request, "task.html", {"form": form, "title": "Add Task"}) + else: + form = TaskForm() + return render(request, "task.html", {"form": form, "title": "Add Task"}) + + +def edit_task(request, task_id): + task = get_object_or_404(Task, id=task_id) + if request.method == "POST": + form = TaskForm(request.POST, instance=task) + if form.is_valid(): + form.save() + messages.success(request, "Task updated successfully!") + return redirect("index") + else: + form = TaskForm(instance=task) + return render(request, "task.html", {"form": form, "title": "Edit Task"}) + + +def delete_task(request, task_id): + task = get_object_or_404(Task, id=task_id) + task.delete() + messages.success(request, "Task deleted successfully!") + return redirect("index") diff --git a/example/pythonlib/web/4-todo-django/todo/src/manage.py b/example/pythonlib/web/4-todo-django/todo/src/manage.py new file mode 100755 index 00000000000..1a64b14a0e1 --- /dev/null +++ b/example/pythonlib/web/4-todo-django/todo/src/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", "app.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() From 355523359cf0089e5b2ac486ae878ed90d37be53 Mon Sep 17 00:00:00 2001 From: himanshumahajan138 Date: Sun, 15 Dec 2024 19:06:31 +0530 Subject: [PATCH 04/10] testing --- example/pythonlib/web/4-todo-django/build.mill | 5 +++++ example/pythonlib/web/4-todo-django/todo/src/app/settings.py | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/example/pythonlib/web/4-todo-django/build.mill b/example/pythonlib/web/4-todo-django/build.mill index 9e9e993e657..fe7700eb1f7 100644 --- a/example/pythonlib/web/4-todo-django/build.mill +++ b/example/pythonlib/web/4-todo-django/build.mill @@ -12,8 +12,13 @@ object todo extends PythonModule { /** Usage > ./mill todo.run makemigrations +...Migrations for 'main'... +...Create model Task... > ./mill todo.run migrate +...Apply all migrations: admin, auth, contenttypes, main, sessions... +...Running migrations... +...OK... > ./mill todo.run test main -v 2 # using inbuilt `django test`, `main` is the app name, `-v 2` is verbosity level 2 ... diff --git a/example/pythonlib/web/4-todo-django/todo/src/app/settings.py b/example/pythonlib/web/4-todo-django/todo/src/app/settings.py index 02216e12238..45191a77e56 100644 --- a/example/pythonlib/web/4-todo-django/todo/src/app/settings.py +++ b/example/pythonlib/web/4-todo-django/todo/src/app/settings.py @@ -78,6 +78,10 @@ "default": { "ENGINE": "django.db.backends.sqlite3", "NAME": BASE_DIR / "db.sqlite3", + }, + 'TEST': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': ':memory:', } } From 14e70577ccbfe24a511b069f0f2241fbb0b6c7df Mon Sep 17 00:00:00 2001 From: himanshumahajan138 Date: Mon, 16 Dec 2024 21:20:53 +0530 Subject: [PATCH 05/10] Final Changes --- docs/modules/ROOT/nav.adoc | 1 + .../ROOT/pages/pythonlib/web-examples.adoc | 25 +++++++++++++++++++ .../pythonlib/web/1-hello-flask/build.mill | 5 ++++ .../web/1-hello-flask/foo/test/src/test.py | 2 +- example/pythonlib/web/2-todo-flask/build.mill | 16 ++++++++++++ .../integrationTest/src/test_integration.py | 4 +-- .../pythonlib/web/3-hello-django/build.mill | 5 ++++ .../pythonlib/web/4-todo-django/build.mill | 18 ++++++++++++- 8 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 docs/modules/ROOT/pages/pythonlib/web-examples.adoc diff --git a/docs/modules/ROOT/nav.adoc b/docs/modules/ROOT/nav.adoc index 8d848c59e04..61d65edf34b 100644 --- a/docs/modules/ROOT/nav.adoc +++ b/docs/modules/ROOT/nav.adoc @@ -35,6 +35,7 @@ *** xref:pythonlib/module-config.adoc[] *** xref:pythonlib/dependencies.adoc[] *** xref:pythonlib/publishing.adoc[] +*** xref:pythonlib/web-examples.adoc[] * xref:comparisons/why-mill.adoc[] ** xref:comparisons/maven.adoc[] ** xref:comparisons/gradle.adoc[] diff --git a/docs/modules/ROOT/pages/pythonlib/web-examples.adoc b/docs/modules/ROOT/pages/pythonlib/web-examples.adoc new file mode 100644 index 00000000000..e24e5034234 --- /dev/null +++ b/docs/modules/ROOT/pages/pythonlib/web-examples.adoc @@ -0,0 +1,25 @@ += Python Web Project Examples +:page-aliases: Python_Web_Examples.adoc + +include::partial$gtag-config.adoc[] + +This page provides examples of using Mill as a build tool for Python web applications. +It includes setting up a basic "Hello, World!" application and developing a fully +functional Todo-MVC app with Flask and Django, showcasing best practices +for project organization, scalability, and maintainability. + +== Flask Hello World App + +include::partial$example/pythonlib/web/1-hello-flask.adoc[] + +== Flask TodoMvc App + +include::partial$example/pythonlib/web/2-todo-flask.adoc[] + +== Django Hello World App + +include::partial$example/pythonlib/web/3-hello-django.adoc[] + +== Django TodoMvc App + +include::partial$example/pythonlib/web/4-todo-django.adoc[] \ No newline at end of file diff --git a/example/pythonlib/web/1-hello-flask/build.mill b/example/pythonlib/web/1-hello-flask/build.mill index 2fc70becb84..0626f1f5a0e 100644 --- a/example/pythonlib/web/1-hello-flask/build.mill +++ b/example/pythonlib/web/1-hello-flask/build.mill @@ -1,3 +1,6 @@ +// This example uses Mill to manage a Flask app that serves "Hello, Mill!" +// at the root URL (`/`), with Flask installed as a dependency +// and tests enabled using `unittest`. package build import mill._, pythonlib._ @@ -11,6 +14,8 @@ object foo extends PythonModule { } +// Running these commands will test and run the Flask server with desired outputs. + /** Usage > ./mill foo.test diff --git a/example/pythonlib/web/1-hello-flask/foo/test/src/test.py b/example/pythonlib/web/1-hello-flask/foo/test/src/test.py index eb7a184cb21..cb7ab80464f 100644 --- a/example/pythonlib/web/1-hello-flask/foo/test/src/test.py +++ b/example/pythonlib/web/1-hello-flask/foo/test/src/test.py @@ -3,7 +3,7 @@ class TestScript(unittest.TestCase): - def setUp(self): + def set_up(self): """Set up the test client before each test.""" self.app = app.test_client() # Initialize the test client self.app.testing = True # Enable testing mode for better error handling diff --git a/example/pythonlib/web/2-todo-flask/build.mill b/example/pythonlib/web/2-todo-flask/build.mill index fd184ad3df0..9ea442c1691 100644 --- a/example/pythonlib/web/2-todo-flask/build.mill +++ b/example/pythonlib/web/2-todo-flask/build.mill @@ -1,3 +1,7 @@ +// This is a `Flask`-based `Todo` application managed and build using `Mill`. +// It allows users to `add`, `edit`, `delete`, and `view` tasks stored in a `SQLite` database. +// `Flask-SQLAlchemy` handles database operations, while `Flask-WTF` manages forms +// for task creation and updates. package build import mill._, pythonlib._ @@ -12,6 +16,18 @@ object todo extends PythonModule { } +// Apart from running a web server, this example demonstrates: + +// - **Serving HTML templates** using **Jinja2** (Flask's default templating engine). +// - **Managing static files** such as JavaScript, CSS, and images. +// - **Querying a SQL database** using **Flask-SQLAlchemy** with an **SQLite** backend. +// - **Form handling and validation** with **Flask-WTF**. +// - **Unit testing** using **unittest** with SQLite in-memory database. +// - **Integration testing** using **unittest**. + +// This example also utilizes **Mill** for managing `dependencies`, `builds`, and `tests`, +// offering an efficient development workflow. + /** Usage > ./mill todo.unitTest diff --git a/example/pythonlib/web/2-todo-flask/todo/integrationTest/src/test_integration.py b/example/pythonlib/web/2-todo-flask/todo/integrationTest/src/test_integration.py index a37267fdc96..9147db52832 100644 --- a/example/pythonlib/web/2-todo-flask/todo/integrationTest/src/test_integration.py +++ b/example/pythonlib/web/2-todo-flask/todo/integrationTest/src/test_integration.py @@ -3,7 +3,7 @@ from datetime import date class IntegrationTest(unittest.TestCase): - def setUp(self): + def set_up(self): """Set up the test client and initialize the in-memory database.""" app.config['TESTING'] = True app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///:memory:" @@ -13,7 +13,7 @@ def setUp(self): with app.app_context(): db.create_all() - def tearDown(self): + def tear_down(self): """Clean up the database after each test.""" with app.app_context(): db.session.remove() diff --git a/example/pythonlib/web/3-hello-django/build.mill b/example/pythonlib/web/3-hello-django/build.mill index d4f9fd5d433..e76366855fa 100644 --- a/example/pythonlib/web/3-hello-django/build.mill +++ b/example/pythonlib/web/3-hello-django/build.mill @@ -1,3 +1,6 @@ +// This example demonstrates a minimal `Django` application managed with Mill. +// It features a simple view that returns `

Hello, Mill!

` and +// includes `Django's` core functionality for `testing` and `running` a development server. package build import mill._, pythonlib._ @@ -8,6 +11,8 @@ object foo extends PythonModule { def pythonDeps = Seq("django==5.1.4") } +// Using `Mill`, we can easily manage dependencies, run `Django tests`, and `launch the server`. +// With just a few commands, the app is ready to serve at `http://localhost:8000`. /** Usage diff --git a/example/pythonlib/web/4-todo-django/build.mill b/example/pythonlib/web/4-todo-django/build.mill index fe7700eb1f7..9f51c561321 100644 --- a/example/pythonlib/web/4-todo-django/build.mill +++ b/example/pythonlib/web/4-todo-django/build.mill @@ -1,3 +1,6 @@ +// This `Django TodoMVC` example is a task management application built using `Mill` Build Tool. +// It features dynamic `HTML` rendering with `Django's` template engine, `CRUD` operations with +// Django `ORM`, and efficient form handling for managing tasks. package build import mill._, pythonlib._ @@ -9,6 +12,19 @@ object todo extends PythonModule { } +// Apart from running a `web server`, this example demonstrates: + +// - **Serving HTML templates** using **Django's template engine**. +// - **Managing static files** such as JavaScript, CSS, and images. +// - **Querying a SQL database** using **Django ORM** with an **SQLite** backend. +// - **Form handling and validation** with **Django forms**. +// - **Unit testing** using Django's inbuilt testing framework with SQLite in-memory database. +// - **URL routing** and **views** for CRUD operations. + +// This Mill Build File simplifies `dependency management`, `database migrations`, and `testing workflows`. + +// Running these commands will `test` and `run` the `Django Todo App`. + /** Usage > ./mill todo.run makemigrations @@ -20,7 +36,7 @@ object todo extends PythonModule { ...Running migrations... ...OK... -> ./mill todo.run test main -v 2 # using inbuilt `django test`, `main` is the app name, `-v 2` is verbosity level 2 +> ./mill todo.run test main -v 2 ... test_add_task_message (main.tests.TestScript...) Test that a success message appears when a task is successfully added. ... ok From 4ad263a921eb111798778e2273f613ce466eb4ef Mon Sep 17 00:00:00 2001 From: himanshumahajan138 Date: Mon, 16 Dec 2024 22:08:02 +0530 Subject: [PATCH 06/10] Fixtures --- example/pythonlib/web/1-hello-flask/foo/test/src/test.py | 2 +- .../2-todo-flask/todo/integrationTest/src/test_integration.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/example/pythonlib/web/1-hello-flask/foo/test/src/test.py b/example/pythonlib/web/1-hello-flask/foo/test/src/test.py index cb7ab80464f..eb7a184cb21 100644 --- a/example/pythonlib/web/1-hello-flask/foo/test/src/test.py +++ b/example/pythonlib/web/1-hello-flask/foo/test/src/test.py @@ -3,7 +3,7 @@ class TestScript(unittest.TestCase): - def set_up(self): + def setUp(self): """Set up the test client before each test.""" self.app = app.test_client() # Initialize the test client self.app.testing = True # Enable testing mode for better error handling diff --git a/example/pythonlib/web/2-todo-flask/todo/integrationTest/src/test_integration.py b/example/pythonlib/web/2-todo-flask/todo/integrationTest/src/test_integration.py index 9147db52832..a37267fdc96 100644 --- a/example/pythonlib/web/2-todo-flask/todo/integrationTest/src/test_integration.py +++ b/example/pythonlib/web/2-todo-flask/todo/integrationTest/src/test_integration.py @@ -3,7 +3,7 @@ from datetime import date class IntegrationTest(unittest.TestCase): - def set_up(self): + def setUp(self): """Set up the test client and initialize the in-memory database.""" app.config['TESTING'] = True app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///:memory:" @@ -13,7 +13,7 @@ def set_up(self): with app.app_context(): db.create_all() - def tear_down(self): + def tearDown(self): """Clean up the database after each test.""" with app.app_context(): db.session.remove() From fb606ebc0024a97cc8c5ba8abb5c83d5a8313190 Mon Sep 17 00:00:00 2001 From: himanshumahajan138 Date: Sun, 22 Dec 2024 00:52:57 +0530 Subject: [PATCH 07/10] Flask Updation --- example/pythonlib/web/2-todo-flask/build.mill | 51 ++- .../integrationTest/src/test_integration.py | 116 ------ .../web/2-todo-flask/todo/itest/src/test.py | 68 ++++ .../web/2-todo-flask/todo/src/app.py | 158 ++++---- .../web/2-todo-flask/todo/src/forms.py | 13 - .../web/2-todo-flask/todo/src/models.py | 15 - .../web/2-todo-flask/todo/static/main.js | 112 ++++++ .../web/2-todo-flask/todo/static/style.css | 21 - .../web/2-todo-flask/todo/static/styles.css | 369 ++++++++++++++++++ .../web/2-todo-flask/todo/templates/base.html | 38 +- .../2-todo-flask/todo/templates/index.html | 64 ++- .../web/2-todo-flask/todo/templates/task.html | 24 -- .../web/2-todo-flask/todo/test/src/test.py | 64 +++ .../todo/unitTest/src/test_unit.py | 23 -- 14 files changed, 769 insertions(+), 367 deletions(-) delete mode 100644 example/pythonlib/web/2-todo-flask/todo/integrationTest/src/test_integration.py create mode 100644 example/pythonlib/web/2-todo-flask/todo/itest/src/test.py delete mode 100644 example/pythonlib/web/2-todo-flask/todo/src/forms.py delete mode 100644 example/pythonlib/web/2-todo-flask/todo/src/models.py create mode 100644 example/pythonlib/web/2-todo-flask/todo/static/main.js delete mode 100644 example/pythonlib/web/2-todo-flask/todo/static/style.css create mode 100644 example/pythonlib/web/2-todo-flask/todo/static/styles.css delete mode 100644 example/pythonlib/web/2-todo-flask/todo/templates/task.html create mode 100644 example/pythonlib/web/2-todo-flask/todo/test/src/test.py delete mode 100644 example/pythonlib/web/2-todo-flask/todo/unitTest/src/test_unit.py diff --git a/example/pythonlib/web/2-todo-flask/build.mill b/example/pythonlib/web/2-todo-flask/build.mill index 9ea442c1691..05b50888ecc 100644 --- a/example/pythonlib/web/2-todo-flask/build.mill +++ b/example/pythonlib/web/2-todo-flask/build.mill @@ -1,7 +1,7 @@ -// This is a `Flask`-based `Todo` application managed and build using `Mill`. -// It allows users to `add`, `edit`, `delete`, and `view` tasks stored in a `SQLite` database. -// `Flask-SQLAlchemy` handles database operations, while `Flask-WTF` manages forms -// for task creation and updates. +// This is a `Flask`-based `TodoMVC` application managed and built using `Mill`. +// It allows users to `add`, `edit`, `delete`, and `view` tasks stored in a Python Data Structure. +// Tasks can be filtered as `all`, `active`, or `completed` based on their state. +// The application demonstrates dynamic rendering using Flask's routing and templating features. package build import mill._, pythonlib._ @@ -11,8 +11,8 @@ object todo extends PythonModule { def pythonDeps = Seq("flask==3.1.0", "Flask-SQLAlchemy==3.1.1", "Flask-WTF==1.2.2") - object unitTest extends PythonTests with TestModule.Unittest - object integrationTest extends PythonTests with TestModule.Unittest + object test extends PythonTests with TestModule.Unittest + object itest extends PythonTests with TestModule.Unittest } @@ -20,43 +20,42 @@ object todo extends PythonModule { // - **Serving HTML templates** using **Jinja2** (Flask's default templating engine). // - **Managing static files** such as JavaScript, CSS, and images. -// - **Querying a SQL database** using **Flask-SQLAlchemy** with an **SQLite** backend. -// - **Form handling and validation** with **Flask-WTF**. -// - **Unit testing** using **unittest** with SQLite in-memory database. -// - **Integration testing** using **unittest**. +// - **Filtering and managing tasks** in-memory using Python data structures. +// - **Unit testing** using **unittest** for testing task operations. +// - **Integration testing** using **unittest** for end-to-end application behavior. // This example also utilizes **Mill** for managing `dependencies`, `builds`, and `tests`, // offering an efficient development workflow. /** Usage -> ./mill todo.unitTest +> ./mill todo.test ... -test_task_creation (test_unit.UnitTest...) ... ok -test_task_status_update (test_unit.UnitTest...) ... ok -... -Ran 2 tests... +test_add_todo (test.TestTodoApp...) ... ok +test_delete_todo (test.TestTodoApp...) ... ok +test_edit_todo (test.TestTodoApp...) ... ok +test_filter_todos (test.TestTodoApp...) ... ok +test_toggle_all (test.TestTodoApp...) ... ok +test_toggle_todo (test.TestTodoApp...) ... ok +...Ran 6 tests... OK ... -> ./mill todo.integrationTest -... -test_add_task (test_integration.IntegrationTest...) -Test adding a task through the /add route. ... ok -test_delete_task (test_integration.IntegrationTest...) ... ok -test_edit_task (test_integration.IntegrationTest...) -Test editing a task through the /edit/ route. ... ok -test_index_empty (test_integration.IntegrationTest...) -Test the index route with no tasks. ... ok +> ./mill todo.itest ... -Ran 4 tests... +test_add_and_list_todos (test.TestTodoAppIntegration...) ... ok +test_delete_todo (test.TestTodoAppIntegration...) ... ok +test_edit_and_list_todos (test.TestTodoAppIntegration...) ... ok +test_toggle_all_todos (test.TestTodoAppIntegration...) ... ok +test_toggle_and_list_todos (test.TestTodoAppIntegration...) ... ok +...Ran 5 tests... OK ... > ./mill todo.runBackground > curl http://localhost:5001 -...To-Do Flask App Using Mill Build Tool... +...What needs to be done... > ./mill clean todo.runBackground diff --git a/example/pythonlib/web/2-todo-flask/todo/integrationTest/src/test_integration.py b/example/pythonlib/web/2-todo-flask/todo/integrationTest/src/test_integration.py deleted file mode 100644 index a37267fdc96..00000000000 --- a/example/pythonlib/web/2-todo-flask/todo/integrationTest/src/test_integration.py +++ /dev/null @@ -1,116 +0,0 @@ -import unittest -from app import app, db, Task -from datetime import date - -class IntegrationTest(unittest.TestCase): - def setUp(self): - """Set up the test client and initialize the in-memory database.""" - app.config['TESTING'] = True - app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///:memory:" - # Disable CSRF for testing - app.config['WTF_CSRF_ENABLED'] = False - self.client = app.test_client() - with app.app_context(): - db.create_all() - - def tearDown(self): - """Clean up the database after each test.""" - with app.app_context(): - db.session.remove() - db.drop_all() - - def test_index_empty(self): - """Test the index route with no tasks.""" - response = self.client.get("/") - self.assertEqual(response.status_code, 200) - self.assertIn(b"No tasks found", response.data) - - def test_add_task(self): - """Test adding a task through the /add route.""" - - # Simulate a POST request with valid form data - response = self.client.post("/add", data={ - "title": "Test Task", - "description": "This is a test task description.", - "status": "Pending", - "deadline": "2024-12-31" - }, follow_redirects=True) - - # Check if the response is successful - self.assertEqual(response.status_code, 200) - self.assertIn(b"Task added successfully!", response.data) - - # Verify the task was added to the database - with app.app_context(): - task = Task.query.first() # Retrieve the first task in the database - self.assertIsNotNone(task, "Task was not added to the database.") - self.assertEqual(task.title, "Test Task") - self.assertEqual(task.description, "This is a test task description.") - self.assertEqual(task.status, "Pending") - self.assertEqual(str(task.deadline), "2024-12-31") - - def test_edit_task(self): - """Test editing a task through the /edit/ route.""" - - # Prepopulate the database with a task - with app.app_context(): - new_task = Task( - title="Original Task", - description="Original description.", - status="Pending", - deadline=date(2024, 12, 31) - ) - db.session.add(new_task) - db.session.commit() - task_id = new_task.id # Get the ID of the newly added task - - # Simulate a POST request to edit the task - response = self.client.post(f"/edit/{task_id}", data={ - "title": "Updated Task", - "description": "Updated description.", - "status": "Completed", - "deadline": "2025-01-15" - }, follow_redirects=True) - - # Check if the response is successful - self.assertEqual(response.status_code, 200) - self.assertIn(b"Task updated successfully!", response.data) - - # Verify the task was updated in the database - with app.app_context(): - task = db.session.get(Task, task_id) - self.assertIsNotNone(task, "Task not found in the database after edit.") - self.assertEqual(task.title, "Updated Task") - self.assertEqual(task.description, "Updated description.") - self.assertEqual(task.status, "Completed") - self.assertEqual(str(task.deadline), "2025-01-15") - - # Test editing a non-existent task - non_existent_id = 9999 - response = self.client.get(f"/edit/{non_existent_id}", follow_redirects=True) - self.assertEqual(response.status_code, 200) - self.assertIn(b"Task not found.", response.data) - - def test_delete_task(self): - with app.app_context(): # Ensure application context is active - # Add a task to delete - task = Task(title="Test Task") - db.session.add(task) - db.session.commit() - task_id = task.id - - # Test deleting an existing task - response = self.client.get(f"/delete/{task_id}", follow_redirects=True) - self.assertEqual(response.status_code, 200) - self.assertIn(b"Task deleted successfully!", response.data) - self.assertIsNone(db.session.get(Task, task_id)) - - # Test deleting a non-existent task - non_existent_id = 9999 - response = self.client.get(f"/delete/{non_existent_id}", follow_redirects=True) - self.assertEqual(response.status_code, 200) - self.assertIn(b"Task not found.", response.data) - - -if __name__ == "__main__": - unittest.main() diff --git a/example/pythonlib/web/2-todo-flask/todo/itest/src/test.py b/example/pythonlib/web/2-todo-flask/todo/itest/src/test.py new file mode 100644 index 00000000000..c371708f2cb --- /dev/null +++ b/example/pythonlib/web/2-todo-flask/todo/itest/src/test.py @@ -0,0 +1,68 @@ +import unittest +from app import app, todos + + +class TestTodoAppIntegration(unittest.TestCase): + @classmethod + def setUpClass(cls): + # Set up the test client for the app + cls.client = app.test_client() + + def setUp(self): + # Clear the todos list before each test + global todos + todos.clear() + + def test_add_and_list_todos(self): + # Test adding a todo and listing all todos + response = self.client.post("/add/all", data="Test Todo") + self.assertEqual(response.status_code, 200) + # Fetch the todos list and verify the item was added + response = self.client.post("/list/all", data="") + self.assertIn(b"Test Todo", response.data) + + def test_toggle_and_list_todos(self): + # Test adding a todo, toggling it, and listing active/completed todos + self.client.post("/add/all", data="Test Todo") + response = self.client.post("/toggle/all/0", data="") + # Check if the todo is toggled + self.assertEqual(response.status_code, 200) + # Now, test filtering todos based on active/completed state + response = self.client.post("/list/active", data="") + self.assertNotIn(b"Test Todo", response.data) + response = self.client.post("/list/completed", data="") + self.assertIn(b"Test Todo", response.data) + + def test_edit_and_list_todos(self): + # Test adding a todo, editing it, and then verifying the updated text + self.client.post("/add/all", data="Test Todo") + response = self.client.post("/edit/all/0", data="Updated Todo") + # Check that the todo was updated + response = self.client.post("/list/all", data="") + self.assertIn(b"Updated Todo", response.data) + self.assertNotIn(b"Test Todo", response.data) + + def test_delete_todo(self): + # Test adding and deleting a todo + self.client.post("/add/all", data="Test Todo") + response = self.client.post("/delete/all/0", data="") + # Verify that the todo was deleted + response = self.client.post("/list/all", data="") + self.assertNotIn(b"Test Todo", response.data) + + def test_toggle_all_todos(self): + # Test toggling all todos + self.client.post("/add/all", data="Todo 1") + self.client.post("/add/all", data="Todo 2") + response = self.client.post("/toggle-all/all", data="") + response = self.client.post("/list/completed", data="") + self.assertIn(b"Todo 1", response.data) + self.assertIn(b"Todo 2", response.data) + response = self.client.post("/toggle-all/all", data="") + response = self.client.post("/list/active", data="") + self.assertIn(b"Todo 1", response.data) + self.assertIn(b"Todo 2", response.data) + + +if __name__ == "__main__": + unittest.main() diff --git a/example/pythonlib/web/2-todo-flask/todo/src/app.py b/example/pythonlib/web/2-todo-flask/todo/src/app.py index d6de93cd6a4..625f10e5dc1 100644 --- a/example/pythonlib/web/2-todo-flask/todo/src/app.py +++ b/example/pythonlib/web/2-todo-flask/todo/src/app.py @@ -1,82 +1,96 @@ -from flask import Flask, render_template, redirect, url_for, flash, request -from flask_sqlalchemy import SQLAlchemy -from flask_wtf import FlaskForm -from wtforms import StringField, TextAreaField, SelectField, DateField, SubmitField -from wtforms.validators import DataRequired, Length +from flask import Flask, render_template, request +from dataclasses import dataclass +from typing import List -# Initialize Flask App and Database app = Flask(__name__, static_folder="../static", template_folder="../templates") -app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///todo.db" -app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False -app.config["SECRET_KEY"] = ( - "8f41b7124eec1c73f2fbe77e6e76c54602a40c44c842da93b09f48b79c023c88" -) -# Import models -from models import Task, db -# Import forms -from forms import TaskForm +@dataclass +class Todo: + checked: bool + text: str -db.init_app(app) + +todos: List[Todo] = [] -# Routes @app.route("/") def index(): - tasks = Task.query.all() - return render_template("index.html", tasks=tasks) - - -@app.route("/add", methods=["GET", "POST"]) -def add_task(): - form = TaskForm() - if form.validate_on_submit(): - new_task = Task( - title=form.title.data, - description=form.description.data, - status=form.status.data, - deadline=form.deadline.data, - ) - db.session.add(new_task) - db.session.commit() - flash("Task added successfully!", "success") - return redirect(url_for("index")) - return render_template("task.html", form=form, title="Add Task") - - -@app.route("/edit/", methods=["GET", "POST"]) -def edit_task(task_id): - task = db.session.get(Task, task_id) - if not task: # Handle case where task doesn't exist - flash("Task not found.", "error") - return redirect(url_for("index")) - form = TaskForm(obj=task) - if form.validate_on_submit(): - task.title = form.title.data - task.description = form.description.data - task.status = form.status.data - task.deadline = form.deadline.data - db.session.commit() - flash("Task updated successfully!", "success") - return redirect(url_for("index")) - return render_template("task.html", form=form, title="Edit Task") - - -@app.route("/delete/") -def delete_task(task_id): - task = db.session.get(Task, task_id) - if not task: # Handle case where task doesn't exist - flash("Task not found.", "error") - return redirect(url_for("index")) - db.session.delete(task) - db.session.commit() - flash("Task deleted successfully!", "success") - return redirect(url_for("index")) - - -# Create database tables and run the app + return render_template("base.html", todos=todos, state="all") + + +def render_body(state: str): + filtered_todos = { + "all": todos, + "active": [todo for todo in todos if not todo.checked], + "completed": [todo for todo in todos if todo.checked], + }[state] + return render_template("index.html", todos=filtered_todos, state=state) + + +def filter_todos(state): + """Filter todos based on the state (all, active, completed).""" + if state == "all": + return todos + elif state == "active": + return [todo for todo in todos if not todo.checked] + elif state == "completed": + return [todo for todo in todos if todo.checked] + + +@app.route("/edit//", methods=["POST"]) +def edit_todo(state, index): + """Edit the text of a todo.""" + global todos + updated_text = request.data.decode("utf-8") + # Update the text attribute of the Todo object + todos[index].text = updated_text + filtered_todos = filter_todos(state) + return render_template("index.html", todos=filtered_todos, state=state) + + +@app.route("/list/", methods=["POST"]) +def list_todos(state): + return render_body(state) + + +@app.route("/add/", methods=["POST"]) +def add_todo(state): + todos.insert(0, Todo(checked=False, text=request.data.decode("utf-8"))) + return render_body(state) + + +@app.route("/delete//", methods=["POST"]) +def delete_todo(state, index): + if 0 <= index < len(todos): + todos.pop(index) + return render_body(state) + + +@app.route("/toggle//", methods=["POST"]) +def toggle(state, index): + if 0 <= index < len(todos): + todos[index].checked = not todos[index].checked + return render_body(state) + + +@app.route("/clear-completed/", methods=["POST"]) +def clear_completed(state): + global todos + todos = [todo for todo in todos if not todo.checked] + return render_body(state) + + +@app.route("/toggle-all/", methods=["POST"]) +def toggle_all(state): + global todos + + all_checked = all(todo.checked for todo in todos) + for todo in todos: + todo.checked = not all_checked + + return render_body(state) + + if __name__ == "__main__": - with app.app_context(): - db.create_all() - app.run(debug=True, port=5001) + app.run(debug=True) diff --git a/example/pythonlib/web/2-todo-flask/todo/src/forms.py b/example/pythonlib/web/2-todo-flask/todo/src/forms.py deleted file mode 100644 index e790d73c2fb..00000000000 --- a/example/pythonlib/web/2-todo-flask/todo/src/forms.py +++ /dev/null @@ -1,13 +0,0 @@ -from flask_wtf import FlaskForm -from wtforms import StringField, TextAreaField, SelectField, DateField, SubmitField -from wtforms.validators import DataRequired, Length - - -class TaskForm(FlaskForm): - title = StringField("Title", validators=[DataRequired(), Length(max=100)]) - description = TextAreaField("Description") - status = SelectField( - "Status", choices=[("Pending", "Pending"), ("Completed", "Completed")] - ) - deadline = DateField("Deadline", format="%Y-%m-%d", validators=[DataRequired()]) - submit = SubmitField("Save") diff --git a/example/pythonlib/web/2-todo-flask/todo/src/models.py b/example/pythonlib/web/2-todo-flask/todo/src/models.py deleted file mode 100644 index 22606eae456..00000000000 --- a/example/pythonlib/web/2-todo-flask/todo/src/models.py +++ /dev/null @@ -1,15 +0,0 @@ -from flask_sqlalchemy import SQLAlchemy - -db = SQLAlchemy() - - -class Task(db.Model): - id = db.Column(db.Integer, primary_key=True) - title = db.Column(db.String(100), nullable=False) - description = db.Column(db.Text, nullable=True) - status = db.Column(db.String(20), default="Pending") # Options: Pending, Completed - created_at = db.Column(db.DateTime, default=db.func.current_timestamp()) - deadline = db.Column(db.Date) - - def __repr__(self): - return f"" diff --git a/example/pythonlib/web/2-todo-flask/todo/static/main.js b/example/pythonlib/web/2-todo-flask/todo/static/main.js new file mode 100644 index 00000000000..dadd364230c --- /dev/null +++ b/example/pythonlib/web/2-todo-flask/todo/static/main.js @@ -0,0 +1,112 @@ +var state = "all"; + +var todoApp = document.getElementsByClassName("todoapp")[0]; + +function postFetchUpdate(url) { + fetch(url, { + method: "POST", + }) + .then(function (response) { return response.text(); }) + .then(function (text) { + todoApp.innerHTML = text; + initListeners(); + }); +} + +function bindEvent(cls, url, endState) { + document.getElementsByClassName(cls)[0].addEventListener( + "mousedown", + function (evt) { + postFetchUpdate(url); + if (endState) state = endState; + } + ); +} + +function bindIndexedEvent(cls, func) { + Array.from(document.getElementsByClassName(cls)).forEach(function (elem) { + elem.addEventListener( + "mousedown", + function (evt) { + postFetchUpdate(func(elem.getAttribute("data-todo-index"))); + } + ); + }); +} + +function initListeners() { + // Bind events for deleting and toggling todos + bindIndexedEvent( + "destroy", + function (index) { return "/delete/" + state + "/" + index; } + ); + bindIndexedEvent( + "toggle", + function (index) { return "/toggle/" + state + "/" + index; } + ); + + // Bind events for global actions + bindEvent("toggle-all", "/toggle-all/" + state); + bindEvent("todo-all", "/list/all", "all"); + bindEvent("todo-active", "/list/active", "active"); + bindEvent("todo-completed", "/list/completed", "completed"); + bindEvent("clear-completed", "/clear-completed/" + state); + + // Event for adding new todos + var newTodoInput = document.getElementsByClassName("new-todo")[0]; + newTodoInput.addEventListener( + "keydown", + function (evt) { + if (evt.keyCode === 13) { // Enter key + fetch("/add/" + state, { + method: "POST", + body: newTodoInput.value + }) + .then(function (response) { return response.text(); }) + .then(function (text) { + newTodoInput.value = ""; + todoApp.innerHTML = text; + initListeners(); + }); + } + } + ); + + // Add double-click event to labels for editing todos + Array.from(document.querySelectorAll(".todo-list label")).forEach(function (label) { + label.addEventListener("dblclick", function () { + var li = label.closest("li"); + li.classList.add("editing"); + + var editInput = li.querySelector(".edit"); + editInput.value = label.textContent; + editInput.focus(); + + // Save on blur or Enter key + function saveEdit() { + var index = editInput.closest("li").querySelector(".toggle").getAttribute("data-todo-index"); + var updatedText = editInput.value; + + fetch("/edit/" + state + "/" + index, { + method: "POST", + body: updatedText + }) + .then(function (response) { return response.text(); }) + .then(function (text) { + todoApp.innerHTML = text; + initListeners(); + }); + } + + editInput.addEventListener("blur", saveEdit); + editInput.addEventListener("keydown", function (evt) { + if (evt.keyCode === 13) { // Enter key + saveEdit(); + } + }); + }); + }); +} + +// Initialize event listeners when the page loads +initListeners(); \ No newline at end of file diff --git a/example/pythonlib/web/2-todo-flask/todo/static/style.css b/example/pythonlib/web/2-todo-flask/todo/static/style.css deleted file mode 100644 index 54f07a91f48..00000000000 --- a/example/pythonlib/web/2-todo-flask/todo/static/style.css +++ /dev/null @@ -1,21 +0,0 @@ -body { - background-color: #f8f9fa; -} - -.navbar-brand { - font-weight: bold; -} - -.table th, -.table td { - text-align: center; - vertical-align: middle; -} - -.btn { - margin-right: 5px; -} - -.container { - max-width: 900px; -} diff --git a/example/pythonlib/web/2-todo-flask/todo/static/styles.css b/example/pythonlib/web/2-todo-flask/todo/static/styles.css new file mode 100644 index 00000000000..c81f4951668 --- /dev/null +++ b/example/pythonlib/web/2-todo-flask/todo/static/styles.css @@ -0,0 +1,369 @@ +@charset "utf-8"; + +html, +body { + margin: 0; + padding: 0; +} + +button { + margin: 0; + padding: 0; + border: 0; + background: none; + font-size: 100%; + vertical-align: baseline; + font-family: inherit; + font-weight: inherit; + color: inherit; + -webkit-appearance: none; + appearance: none; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +body { + font: 14px 'Helvetica Neue', Helvetica, Arial, sans-serif; + line-height: 1.4em; + background: #f5f5f5; + color: #111111; + min-width: 230px; + max-width: 550px; + margin: 0 auto; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; + font-weight: 300; +} + +.hidden { + display: none; +} + +.todoapp { + background: #fff; + margin: 130px 0 40px 0; + position: relative; + box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), + 0 25px 50px 0 rgba(0, 0, 0, 0.1); +} + +.todoapp input::-webkit-input-placeholder { + font-style: italic; + font-weight: 400; + color: rgba(0, 0, 0, 0.4); +} + +.todoapp input::-moz-placeholder { + font-style: italic; + font-weight: 400; + color: rgba(0, 0, 0, 0.4); +} + +.todoapp input::input-placeholder { + font-style: italic; + font-weight: 400; + color: rgba(0, 0, 0, 0.4); +} + +.todoapp h1 { + position: absolute; + top: -140px; + width: 100%; + font-size: 80px; + font-weight: 200; + text-align: center; + color: #b83f45; + -webkit-text-rendering: optimizeLegibility; + -moz-text-rendering: optimizeLegibility; + text-rendering: optimizeLegibility; +} + +.new-todo, +.edit { + position: relative; + margin: 0; + width: 100%; + font-size: 24px; + font-family: inherit; + font-weight: inherit; + line-height: 1.4em; + color: inherit; + padding: 6px; + border: 1px solid #999; + box-shadow: inset 0 -1px 5px 0 rgba(0, 0, 0, 0.2); + box-sizing: border-box; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +.new-todo { + padding: 16px 16px 16px 60px; + height: 65px; + border: none; + background: rgba(0, 0, 0, 0.003); + box-shadow: inset 0 -2px 1px rgba(0, 0, 0, 0.03); +} + +.main { + position: relative; + z-index: 2; + border-top: 1px solid #e6e6e6; +} + + +.toggle-all { + position: absolute; + top: -53px; + left: -10px; + width: 60px; + height: 34px; + text-align: center; +} + +.todo-list { + margin: 0; + padding: 0; + list-style: none; +} + +.todo-list li { + position: relative; + font-size: 24px; + border-bottom: 1px solid #ededed; +} + +.todo-list li:last-child { + border-bottom: none; +} + +.todo-list li.editing { + border-bottom: none; + padding: 0; +} + +.todo-list li.editing .edit { + display: block; + width: calc(100% - 43px); + padding: 12px 16px; + margin: 0 0 0 43px; +} + +.todo-list li.editing .view { + display: none; +} + +.todo-list li .toggle { + text-align: center; + width: 40px; + /* auto, since non-WebKit browsers doesn't support input styling */ + height: auto; + position: absolute; + top: 0; + bottom: 0; + margin: auto 0; + border: none; + /* Mobile Safari */ + -webkit-appearance: none; + appearance: none; +} + +.todo-list li .toggle { + opacity: 0; +} + +.todo-list li .toggle+label { + /* + Firefox requires `#` to be escaped - https://bugzilla.mozilla.org/show_bug.cgi?id=922433 + IE and Edge requires *everything* to be escaped to render, so we do that instead of just the `#` - https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/7157459/ + */ + background-image: url('data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%22-10%20-18%20100%20135%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22none%22%20stroke%3D%22%23949494%22%20stroke-width%3D%223%22/%3E%3C/svg%3E'); + background-repeat: no-repeat; + background-position: center left; +} + +.todo-list li .toggle:checked+label { + background-image: url('data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%22-10%20-18%20100%20135%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22none%22%20stroke%3D%22%2359A193%22%20stroke-width%3D%223%22%2F%3E%3Cpath%20fill%3D%22%233EA390%22%20d%3D%22M72%2025L42%2071%2027%2056l-4%204%2020%2020%2034-52z%22%2F%3E%3C%2Fsvg%3E'); +} + +.todo-list li label { + word-break: break-all; + padding: 15px 15px 15px 60px; + display: block; + line-height: 1.2; + transition: color 0.4s; + font-weight: 400; + color: #484848; +} + +.todo-list li.completed label { + color: #949494; + text-decoration: line-through; +} + +.todo-list li .destroy { + display: none; + position: absolute; + top: 0; + right: 10px; + bottom: 0; + width: 40px; + height: 40px; + margin: auto 0; + font-size: 30px; + color: #949494; + transition: color 0.2s ease-out; +} + +.todo-list li .destroy:hover, +.todo-list li .destroy:focus { + color: #C18585; +} + +.todo-list li .destroy:after { + content: '×'; + display: block; + height: 100%; + line-height: 1.1; +} + +.todo-list li:hover .destroy { + display: block; +} + +.todo-list li .edit { + display: none; +} + +.todo-list li.editing:last-child { + margin-bottom: -1px; +} + +.footer { + padding: 10px 15px; + height: 20px; + text-align: center; + font-size: 15px; + border-top: 1px solid #e6e6e6; +} + +.footer:before { + content: ''; + position: absolute; + right: 0; + bottom: 0; + left: 0; + height: 50px; + overflow: hidden; + box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2), + 0 8px 0 -3px #f6f6f6, + 0 9px 1px -3px rgba(0, 0, 0, 0.2), + 0 16px 0 -6px #f6f6f6, + 0 17px 2px -6px rgba(0, 0, 0, 0.2); +} + +.todo-count { + float: left; + text-align: left; +} + +.todo-count strong { + font-weight: 300; +} + +.filters { + margin: 0; + padding: 0; + list-style: none; + position: absolute; + right: 0; + left: 0; +} + +.filters li { + display: inline; +} + +.filters li a { + color: inherit; + margin: 3px; + padding: 3px 7px; + text-decoration: none; + border: 1px solid transparent; + border-radius: 3px; +} + +.filters li a:hover { + border-color: #DB7676; +} + +.filters li a.selected { + border-color: #CE4646; +} + +.clear-completed, +html .clear-completed:active { + float: right; + position: relative; + line-height: 19px; + text-decoration: none; + cursor: pointer; +} + +.clear-completed:hover { + text-decoration: underline; +} + +.info { + margin: 65px auto 0; + color: #4d4d4d; + font-size: 11px; + text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5); + text-align: center; +} + +.info p { + line-height: 1; +} + +.info a { + color: inherit; + text-decoration: none; + font-weight: 400; +} + +.info a:hover { + text-decoration: underline; +} + +/* + Hack to remove background from Mobile Safari. + Can't use it globally since it destroys checkboxes in Firefox +*/ +@media screen and (-webkit-min-device-pixel-ratio:0) { + + .toggle-all, + .todo-list li .toggle { + background: none; + } + + .todo-list li .toggle { + height: 40px; + } +} + +@media (max-width: 430px) { + .footer { + height: 50px; + } + + .filters { + bottom: 10px; + } +} + +:focus, +.toggle:focus+label, +.toggle-all:focus+label { + box-shadow: 0 0 2px 2px #CF7D7D; + outline: 0; +} \ No newline at end of file diff --git a/example/pythonlib/web/2-todo-flask/todo/templates/base.html b/example/pythonlib/web/2-todo-flask/todo/templates/base.html index f0ef14f688d..e8e5ed9c54b 100644 --- a/example/pythonlib/web/2-todo-flask/todo/templates/base.html +++ b/example/pythonlib/web/2-todo-flask/todo/templates/base.html @@ -1,31 +1,23 @@ + - Flask To-Do App - - + Flask • TodoMVC + + - -
- {% with messages = get_flashed_messages(with_categories=True) %} - {% if messages %} - {% for category, message in messages %} - - {% endfor %} - {% endif %} - {% endwith %} - {% block content %}{% endblock %} -
- +
+ {% include 'index.html' %} +
+ + - + + \ No newline at end of file diff --git a/example/pythonlib/web/2-todo-flask/todo/templates/index.html b/example/pythonlib/web/2-todo-flask/todo/templates/index.html index bd0b59624b3..337b8789507 100644 --- a/example/pythonlib/web/2-todo-flask/todo/templates/index.html +++ b/example/pythonlib/web/2-todo-flask/todo/templates/index.html @@ -1,35 +1,31 @@ -{% extends 'base.html' %} -{% block content %} -

Task List

-Add Task - -{% if tasks %} - - - - - - - - - - - - {% for task in tasks %} - - - - - - - +
+

todos

+ +
+
+ 0 and todos|selectattr('checked')|list|length == todos|length %}checked{% endif %}> +
    + {% for todo in todos %} +
  • +
    + + + +
    + +
  • {% endfor %} -
-
#TitleStatusDeadlineActions
{{ loop.index }}{{ task.title }}{{ task.status }}{{ task.deadline }} - Edit - Delete -
-{% else %} -

No tasks found

-{% endif %} -{% endblock %} + + +
+ + {{ todos|selectattr('checked', 'equalto', False)|list|length }} items left + + + +
\ No newline at end of file diff --git a/example/pythonlib/web/2-todo-flask/todo/templates/task.html b/example/pythonlib/web/2-todo-flask/todo/templates/task.html deleted file mode 100644 index 672696454a9..00000000000 --- a/example/pythonlib/web/2-todo-flask/todo/templates/task.html +++ /dev/null @@ -1,24 +0,0 @@ -{% extends 'base.html' %} -{% block content %} -

{{ title }}

-
- {{ form.hidden_tag() }} -
- {{ form.title.label }}
- {{ form.title(class="form-control") }} -
-
- {{ form.description.label }}
- {{ form.description(class="form-control") }} -
-
- {{ form.status.label }}
- {{ form.status(class="form-control") }} -
-
- {{ form.deadline.label }}
- {{ form.deadline(class="form-control") }} -
- -
-{% endblock %} diff --git a/example/pythonlib/web/2-todo-flask/todo/test/src/test.py b/example/pythonlib/web/2-todo-flask/todo/test/src/test.py new file mode 100644 index 00000000000..5a8e25618b9 --- /dev/null +++ b/example/pythonlib/web/2-todo-flask/todo/test/src/test.py @@ -0,0 +1,64 @@ +import unittest +from app import app, todos, Todo + + +class TestTodoApp(unittest.TestCase): + @classmethod + def setUpClass(cls): + # Set up the test client for the app + cls.client = app.test_client() + + def setUp(self): + # Clear the todos list before each test + global todos + todos.clear() + + def test_add_todo(self): + # Add a todo item + response = self.client.post("/add/all", data="Test Todo") + self.assertEqual(response.status_code, 200) + self.assertEqual(len(todos), 1) + self.assertEqual(todos[0].text, "Test Todo") + + def test_toggle_todo(self): + # Add a todo item and toggle it + todos.append(Todo(checked=False, text="Test Todo")) + response = self.client.post("/toggle/all/0", data="") + self.assertEqual(response.status_code, 200) + self.assertTrue(todos[0].checked) + + def test_edit_todo(self): + # Add a todo item and edit it + todos.append(Todo(checked=False, text="Test Todo")) + response = self.client.post("/edit/all/0", data="Updated Todo") + self.assertEqual(response.status_code, 200) + self.assertEqual(todos[0].text, "Updated Todo") + + def test_delete_todo(self): + # Add a todo item and delete it + todos.append(Todo(checked=False, text="Test Todo")) + response = self.client.post("/delete/all/0", data="") + self.assertEqual(response.status_code, 200) + self.assertEqual(len(todos), 0) + + def test_toggle_all(self): + # Add todos and toggle all + todos.append(Todo(checked=False, text="Todo 1")) + todos.append(Todo(checked=False, text="Todo 2")) + response = self.client.post("/toggle-all/all", data="") + self.assertEqual(response.status_code, 200) + self.assertTrue(all(todo.checked for todo in todos)) + + def test_filter_todos(self): + # Add todos and test the filter functionality + todos.append(Todo(checked=False, text="Active Todo")) + todos.append(Todo(checked=True, text="Completed Todo")) + response = self.client.post("/list/active", data="") + self.assertEqual(response.status_code, 200) + self.assertIn(b"Active Todo", response.data) + self.assertNotIn(b"Completed Todo", response.data) + self.assertEqual(len(todos), 2) # Should still have 2 todos + + +if __name__ == "__main__": + unittest.main() diff --git a/example/pythonlib/web/2-todo-flask/todo/unitTest/src/test_unit.py b/example/pythonlib/web/2-todo-flask/todo/unitTest/src/test_unit.py deleted file mode 100644 index 1e4954daacc..00000000000 --- a/example/pythonlib/web/2-todo-flask/todo/unitTest/src/test_unit.py +++ /dev/null @@ -1,23 +0,0 @@ -import unittest -from app import Task # type: ignore - - -class UnitTest(unittest.TestCase): - def test_task_creation(self): - task = Task( - title="Sample Task", description="Task Description", status="Pending" - ) - self.assertEqual(task.title, "Sample Task") - self.assertEqual(task.description, "Task Description") - self.assertEqual(task.status, "Pending") - - def test_task_status_update(self): - task = Task( - title="Sample Task", description="Task Description", status="Pending" - ) - task.status = "Completed" - self.assertEqual(task.status, "Completed") - - -if __name__ == "__main__": - unittest.main() From 044c761641b03a697e236ce09e8afcb44ecfe0c7 Mon Sep 17 00:00:00 2001 From: Himanshu Mahajan <83700343+himanshumahajan138@users.noreply.github.com> Date: Sun, 22 Dec 2024 09:33:30 +0530 Subject: [PATCH 08/10] Update app.py --- example/pythonlib/web/2-todo-flask/todo/src/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/pythonlib/web/2-todo-flask/todo/src/app.py b/example/pythonlib/web/2-todo-flask/todo/src/app.py index 625f10e5dc1..5b631a3941d 100644 --- a/example/pythonlib/web/2-todo-flask/todo/src/app.py +++ b/example/pythonlib/web/2-todo-flask/todo/src/app.py @@ -93,4 +93,4 @@ def toggle_all(state): if __name__ == "__main__": - app.run(debug=True) + app.run(debug=True, port=5001) From 8fc2a02cfd4b584f855e7e4c17cd2f08c561e75f Mon Sep 17 00:00:00 2001 From: himanshumahajan138 Date: Sun, 22 Dec 2024 18:16:53 +0530 Subject: [PATCH 09/10] Final Updation --- .../pythonlib/web/4-todo-django/build.mill | 45 +-- .../4-todo-django/todo/src/app/settings.py | 4 - .../web/4-todo-django/todo/src/main/forms.py | 11 - .../web/4-todo-django/todo/src/main/models.py | 18 +- .../todo/src/main/static/main.js | 156 ++++++++ .../todo/src/main/static/styles.css | 368 +++++++++++++++++- .../todo/src/main/templates/base.html | 32 +- .../todo/src/main/templates/index.html | 73 ++-- .../todo/src/main/templates/task.html | 82 ---- .../web/4-todo-django/todo/src/main/tests.py | 203 +++------- .../web/4-todo-django/todo/src/main/urls.py | 14 +- .../web/4-todo-django/todo/src/main/views.py | 110 ++++-- 12 files changed, 710 insertions(+), 406 deletions(-) delete mode 100644 example/pythonlib/web/4-todo-django/todo/src/main/forms.py create mode 100644 example/pythonlib/web/4-todo-django/todo/src/main/static/main.js delete mode 100644 example/pythonlib/web/4-todo-django/todo/src/main/templates/task.html diff --git a/example/pythonlib/web/4-todo-django/build.mill b/example/pythonlib/web/4-todo-django/build.mill index 9f51c561321..a5c915cea96 100644 --- a/example/pythonlib/web/4-todo-django/build.mill +++ b/example/pythonlib/web/4-todo-django/build.mill @@ -17,8 +17,8 @@ object todo extends PythonModule { // - **Serving HTML templates** using **Django's template engine**. // - **Managing static files** such as JavaScript, CSS, and images. // - **Querying a SQL database** using **Django ORM** with an **SQLite** backend. -// - **Form handling and validation** with **Django forms**. -// - **Unit testing** using Django's inbuilt testing framework with SQLite in-memory database. +// - **Filtering and managing tasks** using SQLite database. +// - **Unit + Integration testing** using Django's inbuilt testing framework. // - **URL routing** and **views** for CRUD operations. // This Mill Build File simplifies `dependency management`, `database migrations`, and `testing workflows`. @@ -29,44 +29,31 @@ object todo extends PythonModule { > ./mill todo.run makemigrations ...Migrations for 'main'... -...Create model Task... +...+ Create model Todo... > ./mill todo.run migrate +...Operations to perform... ...Apply all migrations: admin, auth, contenttypes, main, sessions... ...Running migrations... ...OK... > ./mill todo.run test main -v 2 -... -test_add_task_message (main.tests.TestScript...) -Test that a success message appears when a task is successfully added. ... ok -test_add_task_view_get (main.tests.TestScript...) -Test that the 'Add Task' page loads successfully with an empty form. ... ok -test_add_task_view_post_invalid_data (main.tests.TestScript...) -Test that submitting the form with invalid data shows errors. ... ok -test_add_task_view_post_valid_data (main.tests.TestScript...) -Test that submitting the form with valid data creates a new task. ... ok -test_default_status (main.tests.TestScript...) -Test that the default status of a task is 'Pending'. ... ok -test_delete_task_message (main.tests.TestScript...) -Test that a success message appears when a task is deleted. ... ok -test_delete_task_view (main.tests.TestScript...) -Test that deleting a task works and redirects to the task list. ... ok -test_edit_task_view_post_valid_data (main.tests.TestScript...) -Test that editing a task updates its details. ... ok -test_str_method (main.tests.TestScript...) -Test the __str__ method of the Task model. ... ok -test_url_resolves_to_correct_view (main.tests.TestScript...) -Test that the correct view function is mapped to each URL. ... ok -... -...Ran 10 tests... -OK -... +...Found 8 test(s)... +test_add_todo_view (main.tests.TodoAppTests...) ... ok +test_clear_completed_view (main.tests.TodoAppTests...) ... ok +test_delete_todo_view (main.tests.TodoAppTests...) ... ok +test_edit_todo_view (main.tests.TodoAppTests...) ... ok +test_index_view (main.tests.TodoAppTests...) ... ok +test_list_todos_view (main.tests.TodoAppTests...) ... ok +test_toggle_all_view (main.tests.TodoAppTests...) ... ok +test_toggle_todo_view (main.tests.TodoAppTests...) ... ok +...Ran 8 tests... +...OK... > ./mill todo.runBackground runserver 8001 > curl http://localhost:8001 -...To-Do Django App Using Mill Build Tool... +...What needs to be done... > ./mill clean todo.runBackground diff --git a/example/pythonlib/web/4-todo-django/todo/src/app/settings.py b/example/pythonlib/web/4-todo-django/todo/src/app/settings.py index 45191a77e56..02216e12238 100644 --- a/example/pythonlib/web/4-todo-django/todo/src/app/settings.py +++ b/example/pythonlib/web/4-todo-django/todo/src/app/settings.py @@ -78,10 +78,6 @@ "default": { "ENGINE": "django.db.backends.sqlite3", "NAME": BASE_DIR / "db.sqlite3", - }, - 'TEST': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': ':memory:', } } diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/forms.py b/example/pythonlib/web/4-todo-django/todo/src/main/forms.py deleted file mode 100644 index dcd31c6f466..00000000000 --- a/example/pythonlib/web/4-todo-django/todo/src/main/forms.py +++ /dev/null @@ -1,11 +0,0 @@ -from django import forms -from .models import Task - - -class TaskForm(forms.ModelForm): - class Meta: - model = Task - fields = ["title", "description", "status", "deadline"] - widgets = { - "deadline": forms.DateInput(attrs={"type": "date"}), - } diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/models.py b/example/pythonlib/web/4-todo-django/todo/src/main/models.py index bd652e5c8c4..16e7fc33dbc 100644 --- a/example/pythonlib/web/4-todo-django/todo/src/main/models.py +++ b/example/pythonlib/web/4-todo-django/todo/src/main/models.py @@ -1,17 +1,5 @@ from django.db import models - -class Task(models.Model): - STATUS_CHOICES = [ - ("Pending", "Pending"), - ("In Progress", "In Progress"), - ("Completed", "Completed"), - ] - - title = models.CharField(max_length=100) - description = models.TextField(blank=True, null=True) - status = models.CharField(max_length=20, choices=STATUS_CHOICES, default="Pending") - deadline = models.DateField(null=True, blank=True) - - def __str__(self): - return self.title +class Todo(models.Model): + text = models.CharField(max_length=255) + checked = models.BooleanField(default=False) \ No newline at end of file diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/static/main.js b/example/pythonlib/web/4-todo-django/todo/src/main/static/main.js new file mode 100644 index 00000000000..0b8ce0100c4 --- /dev/null +++ b/example/pythonlib/web/4-todo-django/todo/src/main/static/main.js @@ -0,0 +1,156 @@ +var state = "all"; + +var todoApp = document.getElementsByClassName("todoapp")[0]; + +function getCookie(name) { + let cookieValue = null; + if (document.cookie && document.cookie !== '') { + const cookies = document.cookie.split(';'); + for (let i = 0; i < cookies.length; i++) { + const cookie = cookies[i].trim(); + // Does this cookie string begin with the name we want? + if (cookie.substring(0, name.length + 1) === (name + '=')) { + cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); + break; + } + } + } + return cookieValue; +} + +const csrftoken = getCookie('csrftoken'); + +function postFetchUpdate(url) { + fetch(url, { + method: "POST", + headers: { + 'X-CSRFToken': csrftoken, + 'Content-Type': 'text/plain' + } + }) + .then(function (response) { return response.text(); }) + .then(function (text) { + todoApp.innerHTML = text; + initListeners(); + }) + .catch(function (error) { + console.error('Error:', error); + }); +} + +function bindEvent(cls, url, endState) { + var element = document.getElementsByClassName(cls)[0]; + if (element) { + element.addEventListener( + "mousedown", + function (evt) { + postFetchUpdate(url + '/'); + if (endState) state = endState; + } + ); + } +} + +function bindIndexedEvent(cls, func) { + Array.from(document.getElementsByClassName(cls)).forEach(function (elem) { + elem.addEventListener( + "mousedown", + function (evt) { + postFetchUpdate(func(elem.getAttribute("data-todo-index")) + '/'); + } + ); + }); +} + +function initListeners() { + // Bind events for deleting and toggling todos + bindIndexedEvent( + "destroy", + function (index) { return "/delete/" + state + "/" + index; } + ); + bindIndexedEvent( + "toggle", + function (index) { return "/toggle/" + state + "/" + index; } + ); + + // Bind events for global actions + bindEvent("toggle-all", "/toggle-all/" + state); + bindEvent("todo-all", "/list/all", "all"); + bindEvent("todo-active", "/list/active", "active"); + bindEvent("todo-completed", "/list/completed", "completed"); + bindEvent("clear-completed", "/clear-completed/" + state); + + // Event for adding new todos + var newTodoInput = document.getElementsByClassName("new-todo")[0]; + if (newTodoInput) { + newTodoInput.addEventListener( + "keydown", + function (evt) { + if (evt.keyCode === 13) { // Enter key + fetch("/add/" + state + '/', { + method: "POST", + body: newTodoInput.value, + headers: { + 'X-CSRFToken': csrftoken, + 'Content-Type': 'text/plain' + } + }) + .then(function (response) { return response.text(); }) + .then(function (text) { + newTodoInput.value = ""; + todoApp.innerHTML = text; + initListeners(); + }) + .catch(function (error) { + console.error('Error:', error); + }); + } + } + ); + } + + // Add double-click event to labels for editing todos + Array.from(document.querySelectorAll(".todo-list label")).forEach(function (label) { + label.addEventListener("dblclick", function () { + var li = label.closest("li"); + li.classList.add("editing"); + + var editInput = li.querySelector(".edit"); + editInput.value = label.textContent; + editInput.focus(); + + // Save on blur or Enter key + function saveEdit() { + var index = editInput.closest("li").querySelector(".toggle").getAttribute("data-todo-index"); + var updatedText = editInput.value; + + fetch("/edit/" + state + "/" + index + '/', { + method: "POST", + body: updatedText, + headers: { + 'X-CSRFToken': csrftoken, + 'Content-Type': 'text/plain' + } + }) + .then(function (response) { return response.text(); }) + .then(function (text) { + todoApp.innerHTML = text; + initListeners(); + }) + .catch(function (error) { + console.error('Error:', error); + }); + } + + editInput.addEventListener("blur", saveEdit); + editInput.addEventListener("keydown", function (evt) { + if (evt.keyCode === 13) { // Enter key + saveEdit(); + } + }); + }); + }); +} + +// Initialize event listeners when the page loads +initListeners(); \ No newline at end of file diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/static/styles.css b/example/pythonlib/web/4-todo-django/todo/src/main/static/styles.css index 54f07a91f48..c81f4951668 100644 --- a/example/pythonlib/web/4-todo-django/todo/src/main/static/styles.css +++ b/example/pythonlib/web/4-todo-django/todo/src/main/static/styles.css @@ -1,21 +1,369 @@ +@charset "utf-8"; + +html, body { - background-color: #f8f9fa; + margin: 0; + padding: 0; +} + +button { + margin: 0; + padding: 0; + border: 0; + background: none; + font-size: 100%; + vertical-align: baseline; + font-family: inherit; + font-weight: inherit; + color: inherit; + -webkit-appearance: none; + appearance: none; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; } -.navbar-brand { - font-weight: bold; +body { + font: 14px 'Helvetica Neue', Helvetica, Arial, sans-serif; + line-height: 1.4em; + background: #f5f5f5; + color: #111111; + min-width: 230px; + max-width: 550px; + margin: 0 auto; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; + font-weight: 300; } -.table th, -.table td { +.hidden { + display: none; +} + +.todoapp { + background: #fff; + margin: 130px 0 40px 0; + position: relative; + box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), + 0 25px 50px 0 rgba(0, 0, 0, 0.1); +} + +.todoapp input::-webkit-input-placeholder { + font-style: italic; + font-weight: 400; + color: rgba(0, 0, 0, 0.4); +} + +.todoapp input::-moz-placeholder { + font-style: italic; + font-weight: 400; + color: rgba(0, 0, 0, 0.4); +} + +.todoapp input::input-placeholder { + font-style: italic; + font-weight: 400; + color: rgba(0, 0, 0, 0.4); +} + +.todoapp h1 { + position: absolute; + top: -140px; + width: 100%; + font-size: 80px; + font-weight: 200; + text-align: center; + color: #b83f45; + -webkit-text-rendering: optimizeLegibility; + -moz-text-rendering: optimizeLegibility; + text-rendering: optimizeLegibility; +} + +.new-todo, +.edit { + position: relative; + margin: 0; + width: 100%; + font-size: 24px; + font-family: inherit; + font-weight: inherit; + line-height: 1.4em; + color: inherit; + padding: 6px; + border: 1px solid #999; + box-shadow: inset 0 -1px 5px 0 rgba(0, 0, 0, 0.2); + box-sizing: border-box; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +.new-todo { + padding: 16px 16px 16px 60px; + height: 65px; + border: none; + background: rgba(0, 0, 0, 0.003); + box-shadow: inset 0 -2px 1px rgba(0, 0, 0, 0.03); +} + +.main { + position: relative; + z-index: 2; + border-top: 1px solid #e6e6e6; +} + + +.toggle-all { + position: absolute; + top: -53px; + left: -10px; + width: 60px; + height: 34px; text-align: center; - vertical-align: middle; } -.btn { - margin-right: 5px; +.todo-list { + margin: 0; + padding: 0; + list-style: none; +} + +.todo-list li { + position: relative; + font-size: 24px; + border-bottom: 1px solid #ededed; +} + +.todo-list li:last-child { + border-bottom: none; +} + +.todo-list li.editing { + border-bottom: none; + padding: 0; +} + +.todo-list li.editing .edit { + display: block; + width: calc(100% - 43px); + padding: 12px 16px; + margin: 0 0 0 43px; +} + +.todo-list li.editing .view { + display: none; +} + +.todo-list li .toggle { + text-align: center; + width: 40px; + /* auto, since non-WebKit browsers doesn't support input styling */ + height: auto; + position: absolute; + top: 0; + bottom: 0; + margin: auto 0; + border: none; + /* Mobile Safari */ + -webkit-appearance: none; + appearance: none; +} + +.todo-list li .toggle { + opacity: 0; +} + +.todo-list li .toggle+label { + /* + Firefox requires `#` to be escaped - https://bugzilla.mozilla.org/show_bug.cgi?id=922433 + IE and Edge requires *everything* to be escaped to render, so we do that instead of just the `#` - https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/7157459/ + */ + background-image: url('data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%22-10%20-18%20100%20135%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22none%22%20stroke%3D%22%23949494%22%20stroke-width%3D%223%22/%3E%3C/svg%3E'); + background-repeat: no-repeat; + background-position: center left; +} + +.todo-list li .toggle:checked+label { + background-image: url('data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%22-10%20-18%20100%20135%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22none%22%20stroke%3D%22%2359A193%22%20stroke-width%3D%223%22%2F%3E%3Cpath%20fill%3D%22%233EA390%22%20d%3D%22M72%2025L42%2071%2027%2056l-4%204%2020%2020%2034-52z%22%2F%3E%3C%2Fsvg%3E'); +} + +.todo-list li label { + word-break: break-all; + padding: 15px 15px 15px 60px; + display: block; + line-height: 1.2; + transition: color 0.4s; + font-weight: 400; + color: #484848; +} + +.todo-list li.completed label { + color: #949494; + text-decoration: line-through; +} + +.todo-list li .destroy { + display: none; + position: absolute; + top: 0; + right: 10px; + bottom: 0; + width: 40px; + height: 40px; + margin: auto 0; + font-size: 30px; + color: #949494; + transition: color 0.2s ease-out; } -.container { - max-width: 900px; +.todo-list li .destroy:hover, +.todo-list li .destroy:focus { + color: #C18585; } + +.todo-list li .destroy:after { + content: '×'; + display: block; + height: 100%; + line-height: 1.1; +} + +.todo-list li:hover .destroy { + display: block; +} + +.todo-list li .edit { + display: none; +} + +.todo-list li.editing:last-child { + margin-bottom: -1px; +} + +.footer { + padding: 10px 15px; + height: 20px; + text-align: center; + font-size: 15px; + border-top: 1px solid #e6e6e6; +} + +.footer:before { + content: ''; + position: absolute; + right: 0; + bottom: 0; + left: 0; + height: 50px; + overflow: hidden; + box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2), + 0 8px 0 -3px #f6f6f6, + 0 9px 1px -3px rgba(0, 0, 0, 0.2), + 0 16px 0 -6px #f6f6f6, + 0 17px 2px -6px rgba(0, 0, 0, 0.2); +} + +.todo-count { + float: left; + text-align: left; +} + +.todo-count strong { + font-weight: 300; +} + +.filters { + margin: 0; + padding: 0; + list-style: none; + position: absolute; + right: 0; + left: 0; +} + +.filters li { + display: inline; +} + +.filters li a { + color: inherit; + margin: 3px; + padding: 3px 7px; + text-decoration: none; + border: 1px solid transparent; + border-radius: 3px; +} + +.filters li a:hover { + border-color: #DB7676; +} + +.filters li a.selected { + border-color: #CE4646; +} + +.clear-completed, +html .clear-completed:active { + float: right; + position: relative; + line-height: 19px; + text-decoration: none; + cursor: pointer; +} + +.clear-completed:hover { + text-decoration: underline; +} + +.info { + margin: 65px auto 0; + color: #4d4d4d; + font-size: 11px; + text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5); + text-align: center; +} + +.info p { + line-height: 1; +} + +.info a { + color: inherit; + text-decoration: none; + font-weight: 400; +} + +.info a:hover { + text-decoration: underline; +} + +/* + Hack to remove background from Mobile Safari. + Can't use it globally since it destroys checkboxes in Firefox +*/ +@media screen and (-webkit-min-device-pixel-ratio:0) { + + .toggle-all, + .todo-list li .toggle { + background: none; + } + + .todo-list li .toggle { + height: 40px; + } +} + +@media (max-width: 430px) { + .footer { + height: 50px; + } + + .filters { + bottom: 10px; + } +} + +:focus, +.toggle:focus+label, +.toggle-all:focus+label { + box-shadow: 0 0 2px 2px #CF7D7D; + outline: 0; +} \ No newline at end of file diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/templates/base.html b/example/pythonlib/web/4-todo-django/todo/src/main/templates/base.html index 4d6aad45c84..91f44493679 100644 --- a/example/pythonlib/web/4-todo-django/todo/src/main/templates/base.html +++ b/example/pythonlib/web/4-todo-django/todo/src/main/templates/base.html @@ -4,27 +4,19 @@ {% load static %} - Django To-Do App - + Django • TodoMVC - -
- {% if messages %} - {% for message in messages %} - - {% endfor %} - {% endif %} - {% block content %}{% endblock %} -
- +
+ {% csrf_token %} + {% include 'index.html' %} +
+ + - + \ No newline at end of file diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/templates/index.html b/example/pythonlib/web/4-todo-django/todo/src/main/templates/index.html index ab56f014146..62e19e5bd34 100644 --- a/example/pythonlib/web/4-todo-django/todo/src/main/templates/index.html +++ b/example/pythonlib/web/4-todo-django/todo/src/main/templates/index.html @@ -1,45 +1,30 @@ -{% extends 'base.html' %} - -{% block content %} -
-

Task List

-
- Total Tasks: {{ tasks|length }} - Add Task -
- - {% if tasks %} - - - - - - - - - - - - {% for task in tasks %} - - - - - - - - {% endfor %} - -
#TitleStatusDeadlineActions
{{ forloop.counter }}{{ task.title }} - - {{ task.status }} - - {{ task.deadline|date:"M d, Y" }} - Edit - Delete -
- {% else %} -

No tasks found. Start by adding a new task!

+
+

todos

+ +
+
+ 0 and completed_count == total_count %}checked{% endif %}> +
    + {% for todo in todos %} +
  • +
    + + + +
    + +
  • + {% endfor %} +
+
+
+ {{ active_count }} items left + + {% if has_completed %} + {% endif %} -
-{% endblock %} + \ No newline at end of file diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/templates/task.html b/example/pythonlib/web/4-todo-django/todo/src/main/templates/task.html deleted file mode 100644 index cff6cb8fd38..00000000000 --- a/example/pythonlib/web/4-todo-django/todo/src/main/templates/task.html +++ /dev/null @@ -1,82 +0,0 @@ -{% extends 'base.html' %} - -{% block content %} -
-

{{ title }}

-
- {% csrf_token %} - {{ form.hidden_tag }} - - -
- - - - {% if form.title.errors %} -
- {% for error in form.title.errors %} -

{{ error }}

- {% endfor %} -
- {% endif %} -
- - -
- - - - {% if form.description.errors %} -
- {% for error in form.description.errors %} -

{{ error }}

- {% endfor %} -
- {% endif %} -
- - -
- - - - {% if form.status.errors %} -
- {% for error in form.status.errors %} -

{{ error }}

- {% endfor %} -
- {% endif %} -
- - -
- - - - {% if form.deadline.errors %} -
- {% for error in form.deadline.errors %} -

{{ error }}

- {% endfor %} -
- {% endif %} -
- -
- -
-
-
-{% endblock %} diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/tests.py b/example/pythonlib/web/4-todo-django/todo/src/main/tests.py index 628d5be1f72..518391df555 100644 --- a/example/pythonlib/web/4-todo-django/todo/src/main/tests.py +++ b/example/pythonlib/web/4-todo-django/todo/src/main/tests.py @@ -1,156 +1,73 @@ -from django.test import TestCase -from django.urls import reverse, resolve -from django.contrib.messages import get_messages -from .models import Task -from .views import index, add_task, edit_task, delete_task - - -class TestScript(TestCase): - """ - Unit and Integration tests. - """ - - def test_str_method(self): - """ - Test the __str__ method of the Task model. - """ - task = Task.objects.create( - title="Buy groceries", description="Get milk, eggs, and bread" - ) - self.assertEqual(str(task), "Buy groceries") - - def test_default_status(self): - """ - Test that the default status of a task is 'Pending'. - """ - task = Task.objects.create(title="Finish project") - self.assertEqual(task.status, "Pending") - - def test_add_task_view_get(self): - """ - Test that the 'Add Task' page loads successfully with an empty form. - """ - url = reverse("add_task") - response = self.client.get(url) +from django.test import TestCase, Client +from django.urls import reverse +from .models import Todo + +class TodoAppTests(TestCase): + def setUp(self): + self.client = Client() + self.todo1 = Todo.objects.create(text="Get started with Django", checked=False) + self.todo2 = Todo.objects.create(text="Profit!", checked=True) + + def test_index_view(self): + response = self.client.get(reverse('index')) self.assertEqual(response.status_code, 200) - self.assertTemplateUsed(response, "task.html") - self.assertContains(response, "Add Task") + self.assertTemplateUsed(response, 'index.html') + self.assertContains(response, self.todo1.text) + self.assertContains(response, self.todo2.text) - def test_add_task_view_post_valid_data(self): - """ - Test that submitting the form with valid data creates a new task. - """ - url = reverse("add_task") - data = { - "title": "Test Task", - "description": "This is a test task description.", - "status": "Pending", - "deadline": "2024-12-31", - } - response = self.client.post(url, data) - - # Check that the task was added and the user is redirected - self.assertEqual(response.status_code, 302) - self.assertTrue(Task.objects.filter(title="Test Task").exists()) - - def test_add_task_view_post_invalid_data(self): - """ - Test that submitting the form with invalid data shows errors. - """ - url = reverse("add_task") - data = { - "title": "", - "description": "No title task", - "status": "Pending", - "deadline": "2024-12-31", - } # Invalid title - response = self.client.post(url, data) - messages = list(get_messages(response.wsgi_request)) - - # Ensure that the form is not saved and the page reloads with errors + def test_list_todos_view(self): + response = self.client.post(reverse('list_todos', args=['all'])) self.assertEqual(response.status_code, 200) - self.assertEqual(str(messages[0]), "Please Add Required Fields!") - - def test_edit_task_view_post_valid_data(self): - """ - Test that editing a task updates its details. - """ - task = Task.objects.create(title="Old Task", description="Old description") - url = reverse("edit_task", args=[task.id]) - data = { - "title": "Updated Task", - "description": "Updated description", - "status": "Completed", - "deadline": "2024-12-31", - } - - response = self.client.post(url, data) + self.assertContains(response, self.todo1.text) + self.assertContains(response, self.todo2.text) - # Verify that the task was updated and the user is redirected - self.assertEqual(response.status_code, 302) - task.refresh_from_db() - self.assertEqual(task.title, "Updated Task") - self.assertEqual(task.description, "Updated description") + response = self.client.post(reverse('list_todos', args=['active'])) + self.assertEqual(response.status_code, 200) + self.assertContains(response, self.todo1.text) + self.assertNotContains(response, self.todo2.text) - def test_delete_task_view(self): - """ - Test that deleting a task works and redirects to the task list. - """ - task = Task.objects.create( - title="Task to delete", description="This task will be deleted" - ) - url = reverse("delete_task", args=[task.id]) + response = self.client.post(reverse('list_todos', args=['completed'])) + self.assertEqual(response.status_code, 200) + self.assertNotContains(response, self.todo1.text) + self.assertContains(response, self.todo2.text) - response = self.client.post(url) + def test_add_todo_view(self): + response = self.client.post(reverse('add', args=['all']), data="New Todo", content_type='text/plain') + self.assertEqual(response.status_code, 200) + self.assertContains(response, "New Todo") + self.assertEqual(Todo.objects.count(), 3) + todos = Todo.objects.all() + self.assertEqual(todos.last().text, "New Todo") - # Ensure the task is deleted and the user is redirected - self.assertEqual(response.status_code, 302) - self.assertFalse(Task.objects.filter(title="Task to delete").exists()) + def test_delete_todo_view(self): + response = self.client.post(reverse('delete', args=['all', self.todo1.id-1])) + self.assertEqual(response.status_code, 200) + self.assertNotContains(response, self.todo1.text) + self.assertEqual(Todo.objects.count(), 1) - def test_url_resolves_to_correct_view(self): - """ - Test that the correct view function is mapped to each URL. - """ - # Mapping URLs to their respective views - url_resolutions = { - reverse("index"): index, - reverse("add_task"): add_task, - reverse("edit_task", args=[1]): edit_task, - reverse("delete_task", args=[1]): delete_task, - } + def test_toggle_todo_view(self): + response = self.client.post(reverse('toggle', args=['all', self.todo1.id-1])) - for url, view in url_resolutions.items(): - with self.subTest(url=url): - resolved = resolve(url) - self.assertEqual(resolved.func, view) + self.assertEqual(response.status_code, 200) + self.todo1.refresh_from_db() + self.assertTrue(self.todo1.checked) - def test_add_task_message(self): - """ - Test that a success message appears when a task is successfully added. - """ - url = reverse("add_task") - data = { - "title": "Complete Assignment", - "description": "Finish Django tutorial", - "status": "Pending", - "deadline": "2024-12-31", - } - response = self.client.post(url, data) + def test_clear_completed_view(self): + response = self.client.post(reverse('clear_completed', args=['all'])) + self.assertEqual(response.status_code, 200) + self.assertEqual(Todo.objects.filter(checked=True).count(), 0) - # Check if the success message appears in the response - messages = list(get_messages(response.wsgi_request)) - self.assertEqual(str(messages[0]), "Task added successfully!") + def test_toggle_all_view(self): + response = self.client.post(reverse('toggle_all', args=['all'])) + self.assertEqual(response.status_code, 200) + self.assertEqual(Todo.objects.filter(checked=True).count(), 2) - def test_delete_task_message(self): - """ - Test that a success message appears when a task is deleted. - """ - task = Task.objects.create( - title="Task to delete", description="Delete this task" - ) - url = reverse("delete_task", args=[task.id]) - response = self.client.post(url) + response = self.client.post(reverse('toggle_all', args=['all'])) + self.assertEqual(response.status_code, 200) + self.assertEqual(Todo.objects.filter(checked=True).count(), 0) - # Check if the success message appears - messages = list(get_messages(response.wsgi_request)) - self.assertEqual(str(messages[0]), "Task deleted successfully!") + def test_edit_todo_view(self): + response = self.client.post(reverse('edit', args=['all', self.todo1.id-1]), data="Updated Todo", content_type='text/plain') + self.assertEqual(response.status_code, 200) + self.todo1.refresh_from_db() + self.assertEqual(self.todo1.text, "Updated Todo") \ No newline at end of file diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/urls.py b/example/pythonlib/web/4-todo-django/todo/src/main/urls.py index d0e2256c95d..b3dd51f6590 100644 --- a/example/pythonlib/web/4-todo-django/todo/src/main/urls.py +++ b/example/pythonlib/web/4-todo-django/todo/src/main/urls.py @@ -2,8 +2,12 @@ from . import views urlpatterns = [ - path("", views.index, name="index"), - path("add/", views.add_task, name="add_task"), - path("edit//", views.edit_task, name="edit_task"), - path("delete//", views.delete_task, name="delete_task"), -] + path('', views.index, name='index'), + path('list//', views.list_todos, name='list_todos'), + path('add//', views.add, name='add'), + path('delete///', views.delete, name='delete'), + path('toggle///', views.toggle, name='toggle'), + path('clear-completed//', views.clear_completed, name='clear_completed'), + path('toggle-all//', views.toggle_all, name='toggle_all'), + path('edit///', views.edit, name='edit'), +] \ No newline at end of file diff --git a/example/pythonlib/web/4-todo-django/todo/src/main/views.py b/example/pythonlib/web/4-todo-django/todo/src/main/views.py index ba1b4e4bdca..652489c7819 100644 --- a/example/pythonlib/web/4-todo-django/todo/src/main/views.py +++ b/example/pythonlib/web/4-todo-django/todo/src/main/views.py @@ -1,45 +1,69 @@ -from django.shortcuts import render, redirect, get_object_or_404 -from django.contrib import messages -from .models import Task -from .forms import TaskForm - +from django.shortcuts import render +from .models import Todo def index(request): - tasks = Task.objects.all() - return render(request, "index.html", {"tasks": tasks}) - - -def add_task(request): - if request.method == "POST": - form = TaskForm(request.POST) - if form.is_valid(): - form.save() - messages.success(request, "Task added successfully!") - return redirect("index") - else: - # If the form is not valid, pass the form back to the template with errors - messages.warning(request, "Please Add Required Fields!") - return render(request, "task.html", {"form": form, "title": "Add Task"}) - else: - form = TaskForm() - return render(request, "task.html", {"form": form, "title": "Add Task"}) - - -def edit_task(request, task_id): - task = get_object_or_404(Task, id=task_id) - if request.method == "POST": - form = TaskForm(request.POST, instance=task) - if form.is_valid(): - form.save() - messages.success(request, "Task updated successfully!") - return redirect("index") - else: - form = TaskForm(instance=task) - return render(request, "task.html", {"form": form, "title": "Edit Task"}) - - -def delete_task(request, task_id): - task = get_object_or_404(Task, id=task_id) - task.delete() - messages.success(request, "Task deleted successfully!") - return redirect("index") + todos = Todo.objects.all() + context = get_todo_context(todos, 'all') + return render(request, 'base.html', context) + +def list_todos(request, state): + todos = get_filtered_todos(state) + context = get_todo_context(todos, state) + return render(request, 'index.html', context) + +def add(request, state): + if request.method == 'POST': + Todo.objects.create(text=request.body.decode('utf-8'), checked=False) + return list_todos(request, state) + +def delete(request, state, index): + todos = get_filtered_todos(state) + todo = todos[int(index)] + todo.delete() + return list_todos(request, state) + +def toggle(request, state, index): + todos = get_filtered_todos(state) + todo = todos[int(index)] + todo.checked = not todo.checked + todo.save() + return list_todos(request, state) + +def clear_completed(request, state): + Todo.objects.filter(checked=True).delete() + return list_todos(request, state) + +def toggle_all(request, state): + todos = Todo.objects.all() + next_state = not all(todo.checked for todo in todos) + todos.update(checked=next_state) + return list_todos(request, state) + +def edit(request, state, index): + if request.method == 'POST': + todos = get_filtered_todos(state) + todo = todos[int(index)] + todo.text = request.body.decode('utf-8') + todo.save() + return list_todos(request, state) + +def get_filtered_todos(state): + if state == 'active': + return Todo.objects.filter(checked=False) + elif state == 'completed': + return Todo.objects.filter(checked=True) + return Todo.objects.all() + +def get_todo_context(todos, state): + total_count = todos.count() + active_count = todos.filter(checked=False).count() + completed_count = todos.filter(checked=True).count() + has_completed = completed_count > 0 + return { + 'todos': todos, + 'state': state, + 'total_count': total_count, + 'active_count': active_count, + 'completed_count': completed_count, + 'has_completed': has_completed, + } \ No newline at end of file From e033073b4080094955a2aba80ce9af9e75786c16 Mon Sep 17 00:00:00 2001 From: himanshumahajan138 Date: Mon, 23 Dec 2024 10:22:11 +0530 Subject: [PATCH 10/10] Final Updates --- docs/modules/ROOT/pages/pythonlib/web-examples.adoc | 6 +++--- example/pythonlib/web/1-hello-flask/build.mill | 6 ++++-- example/pythonlib/web/2-todo-flask/build.mill | 6 ++++-- example/pythonlib/web/3-hello-django/build.mill | 8 ++++---- example/pythonlib/web/4-todo-django/build.mill | 9 +++++---- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/docs/modules/ROOT/pages/pythonlib/web-examples.adoc b/docs/modules/ROOT/pages/pythonlib/web-examples.adoc index e24e5034234..5118e2a3220 100644 --- a/docs/modules/ROOT/pages/pythonlib/web-examples.adoc +++ b/docs/modules/ROOT/pages/pythonlib/web-examples.adoc @@ -5,14 +5,14 @@ include::partial$gtag-config.adoc[] This page provides examples of using Mill as a build tool for Python web applications. It includes setting up a basic "Hello, World!" application and developing a fully -functional Todo-MVC app with Flask and Django, showcasing best practices +functional https://todomvc.com/[TodoMVC] app with Flask and Django, showcasing best practices for project organization, scalability, and maintainability. == Flask Hello World App include::partial$example/pythonlib/web/1-hello-flask.adoc[] -== Flask TodoMvc App +== Flask TodoMVC App include::partial$example/pythonlib/web/2-todo-flask.adoc[] @@ -20,6 +20,6 @@ include::partial$example/pythonlib/web/2-todo-flask.adoc[] include::partial$example/pythonlib/web/3-hello-django.adoc[] -== Django TodoMvc App +== Django TodoMVC App include::partial$example/pythonlib/web/4-todo-django.adoc[] \ No newline at end of file diff --git a/example/pythonlib/web/1-hello-flask/build.mill b/example/pythonlib/web/1-hello-flask/build.mill index 0626f1f5a0e..661c36a9b70 100644 --- a/example/pythonlib/web/1-hello-flask/build.mill +++ b/example/pythonlib/web/1-hello-flask/build.mill @@ -1,4 +1,4 @@ -// This example uses Mill to manage a Flask app that serves "Hello, Mill!" +// This example uses Mill to manage a Flask app that serves `"Hello, Mill!"` // at the root URL (`/`), with Flask installed as a dependency // and tests enabled using `unittest`. package build @@ -6,7 +6,7 @@ import mill._, pythonlib._ object foo extends PythonModule { - def mainScript = Task.Source { millSourcePath / "src" / "foo.py" } + def mainScript = Task.Source { millSourcePath / "src/foo.py" } def pythonDeps = Seq("flask==3.1.0") @@ -16,6 +16,8 @@ object foo extends PythonModule { // Running these commands will test and run the Flask server with desired outputs. +// The app is ready to serve at `http://localhost:5000`. + /** Usage > ./mill foo.test diff --git a/example/pythonlib/web/2-todo-flask/build.mill b/example/pythonlib/web/2-todo-flask/build.mill index 05b50888ecc..ac903686234 100644 --- a/example/pythonlib/web/2-todo-flask/build.mill +++ b/example/pythonlib/web/2-todo-flask/build.mill @@ -1,4 +1,4 @@ -// This is a `Flask`-based `TodoMVC` application managed and built using `Mill`. +// This is a `Flask`-based https://todomvc.com/[TodoMVC] application managed and built using `Mill`. // It allows users to `add`, `edit`, `delete`, and `view` tasks stored in a Python Data Structure. // Tasks can be filtered as `all`, `active`, or `completed` based on their state. // The application demonstrates dynamic rendering using Flask's routing and templating features. @@ -7,7 +7,7 @@ import mill._, pythonlib._ object todo extends PythonModule { - def mainScript = Task.Source { millSourcePath / "src" / "app.py" } + def mainScript = Task.Source { millSourcePath / "src/app.py" } def pythonDeps = Seq("flask==3.1.0", "Flask-SQLAlchemy==3.1.1", "Flask-WTF==1.2.2") @@ -27,6 +27,8 @@ object todo extends PythonModule { // This example also utilizes **Mill** for managing `dependencies`, `builds`, and `tests`, // offering an efficient development workflow. +// The app is ready to serve at `http://localhost:5001`. + /** Usage > ./mill todo.test diff --git a/example/pythonlib/web/3-hello-django/build.mill b/example/pythonlib/web/3-hello-django/build.mill index e76366855fa..cd85cfc7afa 100644 --- a/example/pythonlib/web/3-hello-django/build.mill +++ b/example/pythonlib/web/3-hello-django/build.mill @@ -6,13 +6,13 @@ import mill._, pythonlib._ object foo extends PythonModule { - def mainScript = Task.Source { millSourcePath / "src" / "manage.py" } + def mainScript = Task.Source { millSourcePath / "src/manage.py" } def pythonDeps = Seq("django==5.1.4") } // Using `Mill`, we can easily manage dependencies, run `Django tests`, and `launch the server`. -// With just a few commands, the app is ready to serve at `http://localhost:8000`. +// With just a few commands, the app is ready to serve at `http://localhost:5002`. /** Usage @@ -26,9 +26,9 @@ Ran 1 test... OK ... -> ./mill foo.runBackground runserver 8000 +> ./mill foo.runBackground runserver 5002 -> curl http://localhost:8000 +> curl http://localhost:5002 ...

Hello, Mill!

... > ./mill clean foo.runBackground diff --git a/example/pythonlib/web/4-todo-django/build.mill b/example/pythonlib/web/4-todo-django/build.mill index a5c915cea96..b992704855f 100644 --- a/example/pythonlib/web/4-todo-django/build.mill +++ b/example/pythonlib/web/4-todo-django/build.mill @@ -1,4 +1,4 @@ -// This `Django TodoMVC` example is a task management application built using `Mill` Build Tool. +// This `Django` https://todomvc.com/[TodoMVC] example is a task management application built using `Mill` Build Tool. // It features dynamic `HTML` rendering with `Django's` template engine, `CRUD` operations with // Django `ORM`, and efficient form handling for managing tasks. package build @@ -6,7 +6,7 @@ import mill._, pythonlib._ object todo extends PythonModule { - def mainScript = Task.Source { millSourcePath / "src" / "manage.py" } + def mainScript = Task.Source { millSourcePath / "src/manage.py" } def pythonDeps = Seq("django==5.1.4") @@ -24,6 +24,7 @@ object todo extends PythonModule { // This Mill Build File simplifies `dependency management`, `database migrations`, and `testing workflows`. // Running these commands will `test` and `run` the `Django Todo App`. +// The app is ready to serve at `http://localhost:5003`. /** Usage @@ -50,9 +51,9 @@ test_toggle_todo_view (main.tests.TodoAppTests...) ... ok ...Ran 8 tests... ...OK... -> ./mill todo.runBackground runserver 8001 +> ./mill todo.runBackground runserver 5003 -> curl http://localhost:8001 +> curl http://localhost:5003 ...What needs to be done... > ./mill clean todo.runBackground