Improved notification system.

This commit is contained in:
2019-08-14 17:14:16 +03:00
parent 80cf94d694
commit 9bf114c25d
19 changed files with 818 additions and 504 deletions

View File

@ -2,13 +2,13 @@ from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import JsonResponse
from django.views.generic import View
from YtManagerApp.management.jobs.synchronize import schedule_synchronize_now
from YtManagerApp.management.jobs.synchronize import SynchronizeJob
from YtManagerApp.models import Video
class SyncNowView(LoginRequiredMixin, View):
def post(self, *args, **kwargs):
schedule_synchronize_now()
SynchronizeJob.schedule_now()
return JsonResponse({
'success': True
})

View File

@ -9,8 +9,8 @@ from django.urls import reverse_lazy
from django.views.generic import FormView
from YtManagerApp.management.appconfig import appconfig
from YtManagerApp.management.jobs.synchronize import schedule_synchronize_global
from YtManagerApp.scheduler import initialize_scheduler
from YtManagerApp.management.jobs.synchronize import SynchronizeJob
from YtManagerApp.scheduler import scheduler
from YtManagerApp.views.forms.first_time import WelcomeForm, ApiKeyForm, PickAdminUserForm, ServerConfigForm, DoneForm, \
UserCreationForm, LoginForm
@ -177,8 +177,8 @@ class Step3ConfigureView(WizardStepMixin, FormView):
appconfig.initialized = True
# Start scheduler if not started
initialize_scheduler()
schedule_synchronize_global()
scheduler.initialize()
SynchronizeJob.schedule_global_job()
return super().form_valid(form)

View File

@ -15,7 +15,6 @@ from YtManagerApp.management.appconfig import appconfig
from YtManagerApp.models import Subscription, SubscriptionFolder, VIDEO_ORDER_CHOICES, VIDEO_ORDER_MAPPING
from YtManagerApp.utils import youtube, subscription_file_parser
from YtManagerApp.views.controls.modal import ModalMixin
from YtManagerApp.management.notification_manager import get_current_notification_id
import logging
@ -122,7 +121,6 @@ def index(request: HttpRequest):
if request.user.is_authenticated:
context.update({
'filter_form': VideoFilterForm(),
'current_notification_id': get_current_notification_id(),
})
return render(request, 'YtManagerApp/index.html', context)
else:

View File

@ -1,12 +1,42 @@
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.http import HttpRequest, JsonResponse
from YtManagerApp.management.notification_manager import get_notifications
from YtManagerApp.models import JobExecution, JobMessage, JOB_STATES_MAP
@login_required
def ajax_get_notifications(request: HttpRequest, last_id: int):
user = request.user
notifications = get_notifications(user, last_id)
notifications = list(notifications)
return JsonResponse(notifications, safe=False)
def ajax_get_running_jobs(request: HttpRequest):
jobs = JobExecution.objects\
.filter(status=JOB_STATES_MAP['running'])\
.filter(Q(user__isnull=True) | Q(user=request.user))\
.order_by('start_date')
response = []
for job in jobs:
last_progress_message = JobMessage.objects\
.filter(job=job, progress__isnull=False, suppress_notification=False)\
.order_by('-timestamp').first()
last_message = JobMessage.objects\
.filter(job=job, suppress_notification=False)\
.order_by('-timestamp').first()
message = ''
progress = 0
if last_message is not None:
message = last_message.message
if last_progress_message is not None:
progress = last_progress_message.progress
response.append({
'id': job.id,
'description': job.description,
'progress': progress,
'message': message
})
return JsonResponse(response, safe=False)

View File

@ -3,7 +3,7 @@ from django.http import HttpResponseForbidden
from django.urls import reverse_lazy
from django.views.generic import FormView
from YtManagerApp.management.jobs.synchronize import schedule_synchronize_global
from YtManagerApp.management.jobs.synchronize import SynchronizeJob
from YtManagerApp.views.forms.settings import SettingsForm, AdminSettingsForm
@ -45,5 +45,5 @@ class AdminSettingsView(LoginRequiredMixin, FormView):
def form_valid(self, form):
form.save()
schedule_synchronize_global()
SynchronizeJob.schedule_global_job()
return super().form_valid(form)