Skip to content

Commit

Permalink
copy old quality profile values into new, rename old vs new quality p…
Browse files Browse the repository at this point in the history
…rofile model field names
  • Loading branch information
lardbit committed Jul 29, 2024
1 parent 914211c commit 5e59167
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 81 deletions.
1 change: 1 addition & 0 deletions src/frontend/src/app/settings/settings.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class SettingsComponent implements OnInit, AfterContentChecked {
'open_subtitles_username': [settings['open_subtitles_username']],
'open_subtitles_password': [settings['open_subtitles_password']],
'open_subtitles_auto': [settings['open_subtitles_auto']],
// TODO - use QualityProfile primary key
'quality_profile_tv': [settings['quality_profile_tv'], Validators.required],
'quality_profile_movies': [settings['quality_profile_movies'], Validators.required],
'allow_hardcoded_subs': [settings['allow_hardcoded_subs'], Validators.required],
Expand Down
9 changes: 9 additions & 0 deletions src/nefarious/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class NefariousSettingsSerializer(serializers.ModelSerializer):
websocket_url = serializers.SerializerMethodField()
is_debug = serializers.SerializerMethodField()
host_download_path = serializers.SerializerMethodField()
# TODO - need to handle saving
quality_profile_tv = serializers.SerializerMethodField()
quality_profile_movies = serializers.SerializerMethodField()

def get_websocket_url(self, obj):
return settings.WEBSOCKET_URL
Expand All @@ -40,6 +43,12 @@ def get_is_debug(self, obj):
def get_host_download_path(self, obj):
return settings.HOST_DOWNLOAD_PATH

def get_quality_profile_tv(self, obj):
return QualityProfileSerializer(obj.quality_profile_tv).data

def get_quality_profile_movies(self, obj):
return QualityProfileSerializer(obj.quality_profile_movies).data

class Meta:
model = NefariousSettings
fields = '__all__'
Expand Down
30 changes: 0 additions & 30 deletions src/nefarious/migrations/0079_auto_20240726_1500.py

This file was deleted.

54 changes: 54 additions & 0 deletions src/nefarious/migrations/0079_auto_20240728_1312.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Generated by Django 3.0.2 on 2024-07-28 13:12

import django.core.validators
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('nefarious', '0078_nefarioussettings_jackett_filter_index'),
]

operations = [
migrations.CreateModel(
name='QualityProfile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=500, unique=True)),
('quality', models.CharField(choices=[('any', 'any'), ('sd', 'sd'), ('hd-720p', 'hd-720p'), ('hd-720p-1080p', 'hd-720p-1080p'), ('hd-1080p', 'hd-1080p'), ('ultra-hd', 'ultra-hd')], max_length=500)),
('min_size_gb', models.DecimalField(blank=True, decimal_places=2, help_text='minimum size (gb) to download', max_digits=10, null=True, validators=[django.core.validators.MinValueValidator(0)])),
('max_size_gb', models.DecimalField(blank=True, decimal_places=2, help_text='maximum size (gb) to download', max_digits=10, null=True, validators=[django.core.validators.MinValueValidator(0)])),
],
),
migrations.AlterModelOptions(
name='nefarioussettings',
options={'verbose_name_plural': 'Settings'},
),
migrations.AddField(
model_name='nefarioussettings',
name='quality_profile_movies_default',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='quality_profile_movies_default', to='nefarious.QualityProfile'),
),
migrations.AddField(
model_name='nefarioussettings',
name='quality_profile_tv_default',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='quality_profile_tv_default', to='nefarious.QualityProfile'),
),
migrations.AddField(
model_name='watchmovie',
name='quality_profile',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='nefarious.QualityProfile'),
),
migrations.AddField(
model_name='watchtvseasonrequest',
name='quality_profile',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='nefarious.QualityProfile'),
),
migrations.AddField(
model_name='watchtvshow',
name='quality_profile',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='nefarious.QualityProfile'),
),
]
36 changes: 36 additions & 0 deletions src/nefarious/migrations/0080_populate_quality_profiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django.db import migrations, transaction
from nefarious.quality import PROFILES


def populate_quality_profile(apps, schema_editor):
NefariousSettings = apps.get_model('nefarious', 'NefariousSettings')
QualityProfile = apps.get_model('nefarious', 'QualityProfile')

nefarious_settings = NefariousSettings.objects.get()

# copy values from old field
for profile in PROFILES:
QualityProfile.objects.create(
name=profile,
quality=profile,
)

quality_profile_tv = QualityProfile.objects.filter(quality=nefarious_settings.quality_profile_tv).first()
quality_profile_movies = QualityProfile.objects.filter(quality=nefarious_settings.quality_profile_movies).first()

nefarious_settings.quality_profile_tv_default = quality_profile_tv
nefarious_settings.quality_profile_movies_default = quality_profile_movies

with transaction.atomic():
nefarious_settings.save()


class Migration(migrations.Migration):

dependencies = [
('nefarious', '0079_auto_20240728_1312'),
]

operations = [
migrations.RunPython(populate_quality_profile, reverse_code=lambda a, b: None),
]
29 changes: 0 additions & 29 deletions src/nefarious/migrations/0080_quality_profiles.py

This file was deleted.

35 changes: 35 additions & 0 deletions src/nefarious/migrations/0081_populate_quality_profile_media.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 3.0.2 on 2024-07-27 21:07

from django.db import migrations, transaction


def populate_quality_profile_media(apps, schema_editor):
QualityProfile = apps.get_model('nefarious', 'QualityProfile')
WatchMovie = apps.get_model('nefarious', 'WatchMovie')
WatchTVShow = apps.get_model('nefarious', 'WatchTVShow')
WatchTVSeasonRequest = apps.get_model('nefarious', 'WatchTVSeasonRequest')

# copy default quality profile into new field
for model in [WatchMovie, WatchTVShow, WatchTVSeasonRequest]:
for media in model.objects.all():
if media.quality_profile_custom:
# find matching quality profile by the quality & type
quality_profile = QualityProfile.objects.filter(
quality=media.quality_profile_custom,
).first()
# assign quality profile
media.quality_profile = quality_profile
# must save in transaction
with transaction.atomic():
media.save()


class Migration(migrations.Migration):

dependencies = [
('nefarious', '0080_populate_quality_profiles'),
]

operations = [
migrations.RunPython(populate_quality_profile_media, reverse_code=lambda a, b: None),
]
21 changes: 21 additions & 0 deletions src/nefarious/migrations/0082_auto_20240728_1417.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.0.2 on 2024-07-28 14:17

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('nefarious', '0081_populate_quality_profile_media'),
]

operations = [
migrations.RemoveField(
model_name='nefarioussettings',
name='quality_profile_movies',
),
migrations.RemoveField(
model_name='nefarioussettings',
name='quality_profile_tv',
),
]
23 changes: 23 additions & 0 deletions src/nefarious/migrations/0083_auto_20240728_1418.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.0.2 on 2024-07-28 14:18

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('nefarious', '0082_auto_20240728_1417'),
]

operations = [
migrations.RenameField(
model_name='nefarioussettings',
old_name='quality_profile_movies_default',
new_name='quality_profile_movies',
),
migrations.RenameField(
model_name='nefarioussettings',
old_name='quality_profile_tv_default',
new_name='quality_profile_tv',
),
]
25 changes: 25 additions & 0 deletions src/nefarious/migrations/0084_auto_20240728_1752.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 3.0.2 on 2024-07-28 17:52

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('nefarious', '0083_auto_20240728_1418'),
]

operations = [
migrations.RemoveField(
model_name='watchmovie',
name='quality_profile_custom',
),
migrations.RemoveField(
model_name='watchtvseasonrequest',
name='quality_profile_custom',
),
migrations.RemoveField(
model_name='watchtvshow',
name='quality_profile_custom',
),
]
44 changes: 22 additions & 22 deletions src/nefarious/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from django.conf import settings
from jsonfield import JSONField
from django.db import models

from nefarious import media_category
from nefarious import quality


PERM_CAN_WATCH_IMMEDIATELY_TV = 'can_immediately_watch_tv'
PERM_CAN_WATCH_IMMEDIATELY_MOVIE = 'can_immediately_watch_movie'

Expand All @@ -18,6 +18,18 @@
MEDIA_TYPE_TV_EPISODE = 'TV_EPISODE'


class QualityProfile(models.Model):
name = models.CharField(max_length=500, unique=True)
quality = models.CharField(max_length=500, choices=zip(quality.PROFILE_NAMES, quality.PROFILE_NAMES))
min_size_gb = models.DecimalField(
null=True, blank=True, max_digits=10, decimal_places=2, validators=[MinValueValidator(0)], help_text='minimum size (gb) to download')
max_size_gb = models.DecimalField(
null=True, blank=True, max_digits=10, decimal_places=2, validators=[MinValueValidator(0)], help_text='maximum size (gb) to download')

def __str__(self):
return f'{self.name} ({self.quality})'


class NefariousSettings(models.Model):
JACKETT_TOKEN_DEFAULT = 'COPY_YOUR_JACKETT_TOKEN_HERE'

Expand Down Expand Up @@ -51,12 +63,8 @@ class NefariousSettings(models.Model):
open_subtitles_user_token = models.CharField(max_length=500, blank=True, null=True, help_text='OpenSubtitles user auth token') # generated in auth flow
open_subtitles_auto = models.BooleanField(default=False, help_text='Whether to automatically download subtitles')

# TODO - remove after migration to new quality profiles table
quality_profile_tv = models.CharField(max_length=500, default=quality.PROFILE_ANY.name, choices=zip(quality.PROFILE_NAMES, quality.PROFILE_NAMES))
quality_profile_movies = models.CharField(max_length=500, default=quality.PROFILE_HD_720P_1080P.name, choices=zip(quality.PROFILE_NAMES, quality.PROFILE_NAMES))

# TODO - define "default" quality profiles per media-type (tv/movies)
quality_profiles = models.ForeignKey('QualityProfile', on_delete=models.CASCADE, null=True)
quality_profile_tv = models.ForeignKey(QualityProfile, on_delete=models.CASCADE, null=True, related_name='quality_profile_tv_default')
quality_profile_movies = models.ForeignKey(QualityProfile, on_delete=models.CASCADE, null=True, related_name='quality_profile_movies_default')

# whether to allow hardcoded subtitles
allow_hardcoded_subs = models.BooleanField(default=False)
Expand Down Expand Up @@ -94,23 +102,14 @@ def get_tmdb_poster_url(self, poster_path):
poster_path.lstrip('/'),
)

def should_save_subtitles(self):
def should_save_subtitles(self) -> bool:
return all([
self.open_subtitles_auto,
self.open_subtitles_user_token,
])


class QualityProfile(models.Model):
name = models.CharField(max_length=500, unique=True)
quality = models.CharField(max_length=500, choices=zip(quality.PROFILE_NAMES, quality.PROFILE_NAMES))
min_size_gb = models.DecimalField(
null=True, blank=True, max_digits=10, decimal_places=2, validators=[MinValueValidator(0)], help_text='minimum size to download')
max_size_gb = models.DecimalField(
null=True, blank=True, max_digits=10, decimal_places=2, validators=[MinValueValidator(0)], help_text='maximum size to download')

def __str__(self):
return self.name
class Meta:
verbose_name_plural = "Settings"


class WatchMediaBase(models.Model):
Expand Down Expand Up @@ -144,7 +143,7 @@ class WatchMovie(WatchMediaBase):
tmdb_movie_id = models.IntegerField(unique=True)
name = models.CharField(max_length=255)
poster_image_url = models.CharField(max_length=1000)
quality_profile_custom = models.CharField(max_length=500, null=True, blank=True, choices=zip(quality.PROFILE_NAMES, quality.PROFILE_NAMES))
quality_profile = models.ForeignKey(QualityProfile, on_delete=models.CASCADE, null=True)

class Meta:
ordering = ('name',)
Expand All @@ -167,7 +166,8 @@ class WatchTVShow(models.Model):
release_date = models.DateField(null=True, blank=True)
auto_watch = models.BooleanField(default=False) # whether to automatically watch future seasons
auto_watch_date_updated = models.DateField(null=True, blank=True) # date auto watch requested/updated
quality_profile_custom = models.CharField(max_length=500, null=True, blank=True, choices=zip(quality.PROFILE_NAMES, quality.PROFILE_NAMES))
quality_profile = models.ForeignKey(QualityProfile, on_delete=models.CASCADE, null=True)


class Meta:
ordering = ('name',)
Expand All @@ -190,7 +190,7 @@ class WatchTVSeasonRequest(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
watch_tv_show = models.ForeignKey(WatchTVShow, on_delete=models.CASCADE)
season_number = models.IntegerField()
quality_profile_custom = models.CharField(max_length=500, null=True, blank=True, choices=zip(quality.PROFILE_NAMES, quality.PROFILE_NAMES))
quality_profile = models.ForeignKey(QualityProfile, on_delete=models.CASCADE, null=True)
collected = models.BooleanField(default=False)
date_added = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
Expand Down

0 comments on commit 5e59167

Please sign in to comment.