On permet l'import des liens présents dans une page html collée dans le formulaire
This commit is contained in:
parent
e38d31edc8
commit
8f488cf7c5
@ -540,13 +540,20 @@ def import_events_from_urls(
|
|||||||
):
|
):
|
||||||
for ucat in urls_cat_tags:
|
for ucat in urls_cat_tags:
|
||||||
if ucat is not None:
|
if ucat is not None:
|
||||||
url = ucat[0]
|
url = None
|
||||||
cat = ucat[1]
|
cat = None
|
||||||
tags = ucat[2]
|
tags = None
|
||||||
|
if isinstance(ucat, str):
|
||||||
|
url = ucat
|
||||||
|
elif isinstance(ucat, (list, tuple)):
|
||||||
|
url = ucat[0]
|
||||||
|
cat = ucat[1]
|
||||||
|
tags = ucat[2]
|
||||||
|
|
||||||
import_events_from_url.delay(
|
if url is not None:
|
||||||
url, cat, tags, user_id=user_id, email=email, comments=comments
|
import_events_from_url.delay(
|
||||||
)
|
url, cat, tags, user_id=user_id, email=email, comments=comments
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.task(base=ChromiumTask, bind=True)
|
@app.task(base=ChromiumTask, bind=True)
|
||||||
|
@ -537,10 +537,10 @@ class EventModerateForm(ModelForm):
|
|||||||
class BatchImportationForm(Form):
|
class BatchImportationForm(Form):
|
||||||
required_css_class = "required"
|
required_css_class = "required"
|
||||||
|
|
||||||
json = CharField(
|
data = CharField(
|
||||||
label="JSON",
|
label=_("Data"),
|
||||||
widget=Textarea(attrs={"rows": "10"}),
|
widget=Textarea(attrs={"rows": "10"}),
|
||||||
help_text=_("JSON in the format expected for the import."),
|
help_text=_("Supported formats: json, html."),
|
||||||
required=True,
|
required=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -4,12 +4,30 @@
|
|||||||
{% block og_title %}Importation manuelle{% endblock %}
|
{% block og_title %}Importation manuelle{% endblock %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>Importation manuelle</h1>
|
<div class="grid two-columns">
|
||||||
<article>
|
<article>
|
||||||
<form method="post">
|
<header>
|
||||||
{% csrf_token %}
|
<h1>Importation manuelle</h1>
|
||||||
{{ form.as_p }}
|
</header>
|
||||||
<input type="submit" value="Envoyer">
|
<p>
|
||||||
</form>
|
Le formulaire ci-dessous permet d'importer des événements par lot. Deux formats sont actuellement disponibles :
|
||||||
</article>
|
</p>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
<strong>Format json :</strong> format structuré tel qu'attendu par l'importateur. Voir <a href="https://forge.chapril.org/jmtrivial/agenda_culturel/wiki/Import-JSON">la documentation sur le wiki du projet</a>.
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<strong>Format html :</strong> forme libre, où tous les liens qui sont considérés comme reconnus par l'<a href="{% url 'add_event' %}">outil d'import principal</a> seront importés.
|
||||||
|
Cette fonctionnalité peut être utile quand on veut importer plus que les 8 événements détectés par défaut lors d'un import Facebook. Pour cela, se rendre sur la page de la liste
|
||||||
|
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>
|
||||||
|
<form method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
{{ form.as_p }}
|
||||||
|
<input type="submit" value="Envoyer">
|
||||||
|
</form>
|
||||||
|
</article>
|
||||||
|
{% include "agenda_culturel/side-nav.html" with current="manual-import" %}
|
||||||
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
@ -70,6 +70,10 @@
|
|||||||
<a {% if current == "imports" %}class="selected"{% endif %}
|
<a {% if current == "imports" %}class="selected"{% endif %}
|
||||||
href="{% url 'imports' %}">Historiques des importations</a>
|
href="{% url 'imports' %}">Historiques des importations</a>
|
||||||
</li>
|
</li>
|
||||||
|
<li>
|
||||||
|
<a {% if current == "manual-import" %}class="selected"{% endif %}
|
||||||
|
href="{% url 'add_import' %}">Import manuel</a>
|
||||||
|
</li>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if perms.agenda_culturel.view_recurrentimport %}
|
{% if perms.agenda_culturel.view_recurrentimport %}
|
||||||
<li>
|
<li>
|
||||||
|
@ -10,9 +10,12 @@ from django.urls import reverse_lazy
|
|||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from ..celery import app as celery_app, update_orphan_pure_import_events
|
from ..celery import app as celery_app, update_orphan_pure_import_events
|
||||||
from ..celery import import_events_from_json
|
from ..celery import import_events_from_json, import_events_from_urls
|
||||||
from ..forms import BatchImportationForm
|
from ..forms import BatchImportationForm
|
||||||
from ..models import Event, BatchImportation, RecurrentImport
|
from ..models import Event, BatchImportation, RecurrentImport
|
||||||
|
from ..import_tasks.extractor import Extractor
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
@login_required(login_url="/accounts/login/")
|
@login_required(login_url="/accounts/login/")
|
||||||
@ -72,9 +75,37 @@ def add_import(request):
|
|||||||
form = BatchImportationForm(request.POST)
|
form = BatchImportationForm(request.POST)
|
||||||
|
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
import_events_from_json.delay(form.data["json"])
|
try:
|
||||||
|
# try to load data as a json file
|
||||||
|
json.loads(form.data["data"])
|
||||||
|
# if data is a json, load it
|
||||||
|
import_events_from_json.delay(form.data["data"])
|
||||||
|
messages.success(
|
||||||
|
request, _("The import from json has been run successfully.")
|
||||||
|
)
|
||||||
|
except ValueError:
|
||||||
|
# otherwise, consider it as html, extract all urls, and import them
|
||||||
|
soup = BeautifulSoup(form.data["data"], "html.parser")
|
||||||
|
urls = list(
|
||||||
|
set(
|
||||||
|
[
|
||||||
|
a["href"]
|
||||||
|
for a in soup.find_all("a", href=True)
|
||||||
|
if Extractor.is_known_url_default_extractors(a["href"])
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
# then import events from url
|
||||||
|
import_events_from_urls.delay(
|
||||||
|
urls, user_id=request.user.pk if request.user else None
|
||||||
|
)
|
||||||
|
messages.success(
|
||||||
|
request,
|
||||||
|
_(
|
||||||
|
"The import from html ({} detected links) has been run successfully."
|
||||||
|
).format(len(urls)),
|
||||||
|
)
|
||||||
|
|
||||||
messages.success(request, _("The import has been run successfully."))
|
|
||||||
return HttpResponseRedirect(reverse_lazy("imports"))
|
return HttpResponseRedirect(reverse_lazy("imports"))
|
||||||
|
|
||||||
return render(request, "agenda_culturel/batchimportation_form.html", {"form": form})
|
return render(request, "agenda_culturel/batchimportation_form.html", {"form": form})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user