Amélioration des imports multiples

Fix #417
This commit is contained in:
Jean-Marie Favreau 2025-04-28 14:52:21 +02:00
parent da5d5679fe
commit 2418a8f12b
4 changed files with 91 additions and 20 deletions

View File

@ -458,10 +458,24 @@ def import_events_from_url(
from .db_importer import DBImporterEvents
if isinstance(urls, list):
url = urls[0]
is_list = True
if len(urls) > 0 and isinstance(urls[0], list):
url = urls[0][0][0]
cat = urls[0][0][1]
tags = urls[0][0][2]
if isinstance(tags, str):
tags = [tags]
user_id = urls[1]
email = urls[2]
comments = urls[3]
is_tuple = True
is_list = False
else:
url = urls[0]
is_list = True
is_tuple = False
else:
is_list = False
is_tuple = False
url = urls
with memcache_chromium_lock(self.app.oid) as acquired:
@ -489,7 +503,8 @@ def import_events_from_url(
try:
## create loader
u2e = URL2Events(ChromiumHeadlessDownloader(), single_event=True)
self.chromiumDownloader.pause = True
u2e = URL2Events(self.chromiumDownloader, single_event=True)
# set default values
values = {}
if cat is not None:
@ -528,7 +543,11 @@ def import_events_from_url(
logger.error(e)
close_import_task(self.request.id, False, e, importer)
return urls[1:] if is_list else True
return (
urls[1:]
if is_list
else [urls[0][1:], user_id, email, comments] if is_tuple else True
)
# if chromium is locked, we wait 30 seconds before retrying
raise self.retry(countdown=30)
@ -538,22 +557,20 @@ def import_events_from_url(
def import_events_from_urls(
self, urls_cat_tags, user_id=None, email=None, comments=None
):
for ucat in urls_cat_tags:
if ucat is not None:
url = None
cat = None
tags = None
if isinstance(ucat, str):
url = ucat
elif isinstance(ucat, (list, tuple)):
url = ucat[0]
cat = ucat[1]
tags = ucat[2]
if url is not None:
import_events_from_url.delay(
url, cat, tags, user_id=user_id, email=email, comments=comments
)
print("ça chaine baby")
# run tasks as a chain
tasks = chain(
(
import_events_from_url.s(
[urls_cat_tags, user_id, email, comments], force=True
)
if i == 0
else import_events_from_url.s(force=True)
)
for i in range(len(urls_cat_tags))
)
tasks.delay()
@app.task(base=ChromiumTask, bind=True)

View File

@ -544,6 +544,25 @@ class BatchImportationForm(Form):
required=True,
)
category = ModelChoiceField(
label=_("Category"),
queryset=Category.objects.all().order_by("name"),
help_text=_("Used only if data is html."),
initial=None,
required=False,
)
tags = MultipleChoiceField(
label=_("Tags"),
initial=None,
choices=[],
help_text=_("Used only if data is html."),
required=False,
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["tags"].choices = Tag.get_tag_groups(all=True)
class FixDuplicates(Form):
required_css_class = "required"

View File

@ -22,6 +22,9 @@
d'événements, enregistrer le html de la page (clic droit, enregistrer sous), puis copier le contenu de ce fichier html ci-dessous.
</li>
</ul>
<p>
Les champs catégorie et étiquettes seront utilisés si on utilise le format html, pour catégoriser tous les événements qui seront importés.
</p>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
@ -30,4 +33,35 @@
</article>
{% include "agenda_culturel/side-nav.html" with current="manual-import" %}
</div>
<script src="{% static 'choicejs/choices.min.js' %}"></script>
<script>
show_firstgroup = {
choice(classes, choice) {
const i = Choices.defaults.templates.choice.call(this, classes, choice);
if (this.first_group !== null && choice.groupId == this.first_group)
i.classList.add("visible");
return i;
},
choiceGroup(classes, group) {
const g = Choices.defaults.templates.choiceGroup.call(this, classes, group);
if (this.first_group === undefined && group.value == "Suggestions")
this.first_group = group.id;
if (this.first_group !== null && group.id == this.first_group)
g.classList.add("visible");
return g;
}
};
const tags = document.querySelector('#id_tags');
const choices_tags = new Choices(tags,
{
placeholderValue: 'Sélectionner les étiquettes à ajouter',
allowHTML: true,
delimiter: ',',
removeItemButton: true,
shouldSort: false,
callbackOnCreateTemplates: () => (show_firstgroup)
}
);
</script>
{% endblock %}

View File

@ -97,7 +97,8 @@ def add_import(request):
)
# then import events from url
import_events_from_urls.delay(
urls, user_id=request.user.pk if request.user else None
[(u, form.data["category"], form.data["tags"]) for u in urls],
user_id=request.user.pk if request.user else None,
)
messages.success(
request,