mirror of
https://github.com/Lyxminxx/jast.git
synced 2026-10-11 20:57:43 +02:00
Ready for production deployment
This commit is contained in:
commit
e06c56e76e
23 files changed
+664
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,12 @@
|
||||
from django.contrib import admin
|
||||
from .models import Transaction, Category
|
||||
|
||||
admin.site.register(Category)
|
||||
|
||||
@admin.register(Transaction)
|
||||
class TransactionAdmin(admin.ModelAdmin):
|
||||
list_display = ('date', 'title', 'amount', 'category')
|
||||
|
||||
list_filter = ('category', 'date')
|
||||
|
||||
search_fields = ('title',)
|
||||
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
ASGI config for core 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.1/howto/deployment/asgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.asgi import get_asgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
|
||||
|
||||
application = get_asgi_application()
|
||||
@@ -0,0 +1,11 @@
|
||||
from django import forms
|
||||
from .models import Transaction
|
||||
|
||||
class TransactionForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Transaction
|
||||
fields = ['title', 'amount', 'category', 'date']
|
||||
# This widget forces the browser to show a date picker calendar
|
||||
widgets = {
|
||||
'date': forms.DateInput(attrs={'type': 'date'}),
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
# Generated by Django 6.1.1 on 2026-09-19 22:04
|
||||
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Category',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=255)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Transaction',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('title', models.CharField(max_length=255)),
|
||||
('amount', models.DecimalField(decimal_places=2, max_digits=10)),
|
||||
('date', models.DateField(default=django.utils.timezone.now)),
|
||||
('category', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='core.category')),
|
||||
],
|
||||
options={
|
||||
'ordering': ['-date'],
|
||||
},
|
||||
),
|
||||
]
|
||||
Whitespace-only changes.
@@ -0,0 +1,35 @@
|
||||
from django.db import (
|
||||
models,
|
||||
)
|
||||
from django.utils import(
|
||||
timezone
|
||||
)
|
||||
|
||||
class Transaction(models.Model):
|
||||
title = models.CharField(max_length=255)
|
||||
|
||||
amount = models.DecimalField(max_digits=10, decimal_places=2)
|
||||
|
||||
date = models.DateField(default=timezone.now)
|
||||
|
||||
category = models.ForeignKey('Category', on_delete=models.SET_NULL, null=True, blank=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ['-date']
|
||||
|
||||
@property
|
||||
def is_income(self):
|
||||
return self.amount > 0
|
||||
|
||||
@property
|
||||
def is_expense(self):
|
||||
return self.amount < 0
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.date} | {self.title}: ${self.amount}"
|
||||
|
||||
class Category(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
@@ -0,0 +1,130 @@
|
||||
"""
|
||||
Django settings for core project.
|
||||
|
||||
Generated by 'django-admin startproject' using Django 6.1.1.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/6.1/topics/settings/
|
||||
|
||||
For the full list of settings and their values, see
|
||||
https://docs.djangoproject.com/en/6.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/6.1/howto/deployment/checklist/
|
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = 'django-insecure-m#==%hz74d192o)4!#_!dpz%$yox@!hkabc9)pggkkiuvc7((q'
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = False
|
||||
|
||||
ALLOWED_HOSTS = []
|
||||
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'core',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
'django.middleware.security.SecurityMiddleware',
|
||||
'whitenoise.middleware.WhiteNoiseMiddleware',
|
||||
'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 = 'core.urls'
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||
'DIRS': [BASE_DIR / 'core' / 'templates'],
|
||||
'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 = 'core.wsgi.application'
|
||||
|
||||
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/6.1/ref/settings/#databases
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': BASE_DIR / 'data' / 'db.sqlite3',
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/6.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/6.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/6.1/howto/static-files/
|
||||
|
||||
STATIC_URL = 'static/'
|
||||
|
||||
|
||||
# Email
|
||||
# https://docs.djangoproject.com/en/6.1/topics/email/#topic-email-configuration
|
||||
|
||||
MAILERS = {
|
||||
'default': {
|
||||
'BACKEND': 'django.core.mail.backends.console.EmailBackend',
|
||||
},
|
||||
}
|
||||
STATIC_ROOT = BASE_DIR / "staticfiles"
|
||||
@@ -0,0 +1,23 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Edit Transaction</title>
|
||||
<style>
|
||||
body { font-family: system-ui, sans-serif; max-width: 800px; margin: 40px auto; padding: 20px; }
|
||||
.card { background: #f4f4f5; padding: 20px; border-radius: 8px; }
|
||||
button { background: #3b82f6; color: white; padding: 8px 16px; border: none; border-radius: 4px; cursor: pointer; }
|
||||
.cancel { background: #e4e4e7; color: black; text-decoration: none; padding: 8px 16px; border-radius: 4px; display: inline-block; margin-left: 10px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h2>Editing: {{ transaction.title }}</h2>
|
||||
<div class="card">
|
||||
<form method="POST">
|
||||
{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
<button type="submit">Save Changes</button>
|
||||
<a href="{% url 'home' %}" class="cancel">Cancel</a>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,61 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>DIME Tracker</title>
|
||||
<style>
|
||||
body { font-family: system-ui, sans-serif; max-width: 800px; margin: 40px auto; padding: 20px; }
|
||||
.card { background: #f4f4f5; padding: 20px; border-radius: 8px; margin-bottom: 30px; }
|
||||
table { width: 100%; border-collapse: collapse; }
|
||||
th, td { padding: 12px; border-bottom: 1px solid #e4e4e7; text-align: left; }
|
||||
.income { color: #16a34a; font-weight: bold; }
|
||||
.expense { color: #dc2626; }
|
||||
button { background: #18181b; color: white; padding: 8px 16px; border: none; border-radius: 4px; cursor: pointer; }
|
||||
.btn-edit { background: #3b82f6; color: white; text-decoration: none; padding: 6px 12px; border-radius: 4px; font-size: 14px; margin-right: 5px; }
|
||||
.btn-delete { background: #ef4444; color: white; padding: 6px 12px; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Just another spending tracker</h1>
|
||||
<div style="margin-bottom: 20px;">
|
||||
<h2>Total Balance: {{ total_balance|floatformat:2 }} kr</h2>
|
||||
<a href="{% url 'stats' %}" style="background: #e4e4e7; color: black; text-decoration: none; padding: 8px 16px; border-radius: 4px; font-weight: bold;">View Statistics 📊</a>
|
||||
</div>
|
||||
<div class="card">
|
||||
<h3>Add Transaction</h3>
|
||||
<form method="POST">
|
||||
{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
<button type="submit">Save Transaction</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th>Title</th>
|
||||
<th>Category</th>
|
||||
<th>Amount</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
{% for t in transactions %}
|
||||
<tr>
|
||||
<td>{{ t.date }}</td>
|
||||
<td>{{ t.title }}</td>
|
||||
<td>{{ t.category.name|default:"-" }}</td>
|
||||
<td class="{% if t.is_income %}income{% else %}expense{% endif %}">
|
||||
{{ t.amount }} kr
|
||||
</td>
|
||||
<td>
|
||||
<a href="{% url 'edit_transaction' t.pk %}" class="btn-edit">Edit</a>
|
||||
<form action="{% url 'delete_transaction' t.pk %}" method="POST" style="display:inline;" onsubmit="return confirm('Are you sure you want to delete this transaction?');">
|
||||
{% csrf_token %}
|
||||
<button type="submit" class="btn-delete">Delete</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{% empty %}
|
||||
<tr><td colspan="5">No transactions yet. Start spending!</td></tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,68 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>DIME Statistics</title>
|
||||
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||
<style>
|
||||
body { font-family: system-ui, sans-serif; max-width: 800px; margin: 40px auto; padding: 20px; }
|
||||
.nav { margin-bottom: 20px; }
|
||||
.nav a { background: #e4e4e7; color: black; text-decoration: none; padding: 8px 16px; border-radius: 4px; font-weight: bold; }
|
||||
.filters { margin-bottom: 30px; display: flex; gap: 10px; }
|
||||
.filters a { padding: 6px 12px; border-radius: 4px; text-decoration: none; border: 1px solid #d4d4d8; color: black; }
|
||||
.filters a.active { background: #18181b; color: white; border-color: #18181b; }
|
||||
.chart-container { position: relative; height: 400px; width: 100%; margin-top: 20px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="nav">
|
||||
<a href="{% url 'home' %}">← Back to Tracker</a>
|
||||
</div>
|
||||
|
||||
<h1>Spending Statistics</h1>
|
||||
|
||||
<div class="filters">
|
||||
<a href="?filter=week" class="{% if current_filter == 'week' %}active{% endif %}">Last 7 Days</a>
|
||||
<a href="?filter=month" class="{% if current_filter == 'month' %}active{% endif %}">Last 30 Days</a>
|
||||
<a href="?filter=year" class="{% if current_filter == 'year' %}active{% endif %}">Last 365 Days</a>
|
||||
<a href="?filter=all" class="{% if current_filter == 'all' %}active{% endif %}">All Time</a>
|
||||
</div>
|
||||
|
||||
<div class="chart-container">
|
||||
<canvas id="categoryChart"></canvas>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Parse the JSON data passed from Django
|
||||
const labels = JSON.parse('{{ labels|safe }}');
|
||||
const data = JSON.parse('{{ data|safe }}');
|
||||
|
||||
const ctx = document.getElementById('categoryChart').getContext('2d');
|
||||
|
||||
if (data.length === 0) {
|
||||
ctx.font = "16px system-ui";
|
||||
ctx.fillText("No expenses logged for this time period.", 10, 50);
|
||||
} else {
|
||||
new Chart(ctx, {
|
||||
type: 'doughnut',
|
||||
data: {
|
||||
labels: labels,
|
||||
datasets: [{
|
||||
label: 'Amount Spent (kr)',
|
||||
data: data,
|
||||
// A simple color palette
|
||||
backgroundColor: ['#ef4444', '#3b82f6', '#10b981', '#f59e0b', '#8b5cf6', '#ec4899', '#14b8a6'],
|
||||
borderWidth: 1
|
||||
}]
|
||||
},
|
||||
options: {
|
||||
responsive: true,
|
||||
maintainAspectRatio: false,
|
||||
plugins: {
|
||||
legend: { position: 'right' }
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,11 @@
|
||||
from django.contrib import admin
|
||||
from django.urls import path
|
||||
from core import views
|
||||
|
||||
urlpatterns = [
|
||||
path('admin/', admin.site.urls),
|
||||
path('', views.home, name='home'),
|
||||
path('edit/<int:pk>/', views.edit_transaction, name='edit_transaction'),
|
||||
path('delete/<int:pk>/', views.delete_transaction, name='delete_transaction'),
|
||||
path('stats/', views.stats, name='stats'), # Add this line
|
||||
]
|
||||
@@ -0,0 +1,89 @@
|
||||
from django.shortcuts import render, redirect, get_object_or_404
|
||||
from django.db.models import Sum
|
||||
from .models import Transaction
|
||||
from .forms import TransactionForm
|
||||
import json
|
||||
from datetime import timedelta
|
||||
from django.utils import timezone
|
||||
from django.db.models import Sum
|
||||
|
||||
|
||||
def home(request):
|
||||
# 1. Handle form submission
|
||||
if request.method == 'POST':
|
||||
form = TransactionForm(request.POST)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
return redirect('home') # Refresh the page
|
||||
else:
|
||||
form = TransactionForm()
|
||||
|
||||
# 2. Fetch data for the table
|
||||
transactions = Transaction.objects.all()
|
||||
|
||||
# Calculate total balance (returns 0 if database is empty)
|
||||
total_balance = transactions.aggregate(Sum('amount'))['amount__sum'] or 0
|
||||
|
||||
# 3. Send data to the template
|
||||
context = {
|
||||
'form': form,
|
||||
'transactions': transactions,
|
||||
'total_balance': total_balance,
|
||||
}
|
||||
return render(request, 'home.html', context)
|
||||
|
||||
def edit_transaction(request, pk):
|
||||
transaction = get_object_or_404(Transaction, pk=pk)
|
||||
|
||||
if request.method == 'POST':
|
||||
form = TransactionForm(request.POST, instance=transaction)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
return redirect('home')
|
||||
else:
|
||||
form = TransactionForm(instance=transaction)
|
||||
|
||||
return render(request, 'edit.html', {'form': form, 'transaction': transaction})
|
||||
|
||||
def delete_transaction(request, pk):
|
||||
transaction = get_object_or_404(Transaction, pk=pk)
|
||||
if request.method == 'POST':
|
||||
transaction.delete()
|
||||
return redirect('home')
|
||||
|
||||
def stats(request):
|
||||
# 1. Figure out what time period we are looking at
|
||||
time_filter = request.GET.get('filter', 'all')
|
||||
now = timezone.now().date()
|
||||
|
||||
# Start with only expenses (numbers less than 0)
|
||||
expenses = Transaction.objects.filter(amount__lt=0)
|
||||
|
||||
# 2. Filter by date based on the button clicked
|
||||
if time_filter == 'week':
|
||||
expenses = expenses.filter(date__gte=now - timedelta(days=7))
|
||||
elif time_filter == 'month':
|
||||
expenses = expenses.filter(date__gte=now - timedelta(days=30))
|
||||
elif time_filter == 'year':
|
||||
expenses = expenses.filter(date__gte=now - timedelta(days=365))
|
||||
|
||||
# 3. Group by category and sum the amounts
|
||||
category_data = expenses.values('category__name').annotate(total=Sum('amount'))
|
||||
|
||||
# 4. Format data for Chart.js
|
||||
labels = []
|
||||
data = []
|
||||
|
||||
for item in category_data:
|
||||
# If category is null, call it 'Uncategorized'
|
||||
cat_name = item['category__name'] or 'Uncategorized'
|
||||
labels.append(cat_name)
|
||||
# Make the number positive for the chart (so it draws correctly)
|
||||
data.append(abs(float(item['total'])))
|
||||
|
||||
context = {
|
||||
'labels': json.dumps(labels),
|
||||
'data': json.dumps(data),
|
||||
'current_filter': time_filter,
|
||||
}
|
||||
return render(request, 'stats.html', context)
|
||||
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
WSGI config for core 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.1/howto/deployment/wsgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
|
||||
|
||||
application = get_wsgi_application()
|
||||
Reference in new issue
Block a user