Compare commits

..

No commits in common. "7d0ed1dd5a3aa5599438f2006c23474939bce145" and "df2f2e4c37c7674aa44a40ab179013ee91d0b813" have entirely different histories.

6 changed files with 28 additions and 192 deletions

View file

@ -1,60 +0,0 @@
# Generated by Django 4.1.4 on 2022-12-29 18:03
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("main", "0009_alter_invoice_file_alter_invoice_transaction_and_more"),
]
operations = [
migrations.AddField(
model_name="category",
name="user",
field=models.ForeignKey(
default=1,
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
verbose_name="Utilisateur",
),
preserve_default=False,
),
migrations.AddField(
model_name="invoice",
name="user",
field=models.ForeignKey(
default=1,
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
verbose_name="Utilisateur",
),
preserve_default=False,
),
migrations.AddField(
model_name="snapshot",
name="user",
field=models.ForeignKey(
default=1,
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
verbose_name="Utilisateur",
),
preserve_default=False,
),
migrations.AddField(
model_name="transaction",
name="user",
field=models.ForeignKey(
default=1,
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
verbose_name="Utilisateur",
),
preserve_default=False,
),
]

View file

@ -1,56 +0,0 @@
# Generated by Django 4.1.4 on 2022-12-29 18:14
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("main", "0010_category_user_invoice_user_snapshot_user_and_more"),
]
operations = [
migrations.AlterField(
model_name="category",
name="user",
field=models.ForeignKey(
editable=False,
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
verbose_name="Utilisateur",
),
),
migrations.AlterField(
model_name="invoice",
name="user",
field=models.ForeignKey(
editable=False,
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
verbose_name="Utilisateur",
),
),
migrations.AlterField(
model_name="snapshot",
name="user",
field=models.ForeignKey(
editable=False,
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
verbose_name="Utilisateur",
),
),
migrations.AlterField(
model_name="transaction",
name="user",
field=models.ForeignKey(
editable=False,
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
verbose_name="Utilisateur",
),
),
]

View file

@ -1,19 +0,0 @@
# Generated by Django 4.1.4 on 2022-12-29 18:22
import datetime
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("main", "0011_alter_category_user_alter_invoice_user_and_more"),
]
operations = [
migrations.AlterField(
model_name="snapshot",
name="date",
field=models.DateField(default=datetime.date.today, verbose_name="Date"),
),
]

View file

@ -7,19 +7,9 @@ from django.db import models
from django.forms import ModelForm from django.forms import ModelForm
from django.urls import reverse from django.urls import reverse
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from django.contrib.auth.models import User
class UserModel(models.Model): class Category(models.Model):
user = models.ForeignKey(
User, on_delete=models.CASCADE, verbose_name=_("User"), editable=False
)
class Meta:
abstract = True
class Category(UserModel):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField( name = models.CharField(
max_length=64, default=_("Category"), verbose_name=_("Name") max_length=64, default=_("Category"), verbose_name=_("Name")
@ -58,7 +48,7 @@ class CategoryForm(ModelForm):
fields = ["name", "icon", "budget"] fields = ["name", "icon", "budget"]
class Transaction(UserModel): class Transaction(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField( name = models.CharField(
max_length=256, default=_("Transaction"), verbose_name=_("Name") max_length=256, default=_("Transaction"), verbose_name=_("Name")
@ -131,7 +121,7 @@ def invoice_path(instance, filename):
return pathlib.Path("invoices", str(instance.id)).with_suffix(".pdf") return pathlib.Path("invoices", str(instance.id)).with_suffix(".pdf")
class Invoice(UserModel): class Invoice(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField( name = models.CharField(
max_length=256, default=_("Invoice"), verbose_name=_("Name") max_length=256, default=_("Invoice"), verbose_name=_("Name")
@ -187,9 +177,9 @@ def snapshot_path(instance, filename):
return pathlib.Path("snapshots", str(instance.id)).with_suffix(".pdf") return pathlib.Path("snapshots", str(instance.id)).with_suffix(".pdf")
class Snapshot(UserModel): class Snapshot(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
date = models.DateField(default=date.today, verbose_name=_("Date")) date = models.DateField(default=date.today, unique=True, verbose_name=_("Date"))
value = models.DecimalField( value = models.DecimalField(
max_digits=12, decimal_places=2, default=0, verbose_name=_("Value") max_digits=12, decimal_places=2, default=0, verbose_name=_("Value")
) )

View file

@ -1 +1,2 @@
# Create your tests here. # Create your tests here.

View file

@ -35,23 +35,12 @@ class IndexView(LoginRequiredMixin, TemplateView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
return super().get_context_data(**kwargs) | { return super().get_context_data(**kwargs) | {
"transactions": Transaction.objects.filter(user=self.request.user)[:10], "transactions": Transaction.objects.all()[:10],
"categories": Category.objects.filter(user=self.request.user), "categories": Category.objects.all(),
"snapshots": Snapshot.objects.filter(user=self.request.user), "snapshots": Snapshot.objects.all(),
} }
class UserMixin(LoginRequiredMixin):
def get_queryset(self, **kwargs):
return super().get_queryset().filter(user=self.request.user)
class UserCreateView(LoginRequiredMixin, CreateView):
def form_valid(self, form):
form.instance.user = self.request.user
return super().form_valid(form)
class LoginView(auth_views.LoginView): class LoginView(auth_views.LoginView):
template_name = "main/login.html" template_name = "main/login.html"
next_page = "index" next_page = "index"
@ -61,26 +50,25 @@ class LogoutView(auth_views.LogoutView):
next_page = "login" next_page = "login"
class TransactionListView(UserMixin, ListView): class TransactionListView(LoginRequiredMixin, ListView):
paginate_by = 24 paginate_by = 24
model = Transaction model = Transaction
template_name = "main/transactions.html" template_name = "main/transactions.html"
context_object_name = "transactions" context_object_name = "transactions"
class TransactionCreateView(UserCreateView): class TransactionCreateView(LoginRequiredMixin, CreateView):
model = Transaction model = Transaction
form_class = TransactionForm form_class = TransactionForm
class InvoiceCreateView(UserCreateView): class InvoiceCreateView(LoginRequiredMixin, CreateView):
model = Invoice model = Invoice
form_class = InvoiceForm form_class = InvoiceForm
def form_valid(self, form): def form_valid(self, form):
form.instance.transaction = get_object_or_404( form.instance.transaction = get_object_or_404(
Transaction.objects.filter(user=self.request.user), Transaction, pk=self.kwargs["transaction_pk"]
pk=self.kwargs["transaction_pk"],
) )
return super().form_valid(form) return super().form_valid(form)
@ -88,22 +76,22 @@ class InvoiceCreateView(UserCreateView):
return reverse_lazy("transaction", kwargs={"pk": self.object.transaction.pk}) return reverse_lazy("transaction", kwargs={"pk": self.object.transaction.pk})
class CategoryCreateView(UserCreateView): class CategoryCreateView(LoginRequiredMixin, CreateView):
model = Category model = Category
form_class = CategoryForm form_class = CategoryForm
class SnapshotCreateView(UserCreateView): class SnapshotCreateView(LoginRequiredMixin, CreateView):
model = Snapshot model = Snapshot
form_class = SnapshotForm form_class = SnapshotForm
class TransactionUpdateView(UserMixin, UpdateView): class TransactionUpdateView(LoginRequiredMixin, UpdateView):
model = Transaction model = Transaction
form_class = TransactionForm form_class = TransactionForm
class InvoiceUpdateView(UserMixin, UpdateView): class InvoiceUpdateView(LoginRequiredMixin, UpdateView):
model = Invoice model = Invoice
form_class = InvoiceForm form_class = InvoiceForm
@ -111,34 +99,28 @@ class InvoiceUpdateView(UserMixin, UpdateView):
return reverse_lazy("transaction", kwargs={"pk": self.object.transaction.pk}) return reverse_lazy("transaction", kwargs={"pk": self.object.transaction.pk})
def get_queryset(self): def get_queryset(self):
return ( return Invoice.objects.filter(
super() transaction=get_object_or_404(Transaction, pk=self.kwargs["transaction_pk"])
.get_queryset()
.filter(
transaction=get_object_or_404(
Transaction, pk=self.kwargs["transaction_pk"]
)
)
) )
class CategoryUpdateView(UserMixin, UpdateView): class CategoryUpdateView(LoginRequiredMixin, UpdateView):
model = Category model = Category
form_class = CategoryForm form_class = CategoryForm
class SnapshotUpdateView(UserMixin, UpdateView): class SnapshotUpdateView(LoginRequiredMixin, UpdateView):
model = Snapshot model = Snapshot
form_class = SnapshotForm form_class = SnapshotForm
class TransactionDeleteView(UserMixin, DeleteView): class TransactionDeleteView(LoginRequiredMixin, DeleteView):
model = Transaction model = Transaction
template_name = "main/confirm_delete.html" template_name = "main/confirm_delete.html"
success_url = reverse_lazy("index") success_url = reverse_lazy("index")
class InvoiceDeleteView(UserMixin, DeleteView): class InvoiceDeleteView(LoginRequiredMixin, DeleteView):
model = Invoice model = Invoice
template_name = "main/confirm_delete.html" template_name = "main/confirm_delete.html"
@ -146,24 +128,24 @@ class InvoiceDeleteView(UserMixin, DeleteView):
return reverse_lazy("transaction", kwargs={"pk": self.object.transaction.pk}) return reverse_lazy("transaction", kwargs={"pk": self.object.transaction.pk})
def get_queryset(self): def get_queryset(self):
return super.get_queryset().filter( return Invoice.objects.filter(
transaction=get_object_or_404(Transaction, pk=self.kwargs["transaction_pk"]) transaction=get_object_or_404(Transaction, pk=self.kwargs["transaction_pk"])
) )
class CategoryDeleteView(UserMixin, DeleteView): class CategoryDeleteView(LoginRequiredMixin, DeleteView):
model = Category model = Category
template_name = "main/confirm_delete.html" template_name = "main/confirm_delete.html"
success_url = reverse_lazy("index") success_url = reverse_lazy("index")
class SnapshotDeleteView(UserMixin, DeleteView): class SnapshotDeleteView(LoginRequiredMixin, DeleteView):
model = Snapshot model = Snapshot
template_name = "main/confirm_delete.html" template_name = "main/confirm_delete.html"
success_url = reverse_lazy("index") success_url = reverse_lazy("index")
class SearchView(UserMixin, ListView, ProcessFormView): class SearchView(LoginRequiredMixin, ListView, ProcessFormView):
paginate_by = 24 paginate_by = 24
model = Transaction model = Transaction
template_name = "main/transactions.html" template_name = "main/transactions.html"
@ -175,9 +157,7 @@ class SearchView(UserMixin, ListView, ProcessFormView):
def get_queryset(self): def get_queryset(self):
self.search = self.kwargs["search"] self.search = self.kwargs["search"]
return ( return (
super() Transaction.objects.annotate(
.get_queryset()
.annotate(
rank=SearchRank( rank=SearchRank(
SearchVector("name", weight="A") SearchVector("name", weight="A")
+ SearchVector("description", weight="B") + SearchVector("description", weight="B")