• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

inventree / InvenTree / 8604849533

08 Apr 2024 06:20PM CUT coverage: 78.857%. First build
8604849533

Pull #6981

github

web-flow
Merge 3d6fa4389 into 4adce85ef
Pull Request #6981: Bump djangorestframework from 3.14.0 to 3.15.1 in /src/backend

19152 of 24287 relevant lines covered (78.86%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

4.55
/src/backend/InvenTree/common/tasks.py
1
"""Tasks (processes that get offloaded) for common app."""
2

3
import logging
4
import os
5
from datetime import timedelta
6

7
from django.conf import settings
8
from django.core.exceptions import AppRegistryNotReady
9
from django.db.utils import IntegrityError, OperationalError
10
from django.utils import timezone
11

12
import feedparser
13
import requests
14

15
import InvenTree.helpers
16
from InvenTree.helpers_model import getModelsWithMixin
17
from InvenTree.models import InvenTreeNotesMixin
18
from InvenTree.tasks import ScheduledTask, scheduled_task
19

20
logger = logging.getLogger('inventree')
21

22

23
@scheduled_task(ScheduledTask.DAILY)
24
def delete_old_notifications():
25
    """Remove old notifications from the database.
26

27
    Anything older than ~3 months is removed
28
    """
29
    try:
×
30
        from common.models import NotificationEntry
×
31
    except AppRegistryNotReady:  # pragma: no cover
×
32
        logger.info(
33
            "Could not perform 'delete_old_notifications' - App registry not ready"
34
        )
35
        return
×
36

37
    before = timezone.now() - timedelta(days=90)
×
38

39
    # Delete notification records before the specified date
40
    NotificationEntry.objects.filter(updated__lte=before).delete()
×
41

42

43
@scheduled_task(ScheduledTask.DAILY)
1✔
44
def update_news_feed():
1✔
45
    """Update the newsfeed."""
46
    try:
47
        from common.models import NewsFeedEntry
48
    except AppRegistryNotReady:  # pragma: no cover
49
        logger.info("Could not perform 'update_news_feed' - App registry not ready")
50
        return
51

52
    # News feed isn't defined, no need to continue
53
    if not settings.INVENTREE_NEWS_URL or type(settings.INVENTREE_NEWS_URL) != str:
54
        return
55

56
    # Fetch and parse feed
57
    try:
58
        feed = requests.get(settings.INVENTREE_NEWS_URL)
59
        d = feedparser.parse(feed.content)
60
    except Exception:  # pragma: no cover
61
        logger.warning('update_news_feed: Error parsing the newsfeed')
62
        return
63

64
    # Get a reference list
65
    id_list = [a.feed_id for a in NewsFeedEntry.objects.all()]
66

67
    # Iterate over entries
68
    for entry in d.entries:
69
        # Check if id already exists
70
        if entry.id in id_list:
71
            continue
72

73
        # Create entry
74
        try:
75
            NewsFeedEntry.objects.create(
76
                feed_id=entry.id,
77
                title=entry.title,
78
                link=entry.link,
79
                published=entry.published,
80
                author=entry.author,
81
                summary=entry.summary,
82
            )
83
        except (IntegrityError, OperationalError):
84
            # Sometimes errors-out on database start-up
85
            pass
86

87
    logger.info('update_news_feed: Sync done')
88

89

90
@scheduled_task(ScheduledTask.DAILY)
91
def delete_old_notes_images():
92
    """Remove old notes images from the database.
93

94
    Anything older than ~3 months is removed, unless it is linked to a note
95
    """
96
    try:
×
97
        from common.models import NotesImage
×
98
    except AppRegistryNotReady:
×
99
        logger.info(
100
            "Could not perform 'delete_old_notes_images' - App registry not ready"
101
        )
102
        return
×
103

104
    # Remove any notes which point to non-existent image files
105
    for note in NotesImage.objects.all():
×
106
        if not os.path.exists(note.image.path):
×
107
            logger.info('Deleting note %s - image file does not exist', note.image.path)
×
108
            note.delete()
×
109

110
    note_classes = getModelsWithMixin(InvenTreeNotesMixin)
×
111
    before = InvenTree.helpers.current_date() - timedelta(days=90)
×
112

113
    for note in NotesImage.objects.filter(date__lte=before):
×
114
        # Find any images which are no longer referenced by a note
115

116
        found = False
×
117

118
        img = note.image.name
×
119

120
        for model in note_classes:
×
121
            if model.objects.filter(notes__icontains=img).exists():
×
122
                found = True
×
123
                break
×
124

125
        if not found:
×
126
            logger.info('Deleting note %s - image file not linked to a note', img)
×
127
            note.delete()
×
128

129
    # Finally, remove any images in the notes dir which are not linked to a note
130
    notes_dir = os.path.join(settings.MEDIA_ROOT, 'notes')
×
131

132
    try:
×
133
        images = os.listdir(notes_dir)
×
134
    except FileNotFoundError:
×
135
        # Thrown if the directory does not exist
136
        images = []
×
137

138
    all_notes = NotesImage.objects.all()
×
139

140
    for image in images:
×
141
        found = False
×
142
        for note in all_notes:
×
143
            img_path = os.path.basename(note.image.path)
×
144
            if img_path == image:
×
145
                found = True
×
146
                break
×
147

148
        if not found:
×
149
            logger.info('Deleting note %s - image file not linked to a note', image)
×
150
            os.remove(os.path.join(notes_dir, image))
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc