diff --git a/pod/playlist/apps.py b/pod/playlist/apps.py index 02508a6030..7a06a93a1b 100644 --- a/pod/playlist/apps.py +++ b/pod/playlist/apps.py @@ -24,6 +24,7 @@ def ready(self) -> None: def save_previous_data(self, sender, **kwargs): """Save previous data from various database tables.""" + print("Start save_previous_data | playlists") self.save_playlists() if len(PLAYLIST_INFORMATIONS) > 0: print("PLAYLIST_INFORMATIONS saved %s playlists" % len(PLAYLIST_INFORMATIONS)) @@ -37,6 +38,7 @@ def save_previous_data(self, sender, **kwargs): self.save_favorites() if len(FAVORITES_DATA) > 0: print("FAVORITES_DATA saved for %s persons" % len(FAVORITES_DATA)) + print("save_previous_data --> OK | playlists") def save_favorites(self): """Save previous data from favorites table.""" @@ -100,7 +102,7 @@ def save_playlist_content(self): def send_previous_data(self, sender, **kwargs): """Send previous data from various database tables.""" - print("Sending datas") + print("Start send_previous_data | playlists") if len(PLAYLIST_INFORMATIONS) > 0: self.update_playlists() @@ -109,6 +111,7 @@ def send_previous_data(self, sender, **kwargs): self.add_playlists_contents() self.create_new_favorites() + print("send_previous_data --> OK | playlists") def create_new_favorites(self): """Create favorites records from existing datas or create favorites playlist.""" @@ -116,22 +119,36 @@ def create_new_favorites(self): from django.utils.translation import gettext_lazy as _ from django.contrib.auth.models import User from django.contrib.sites.models import Site + from django.utils import timezone + from django.db.models import Max + from django.template.defaultfilters import slugify + print("Start create_new_favorites") # Add Favorites playlist for users without favorites - existing_users = User.objects.all() - users_without_favorites = existing_users.exclude(id__in=FAVORITES_DATA.keys()) + users_without_favorites = User.objects.exclude(id__in=FAVORITES_DATA.keys()) + users_with_favorite_playlist = set( + Playlist.objects.filter(name=FAVORITE_PLAYLIST_NAME).values_list( + 'owner_id', flat=True) + ) + playlist_id = Playlist.objects.aggregate(Max('id'))['id__max'] + playlist_id = playlist_id + 1 if playlist_id is not None else 1 + favorites_playlists_to_create = [] for user in users_without_favorites: - Playlist.objects.get_or_create( - name=FAVORITE_PLAYLIST_NAME, - owner=user, - site=Site.objects.get_current(), - defaults={ - "description": _("Your favorites videos."), - "visibility": "private", - "autoplay": True, - "editable": False, - }, - ) + if user.id not in users_with_favorite_playlist: + playlist = Playlist( + slug=f"{playlist_id}-{slugify(FAVORITE_PLAYLIST_NAME)}", + name=FAVORITE_PLAYLIST_NAME, + owner=user, + site=Site.objects.get_current(), + description=_("Your favorites videos."), + visibility="private", + autoplay=True, + editable=False, + ) + favorites_playlists_to_create.append(playlist) + playlist_id += 1 + Playlist.objects.bulk_create(favorites_playlists_to_create, batch_size=1000) + # Converting previous favorites to new system for owner_id, data_lists in FAVORITES_DATA.items(): new_favorites_playlist = Playlist.objects.create( @@ -146,7 +163,7 @@ def create_new_favorites(self): for favorites_datas in data_lists: date_added, rank, video_id = favorites_datas PlaylistContent.objects.create( - date_added=date_added, + date_added=timezone.make_aware(date_added), rank=rank, playlist=new_favorites_playlist, video_id=video_id, @@ -158,6 +175,7 @@ def update_playlists(self): """Update previous playlist table.""" from pod.playlist.models import Playlist + print("Start update_playlists") playlists_to_update = [] for playlist_id, playlist_datas in PLAYLIST_INFORMATIONS.items(): @@ -195,6 +213,7 @@ def add_playlists_contents(self): """Add playlist content record from existing datas""" from pod.playlist.models import PlaylistContent + print("Start add_playlists_contents") content_list_to_bulk = [] for content_datas in PLAYLIST_CONTENTS.values(): position, playlist_id, video_id = content_datas diff --git a/pod/video/apps.py b/pod/video/apps.py index 6e5758f5b9..f81bab045f 100644 --- a/pod/video/apps.py +++ b/pod/video/apps.py @@ -33,6 +33,7 @@ def set_default_site(sender, **kwargs): from pod.video.models import Type from django.contrib.sites.models import Site + print("Start set_default_site") site = Site.objects.get_current() for vid in Video.objects.filter(sites__isnull=True): apply_default_site(vid, site) @@ -44,6 +45,7 @@ def set_default_site(sender, **kwargs): apply_default_site(typ, site) for vr in VideoRendition.objects.filter(sites__isnull=True): apply_default_site(vr, site) + print("set_default_site --> OK") def fix_transcript(sender, **kwargs): @@ -54,8 +56,10 @@ def fix_transcript(sender, **kwargs): from pod.video.models import Video from django.db.models import F + print("Start fix_transcript") Video.objects.filter(transcript="1").update(transcript=F("main_lang")) Video.objects.filter(transcript="0").update(transcript="") + print("fix_transcript --> OK") def update_video_passwords(sender, **kwargs): @@ -64,13 +68,15 @@ def update_video_passwords(sender, **kwargs): from django.contrib.auth.hashers import make_password from django.db.models import Q + print("Start update_video_passwords") # Filter insecure protected videos - videos_to_update = Video.objects.filter( - Q(password__isnull=False) & ~Q(password__startswith=("pbkdf2", "sha256$")) + videos_to_update = Video.objects.exclude( + Q(password__isnull=True) | Q(password__startswith=("pbkdf2_sha256$")) ) for video in videos_to_update: video.password = make_password(video.password, hasher="pbkdf2_sha256") video.save() + print("update_video_passwords --> OK") class VideoConfig(AppConfig): @@ -105,6 +111,7 @@ def execute_query(self, query, mapping_dict): def save_previous_data(self, sender, **kwargs): """Save previous data from various database tables.""" + print("pre_migrate - Start save_previous_data") self.execute_query( """ SELECT id, @@ -190,9 +197,11 @@ def save_previous_data(self, sender, **kwargs): ) if len(PLAYLIST_VIDEO) > 0: print("%s PLAYLIST_VIDEO saved" % len(PLAYLIST_VIDEO)) + print("pre_migrate - save_previous_data --> OK") def send_previous_data(self, sender, **kwargs): """Send previous data from various database tables.""" + print("post_migrate - Start send_previous_data") nb_batch = 1000 if ( len(VIDEO_RENDITION) > 0 diff --git a/pod/video/models.py b/pod/video/models.py index 7ed0edab44..d4b722d585 100644 --- a/pod/video/models.py +++ b/pod/video/models.py @@ -819,7 +819,7 @@ class Video(models.Model): help_text=_("Viewing this video will not be possible without this password.") + " " + _("The password is / will be encrypted."), - max_length=50, + max_length=250, blank=True, null=True, ) @@ -863,7 +863,7 @@ class Meta: def set_password(self): """Encrypt the password if video is protected. An encrypted password cannot be re-encrypted.""" - if self.password and not self.password.startswith(("pbkdf2", "sha256$")): + if self.password and not self.password.startswith(("pbkdf2_sha256$")): self.password = make_password(self.password, hasher="pbkdf2_sha256") def save(self, *args, **kwargs):