2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-02 21:38:48 +00:00
Matthias Mair 71c416bafd
Add flake8-logging linting (#5620)
* added flake8-logging to make logging more robust

* fixed LOG005 warnings

* fixed LOG008 warnings

* fixed LOG011 warnings

* fixed more LOG011 errors

* added ignores for intentional logger.error calls

* fixed even more LOG011 errors
2023-09-28 14:53:22 +10:00

141 lines
4.2 KiB
Python

"""Tasks (processes that get offloaded) for common app."""
import logging
import os
from datetime import datetime, timedelta
from django.conf import settings
from django.core.exceptions import AppRegistryNotReady
from django.db.utils import IntegrityError, OperationalError
from django.utils import timezone
import feedparser
from InvenTree.helpers_model import getModelsWithMixin
from InvenTree.models import InvenTreeNotesMixin
from InvenTree.tasks import ScheduledTask, scheduled_task
logger = logging.getLogger('inventree')
@scheduled_task(ScheduledTask.DAILY)
def delete_old_notifications():
"""Remove old notifications from the database.
Anything older than ~3 months is removed
"""
try:
from common.models import NotificationEntry
except AppRegistryNotReady: # pragma: no cover
logger.info("Could not perform 'delete_old_notifications' - App registry not ready")
return
before = timezone.now() - timedelta(days=90)
# Delete notification records before the specified date
NotificationEntry.objects.filter(updated__lte=before).delete()
@scheduled_task(ScheduledTask.DAILY)
def update_news_feed():
"""Update the newsfeed."""
try:
from common.models import NewsFeedEntry
except AppRegistryNotReady: # pragma: no cover
logger.info("Could not perform 'update_news_feed' - App registry not ready")
return
# Fetch and parse feed
try:
d = feedparser.parse(settings.INVENTREE_NEWS_URL)
except Exception as entry: # pragma: no cover
logger.warning("update_news_feed: Error parsing the newsfeed", entry)
return
# Get a reference list
id_list = [a.feed_id for a in NewsFeedEntry.objects.all()]
# Iterate over entries
for entry in d.entries:
# Check if id already exists
if entry.id in id_list:
continue
# Create entry
try:
NewsFeedEntry.objects.create(
feed_id=entry.id,
title=entry.title,
link=entry.link,
published=entry.published,
author=entry.author,
summary=entry.summary,
)
except (IntegrityError, OperationalError):
# Sometimes errors-out on database start-up
pass
logger.info('update_news_feed: Sync done')
@scheduled_task(ScheduledTask.DAILY)
def delete_old_notes_images():
"""Remove old notes images from the database.
Anything older than ~3 months is removed, unless it is linked to a note
"""
try:
from common.models import NotesImage
except AppRegistryNotReady:
logger.info("Could not perform 'delete_old_notes_images' - App registry not ready")
return
# Remove any notes which point to non-existent image files
for note in NotesImage.objects.all():
if not os.path.exists(note.image.path):
logger.info("Deleting note %s - image file does not exist", note.image.path)
note.delete()
note_classes = getModelsWithMixin(InvenTreeNotesMixin)
before = datetime.now() - timedelta(days=90)
for note in NotesImage.objects.filter(date__lte=before):
# Find any images which are no longer referenced by a note
found = False
img = note.image.name
for model in note_classes:
if model.objects.filter(notes__icontains=img).exists():
found = True
break
if not found:
logger.info("Deleting note %s - image file not linked to a note", img)
note.delete()
# Finally, remove any images in the notes dir which are not linked to a note
notes_dir = os.path.join(settings.MEDIA_ROOT, 'notes')
try:
images = os.listdir(notes_dir)
except FileNotFoundError:
# Thrown if the directory does not exist
images = []
all_notes = NotesImage.objects.all()
for image in images:
found = False
for note in all_notes:
img_path = os.path.basename(note.image.path)
if img_path == image:
found = True
break
if not found:
logger.info("Deleting note %s - image file not linked to a note", image)
os.remove(os.path.join(notes_dir, image))