From 964c10f4176cbbaf3ea432fd298dfdef4dd5c434 Mon Sep 17 00:00:00 2001 From: Cutty Date: Wed, 18 Mar 2026 19:49:04 -0600 Subject: [PATCH] django framework to run in docker w standalone mysql instance --- Dockerfile | 18 ++ docker-compose.yml | 42 ++++ requirements.txt | 2 + zed_workouts/manage.py | 22 ++ zed_workouts/tracker/__init__.py | 0 zed_workouts/tracker/admin.py | 3 + zed_workouts/tracker/apps.py | 5 + zed_workouts/tracker/migrations/__init__.py | 0 zed_workouts/tracker/models.py | 45 +++++ .../tracker/templates/tracker/index.html | 188 ++++++++++++++++++ zed_workouts/tracker/tests.py | 3 + zed_workouts/tracker/urls.py | 6 + zed_workouts/tracker/views.py | 53 +++++ zed_workouts/zed_workouts/__init__.py | 0 zed_workouts/zed_workouts/asgi.py | 16 ++ zed_workouts/zed_workouts/settings.py | 128 ++++++++++++ zed_workouts/zed_workouts/urls.py | 23 +++ zed_workouts/zed_workouts/wsgi.py | 16 ++ 18 files changed, 570 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 requirements.txt create mode 100644 zed_workouts/manage.py create mode 100644 zed_workouts/tracker/__init__.py create mode 100644 zed_workouts/tracker/admin.py create mode 100644 zed_workouts/tracker/apps.py create mode 100644 zed_workouts/tracker/migrations/__init__.py create mode 100644 zed_workouts/tracker/models.py create mode 100644 zed_workouts/tracker/templates/tracker/index.html create mode 100644 zed_workouts/tracker/tests.py create mode 100644 zed_workouts/tracker/urls.py create mode 100644 zed_workouts/tracker/views.py create mode 100644 zed_workouts/zed_workouts/__init__.py create mode 100644 zed_workouts/zed_workouts/asgi.py create mode 100644 zed_workouts/zed_workouts/settings.py create mode 100644 zed_workouts/zed_workouts/urls.py create mode 100644 zed_workouts/zed_workouts/wsgi.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..cce6d33 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM python:3.9-slim + +# Install system dependencies for mysqlclient +RUN apt-get update && apt-get install -y \ + pkg-config \ + default-libmysqlclient-dev \ + build-essential \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /app + +COPY requirements.txt /app/ +RUN pip install --no-cache-dir -r requirements.txt + +COPY . /app/ + +# We'll run the server via docker-compose command, but this is a default +CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f17149e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,42 @@ +version: '3.8' + +services: + db: + image: mysql:8.0 + container_name: workouts_db + restart: always + environment: + MYSQL_DATABASE: workouts + MYSQL_ROOT_PASSWORD: rootpassword + MYSQL_USER: user + MYSQL_PASSWORD: userpassword + volumes: + - db_data:/var/lib/mysql + networks: + - workout_net + + web: + build: . + container_name: workouts_web + command: > + sh -c "python manage.py migrate && + python manage.py runserver 0.0.0.0:8000" + volumes: + - .:/app + ports: + - "8000:8000" + depends_on: + - db + environment: + - DB_NAME=workouts + - DB_USER=user + - DB_PASSWORD=userpassword + - DB_HOST=db + networks: + - workout_net + +volumes: + db_data: + +networks: + workout_net: diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d595b36 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +Django>=4.2 +mysqlclient>=2.2.0 diff --git a/zed_workouts/manage.py b/zed_workouts/manage.py new file mode 100644 index 0000000..899aebf --- /dev/null +++ b/zed_workouts/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', 'zed_workouts.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/zed_workouts/tracker/__init__.py b/zed_workouts/tracker/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/zed_workouts/tracker/admin.py b/zed_workouts/tracker/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/zed_workouts/tracker/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/zed_workouts/tracker/apps.py b/zed_workouts/tracker/apps.py new file mode 100644 index 0000000..6e7871e --- /dev/null +++ b/zed_workouts/tracker/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class TrackerConfig(AppConfig): + name = 'tracker' diff --git a/zed_workouts/tracker/migrations/__init__.py b/zed_workouts/tracker/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/zed_workouts/tracker/models.py b/zed_workouts/tracker/models.py new file mode 100644 index 0000000..ce41f67 --- /dev/null +++ b/zed_workouts/tracker/models.py @@ -0,0 +1,45 @@ +from django.db import models + +class Exercise(models.Model): + GROUP_CHOICES = [ + ('Primary', 'Primary'), + ('Secondary', 'Secondary'), + ('Core', 'Core'), + ] + + exercise = models.CharField(max_length=255, unique=True) + type = models.CharField(max_length=100) # e.g., Chest, Back, Arms + group = models.CharField(max_length=20, choices=GROUP_CHOICES) + + def __str__(self): + return f"{self.exercise} ({self.group})" + +class BaseWorkoutData(models.Model): + """Abstract base to ensure identical columns for templates and history.""" + date = models.DateField(auto_now_add=True) + group = models.CharField(max_length=20) + type = models.CharField(max_length=100) + exercise = models.CharField(max_length=255) + set = models.IntegerField() + weight = models.DecimalField(max_digits=6, decimal_places=2, default=0.0) + reps = models.IntegerField(default=0) + notes = models.TextField(blank=True, null=True) + + class Meta: + abstract = True + +class Workout(BaseWorkoutData): + class Meta: + db_table = 'workouts' + +class ChestTemplate(BaseWorkoutData): + class Meta: + db_table = 'chest' + +class BackTemplate(BaseWorkoutData): + class Meta: + db_table = 'back' + +class LegsTemplate(BaseWorkoutData): + class Meta: + db_table = 'legs' diff --git a/zed_workouts/tracker/templates/tracker/index.html b/zed_workouts/tracker/templates/tracker/index.html new file mode 100644 index 0000000..33747b0 --- /dev/null +++ b/zed_workouts/tracker/templates/tracker/index.html @@ -0,0 +1,188 @@ + + + + + + Zed Workouts + + + + +
+

Workout Tracker

+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + + + +

Current Block (4 Sets)

+ + + + + + + + + + + + +
ExerciseGroupSetRepsWeightNotes
+ + + + +
+ {% csrf_token %} + + +

Completed Workout Log

+ + + + + +
ExerciseSetRepsWeightNotes
+ + +
+ + +
+
+ + + + diff --git a/zed_workouts/tracker/tests.py b/zed_workouts/tracker/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/zed_workouts/tracker/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/zed_workouts/tracker/urls.py b/zed_workouts/tracker/urls.py new file mode 100644 index 0000000..f72099b --- /dev/null +++ b/zed_workouts/tracker/urls.py @@ -0,0 +1,6 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path('', views.workout_tracker, name='workout_tracker'), +] diff --git a/zed_workouts/tracker/views.py b/zed_workouts/tracker/views.py new file mode 100644 index 0000000..113978e --- /dev/null +++ b/zed_workouts/tracker/views.py @@ -0,0 +1,53 @@ +from django.shortcuts import render, redirect +from django.db import transaction +from .models import Exercise, Workout, ChestTemplate, BackTemplate, LegsTemplate +import json +from datetime import date + +def workout_tracker(request): + template_models = { + 'Chest': ChestTemplate, + 'Back': BackTemplate, + 'Legs': LegsTemplate, + } + + if request.method == 'POST': + # Data comes in as lists from the form + selected_template = request.POST.get('template_name') + exercises = request.POST.getlist('exercise[]') + groups = request.POST.getlist('group[]') + types = request.POST.getlist('type[]') + sets = request.POST.getlist('set[]') + weights = request.POST.getlist('weight[]') + reps = request.POST.getlist('reps[]') + notes = request.POST.getlist('notes[]') + + TemplateModel = template_models.get(selected_template) + + if TemplateModel and exercises: + with transaction.atomic(): + # 1. Clear the specific Template table to replace with new data + TemplateModel.objects.all().delete() + + # 2. Iterate and save to both Workouts (History) and Template (Current) + for i in range(len(exercises)): + row_data = { + 'group': groups[i], + 'type': types[i], + 'exercise': exercises[i], + 'set': int(sets[i]), + 'weight': float(weights[i] or 0), + 'reps': int(reps[i] or 0), + 'notes': notes[i] + } + + # Save to history + Workout.objects.create(**row_data) + # Save to template (date will auto-set to today) + TemplateModel.objects.create(**row_data) + + return redirect('workout_tracker') + + # GET: Fetch exercises for dropdowns + exercises = list(Exercise.objects.values('exercise', 'type', 'group')) + return render(request, 'tracker/index.html', {'exercises_json': json.dumps(exercises)}) diff --git a/zed_workouts/zed_workouts/__init__.py b/zed_workouts/zed_workouts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/zed_workouts/zed_workouts/asgi.py b/zed_workouts/zed_workouts/asgi.py new file mode 100644 index 0000000..854b980 --- /dev/null +++ b/zed_workouts/zed_workouts/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for zed_workouts 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/6.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'zed_workouts.settings') + +application = get_asgi_application() diff --git a/zed_workouts/zed_workouts/settings.py b/zed_workouts/zed_workouts/settings.py new file mode 100644 index 0000000..8ed8b6f --- /dev/null +++ b/zed_workouts/zed_workouts/settings.py @@ -0,0 +1,128 @@ +""" +Django settings for zed_workouts project. + +Generated by 'django-admin startproject' using Django 6.0.3. + +For more information on this file, see +https://docs.djangoproject.com/en/6.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/6.0/ref/settings/ +""" + +from pathlib import Path +import os + +# 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/6.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-a%mr&&f^u06y=$!cylb-wv&w3^booxo*48hymemf$ldj=$vkb%' + +# 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', + 'tracker', +] + +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 = 'zed_workouts.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'zed_workouts.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/6.0/ref/settings/#databases + +# DATABASES = { +# 'default': { +# 'ENGINE': 'django.db.backends.sqlite3', +# 'NAME': BASE_DIR / 'db.sqlite3', +# } +#} +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.mysql', + 'NAME': os.environ.get('DB_NAME', 'workouts'), + 'USER': os.environ.get('DB_USER', 'user'), + 'PASSWORD': os.environ.get('DB_PASSWORD', 'userpassword'), + 'HOST': os.environ.get('DB_HOST', 'db'), + 'PORT': '3306', + } +} + +# Password validation +# https://docs.djangoproject.com/en/6.0/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/6.0/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/6.0/howto/static-files/ + +STATIC_URL = 'static/' diff --git a/zed_workouts/zed_workouts/urls.py b/zed_workouts/zed_workouts/urls.py new file mode 100644 index 0000000..30c6dbe --- /dev/null +++ b/zed_workouts/zed_workouts/urls.py @@ -0,0 +1,23 @@ +""" +URL configuration for zed_workouts project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/6.0/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('tracker.urls')), +] diff --git a/zed_workouts/zed_workouts/wsgi.py b/zed_workouts/zed_workouts/wsgi.py new file mode 100644 index 0000000..195b02a --- /dev/null +++ b/zed_workouts/zed_workouts/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for zed_workouts 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/6.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'zed_workouts.settings') + +application = get_wsgi_application()