On permet l'import des liens présents dans une page html collée dans le formulaire
This commit is contained in:
		@@ -540,13 +540,20 @@ def import_events_from_urls(
 | 
			
		||||
):
 | 
			
		||||
    for ucat in urls_cat_tags:
 | 
			
		||||
        if ucat is not None:
 | 
			
		||||
            url = ucat[0]
 | 
			
		||||
            cat = ucat[1]
 | 
			
		||||
            tags = ucat[2]
 | 
			
		||||
            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]
 | 
			
		||||
 | 
			
		||||
            import_events_from_url.delay(
 | 
			
		||||
                url, cat, tags, user_id=user_id, email=email, comments=comments
 | 
			
		||||
            )
 | 
			
		||||
            if url is not None:
 | 
			
		||||
                import_events_from_url.delay(
 | 
			
		||||
                    url, cat, tags, user_id=user_id, email=email, comments=comments
 | 
			
		||||
                )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@app.task(base=ChromiumTask, bind=True)
 | 
			
		||||
 
 | 
			
		||||
@@ -537,10 +537,10 @@ class EventModerateForm(ModelForm):
 | 
			
		||||
class BatchImportationForm(Form):
 | 
			
		||||
    required_css_class = "required"
 | 
			
		||||
 | 
			
		||||
    json = CharField(
 | 
			
		||||
        label="JSON",
 | 
			
		||||
    data = CharField(
 | 
			
		||||
        label=_("Data"),
 | 
			
		||||
        widget=Textarea(attrs={"rows": "10"}),
 | 
			
		||||
        help_text=_("JSON in the format expected for the import."),
 | 
			
		||||
        help_text=_("Supported formats: json, html."),
 | 
			
		||||
        required=True,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@@ -4,12 +4,30 @@
 | 
			
		||||
  {% block og_title %}Importation manuelle{% endblock %}
 | 
			
		||||
{% endblock %}
 | 
			
		||||
{% block content %}
 | 
			
		||||
  <h1>Importation manuelle</h1>
 | 
			
		||||
  <article>
 | 
			
		||||
    <form method="post">
 | 
			
		||||
      {% csrf_token %}
 | 
			
		||||
      {{ form.as_p }}
 | 
			
		||||
      <input type="submit" value="Envoyer">
 | 
			
		||||
    </form>
 | 
			
		||||
  </article>
 | 
			
		||||
  <div class="grid two-columns">
 | 
			
		||||
    <article>
 | 
			
		||||
      <header>
 | 
			
		||||
        <h1>Importation manuelle</h1>
 | 
			
		||||
      </header>
 | 
			
		||||
      <p>
 | 
			
		||||
        Le formulaire ci-dessous permet d'importer des événements par lot. Deux formats sont actuellement disponibles :
 | 
			
		||||
      </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 %}
 | 
			
		||||
 
 | 
			
		||||
@@ -70,6 +70,10 @@
 | 
			
		||||
              <a {% if current == "imports" %}class="selected"{% endif %}
 | 
			
		||||
                 href="{% url 'imports' %}">Historiques des importations</a>
 | 
			
		||||
            </li>
 | 
			
		||||
            <li>
 | 
			
		||||
              <a {% if current == "manual-import" %}class="selected"{% endif %}
 | 
			
		||||
                 href="{% url 'add_import' %}">Import manuel</a>
 | 
			
		||||
            </li>
 | 
			
		||||
          {% endif %}
 | 
			
		||||
          {% if perms.agenda_culturel.view_recurrentimport %}
 | 
			
		||||
            <li>
 | 
			
		||||
 
 | 
			
		||||
@@ -10,9 +10,12 @@ from django.urls import reverse_lazy
 | 
			
		||||
from django.utils.translation import gettext_lazy as _
 | 
			
		||||
 | 
			
		||||
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 ..models import Event, BatchImportation, RecurrentImport
 | 
			
		||||
from ..import_tasks.extractor import Extractor
 | 
			
		||||
from bs4 import BeautifulSoup
 | 
			
		||||
import json
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@login_required(login_url="/accounts/login/")
 | 
			
		||||
@@ -72,9 +75,37 @@ def add_import(request):
 | 
			
		||||
        form = BatchImportationForm(request.POST)
 | 
			
		||||
 | 
			
		||||
        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 render(request, "agenda_culturel/batchimportation_form.html", {"form": form})
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user