From 4f80d0048512a9525a8475ad29718645298b9004 Mon Sep 17 00:00:00 2001 From: SebF Date: Fri, 3 May 2024 19:30:36 +0200 Subject: [PATCH] make format --- src/agenda_culturel/calendar.py | 4 +- src/agenda_culturel/celery.py | 3 +- src/agenda_culturel/db_importer.py | 5 +-- src/agenda_culturel/forms.py | 4 +- .../custom_extractors/lacomedie.py | 2 - .../import_tasks/custom_extractors/lacoope.py | 1 - .../custom_extractors/lapucealoreille.py | 2 - .../custom_extractors/lefotomat.py | 3 -- .../import_tasks/downloader.py | 2 +- src/agenda_culturel/import_tasks/extractor.py | 1 - .../import_tasks/extractor_facebook.py | 9 ++-- .../import_tasks/extractor_ical.py | 2 - .../import_tasks/generic_extractors.py | 14 +++--- src/agenda_culturel/import_tasks/importer.py | 2 - .../migrations/0036_auto_20240331_1421.py | 3 +- .../migrations/0038_auto_20240331_1815.py | 1 - .../migrations/0040_auto_20240403_1924.py | 1 - .../migrations/0048_auto_20240417_1212.py | 1 - .../migrations/0059_auto_20240427_1829.py | 1 - src/agenda_culturel/models.py | 2 +- src/agenda_culturel/templatetags/cat_extra.py | 1 - .../templatetags/event_extra.py | 9 ++-- .../templatetags/static_content_extra.py | 2 - src/agenda_culturel/urls.py | 3 +- src/agenda_culturel/views.py | 23 ++++------ src/manage.py | 4 +- src/scripts/create_categories.py | 10 +++-- src/scripts/create_fake_events.py | 44 +++++++++---------- 28 files changed, 65 insertions(+), 94 deletions(-) diff --git a/src/agenda_culturel/calendar.py b/src/agenda_culturel/calendar.py index dff1816..2add4ef 100644 --- a/src/agenda_culturel/calendar.py +++ b/src/agenda_culturel/calendar.py @@ -76,11 +76,11 @@ class DayInCalendar: def _add_event_internal(self, event): self.events.append(event) if event.category is None: - if not "" in self.events_by_category: + if "" not in self.events_by_category: self.events_by_category[""] = [] self.events_by_category[""].append(event) else: - if not event.category.name in self.events_by_category: + if event.category.name not in self.events_by_category: self.events_by_category[event.category.name] = [] self.events_by_category[event.category.name].append(event) diff --git a/src/agenda_culturel/celery.py b/src/agenda_culturel/celery.py index b92b927..eec6dfa 100644 --- a/src/agenda_culturel/celery.py +++ b/src/agenda_culturel/celery.py @@ -52,7 +52,7 @@ def close_import_task(taskid, success, error_message, importer): @app.task(bind=True) def import_events_from_json(self, json): - from agenda_culturel.models import Event, BatchImportation + from agenda_culturel.models import BatchImportation from .db_importer import DBImporterEvents # create a batch importation @@ -78,7 +78,6 @@ def import_events_from_json(self, json): def run_recurrent_import(self, pk): from agenda_culturel.models import RecurrentImport, BatchImportation from .db_importer import DBImporterEvents - from django.shortcuts import get_object_or_404 logger.info("Run recurrent import: {}".format(self.request.id)) diff --git a/src/agenda_culturel/db_importer.py b/src/agenda_culturel/db_importer.py index 6c513d4..9ffc237 100644 --- a/src/agenda_culturel/db_importer.py +++ b/src/agenda_culturel/db_importer.py @@ -1,6 +1,5 @@ from agenda_culturel.models import Event import json -from datetime import datetime from django.utils import timezone import logging @@ -44,9 +43,9 @@ class DBImporterEvents: except: return (False, "JSON file is not correctly structured") - if not "header" in structure: + if "header" not in structure: return (False, "JSON is not correctly structured: missing header") - if not "events" in structure: + if "events" not in structure: return (False, "JSON is not correctly structured: missing events") if "url" in structure["header"]: diff --git a/src/agenda_culturel/forms.py b/src/agenda_culturel/forms.py index 9fd9f27..bdb601d 100644 --- a/src/agenda_culturel/forms.py +++ b/src/agenda_culturel/forms.py @@ -14,12 +14,10 @@ from django.forms import ( HiddenInput, ModelChoiceField, ) -from datetime import date from django_better_admin_arrayfield.forms.widgets import DynamicArrayWidget from .models import ( Event, - BatchImportation, RecurrentImport, CategorisationRule, ModerationAnswer, @@ -327,7 +325,7 @@ class MergeDuplicates(Form): def get_selected_events_id(self, key): value = self.cleaned_data.get(key) - if not key in self.fields: + if key not in self.fields: return None else: if isinstance(value, list): diff --git a/src/agenda_culturel/import_tasks/custom_extractors/lacomedie.py b/src/agenda_culturel/import_tasks/custom_extractors/lacomedie.py index a225adc..e4f4db5 100644 --- a/src/agenda_culturel/import_tasks/custom_extractors/lacomedie.py +++ b/src/agenda_culturel/import_tasks/custom_extractors/lacomedie.py @@ -1,7 +1,5 @@ from ..generic_extractors import * -import re import json5 -from datetime import timedelta # A class dedicated to get events from La Coopérative de Mai: diff --git a/src/agenda_culturel/import_tasks/custom_extractors/lacoope.py b/src/agenda_culturel/import_tasks/custom_extractors/lacoope.py index d54a965..df79cf4 100644 --- a/src/agenda_culturel/import_tasks/custom_extractors/lacoope.py +++ b/src/agenda_culturel/import_tasks/custom_extractors/lacoope.py @@ -1,7 +1,6 @@ from ..generic_extractors import * import re import json5 -from datetime import timedelta # A class dedicated to get events from La Coopérative de Mai: diff --git a/src/agenda_culturel/import_tasks/custom_extractors/lapucealoreille.py b/src/agenda_culturel/import_tasks/custom_extractors/lapucealoreille.py index 3ea6eb0..67bd554 100644 --- a/src/agenda_culturel/import_tasks/custom_extractors/lapucealoreille.py +++ b/src/agenda_culturel/import_tasks/custom_extractors/lapucealoreille.py @@ -1,7 +1,5 @@ from ..generic_extractors import * import re -import json5 -from datetime import timedelta # A class dedicated to get events from La puce à l'oreille diff --git a/src/agenda_culturel/import_tasks/custom_extractors/lefotomat.py b/src/agenda_culturel/import_tasks/custom_extractors/lefotomat.py index 90fc8fc..0beedd1 100644 --- a/src/agenda_culturel/import_tasks/custom_extractors/lefotomat.py +++ b/src/agenda_culturel/import_tasks/custom_extractors/lefotomat.py @@ -1,7 +1,4 @@ from ..generic_extractors import * -import re -import json5 -from datetime import timedelta # A class dedicated to get events from Le Fotomat' diff --git a/src/agenda_culturel/import_tasks/downloader.py b/src/agenda_culturel/import_tasks/downloader.py index c88c7a6..ae6e5f5 100644 --- a/src/agenda_culturel/import_tasks/downloader.py +++ b/src/agenda_culturel/import_tasks/downloader.py @@ -1,4 +1,4 @@ -from urllib.parse import urlparse, urlencode +from urllib.parse import urlencode import urllib.request import os from selenium import webdriver diff --git a/src/agenda_culturel/import_tasks/extractor.py b/src/agenda_culturel/import_tasks/extractor.py index 66487b5..13f9a55 100644 --- a/src/agenda_culturel/import_tasks/extractor.py +++ b/src/agenda_culturel/import_tasks/extractor.py @@ -1,5 +1,4 @@ from abc import ABC, abstractmethod -from bs4 import BeautifulSoup from datetime import datetime, time, date, timedelta import re import unicodedata diff --git a/src/agenda_culturel/import_tasks/extractor_facebook.py b/src/agenda_culturel/import_tasks/extractor_facebook.py index 983a1a1..5890ed8 100644 --- a/src/agenda_culturel/import_tasks/extractor_facebook.py +++ b/src/agenda_culturel/import_tasks/extractor_facebook.py @@ -1,8 +1,5 @@ -import icalendar -import warnings - -from datetime import datetime, date -from bs4 import BeautifulSoup, MarkupResemblesLocatorWarning +from datetime import datetime +from bs4 import BeautifulSoup from urllib.parse import urlparse from .extractor import * @@ -182,7 +179,7 @@ class FacebookEventExtractor(Extractor): return event def build_event(self, url): - image = self.get_element("image") + self.get_element("image") return { "title": self.get_element("name"), diff --git a/src/agenda_culturel/import_tasks/extractor_ical.py b/src/agenda_culturel/import_tasks/extractor_ical.py index 5d02b45..5a60f10 100644 --- a/src/agenda_culturel/import_tasks/extractor_ical.py +++ b/src/agenda_culturel/import_tasks/extractor_ical.py @@ -1,12 +1,10 @@ import icalendar import warnings -from icalendar import vDatetime import bbcode from datetime import datetime, date, timedelta from bs4 import BeautifulSoup, MarkupResemblesLocatorWarning -from django.utils.translation import gettext_lazy as _ from .extractor import * diff --git a/src/agenda_culturel/import_tasks/generic_extractors.py b/src/agenda_culturel/import_tasks/generic_extractors.py index e7dd2fa..c058477 100644 --- a/src/agenda_culturel/import_tasks/generic_extractors.py +++ b/src/agenda_culturel/import_tasks/generic_extractors.py @@ -63,34 +63,34 @@ class TwoStepsExtractor(Extractor): return True def add_event_start_day(self, url, start_day): - if not url in self.event_properties: + if url not in self.event_properties: self.event_properties[url] = {} self.event_properties[url]["start_day"] = start_day def add_event_start_time(self, url, start_time): - if not url in self.event_properties: + if url not in self.event_properties: self.event_properties[url] = {} self.event_properties[url]["start_time"] = start_time def add_event_title(self, url, title): - if not url in self.event_properties: + if url not in self.event_properties: self.event_properties[url] = {} self.event_properties[url]["title"] = title def add_event_tag(self, url, tag): - if not url in self.event_properties: + if url not in self.event_properties: self.event_properties[url] = {} - if not "tags" in self.event_properties[url]: + if "tags" not in self.event_properties[url]: self.event_properties[url]["tags"] = [] self.event_properties[url]["tags"].append(tag) def add_event_category(self, url, cat): - if not url in self.event_properties: + if url not in self.event_properties: self.event_properties[url] = {} self.event_properties[url]["category"] = cat def add_event_location(self, url, loc): - if not url in self.event_properties: + if url not in self.event_properties: self.event_properties[url] = {} self.event_properties[url]["location"] = loc diff --git a/src/agenda_culturel/import_tasks/importer.py b/src/agenda_culturel/import_tasks/importer.py index d289df5..f3bc829 100644 --- a/src/agenda_culturel/import_tasks/importer.py +++ b/src/agenda_culturel/import_tasks/importer.py @@ -1,5 +1,3 @@ -import os - from .downloader import * from .extractor import * diff --git a/src/agenda_culturel/migrations/0036_auto_20240331_1421.py b/src/agenda_culturel/migrations/0036_auto_20240331_1421.py index 40015ea..b8bd566 100644 --- a/src/agenda_culturel/migrations/0036_auto_20240331_1421.py +++ b/src/agenda_culturel/migrations/0036_auto_20240331_1421.py @@ -1,8 +1,7 @@ # Create groups for several tasks from django.db import migrations -from django.contrib.auth.management import create_permissions -from django.contrib.auth.models import Group, Permission +from django.contrib.auth.models import Group def groups_permissions_creation(apps, schema_editor): diff --git a/src/agenda_culturel/migrations/0038_auto_20240331_1815.py b/src/agenda_culturel/migrations/0038_auto_20240331_1815.py index bab4d22..e50ae6c 100644 --- a/src/agenda_culturel/migrations/0038_auto_20240331_1815.py +++ b/src/agenda_culturel/migrations/0038_auto_20240331_1815.py @@ -3,7 +3,6 @@ from django.db import migrations -from django.contrib.auth.management import create_permissions from django.contrib.auth.models import Group, Permission diff --git a/src/agenda_culturel/migrations/0040_auto_20240403_1924.py b/src/agenda_culturel/migrations/0040_auto_20240403_1924.py index 18aaeb4..cdcadcd 100644 --- a/src/agenda_culturel/migrations/0040_auto_20240403_1924.py +++ b/src/agenda_culturel/migrations/0040_auto_20240403_1924.py @@ -1,7 +1,6 @@ # Generated by Django 4.2.7 on 2024-04-03 17:24 from django.db import migrations -from django.contrib.auth.management import create_permissions from django.contrib.auth.models import Group, Permission diff --git a/src/agenda_culturel/migrations/0048_auto_20240417_1212.py b/src/agenda_culturel/migrations/0048_auto_20240417_1212.py index a1f0eb3..dea65db 100644 --- a/src/agenda_culturel/migrations/0048_auto_20240417_1212.py +++ b/src/agenda_culturel/migrations/0048_auto_20240417_1212.py @@ -1,7 +1,6 @@ # Generated by Django 4.2.7 on 2024-04-17 10:12 from django.db import migrations -from django.contrib.auth.management import create_permissions from django.contrib.auth.models import Group, Permission def update_groups_permissions(apps, schema_editor): diff --git a/src/agenda_culturel/migrations/0059_auto_20240427_1829.py b/src/agenda_culturel/migrations/0059_auto_20240427_1829.py index 771bbf1..843b354 100644 --- a/src/agenda_culturel/migrations/0059_auto_20240427_1829.py +++ b/src/agenda_culturel/migrations/0059_auto_20240427_1829.py @@ -1,7 +1,6 @@ # Generated by Django 4.2.7 on 2024-04-27 16:29 from django.db import migrations -from django.contrib.auth.management import create_permissions from django.contrib.auth.models import Group, Permission def update_groups_permissions(apps, schema_editor): diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index 01de085..e7dfc7f 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -930,7 +930,7 @@ class Event(models.Model): if self.uuids is None: self.uuids = [] for uuid in other.uuids: - if not uuid in self.uuids: + if uuid not in self.uuids: self.uuids.append(uuid) # Limitation: the given events should not be considered similar one to another... diff --git a/src/agenda_culturel/templatetags/cat_extra.py b/src/agenda_culturel/templatetags/cat_extra.py index 0222844..eeadf91 100644 --- a/src/agenda_culturel/templatetags/cat_extra.py +++ b/src/agenda_culturel/templatetags/cat_extra.py @@ -2,7 +2,6 @@ from django import template from django.utils.safestring import mark_safe from agenda_culturel.models import Category -import statistics import colorsys from .utils_extra import * diff --git a/src/agenda_culturel/templatetags/event_extra.py b/src/agenda_culturel/templatetags/event_extra.py index 8bc7ed4..6eb38fc 100644 --- a/src/agenda_culturel/templatetags/event_extra.py +++ b/src/agenda_culturel/templatetags/event_extra.py @@ -4,7 +4,7 @@ from django.urls import reverse_lazy from django.template.defaultfilters import pluralize, linebreaks, urlize -from agenda_culturel.models import Event, Category +from agenda_culturel.models import Event from django.db.models import Q from .utils_extra import picto_from_name @@ -22,14 +22,14 @@ def in_date(event, date): @register.filter def can_show_start_time(event, day=None): - if not day is None and day == event.start_day: + if day is not None and day == event.start_day: return True return event.start_time and (not event.end_day or event.end_day == event.start_day) @register.filter def can_show_end_time(event, day=None): - if not day is None and day == event.end_day and event.start_day != event.end_day: + if day is not None and day == event.end_day and event.start_day != event.end_day: return True return False @@ -126,6 +126,7 @@ def field_to_html(field, key): else: return field + @register.filter def add_url_category(url, c): if c is None: @@ -133,4 +134,4 @@ def add_url_category(url, c): if url == "": return "category=" + str(c.pk) else: - return url + "&category=" + str(c.pk) \ No newline at end of file + return url + "&category=" + str(c.pk) diff --git a/src/agenda_culturel/templatetags/static_content_extra.py b/src/agenda_culturel/templatetags/static_content_extra.py index dd90b15..258d940 100644 --- a/src/agenda_culturel/templatetags/static_content_extra.py +++ b/src/agenda_culturel/templatetags/static_content_extra.py @@ -1,8 +1,6 @@ from django import template -from django.utils.safestring import mark_safe from agenda_culturel.models import StaticContent -from django.db.models import Q register = template.Library() diff --git a/src/agenda_culturel/urls.py b/src/agenda_culturel/urls.py index 4679fcd..e526ff3 100644 --- a/src/agenda_culturel/urls.py +++ b/src/agenda_culturel/urls.py @@ -2,8 +2,7 @@ from django.conf import settings from django.conf.urls.static import static from django.contrib import admin from django.contrib.staticfiles.urls import staticfiles_urlpatterns -from django.urls import path, include, re_path -from django.contrib.auth import views as auth_views +from django.urls import path, include from django.views.i18n import JavaScriptCatalog diff --git a/src/agenda_culturel/views.py b/src/agenda_culturel/views.py index e394fb0..15ba308 100644 --- a/src/agenda_culturel/views.py +++ b/src/agenda_culturel/views.py @@ -1,5 +1,5 @@ from django.shortcuts import render, get_object_or_404 -from django.views.generic import ListView, DetailView, FormView +from django.views.generic import ListView, DetailView from django.views.generic.edit import CreateView, UpdateView, DeleteView from django.contrib.auth.mixins import ( LoginRequiredMixin, @@ -9,11 +9,9 @@ from django.contrib.auth.mixins import ( from django.http import QueryDict from django import forms from django.contrib.postgres.search import SearchQuery, SearchHeadline -from django.core.exceptions import PermissionDenied -from django.http import HttpResponseRedirect, HttpResponse, HttpResponseNotFound +from django.http import HttpResponseRedirect from django.urls import reverse -import urllib from collections import Counter from .forms import ( @@ -48,13 +46,11 @@ from .models import ( Place, ) from django.utils import timezone -from enum import StrEnum from datetime import date, timedelta -from django.db.models import Q, F +from django.db.models import Q from django.urls import reverse_lazy from django.utils.translation import gettext_lazy as _ -from django.utils.translation import activate, get_language_info import django_filters from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.contrib.auth.decorators import login_required, permission_required @@ -75,7 +71,6 @@ from .celery import ( run_all_recurrent_imports, ) -import unicodedata import logging logger = logging.getLogger(__name__) @@ -429,7 +424,7 @@ class EventModerateView( template_name = "agenda_culturel/event_moderation_form.html" def form_valid(self, form): - mas = ModerationAnswer.objects.all() + ModerationAnswer.objects.all() logger.warning("ON valide la forme") for f in form.cleaned_data: @@ -470,7 +465,7 @@ def change_status_event(request, pk, status): def import_from_url(request): import logging - logger = logging.getLogger(__name__) + logging.getLogger(__name__) if request.method == "POST" and "title" in request.POST: form = EventForm(request.POST, is_authenticated=request.user.is_authenticated) @@ -532,7 +527,7 @@ def import_from_url(request): ) # TODO: use celery to import the other events - if event != None: + if event is not None: form = EventForm( instance=event, is_authenticated=request.user.is_authenticated, @@ -876,7 +871,7 @@ def add_import(request): form = BatchImportationForm(request.POST) if form.is_valid(): - result = import_events_from_json.delay(form.data["json"]) + import_events_from_json.delay(form.data["json"]) messages.success(request, _("The import has been run successfully.")) return HttpResponseRedirect(reverse_lazy("imports")) @@ -993,7 +988,7 @@ def run_rimport(request, pk): if request.method == "POST": # run recurrent import - result = run_recurrent_import.delay(pk) + run_recurrent_import.delay(pk) messages.success(request, _("The import has been launched.")) return HttpResponseRedirect(reverse_lazy("view_rimport", args=[pk])) @@ -1010,7 +1005,7 @@ def run_rimport(request, pk): def run_all_rimports(request): if request.method == "POST": # run recurrent import - result = run_all_recurrent_imports.delay() + run_all_recurrent_imports.delay() messages.success(request, _("Imports has been launched.")) return HttpResponseRedirect(reverse_lazy("recurrent_imports")) diff --git a/src/manage.py b/src/manage.py index 0a2ded9..6cd702c 100755 --- a/src/manage.py +++ b/src/manage.py @@ -7,7 +7,9 @@ import sys def main(): """Run administrative tasks.""" APP_ENV = os.getenv("APP_ENV", "dev") - os.environ.setdefault("DJANGO_SETTINGS_MODULE", f"agenda_culturel.settings.{APP_ENV}") + os.environ.setdefault( + "DJANGO_SETTINGS_MODULE", f"agenda_culturel.settings.{APP_ENV}" + ) try: from django.core.management import execute_from_command_line except ImportError as exc: diff --git a/src/scripts/create_categories.py b/src/scripts/create_categories.py index 4aa2b48..16a1a01 100644 --- a/src/scripts/create_categories.py +++ b/src/scripts/create_categories.py @@ -1,8 +1,9 @@ from agenda_culturel.models import Category + def run(): # concert, théâtre, jeune public, danse, arts du spectacle, exposition - # conférence, nature, + # conférence, nature, # divers categories = [ @@ -14,12 +15,13 @@ def run(): ("Exposition", "Expositions", "E"), ("Conférence", "Conférences", "C"), ("Nature", "Événements nature", "N"), - ("Autre", "Autres événements", "A") + ("Autre", "Autres événements", "A"), ] if len(Category.objects.all()) <= 1: print("On créée des catégories") for c in categories: - cat = Category(name=c[0], alt_name = c[1] if c[1] is not None else c[0], codename=c[2]) + cat = Category( + name=c[0], alt_name=c[1] if c[1] is not None else c[0], codename=c[2] + ) cat.save() - diff --git a/src/scripts/create_fake_events.py b/src/scripts/create_fake_events.py index 40318c7..b78c52a 100644 --- a/src/scripts/create_fake_events.py +++ b/src/scripts/create_fake_events.py @@ -1,38 +1,39 @@ from djipsum.faker import FakerModel -from agenda_culturel.models import Category, Event +from agenda_culturel.models import Event import random from datetime import datetime, timedelta -def run(): +def run(): tags = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." tags = [w for w in tags.replace(",", "").replace(".", "").split() if len(w) >= 3] - faker = FakerModel(app='agenda_culturel', model='Event') + faker = FakerModel(app="agenda_culturel", model="Event") def random_hour(): - m = random.randint(0,59) - h = random.randint(0,23) - s = random.randint(0,59) - return f'{h}:{m}:{s}' + m = random.randint(0, 59) + h = random.randint(0, 23) + s = random.randint(0, 59) + return f"{h}:{m}:{s}" for j in range(20): sday = datetime.now() + timedelta(days=random.randint(0, 40)) fields = { - 'title': faker.fake.text(max_nb_chars=100), - 'status': Event.STATUS.PUBLISHED, - 'category': faker.fake_relations( - type='fk', - field_name='category' - ), - 'start_day': sday.date(), - 'location': faker.fake.text(max_nb_chars=100), - 'description': ' '.join(faker.fake.paragraphs()), - 'image': faker.fake.url(), - 'image_alt': faker.fake.text(max_nb_chars=100), - 'reference_urls': [faker.fake.url() for i in range(0, random.randint(0, 5))], - 'tags': [tags[random.randint(0, len(tags) - 1)] for i in range(0, random.randint(0, 10))] - + "title": faker.fake.text(max_nb_chars=100), + "status": Event.STATUS.PUBLISHED, + "category": faker.fake_relations(type="fk", field_name="category"), + "start_day": sday.date(), + "location": faker.fake.text(max_nb_chars=100), + "description": " ".join(faker.fake.paragraphs()), + "image": faker.fake.url(), + "image_alt": faker.fake.text(max_nb_chars=100), + "reference_urls": [ + faker.fake.url() for i in range(0, random.randint(0, 5)) + ], + "tags": [ + tags[random.randint(0, len(tags) - 1)] + for i in range(0, random.randint(0, 10)) + ], } if random.randint(0, 1) == 1: fields["start_time"] = random_hour() @@ -42,4 +43,3 @@ def run(): if random.randint(0, 1) == 1: fields["end_time"] = random_hour() faker.create(fields) -