-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Dépôt de besoin] Envoyer en asynchrone les besoins validés #998
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/bin/bash -l | ||
|
||
# Send Tenders who are validated but not sent | ||
|
||
# Do not run if this env var is not set: | ||
if [[ -z "$CRON_TENDER_SEND_VALIDATED_ENABLED" ]]; then | ||
echo "CRON_TENDER_SEND_VALIDATED_ENABLED not set. Exiting..." | ||
exit 0 | ||
fi | ||
|
||
# About clever cloud cronjobs: | ||
# https://www.clever-cloud.com/doc/tools/crons/ | ||
|
||
if [[ "$INSTANCE_NUMBER" != "0" ]]; then | ||
echo "Instance number is ${INSTANCE_NUMBER}. Stop here." | ||
exit 0 | ||
fi | ||
|
||
# $APP_HOME is set by default by clever cloud. | ||
cd $APP_HOME | ||
|
||
django-admin send_validated_tenders |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
from ckeditor.widgets import CKEditorWidget | ||
from django import forms | ||
from django.conf import settings | ||
from django.contrib import admin | ||
from django.contrib.contenttypes.admin import GenericTabularInline | ||
from django.db import models | ||
from django.http import HttpResponseRedirect | ||
from django.urls import reverse | ||
from django.utils import timezone | ||
from django.utils.html import format_html | ||
from django_admin_filters import MultiChoice | ||
from django_better_admin_arrayfield.admin.mixins import DynamicArrayMixin | ||
|
@@ -18,13 +16,8 @@ | |
from lemarche.tenders.forms import TenderAdminForm | ||
from lemarche.tenders.models import PartnerShareTender, Tender, TenderQuestion, TenderStepsData | ||
from lemarche.utils.admin.admin_site import admin_site | ||
from lemarche.utils.apis import api_hubspot | ||
from lemarche.utils.fields import ChoiceArrayField, pretty_print_readonly_jsonfield | ||
from lemarche.www.tenders.tasks import ( | ||
send_confirmation_published_email_to_author, | ||
send_tender_emails_to_partners, | ||
send_tender_emails_to_siaes, | ||
) | ||
from lemarche.www.tenders.tasks import restart_send_tender_task | ||
|
||
|
||
class KindFilter(MultiChoice): | ||
|
@@ -95,29 +88,6 @@ class TenderQuestionInline(admin.TabularInline): | |
extra = 0 | ||
|
||
|
||
def validate_and_send_tender_task(tender: Tender): | ||
# 1) validate the tender | ||
tender.set_validated_and_sent(with_save=True) | ||
# 2) find the matching Siaes? done in Tender post_save signal | ||
send_confirmation_published_email_to_author(tender, nb_matched_siaes=tender.siaes.count()) | ||
# 3) send the tender to all matching Siaes & Partners | ||
send_tender_emails_to_siaes(tender) | ||
send_tender_emails_to_partners(tender) | ||
|
||
|
||
def restart_send_tender_task(tender: Tender): | ||
# 1) log the tender send restart | ||
log_item = { | ||
"action": "restart_send", | ||
"date": timezone.now().isoformat(), | ||
} | ||
tender.logs.append(log_item) | ||
tender.save() | ||
# 2) send the tender to all matching Siaes & Partners | ||
send_tender_emails_to_siaes(tender) | ||
send_tender_emails_to_partners(tender) | ||
|
||
|
||
@admin.register(Tender, site=admin_site) | ||
class TenderAdmin(FieldsetsInlineMixin, admin.ModelAdmin): | ||
list_display = [ | ||
|
@@ -425,7 +395,7 @@ def siae_count_annotated_with_link(self, tender): | |
url = reverse("admin:siaes_siae_changelist") + f"?tenders__in={tender.id}" | ||
return format_html(f'<a href="{url}">{getattr(tender, "siae_count_annotated", 0)}</a>') | ||
|
||
siae_count_annotated_with_link.short_description = "Structures concernées" | ||
siae_count_annotated_with_link.short_description = "S. concernées" | ||
siae_count_annotated_with_link.admin_order_field = "siae_count_annotated" | ||
|
||
def siae_email_send_count_annotated_with_link(self, tender): | ||
|
@@ -494,10 +464,8 @@ def response_change(self, request, obj: Tender): | |
Catch submit of custom admin button to Validate or Resend Tender | ||
""" | ||
if request.POST.get("_validate_tender"): | ||
validate_and_send_tender_task(tender=obj) | ||
self.message_user(request, "Ce dépôt de besoin a été validé et envoyé aux structures") | ||
if settings.BITOUBI_ENV == "prod": | ||
api_hubspot.create_deal_from_tender(tender=obj) | ||
obj.set_validated() | ||
self.message_user(request, "Ce dépôt de besoin a été validé. Il sera envoyé en temps voulu :)") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😄 |
||
return HttpResponseRedirect(".") | ||
elif request.POST.get("_restart_tender"): | ||
restart_send_tender_task(tender=obj) | ||
|
23 changes: 23 additions & 0 deletions
23
lemarche/tenders/management/commands/send_validated_tenders.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
from lemarche.tenders.models import Tender | ||
from lemarche.www.tenders.tasks import send_validated_tender | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Command to send validated tenders | ||
|
||
Note: run via a CRON | ||
"*/5 9-17 * * 1-5" = Every 5 minutes from 9am through 5pm, Monday through Friday | ||
https://cron.help/#*/5_9-17_*_*_1-5 | ||
|
||
Usage: python manage.py send_validated_tenders | ||
""" | ||
|
||
def handle(self, *args, **options): | ||
validated_tenders_to_send = Tender.objects.validated_but_not_sent() | ||
if validated_tenders_to_send.count(): | ||
self.stdout.write(f"Found {validated_tenders_to_send.count()} validated tender(s) to send") | ||
for tender in validated_tenders_to_send: | ||
send_validated_tender(tender) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
J'adore cette possibilité de débrayer !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seul hic, c'est qu'il n'y a pas d'option intermédiaire (désactiver le cron mais permettre l'envoi manuel 🤔 )