From c95edd9c095fcf2474cb8693580263c4cf5bc9f8 Mon Sep 17 00:00:00 2001 From: Marcus Date: Thu, 16 Feb 2023 11:47:37 -0300 Subject: [PATCH] no message --- controle/crons/cron.py | 26 ++++++++++++++++++++++++++ controle/models.py | 10 ++++++++++ controleFaturas/settings.py | 6 ++++-- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/controle/crons/cron.py b/controle/crons/cron.py index e8546eb..b3ebdb2 100644 --- a/controle/crons/cron.py +++ b/controle/crons/cron.py @@ -1,2 +1,28 @@ +from datetime import datetime + +from django.contrib.admin.models import LogEntry, ContentType, CHANGE +# from django.contrib.contenttypes.models import ContentType +from controle.models import Boleto, Cliente +from django.db.models import Count + + def my_cron_job(): print('my cron job is working!') + + +def check_boletos_vencidos_cron_job(): + boletos = Boleto.objects.filter(situacao='A', data_vencimento__lt=datetime.today( + ).date()).values('cliente_id').annotate(ccount=Count('cliente')).order_by() + for boleto in boletos: + cliente = Cliente.objects.get(pk=boleto['cliente_id']) + cliente.situacao = 'A' + cliente.save() + LogEntry.objects.log_action( + user_id=1, + content_type_id=ContentType.objects.get_for_model( + cliente, False).pk, + object_id=cliente.pk, + object_repr=str(cliente), + action_flag=CHANGE, + change_message='Situação atualizada para ATRASADO pelo cronjob.' + ) diff --git a/controle/models.py b/controle/models.py index 3d37caa..beeed8c 100644 --- a/controle/models.py +++ b/controle/models.py @@ -1,14 +1,24 @@ from datetime import datetime +from django.contrib.auth.models import AbstractUser from django.core.validators import MinValueValidator from django.db.models import CASCADE, PROTECT, Q, BooleanField, CharField, DateField, DateTimeField, DecimalField, FileField, ForeignKey, IntegerField, ManyToManyField, Model, PositiveIntegerField, TextField from django.utils.translation import ugettext_lazy as _ import os +from django.core.exceptions import PermissionDenied def get_upload_path(instance, filename): return os.path.join(str(type(instance).__name__), filename) +class User(AbstractUser): + def delete(self): + if self.pk != 1: + self.delete() + else: + raise PermissionDenied + + class Estado(Model): nome = CharField(_('nome'), max_length=60) sigla = CharField(_('sigla'), max_length=2) diff --git a/controleFaturas/settings.py b/controleFaturas/settings.py index d919274..62a2f72 100644 --- a/controleFaturas/settings.py +++ b/controleFaturas/settings.py @@ -79,6 +79,8 @@ TEMPLATES = [ WSGI_APPLICATION = 'controleFaturas.wsgi.application' +AUTH_USER_MODEL = 'controle.User' + # Database # https://docs.djangoproject.com/en/3.2/ref/settings/#databases @@ -95,7 +97,7 @@ DATABASES = { 'OPTIONS': { "init_command": "SET storage_engine=MYISAM; \ SET sql_mode='STRICT_TRANS_TABLES'", - } + } # 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': BASE_DIR / 'db.sqlite3', @@ -178,5 +180,5 @@ REST_FRAMEWORK = { } CRONJOBS = [ - ('*/1 * * * *', 'controle.cron.my_cron_job') + ('*/1 * * * *', 'controle.check_boletos_vencidos_cron_job') ]