From 6fd1c0a963e43e07362f1759c17ae34385c62244 Mon Sep 17 00:00:00 2001 From: Tiberiu Chibici Date: Sun, 21 Oct 2018 01:37:51 +0300 Subject: [PATCH] Implemented mark as watched/unwatched auto-delete and auto-download funcitonality. --- .idea/workspace.xml | 331 +++++++++++++--------------------- YtManagerApp/models.py | 36 +++- YtManagerApp/scheduler.py | 4 +- YtManagerApp/views/actions.py | 11 +- 4 files changed, 162 insertions(+), 220 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d85d4a9..f20c1a0 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,26 +2,9 @@ - - - - - - - - - - - - - - - - - - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + - - - - - - - - - @@ -213,29 +161,6 @@ - - - - - - - - - - - - - - - - - - - - - - - @@ -252,39 +177,46 @@ - - + + + + + + + + + + + + + + + + + + + - + - - - - - - - - - - - - + + - - + + - - + + @@ -399,7 +331,6 @@ @@ -747,8 +679,8 @@ - - + + @@ -798,17 +730,6 @@ - - - - - file://$PROJECT_DIR$/YtManagerApp/management/jobs/synchronize.py - 32 - - - - @@ -993,16 +914,6 @@ - - - - - - - - - - @@ -1107,28 +1018,6 @@ - - - - - - - - - - - - - - - - - - - - - - @@ -1143,23 +1032,15 @@ - + - - - - - - + + - - - - - - + + @@ -1171,61 +1052,91 @@ - + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + - - + + + + + + + + + + + + + + + + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/YtManagerApp/models.py b/YtManagerApp/models.py index 0178118..d0b1b9b 100644 --- a/YtManagerApp/models.py +++ b/YtManagerApp/models.py @@ -8,6 +8,8 @@ from django.db import models from django.db.models.functions import Lower from YtManagerApp.utils.youtube import YoutubeAPI, YoutubeChannelInfo, YoutubePlaylistInfo + + # help_text = user shown text # verbose_name = user shown name # null = nullable, blank = user is allowed to set value to empty @@ -285,9 +287,22 @@ class Video(models.Model): def mark_watched(self): self.watched = True + self.save() + if self.downloaded_path is not None: + from YtManagerApp.appconfig import get_user_config + from YtManagerApp.management.jobs.delete_video import schedule_delete_video + from YtManagerApp.management.jobs.synchronize import schedule_synchronize_now_subscription + + user_cfg = get_user_config(self.subscription.user) + if user_cfg.getboolean('user', 'DeleteWatched'): + schedule_delete_video(self) + schedule_synchronize_now_subscription(self.subscription) def mark_unwatched(self): + from YtManagerApp.management.jobs.synchronize import schedule_synchronize_now_subscription self.watched = False + self.save() + schedule_synchronize_now_subscription(self.subscription) def get_files(self): if self.downloaded_path is not None: @@ -296,5 +311,24 @@ class Video(models.Model): if file.startswith(file_pattern): yield os.path.join(directory, file) + def delete_files(self): + if self.downloaded_path is not None: + from YtManagerApp.management.jobs.delete_video import schedule_delete_video + from YtManagerApp.appconfig import get_user_config + from YtManagerApp.management.jobs.synchronize import schedule_synchronize_now_subscription + + schedule_delete_video(self) + + # Mark watched? + user_cfg = get_user_config(self.subscription.user) + if user_cfg.getboolean('user', 'MarkDeletedAsWatched'): + self.watched = True + schedule_synchronize_now_subscription(self.subscription) + + def download(self): + if not self.downloaded_path: + from YtManagerApp.management.jobs.download_video import schedule_download_video + schedule_download_video(self) + def __str__(self): - return self.name \ No newline at end of file + return self.name diff --git a/YtManagerApp/scheduler.py b/YtManagerApp/scheduler.py index 8a846ab..b149c05 100644 --- a/YtManagerApp/scheduler.py +++ b/YtManagerApp/scheduler.py @@ -2,13 +2,13 @@ import logging import sys from apscheduler.schedulers.background import BackgroundScheduler -from .appconfig import settings - instance: BackgroundScheduler = None def initialize_scheduler(): + from .appconfig import settings global instance + logger = logging.getLogger('scheduler') executors = { 'default': { diff --git a/YtManagerApp/views/actions.py b/YtManagerApp/views/actions.py index 6e120b6..3886572 100644 --- a/YtManagerApp/views/actions.py +++ b/YtManagerApp/views/actions.py @@ -11,8 +11,6 @@ from crispy_forms.layout import Layout, Field, Div, HTML from django.db.models import Q from YtManagerApp.utils import youtube from YtManagerApp.management.jobs.synchronize import schedule_synchronize_now -from YtManagerApp.management.jobs.delete_video import schedule_delete_video -from YtManagerApp.management.jobs.download_video import schedule_download_video class SyncNowView(View): @@ -26,7 +24,7 @@ class SyncNowView(View): class DeleteVideoFilesView(View): def post(self, *args, **kwargs): video = Video.objects.get(id=kwargs['pk']) - schedule_delete_video(video) + video.delete_files() return JsonResponse({ 'success': True }) @@ -35,7 +33,7 @@ class DeleteVideoFilesView(View): class DownloadVideoFilesView(View): def post(self, *args, **kwargs): video = Video.objects.get(id=kwargs['pk']) - schedule_download_video(video) + video.download() return JsonResponse({ 'success': True }) @@ -44,8 +42,7 @@ class DownloadVideoFilesView(View): class MarkVideoWatchedView(View): def post(self, *args, **kwargs): video = Video.objects.get(id=kwargs['pk']) - video.watched = True - video.save() + video.mark_watched() return JsonResponse({ 'success': True }) @@ -54,7 +51,7 @@ class MarkVideoWatchedView(View): class MarkVideoUnwatchedView(View): def post(self, *args, **kwargs): video = Video.objects.get(id=kwargs['pk']) - video.watched = False + video.mark_unwatched() video.save() return JsonResponse({ 'success': True