Noel-Teens commited on
Commit
c558cde
·
1 Parent(s): a82fca4
.gitignore ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python cache
2
+ __pycache__/
3
+ *.py[cod]
4
+
5
+ # Virtual environment
6
+ env/
7
+ venv/
8
+
9
+ # Django
10
+ *.sqlite3
11
+ *.log
12
+ /staticfiles/
13
+ /media/
14
+
15
+ # Environment variables
16
+ .env
17
+
18
+ # VSCode or PyCharm
19
+ .vscode/
20
+ .idea/
Dockerfile ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use Python 3.10 slim image
2
+ FROM python:3.10-slim
3
+
4
+ # Create a non-root user
5
+ RUN useradd -m -u 1000 user
6
+
7
+ # Set environment path and working directory
8
+ ENV PATH="/home/user/.local/bin:$PATH"
9
+ WORKDIR /app
10
+
11
+ # Copy and install requirements
12
+ COPY --chown=user requirements.txt requirements.txt
13
+
14
+ RUN pip install --no-cache-dir --upgrade pip \
15
+ && pip install --no-cache-dir --upgrade -r requirements.txt
16
+
17
+ # Debug step: search for deprecated MutableSet usage
18
+ RUN grep -r "MutableSet" /usr/local/lib/python*/site-packages || echo "No MutableSet found"
19
+
20
+
21
+
22
+ # Copy all app code
23
+ COPY --chown=user . /app
24
+
25
+ RUN python manage.py makemigrations
26
+ RUN python manage.py migrate
27
+ # Switch to non-root user
28
+ USER user
29
+
30
+ # Optional: drop into a bash shell for exploration (instead of running Django immediately)
31
+ # CMD ["/bin/bash"]
32
+ CMD ["python", "manage.py", "runserver", "0.0.0.0:7860"]
add_sub/__init__.py ADDED
File without changes
add_sub/asgi.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ASGI config for add_sub project.
3
+
4
+ It exposes the ASGI callable as a module-level variable named ``application``.
5
+
6
+ For more information on this file, see
7
+ https://docs.djangoproject.com/en/5.2/howto/deployment/asgi/
8
+ """
9
+
10
+ import os
11
+
12
+ from django.core.asgi import get_asgi_application
13
+
14
+ os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'add_sub.settings')
15
+
16
+ application = get_asgi_application()
add_sub/settings.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Django settings for add_sub project.
3
+
4
+ Generated by 'django-admin startproject' using Django 5.2.
5
+
6
+ For more information on this file, see
7
+ https://docs.djangoproject.com/en/5.2/topics/settings/
8
+
9
+ For the full list of settings and their values, see
10
+ https://docs.djangoproject.com/en/5.2/ref/settings/
11
+ """
12
+
13
+ from pathlib import Path
14
+ import os
15
+
16
+ # Build paths inside the project like this: BASE_DIR / 'subdir'.
17
+ BASE_DIR = Path(__file__).resolve().parent.parent
18
+
19
+
20
+ # Quick-start development settings - unsuitable for production
21
+ # See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/
22
+
23
+ # SECURITY WARNING: keep the secret key used in production secret!
24
+ SECRET_KEY = 'django-insecure-=8%wnm033*tte(g%h%^&b=u(r!vde#@dbc^tadmt!d&zvuiz%r'
25
+
26
+ # SECURITY WARNING: don't run with debug turned on in production!
27
+ DEBUG = True
28
+
29
+ ALLOWED_HOSTS = ['*']
30
+
31
+
32
+ # Application definition
33
+
34
+ INSTALLED_APPS = [
35
+ 'django.contrib.admin',
36
+ 'django.contrib.auth',
37
+ 'django.contrib.contenttypes',
38
+ 'django.contrib.sessions',
39
+ 'django.contrib.messages',
40
+ 'django.contrib.staticfiles',
41
+ 'myapp',
42
+ ]
43
+
44
+ MIDDLEWARE = [
45
+ 'django.middleware.security.SecurityMiddleware',
46
+ 'django.contrib.sessions.middleware.SessionMiddleware',
47
+ 'django.middleware.common.CommonMiddleware',
48
+ 'django.middleware.csrf.CsrfViewMiddleware',
49
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
50
+ 'django.contrib.messages.middleware.MessageMiddleware',
51
+ 'django.middleware.clickjacking.XFrameOptionsMiddleware',
52
+ ]
53
+
54
+ ROOT_URLCONF = 'add_sub.urls'
55
+
56
+ TEMPLATES = [
57
+ {
58
+ 'BACKEND': 'django.template.backends.django.DjangoTemplates',
59
+ 'DIRS': [],
60
+ 'APP_DIRS': True,
61
+ 'OPTIONS': {
62
+ 'context_processors': [
63
+ 'django.template.context_processors.request',
64
+ 'django.contrib.auth.context_processors.auth',
65
+ 'django.contrib.messages.context_processors.messages',
66
+ ],
67
+ },
68
+ },
69
+ ]
70
+
71
+ WSGI_APPLICATION = 'add_sub.wsgi.application'
72
+
73
+
74
+ # Database
75
+ # https://docs.djangoproject.com/en/5.2/ref/settings/#databases
76
+
77
+ DATABASES = {
78
+ 'default': {
79
+ 'ENGINE': 'django.db.backends.sqlite3',
80
+ 'NAME': BASE_DIR / 'db.sqlite3',
81
+ }
82
+ }
83
+
84
+
85
+ # Password validation
86
+ # https://docs.djangoproject.com/en/5.2/ref/settings/#auth-password-validators
87
+
88
+ AUTH_PASSWORD_VALIDATORS = [
89
+ {
90
+ 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
91
+ },
92
+ {
93
+ 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
94
+ },
95
+ {
96
+ 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
97
+ },
98
+ {
99
+ 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
100
+ },
101
+ ]
102
+
103
+
104
+ # Internationalization
105
+ # https://docs.djangoproject.com/en/5.2/topics/i18n/
106
+
107
+ LANGUAGE_CODE = 'en-us'
108
+
109
+ TIME_ZONE = 'UTC'
110
+
111
+ USE_I18N = True
112
+
113
+ USE_TZ = True
114
+
115
+
116
+ # Static files (CSS, JavaScript, Images)
117
+ # https://docs.djangoproject.com/en/5.2/howto/static-files/
118
+
119
+ STATIC_URL = 'static/'
120
+
121
+ STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
122
+
123
+ # Default primary key field type
124
+ # https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
125
+
126
+ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
add_sub/urls.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ URL configuration for add_sub project.
3
+
4
+ The `urlpatterns` list routes URLs to views. For more information please see:
5
+ https://docs.djangoproject.com/en/5.2/topics/http/urls/
6
+ Examples:
7
+ Function views
8
+ 1. Add an import: from my_app import views
9
+ 2. Add a URL to urlpatterns: path('', views.home, name='home')
10
+ Class-based views
11
+ 1. Add an import: from other_app.views import Home
12
+ 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
13
+ Including another URLconf
14
+ 1. Import the include() function: from django.urls import include, path
15
+ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
16
+ """
17
+ from django.contrib import admin
18
+ from django.urls import path, include
19
+
20
+ urlpatterns = [
21
+ path('admin/', admin.site.urls),
22
+ path("", include("myapp.urls"))
23
+ ]
add_sub/wsgi.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ WSGI config for add_sub project.
3
+
4
+ It exposes the WSGI callable as a module-level variable named ``application``.
5
+
6
+ For more information on this file, see
7
+ https://docs.djangoproject.com/en/5.2/howto/deployment/wsgi/
8
+ """
9
+
10
+ import os
11
+
12
+ from django.core.wsgi import get_wsgi_application
13
+
14
+ os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'add_sub.settings')
15
+
16
+ application = get_wsgi_application()
manage.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ """Django's command-line utility for administrative tasks."""
3
+ import os
4
+ import sys
5
+
6
+
7
+ def main():
8
+ """Run administrative tasks."""
9
+ os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'add_sub.settings')
10
+ try:
11
+ from django.core.management import execute_from_command_line
12
+ except ImportError as exc:
13
+ raise ImportError(
14
+ "Couldn't import Django. Are you sure it's installed and "
15
+ "available on your PYTHONPATH environment variable? Did you "
16
+ "forget to activate a virtual environment?"
17
+ ) from exc
18
+ execute_from_command_line(sys.argv)
19
+
20
+
21
+ if __name__ == '__main__':
22
+ main()
myapp/__init__.py ADDED
File without changes
myapp/admin.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ from django.contrib import admin
2
+
3
+ # Register your models here.
myapp/apps.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ from django.apps import AppConfig
2
+
3
+
4
+ class MyappConfig(AppConfig):
5
+ default_auto_field = 'django.db.models.BigAutoField'
6
+ name = 'myapp'
myapp/migrations/0001_initial.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Generated by Django 5.2 on 2025-04-15 09:14
2
+
3
+ from django.db import migrations, models
4
+
5
+
6
+ class Migration(migrations.Migration):
7
+
8
+ initial = True
9
+
10
+ dependencies = [
11
+ ]
12
+
13
+ operations = [
14
+ migrations.CreateModel(
15
+ name='Calc',
16
+ fields=[
17
+ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18
+ ('num1', models.IntegerField()),
19
+ ('num2', models.IntegerField()),
20
+ ('operation', models.CharField(max_length=3)),
21
+ ('time', models.DateTimeField(auto_now=True)),
22
+ ],
23
+ ),
24
+ ]
myapp/migrations/__init__.py ADDED
File without changes
myapp/models.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from django.db import models
2
+
3
+ # Create your models here.
4
+
5
+ class Calc(models.Model):
6
+ num1 = models.IntegerField()
7
+ num2 = models.IntegerField()
8
+ operation = models.CharField(max_length=3)
9
+ time = models.DateTimeField(auto_now= True)
myapp/templates/add.html ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Addition</title>
7
+ </head>
8
+ <body>
9
+ <center>
10
+ <h2>Addition</h2>
11
+ <table>
12
+ <tr>
13
+ <td>Num1:</td>
14
+ <td><input type="number" id="num1Val"></td>
15
+ </tr>
16
+ <tr>
17
+ <td>Num2:</td>
18
+ <td><input type="number" id="num2Val"></td>
19
+ </tr>
20
+ <tr>
21
+ <td colspan="2"><button onclick="submitData()">Submit</button></td>
22
+ </tr>
23
+ </table>
24
+ <p id="response"></p>
25
+ </center>
26
+
27
+ <script>
28
+ function submitData() {
29
+ const num1 = document.getElementById('num1Val').value;
30
+ const num2 = document.getElementById('num2Val').value;
31
+
32
+ fetch('/add/', {
33
+ method: 'POST',
34
+ headers: {
35
+ 'Content-Type': 'application/json'
36
+ },
37
+ body: JSON.stringify({ num1: parseInt(num1), num2: parseInt(num2) })
38
+ })
39
+ .then(response => response.json())
40
+ .then(data => {
41
+ document.getElementById('response').innerText = JSON.stringify(data);
42
+ })
43
+ .catch(error => {
44
+ console.error('Error:', error);
45
+ });
46
+ }
47
+ </script>
48
+ </body>
49
+ </html>
myapp/templates/home.html ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>Welcome Home</title>
6
+ </head>
7
+ <body>
8
+ <h1>Hello, welcome to the homepage!</h1>
9
+ </body>
10
+ </html>
myapp/tests.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ from django.test import TestCase
2
+
3
+ # Create your tests here.
myapp/urls.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from django.urls import path
2
+ from . import views
3
+
4
+ urlpatterns = [
5
+ path("", views.home, name= "home"),
6
+ path("addpage/", views.add_page, name= "addpage"),
7
+ path("add/", views.add, name= "add"),
8
+ path("sub/", views.sub, name= "sub"),
9
+ ]
myapp/views.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from django.shortcuts import render
2
+ from .models import Calc
3
+ from django.http import JsonResponse
4
+ from django.views.decorators.csrf import csrf_exempt
5
+ import json
6
+
7
+
8
+ # Create your views here.
9
+
10
+ def home(request):
11
+ return render(request, "home.html")
12
+
13
+ @csrf_exempt
14
+ def add(request):
15
+ if(request.method == "POST"):
16
+ data = json.loads(request.body.decode("utf-8"))
17
+ num1 = data.get("num1")
18
+ num2 = data.get("num2")
19
+ add_obj = Calc.objects.create(
20
+ num1=num1,
21
+ num2=num2,
22
+ operation="add",
23
+ )
24
+ total = num1 + num2
25
+ return JsonResponse({"ADD": total, "Operation": add_obj.operation, "Time": add_obj.time.strftime("%Y-%m-%d %H:%M:%S")})
26
+
27
+ @csrf_exempt
28
+ def sub(request):
29
+ if(request.method == "POST"):
30
+ data = json.loads(request.body.decode("utf-8"))
31
+ num1 = data.get("num1")
32
+ num2 = data.get("num2")
33
+ add_obj = Calc.objects.create(
34
+ num1=num1,
35
+ num2=num2,
36
+ operation="sub",
37
+ )
38
+ total = num1 - num2
39
+ return JsonResponse({"SUB": total, "Operation": add_obj.operation, "Time": add_obj.time.strftime("%Y-%m-%d %H:%M:%S")})
40
+
41
+ def add_page(request):
42
+ return render(request, 'add.html')
43
+
44
+ def history(request):
45
+ data = list(Calc.objects.all().values())
46
+ return JsonResponse({"history": data, "message": "successfully retreived"})
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ Django
2
+ uvicorn
3
+ pytz
4
+ include