-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
660 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import jinja2 | ||
|
||
OVERWRITES = { | ||
"trim_blocks": True, | ||
"lstrip_blocks": True, | ||
} | ||
|
||
|
||
def environment(**options): | ||
return jinja2.Environment( | ||
**{ | ||
**options, | ||
**OVERWRITES, | ||
} | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class NotificationConfig(AppConfig): | ||
name = "outdated.notifications" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Generated by Django 4.1.10 on 2023-08-09 09:44 | ||
|
||
from django.db import migrations, models | ||
import uuid | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Notification", | ||
fields=[ | ||
( | ||
"id", | ||
models.UUIDField( | ||
default=uuid.uuid4, | ||
editable=False, | ||
primary_key=True, | ||
serialize=False, | ||
), | ||
), | ||
("schedule", models.DurationField()), | ||
( | ||
"template", | ||
models.CharField( | ||
choices=[ | ||
("first-warning", "first-warning"), | ||
("second-warning", "second-warning"), | ||
("third-warning", "third-warning"), | ||
("final-warning", "final-warning"), | ||
("first-alert", "first-alert"), | ||
("second-alert", "second-alert"), | ||
("third-alert", "third-alert"), | ||
("final-alert", "final-alert"), | ||
], | ||
max_length=50, | ||
), | ||
), | ||
], | ||
options={ | ||
"ordering": ("-schedule",), | ||
"unique_together": {("schedule", "template")}, | ||
}, | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from django.conf import settings | ||
from django.db import models | ||
from django.db.models.signals import m2m_changed, post_save | ||
from django.dispatch import receiver | ||
|
||
from outdated.models import UUIDModel | ||
from outdated.outdated.models import Project, ReleaseVersion | ||
|
||
TEMPLATE_CHOICES = [(template, template) for template, _ in settings.NOTIFICATIONS] | ||
|
||
|
||
class Notification(UUIDModel): | ||
schedule = models.DurationField() | ||
template = models.CharField(max_length=50, choices=TEMPLATE_CHOICES) | ||
|
||
def __str__(self) -> str: | ||
return f"{self.template} ({self.schedule.days} days before EOL)" | ||
|
||
class Meta: | ||
unique_together = ("schedule", "template") | ||
ordering = ("-schedule",) | ||
|
||
|
||
def build_notification_queue(project: Project): | ||
duration_until_outdated = project.duration_until_outdated | ||
notifications = project.notification_queue | ||
unsent_notifications = Notification.objects.filter( | ||
schedule__gte=duration_until_outdated | ||
) | ||
notifications.set( | ||
[ | ||
*list(unsent_notifications)[-1:], | ||
*Notification.objects.filter(schedule__lte=duration_until_outdated), | ||
] | ||
) | ||
project.save() | ||
|
||
|
||
@receiver(post_save, sender=ReleaseVersion) | ||
def release_version_changed(instance: ReleaseVersion, **kwargs): | ||
if not instance.end_of_life: | ||
return | ||
concerned_projects = [] | ||
for version in instance.versions.all(): | ||
concerned_projects.extend(version.projects.all()) | ||
|
||
for project in set(concerned_projects): | ||
if project.duration_until_outdated is not None: | ||
build_notification_queue(project) | ||
|
||
|
||
@receiver(m2m_changed, sender=Project.versioned_dependencies.through) | ||
def versioned_dependencies_changed(action: str, instance: Project, **kwargs): | ||
if ( | ||
action.startswith("pre") | ||
or action.endswith("clear") | ||
or instance.duration_until_outdated is None | ||
): | ||
return | ||
build_notification_queue(instance) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from django.core.mail import EmailMessage | ||
from django.template.base import Template | ||
from django.template.loader import get_template | ||
|
||
from outdated.outdated.models import Project | ||
|
||
from .models import Notification | ||
|
||
|
||
class Notifier: | ||
def __init__(self, project: Project) -> None: | ||
self.project = project | ||
|
||
def notify(self) -> None: | ||
try: | ||
notification: Notification = self.project.notification_queue.get( | ||
schedule__gte=self.project.duration_until_outdated | ||
) | ||
except Notification.DoesNotExist: | ||
return | ||
|
||
template: Template = get_template(notification.template + ".txt", using="text") | ||
subject, _, body = template.render({"project": self.project}).partition("\n") | ||
maintainers = [m.user.email for m in self.project.maintainers.all()] | ||
message = EmailMessage(subject, body, to=maintainers[:1], cc=maintainers[1:]) | ||
message.send() | ||
self.project.notification_queue.remove(notification) | ||
self.project.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{% extends 'base.txt' %} | ||
|
||
{% block description %} | ||
Outdated since {{ project.duration_until_outdated.days * -1 }} days | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{% extends 'base.txt' %} | ||
|
||
{% block description %} | ||
Outdated in {{project.duration_until_outdated.days}} days | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{% block subject required %} | ||
{% endblock %} | ||
Project: {{ project.name }} | ||
Repo: {{ project.repo }} | ||
|
||
{% block description %} | ||
{% endblock %} | ||
|
||
{% block content %} | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% extends 'base-alert.txt' %} | ||
|
||
{% block subject %} | ||
Your Project has reached EOL | ||
{% endblock %} | ||
|
||
{% block content %} | ||
final-alert.txt contents | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% extends 'base-warning.txt' %} | ||
|
||
{% block subject %} | ||
Your project will be out of date shortly! | ||
{% endblock %} | ||
|
||
{% block content %} | ||
final warning content | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% extends 'base-alert.txt' %} | ||
|
||
{% block subject %} | ||
Your project is now outdated | ||
{% endblock %} | ||
|
||
{% block content %} | ||
first-alert.txt contents | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% extends 'base-warning.txt' %} | ||
|
||
{% block subject %} | ||
Your project will go out of date soon | ||
{% endblock %} | ||
|
||
{% block content %} | ||
first warning text | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% extends 'base-alert.txt' %} | ||
|
||
{% block subject %} | ||
Your project is using outdated dependencies! | ||
{% endblock %} | ||
|
||
{% block content %} | ||
second-alert.txt contents | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% extends 'base-warning.txt' %} | ||
|
||
{% block subject %} | ||
Your project is approaching it's EOL | ||
{% endblock %} | ||
|
||
{% block content %} | ||
second warning text | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% extends 'base-alert.txt' %} | ||
|
||
{% block subject %} | ||
Your Project has outdated! | ||
{% endblock %} | ||
|
||
{% block content %} | ||
third-alert.txt contents | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% extends 'base-warning.txt' %} | ||
|
||
{% block subject %} | ||
Your project will soon be EOL! | ||
{% endblock %} | ||
|
||
{% block content %} | ||
third warning text | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.