Commit 8ae0f051 authored by Guilhem Saurel's avatar Guilhem Saurel
Browse files

heavier annotations on adhesion, services & banking managers

parent 7c64872a
......@@ -6,13 +6,15 @@ from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('adhesions', '0020_auto_20190227_2058'),
("adhesions", "0020_auto_20190227_2058"),
]
operations = [
migrations.AlterField(
model_name='adhesion',
name='active_legacy',
field=models.BooleanField(default=None, null=True, verbose_name='Adhésion en cours'),
model_name="adhesion",
name="active_legacy",
field=models.BooleanField(
default=None, null=True, verbose_name="Adhésion en cours"
),
),
]
from django.db import models
from django.urls import reverse
from django.contrib.auth.models import User as AuthUser
from django.utils import timezone
from django.utils.html import format_html
from django.contrib.humanize.templatetags.humanize import naturalday
from datetime import timedelta
from banking.models import RecurringPayment, PaymentUpdate, Echeance
class ActiveAdhesionManager(models.Manager):
def get_queryset(self):
qs = super().get_queryset()
qs = qs.annotate(
one_year_ago = timezone.now() - timedelta(days=366)
return qs.annotate(
last_echeance=models.Subquery(
Echeance.objects.filter(adhesion=models.OuterRef("pk")).values("date")[
:1
]
)
)
qs = qs.annotate(
),
payment_method=models.Subquery(
PaymentUpdate.objects.filter(
payment=models.OuterRef("membership__pk")
).values("payment_method")[:1]
)
)
qs = qs.annotate(
),
payment_start=models.Subquery(
PaymentUpdate.objects.filter(
payment=models.OuterRef("membership__pk")
).values("payment_start")[:1]
),
active=models.Case(
models.When(payment_method__isnull=True, then=None),
models.When(payment_method=PaymentUpdate.STOP, then=False),
default=True,
output_field=models.NullBooleanField(),
)
output_field=models.BooleanField(null=True),
),
services_long_stopped=~models.Exists(services__long_stopped=False),
long_stopped=models.Case(
models.When(services_long_stopped=False, then=False),
models.When(
payment_method=PaymentUpdate.STOP,
payment_start__lte=one_year_ago,
then=True,
),
default=False,
output_field=models.BooleanField(),
),
)
return qs
class User(AuthUser):
......
......@@ -2,6 +2,7 @@ from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator
from django.core.exceptions import ValidationError
from django.urls import reverse
from django.utils import timezone
import datetime
......@@ -9,22 +10,34 @@ import datetime
class CurrentPaymentManager(models.Manager):
def get_queryset(self):
qs = super().get_queryset()
qs = qs.annotate(
one_year_ago = timezone.now() - datetime.timedelta(days=365)
return qs.annotate(
payment_method=models.Subquery(
PaymentUpdate.objects.filter(
payment=models.OuterRef("pk"), validated=True
).values("payment_method")[:1]
)
)
qs = qs.annotate(
),
payment_start=models.Subquery(
PaymentUpdate.objects.filter(
payment=models.OuterRef("pk"), validated=True
).values("payment_start")[:1]
),
long_stopped=models.Case(
models.When(
payment_method=PaymentUpdate.STOP,
payment_start__lte=one_year_ago,
then=True,
),
default=False,
output_field=models.BooleanField(),
),
active=models.Case(
models.When(payment_method__isnull=True, then=None),
models.When(payment_method=PaymentUpdate.STOP, then=False),
default=True,
output_field=models.NullBooleanField(),
)
),
)
return qs
class RecurringPayment(models.Model):
......
......@@ -12,12 +12,10 @@ from django.http import HttpResponseRedirect
from django.core.exceptions import ValidationError
from datetime import timedelta
from ipaddress import IPv4Address
from djadhere.utils import get_active_filter
from adhesions.models import Adhesion
from banking.models import PaymentUpdate
from .models import (
Service,
ServiceType,
......@@ -467,21 +465,7 @@ class ServiceAdmin(admin.ModelAdmin):
return actions
def has_delete_permission(self, request, obj=None):
if not obj:
return False
one_year_ago = timezone.now() - timedelta(days=365)
contribution = obj.contribution.updates.filter(validated=True).first()
# s’il y avait un paiement actif il y a moins d’un an
if (
not contribution
or contribution.payment_method != PaymentUpdate.STOP
or contribution.start > one_year_ago
):
return False
# s’il y avait une allocation active il y a moins d’un an
if any(
map(lambda a: a.end is None or a.end > one_year_ago, obj.allocations.all())
):
if not obj or not obj.long_stopped:
return False
return True
......
......@@ -6,13 +6,13 @@ from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('services', '0059_auto_20201119_1952'),
("services", "0059_auto_20201119_1952"),
]
operations = [
migrations.AlterField(
model_name='port',
name='up',
model_name="port",
name="up",
field=models.BooleanField(null=True),
),
]
......@@ -12,6 +12,7 @@ from ipaddress import ip_network
from urllib.parse import quote
from hashlib import sha256
from base64 import urlsafe_b64encode
from datetime import timedelta
from djadhere.utils import get_active_filter, is_overlapping
from adhesions.models import Adhesion
......@@ -74,14 +75,29 @@ class ActiveAllocationManager(models.Manager):
class ActiveServiceManager(models.Manager):
def get_queryset(self):
qs = super().get_queryset()
qs = qs.annotate(
one_year_ago = timezone.now() - timedelta(days=365)
return qs.annotate(
active=models.Exists(
ServiceAllocation.objects.filter(
Q(service=models.OuterRef("pk")) & get_active_filter()
)
)
),
allocations_long_stopped=~models.Exists(
ServiceAllocation.object.filter(
Q(service=models.OuterRef("pk"))
& (Q(end__isnull=True) | Q(end__gte=one_year_ago))
)
),
long_stopped=models.Case(
models.When(
models.Case(contribution__isnull=True, then=False),
models.Case(contribution__long_stopped=False, then=False),
models.Case(allocations_long_stopped=False, then=False),
default=True,
output_field=models.BooleanField(),
)
),
)
return qs
class IPResource(models.Model):
......
......@@ -6,23 +6,23 @@ from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('stocking', '0003_auto_20180531_1457'),
("stocking", "0003_auto_20180531_1457"),
]
operations = [
migrations.AlterField(
model_name='category',
name='level',
model_name="category",
name="level",
field=models.PositiveIntegerField(editable=False),
),
migrations.AlterField(
model_name='category',
name='lft',
model_name="category",
name="lft",
field=models.PositiveIntegerField(editable=False),
),
migrations.AlterField(
model_name='category',
name='rght',
model_name="category",
name="rght",
field=models.PositiveIntegerField(editable=False),
),
]
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment