add notification url handling

This commit is contained in:
Simon 2023-12-12 11:57:43 +07:00
parent fbccc90411
commit caf5854f82
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
8 changed files with 139 additions and 66 deletions

View File

@ -126,6 +126,11 @@ urlpatterns = [
views.ScheduleView.as_view(),
name="api-schedule",
),
path(
"schedule/notification/",
views.ScheduleNotification.as_view(),
name="api-schedule-notification",
),
path(
"config/user/",
views.UserConfigView.as_view(),

View File

@ -28,6 +28,7 @@ from home.src.index.playlist import YoutubePlaylist
from home.src.index.reindex import ReindexProgress
from home.src.index.video import SponsorBlock, YoutubeVideo
from home.src.ta.config import AppConfig, ReleaseVersion
from home.src.ta.notify import Notifications, get_all_notifications
from home.src.ta.settings import EnvironmentSettings
from home.src.ta.ta_redis import RedisArchivist
from home.src.ta.task_manager import TaskCommand, TaskManager
@ -978,6 +979,35 @@ class ScheduleView(ApiBaseView):
return Response({"success": True})
class ScheduleNotification(ApiBaseView):
"""resolvues to /api/schedule/notification/
GET: get all schedule notifications
DEL: delete notification
"""
def get(self, request):
"""handle get request"""
return Response(get_all_notifications())
def delete(self, request):
"""handle delete"""
task_name = request.data.get("task_name")
url = request.data.get("url")
if not BaseTask.TASK_CONFIG.get(task_name):
message = {"message": "task_name not found"}
return Response(message, status=404)
if url:
response, status_code = Notifications(task_name).remove_url(url)
else:
response, status_code = Notifications(task_name).remove_task()
return Response({"response": response, "status_code": status_code})
class RefreshView(ApiBaseView):
"""resolves to /api/refresh/
GET: get refresh progress

View File

@ -60,48 +60,36 @@ def validate_cron(cron_expression):
class SchedulerSettingsForm(forms.Form):
"""handle scheduler settings"""
HELP_TEXT = "Add Apprise notification URLs, one per line"
update_subscribed = forms.CharField(
required=False, validators=[validate_cron]
)
update_subscribed_notify = forms.CharField(
label=False,
widget=forms.Textarea(
attrs={
"rows": 2,
"placeholder": HELP_TEXT,
}
),
required=False,
)
download_pending = forms.CharField(
required=False, validators=[validate_cron]
)
download_pending_notify = forms.CharField(
label=False,
widget=forms.Textarea(
attrs={
"rows": 2,
"placeholder": HELP_TEXT,
}
),
required=False,
)
check_reindex = forms.CharField(required=False, validators=[validate_cron])
check_reindex_notify = forms.CharField(
label=False,
widget=forms.Textarea(
attrs={
"rows": 2,
"placeholder": HELP_TEXT,
}
),
required=False,
)
check_reindex_days = forms.IntegerField(required=False)
thumbnail_check = forms.CharField(
required=False, validators=[validate_cron]
)
run_backup = forms.CharField(required=False, validators=[validate_cron])
run_backup_rotate = forms.IntegerField(required=False)
class NotificationSettingsForm(forms.Form):
"""add notification URL"""
TASK_CHOICES = [
("", "-- select task --"),
("update_subscribed", "Rescan your Subscriptions"),
("download_pending", "Downloading"),
("check_reindex", "Reindex Documents"),
]
PLACEHOLDER = "Apprise notification URL"
task = forms.ChoiceField(
widget=forms.Select, choices=TASK_CHOICES, required=False
)
notification_url = forms.CharField(
required=False,
widget=forms.TextInput(attrs={"placeholder": PLACEHOLDER}),
)

View File

@ -90,7 +90,11 @@ class Notifications:
}
}
_, _ = ElasticWrap(self.UPDATE_PATH).post(data)
response, status_code = ElasticWrap(self.UPDATE_PATH).post(data)
if not self.get_urls():
_, _ = self.remove_task()
return response, status_code
def remove_task(self) -> None:
"""remove all notifications from task"""
@ -106,4 +110,29 @@ class Notifications:
}
}
_, _ = ElasticWrap(self.UPDATE_PATH).post(data)
response, status_code = ElasticWrap(self.UPDATE_PATH).post(data)
return response, status_code
def get_all_notifications() -> dict[str, list[str]]:
"""get all notifications stored"""
path = "ta_config/_doc/notify"
response, status_code = ElasticWrap(path).get(print_error=False)
if not status_code == 200:
return {}
notifications = {}
task_config = task_manager.ta_tasks.BaseTask.TASK_CONFIG
source = response.get("_source")
for task_id, urls in source.items():
notifications.update(
{
task_id: {
"urls": urls,
"title": task_config[task_id].get("title"),
}
}
)
return notifications

View File

@ -38,16 +38,6 @@
{% endfor %}
{{ scheduler_form.update_subscribed }}
</div>
<div class="settings-item">
<p>Send notification on task completed:</p>
{% if download_pending.task_config.notify %}
<p><button type="button" onclick="textReveal(this)" id="text-reveal-button">Show</button> stored notification links</p>
<div id="text-reveal" class="description-text">
<p>{{ download_pending.task_config.notify|linebreaks }}</p>
</div>
{% endif %}
{{ scheduler_form.update_subscribed_notify }}
</div>
</div>
<div class="settings-group">
<h2>Start Download</h2>
@ -65,16 +55,6 @@
{% endfor %}
{{ scheduler_form.download_pending }}
</div>
<div class="settings-item">
<p>Send notification on task completed:</p>
{% if download_pending.task_config.notify %}
<p><button type="button" onclick="textReveal(this)" id="text-reveal-button">Show</button> stored notification links</p>
<div id="text-reveal" class="description-text">
<p>{{ download_pending.task_config.notify|linebreaks }}</p>
</div>
{% endif %}
{{ scheduler_form.download_pending_notify }}
</div>
</div>
<div class="settings-group">
<h2>Refresh Metadata</h2>
@ -97,16 +77,6 @@
{% endfor %}
{{ scheduler_form.check_reindex_days }}
</div>
<div class="settings-item">
<p>Send notification on task completed:</p>
{% if check_reindex.task_config.notify %}
<p><button type="button" onclick="textReveal(this)" id="text-reveal-button">Show</button> stored notification links</p>
<div id="text-reveal" class="description-text">
<p>{{ check_reindex.task_config.notify|linebreaks }}</p>
</div>
{% endif %}
{{ scheduler_form.check_reindex_notify }}
</div>
</div>
<div class="settings-group">
<h2>Thumbnail Check</h2>
@ -148,6 +118,29 @@
{{ scheduler_form.run_backup_rotate }}
</div>
</div>
<div class="settings-group">
<h2>Add Notification URL</h2>
<div class="settings-item">
{% if notifications %}
<p><button type="button" onclick="textReveal(this)" id="text-reveal-button">Show</button> stored notification links</p>
<div id="text-reveal" class="description-text">
{% for task, notifications in notifications.items %}
<h3>{{ notifications.title }}</h3>
{% for url in notifications.urls %}
<p><button type="button" class="danger-button" data-url="{{ url }}" data-task="{{ task }}" onclick="deleteNotificationUrl(this)"> Delete</button>{{ url }}</p>
{% endfor %}
{% endfor %}
</div>
{% else %}
<p>No notifications stored</p>
{% endif %}
</div>
<div class="settings-item">
<p><i>Send notification on completed tasks with the help of the <a href="https://github.com/caronc/apprise" target="_blank">Apprise</a> library.</i></p>
{{ notification_form.task }}
{{ notification_form.notification_url }}
</div>
</div>
<button type="submit" name="scheduler-settings">Update Scheduler Settings</button>
</form>
{% endblock settings_content %}

View File

@ -33,7 +33,10 @@ from home.src.frontend.forms import (
SubscribeToPlaylistForm,
UserSettingsForm,
)
from home.src.frontend.forms_schedule import SchedulerSettingsForm
from home.src.frontend.forms_schedule import (
NotificationSettingsForm,
SchedulerSettingsForm,
)
from home.src.index.channel import channel_overwrites
from home.src.index.generic import Pagination
from home.src.index.playlist import YoutubePlaylist
@ -42,6 +45,7 @@ from home.src.index.video_constants import VideoTypeEnum
from home.src.ta.config import AppConfig, ReleaseVersion
from home.src.ta.config_schedule import ScheduleBuilder
from home.src.ta.helper import check_stylesheet, time_parser
from home.src.ta.notify import Notifications, get_all_notifications
from home.src.ta.settings import EnvironmentSettings
from home.src.ta.ta_redis import RedisArchivist
from home.src.ta.users import UserConfig
@ -1105,6 +1109,16 @@ class SettingsSchedulingView(MinView):
def post(self, request):
"""handle form post to update settings"""
scheduler_form = SchedulerSettingsForm(request.POST)
notification_form = NotificationSettingsForm(request.POST)
if notification_form.is_valid():
notification_form_post = notification_form.cleaned_data
print(notification_form_post)
if any(notification_form_post.values()):
task_name = notification_form_post.get("task")
url = notification_form_post.get("notification_url")
Notifications(task_name).add_url(url)
if scheduler_form.is_valid():
scheduler_form_post = scheduler_form.cleaned_data
if any(scheduler_form_post.values()):
@ -1126,6 +1140,8 @@ class SettingsSchedulingView(MinView):
{
"title": "Scheduling Settings",
"scheduler_form": scheduler_form,
"notification_form": NotificationSettingsForm(),
"notifications": get_all_notifications(),
}
)
for task in all_tasks:

View File

@ -375,7 +375,8 @@ button:hover {
display: none;
}
#hidden-form button {
#hidden-form button,
#text-reveal button {
margin-right: 1rem;
}

View File

@ -363,6 +363,17 @@ function createSnapshot() {
document.getElementById('createButton').replaceWith(message);
}
function deleteNotificationUrl(button) {
console.log('delete notification url');
let apiEndpoint = '/api/schedule/notification/';
let data = {
task_name: button.dataset.task,
url: button.dataset.url,
};
apiRequest(apiEndpoint, 'DELETE', data);
button.parentElement.remove();
}
// delete from file system
function deleteConfirm() {
let to_show = document.getElementById('delete-button');