Skip to content

Commit

Permalink
[DONE] Fix migrations for 3.4.0 (#957)
Browse files Browse the repository at this point in the history
* Improve code & change constraint of video password

* Optimisation of favorites playlist creation

* Fix favorites migration

* Fix password encryption

* Add batch_size
  • Loading branch information
AymericJak authored Sep 22, 2023
1 parent 9a2c214 commit 50801f7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
49 changes: 34 additions & 15 deletions pod/playlist/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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."""
Expand Down Expand Up @@ -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()
Expand All @@ -109,29 +111,44 @@ 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."""
from pod.playlist.models import Playlist, PlaylistContent
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(
Expand All @@ -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,
Expand All @@ -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():
Expand Down Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions pod/video/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pod/video/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 50801f7

Please sign in to comment.