From 819723b927a5a16f0ab7c1c10d7771acb4762770 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Thu, 5 Jan 2023 14:39:47 -0500 Subject: [PATCH] Big reorg to support pacakging --- .gitignore | 1 + scripts.py | 13 ------------- .../apps/scrobbles}/__init__.py | 0 .../apps/scrobbles}/admin.py | 0 {scrobbles => vrobbler/apps/scrobbles}/apps.py | 0 .../apps/scrobbles}/migrations/0001_initial.py | 0 .../migrations/0002_scrobble_counted.py | 0 ...ve_scrobble_counted_scrobble_in_progress.py | 0 .../migrations/0004_scrobble_scrobble_log.py | 0 ...5_alter_scrobble_playback_position_ticks.py | 18 ++++++++++++++++++ .../apps/scrobbles}/migrations/__init__.py | 0 .../apps/scrobbles}/models.py | 2 +- .../apps/scrobbles}/serializers.py | 0 {scrobbles => vrobbler/apps/scrobbles}/urls.py | 0 .../apps/scrobbles}/views.py | 16 +++++++++------- {videos => vrobbler/apps/videos}/__init__.py | 0 {videos => vrobbler/apps/videos}/admin.py | 0 {videos => vrobbler/apps/videos}/apps.py | 0 .../apps/videos}/migrations/0001_initial.py | 0 .../migrations/0002_alter_series_options.py | 0 .../0003_alter_video_run_time_ticks.py | 18 ++++++++++++++++++ .../apps/videos}/migrations/__init__.py | 0 {videos => vrobbler/apps/videos}/models.py | 2 +- vrobbler/settings.py | 16 +++++++++------- {templates => vrobbler/templates}/base.html | 0 .../templates}/scrobbles/scrobble_list.html | 0 26 files changed, 57 insertions(+), 29 deletions(-) delete mode 100644 scripts.py rename {scrobbles => vrobbler/apps/scrobbles}/__init__.py (100%) rename {scrobbles => vrobbler/apps/scrobbles}/admin.py (100%) rename {scrobbles => vrobbler/apps/scrobbles}/apps.py (100%) rename {scrobbles => vrobbler/apps/scrobbles}/migrations/0001_initial.py (100%) rename {scrobbles => vrobbler/apps/scrobbles}/migrations/0002_scrobble_counted.py (100%) rename {scrobbles => vrobbler/apps/scrobbles}/migrations/0003_remove_scrobble_counted_scrobble_in_progress.py (100%) rename {scrobbles => vrobbler/apps/scrobbles}/migrations/0004_scrobble_scrobble_log.py (100%) create mode 100644 vrobbler/apps/scrobbles/migrations/0005_alter_scrobble_playback_position_ticks.py rename {scrobbles => vrobbler/apps/scrobbles}/migrations/__init__.py (100%) rename {scrobbles => vrobbler/apps/scrobbles}/models.py (94%) rename {scrobbles => vrobbler/apps/scrobbles}/serializers.py (100%) rename {scrobbles => vrobbler/apps/scrobbles}/urls.py (100%) rename {scrobbles => vrobbler/apps/scrobbles}/views.py (93%) rename {videos => vrobbler/apps/videos}/__init__.py (100%) rename {videos => vrobbler/apps/videos}/admin.py (100%) rename {videos => vrobbler/apps/videos}/apps.py (100%) rename {videos => vrobbler/apps/videos}/migrations/0001_initial.py (100%) rename {videos => vrobbler/apps/videos}/migrations/0002_alter_series_options.py (100%) create mode 100644 vrobbler/apps/videos/migrations/0003_alter_video_run_time_ticks.py rename {videos => vrobbler/apps/videos}/migrations/__init__.py (100%) rename {videos => vrobbler/apps/videos}/models.py (96%) rename {templates => vrobbler/templates}/base.html (100%) rename {templates => vrobbler/templates}/scrobbles/scrobble_list.html (100%) diff --git a/.gitignore b/.gitignore index e0a3487..af577e4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ db.sqlite3 vrobbler.conf /media/ +/dist/ diff --git a/scripts.py b/scripts.py deleted file mode 100644 index d8a2ec9..0000000 --- a/scripts.py +++ /dev/null @@ -1,13 +0,0 @@ -import subprocess - -def server(): - cmd =['python', 'manage.py', 'runserver_plus'] - subprocess.run(cmd) - -def migrate(): - cmd =['python', 'manage.py', 'migrate'] - subprocess.run(cmd) - -def shell(): - cmd =['python', 'manage.py', 'shell_plus'] - subprocess.run(cmd) diff --git a/scrobbles/__init__.py b/vrobbler/apps/scrobbles/__init__.py similarity index 100% rename from scrobbles/__init__.py rename to vrobbler/apps/scrobbles/__init__.py diff --git a/scrobbles/admin.py b/vrobbler/apps/scrobbles/admin.py similarity index 100% rename from scrobbles/admin.py rename to vrobbler/apps/scrobbles/admin.py diff --git a/scrobbles/apps.py b/vrobbler/apps/scrobbles/apps.py similarity index 100% rename from scrobbles/apps.py rename to vrobbler/apps/scrobbles/apps.py diff --git a/scrobbles/migrations/0001_initial.py b/vrobbler/apps/scrobbles/migrations/0001_initial.py similarity index 100% rename from scrobbles/migrations/0001_initial.py rename to vrobbler/apps/scrobbles/migrations/0001_initial.py diff --git a/scrobbles/migrations/0002_scrobble_counted.py b/vrobbler/apps/scrobbles/migrations/0002_scrobble_counted.py similarity index 100% rename from scrobbles/migrations/0002_scrobble_counted.py rename to vrobbler/apps/scrobbles/migrations/0002_scrobble_counted.py diff --git a/scrobbles/migrations/0003_remove_scrobble_counted_scrobble_in_progress.py b/vrobbler/apps/scrobbles/migrations/0003_remove_scrobble_counted_scrobble_in_progress.py similarity index 100% rename from scrobbles/migrations/0003_remove_scrobble_counted_scrobble_in_progress.py rename to vrobbler/apps/scrobbles/migrations/0003_remove_scrobble_counted_scrobble_in_progress.py diff --git a/scrobbles/migrations/0004_scrobble_scrobble_log.py b/vrobbler/apps/scrobbles/migrations/0004_scrobble_scrobble_log.py similarity index 100% rename from scrobbles/migrations/0004_scrobble_scrobble_log.py rename to vrobbler/apps/scrobbles/migrations/0004_scrobble_scrobble_log.py diff --git a/vrobbler/apps/scrobbles/migrations/0005_alter_scrobble_playback_position_ticks.py b/vrobbler/apps/scrobbles/migrations/0005_alter_scrobble_playback_position_ticks.py new file mode 100644 index 0000000..d8acbff --- /dev/null +++ b/vrobbler/apps/scrobbles/migrations/0005_alter_scrobble_playback_position_ticks.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.5 on 2023-01-05 19:38 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('scrobbles', '0004_scrobble_scrobble_log'), + ] + + operations = [ + migrations.AlterField( + model_name='scrobble', + name='playback_position_ticks', + field=models.PositiveBigIntegerField(blank=True, null=True), + ), + ] diff --git a/scrobbles/migrations/__init__.py b/vrobbler/apps/scrobbles/migrations/__init__.py similarity index 100% rename from scrobbles/migrations/__init__.py rename to vrobbler/apps/scrobbles/migrations/__init__.py diff --git a/scrobbles/models.py b/vrobbler/apps/scrobbles/models.py similarity index 94% rename from scrobbles/models.py rename to vrobbler/apps/scrobbles/models.py index 33cb2b9..7cdb9dd 100644 --- a/scrobbles/models.py +++ b/vrobbler/apps/scrobbles/models.py @@ -14,7 +14,7 @@ class Scrobble(TimeStampedModel): User, blank=True, null=True, on_delete=models.DO_NOTHING ) timestamp = models.DateTimeField(**BNULL) - playback_position_ticks = models.PositiveIntegerField(**BNULL) + playback_position_ticks = models.PositiveBigIntegerField(**BNULL) playback_position = models.CharField(max_length=8, **BNULL) is_paused = models.BooleanField(default=False) played_to_completion = models.BooleanField(default=False) diff --git a/scrobbles/serializers.py b/vrobbler/apps/scrobbles/serializers.py similarity index 100% rename from scrobbles/serializers.py rename to vrobbler/apps/scrobbles/serializers.py diff --git a/scrobbles/urls.py b/vrobbler/apps/scrobbles/urls.py similarity index 100% rename from scrobbles/urls.py rename to vrobbler/apps/scrobbles/urls.py diff --git a/scrobbles/views.py b/vrobbler/apps/scrobbles/views.py similarity index 93% rename from scrobbles/views.py rename to vrobbler/apps/scrobbles/views.py index dae4150..a5ff71a 100644 --- a/scrobbles/views.py +++ b/vrobbler/apps/scrobbles/views.py @@ -14,6 +14,7 @@ from rest_framework.response import Response from scrobbles.models import Scrobble from scrobbles.serializers import ScrobbleSerializer from videos.models import Series, Video +from vrobbler.settings import DELETE_STALE_SCROBBLES logger = logging.getLogger(__name__) @@ -115,18 +116,21 @@ def jellyfin_websocket(request): ) return Response(video_dict, status=status.HTTP_204_NO_CONTENT) - # Check if found in progress scrobble is more than a day old - if not ( + existing_scrobble_more_than_a_day_old = ( existing_in_progress_scrobble - and existing_in_progress_scrobble.modified < a_day_from_now - ): + and existing_in_progress_scrobble.modified > a_day_from_now + ) + delete_stale_scrobbles = getattr(settings, "DELETE_STALE_SCROBBLES", True) + + # Check if found in progress scrobble is more than a day old + if existing_scrobble_more_than_a_day_old: logger.info( 'Found a scrobble for this video more than a day old, creating a new scrobble' ) scrobble = existing_in_progress_scrobble scrobble_created = False else: - if getattr(settings, "DELETE_STALE_SCROBBLES", True): + if existing_scrobble_more_than_a_day_old and delete_stale_scrobbles: existing_in_progress_scrobble.delete() scrobble, scrobble_created = Scrobble.objects.get_or_create( **scrobble_dict @@ -137,8 +141,6 @@ def jellyfin_websocket(request): scrobble.source = data_dict['ClientName'] scrobble.source_id = data_dict['MediaSourceId'] scrobble.scrobble_log = "" - else: - last_tick = scrobble.playback_position_ticks # Update a found scrobble with new position and timestamp scrobble.playback_position_ticks = data_dict["PlaybackPositionTicks"] diff --git a/videos/__init__.py b/vrobbler/apps/videos/__init__.py similarity index 100% rename from videos/__init__.py rename to vrobbler/apps/videos/__init__.py diff --git a/videos/admin.py b/vrobbler/apps/videos/admin.py similarity index 100% rename from videos/admin.py rename to vrobbler/apps/videos/admin.py diff --git a/videos/apps.py b/vrobbler/apps/videos/apps.py similarity index 100% rename from videos/apps.py rename to vrobbler/apps/videos/apps.py diff --git a/videos/migrations/0001_initial.py b/vrobbler/apps/videos/migrations/0001_initial.py similarity index 100% rename from videos/migrations/0001_initial.py rename to vrobbler/apps/videos/migrations/0001_initial.py diff --git a/videos/migrations/0002_alter_series_options.py b/vrobbler/apps/videos/migrations/0002_alter_series_options.py similarity index 100% rename from videos/migrations/0002_alter_series_options.py rename to vrobbler/apps/videos/migrations/0002_alter_series_options.py diff --git a/vrobbler/apps/videos/migrations/0003_alter_video_run_time_ticks.py b/vrobbler/apps/videos/migrations/0003_alter_video_run_time_ticks.py new file mode 100644 index 0000000..5e5b226 --- /dev/null +++ b/vrobbler/apps/videos/migrations/0003_alter_video_run_time_ticks.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.5 on 2023-01-05 19:38 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('videos', '0002_alter_series_options'), + ] + + operations = [ + migrations.AlterField( + model_name='video', + name='run_time_ticks', + field=models.PositiveBigIntegerField(blank=True, null=True), + ), + ] diff --git a/videos/migrations/__init__.py b/vrobbler/apps/videos/migrations/__init__.py similarity index 100% rename from videos/migrations/__init__.py rename to vrobbler/apps/videos/migrations/__init__.py diff --git a/videos/models.py b/vrobbler/apps/videos/models.py similarity index 96% rename from videos/models.py rename to vrobbler/apps/videos/models.py index 53a73cf..ff4c6fe 100644 --- a/videos/models.py +++ b/vrobbler/apps/videos/models.py @@ -33,7 +33,7 @@ class Video(TimeStampedModel): overview = models.TextField(**BNULL) tagline = models.TextField(**BNULL) run_time = models.CharField(max_length=8, **BNULL) - run_time_ticks = models.BigIntegerField(**BNULL) + run_time_ticks = models.PositiveBigIntegerField(**BNULL) year = models.IntegerField() # TV show specific fields diff --git a/vrobbler/settings.py b/vrobbler/settings.py index a6a7d01..063aa2a 100644 --- a/vrobbler/settings.py +++ b/vrobbler/settings.py @@ -4,7 +4,12 @@ import sys from django.utils.translation import gettext_lazy as _ from dotenv import load_dotenv +from pathlib import Path +# PROJECT_ROOT = os.path.dirname(__file__) +PROJECT_ROOT = Path(__file__).resolve().parent +BASE_DIR = Path(__file__).resolve().parent.parent +sys.path.insert(0, os.path.join(PROJECT_ROOT, 'apps')) # Tap vrobbler.conf if it's available if os.path.exists("vrobbler.conf"): @@ -14,10 +19,7 @@ elif os.path.exists("/etc/vrobbler.conf"): elif os.path.exists("/usr/local/etc/vrobbler.conf"): load_dotenv("/usr/local/etc/vrobbler.conf") -from pathlib import Path - # Build paths inside the project like this: BASE_DIR / 'subdir'. -BASE_DIR = Path(__file__).resolve().parent.parent # Quick-start development settings - unsuitable for production @@ -94,7 +96,7 @@ ROOT_URLCONF = "vrobbler.urls" TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", - "DIRS": [str(BASE_DIR.joinpath("templates"))], # new + "DIRS": [str(PROJECT_ROOT.joinpath("templates"))], "APP_DIRS": True, "OPTIONS": { "context_processors": [ @@ -111,7 +113,7 @@ WSGI_APPLICATION = "vrobbler.wsgi.application" DATABASES = { "default": dj_database_url.config( - default=os.getenv("vrobbler_DATABASE_URL", "sqlite:///db.sqlite3"), + default=os.getenv("VROBBLER_DATABASE_URL", "sqlite:///db.sqlite3"), conn_max_age=600, ), } @@ -181,7 +183,7 @@ AUTH_PASSWORD_VALIDATORS = [ LANGUAGE_CODE = "en-us" -TIME_ZONE = os.getenv("vrobbler_TIME_ZONE", "EST") +TIME_ZONE = os.getenv("VROBBLER_TIME_ZONE", "EST") USE_I18N = True @@ -195,7 +197,7 @@ USE_TZ = True STATIC_URL = "static/" STATIC_ROOT = os.getenv( - "VROBBLER_STATIC_ROOT", os.path.join(BASE_DIR, "static") + "VROBBLER_STATIC_ROOT", os.path.join(PROJECT_ROOT, "static") ) if not DEBUG: STATICFILES_STORAGE = ( diff --git a/templates/base.html b/vrobbler/templates/base.html similarity index 100% rename from templates/base.html rename to vrobbler/templates/base.html diff --git a/templates/scrobbles/scrobble_list.html b/vrobbler/templates/scrobbles/scrobble_list.html similarity index 100% rename from templates/scrobbles/scrobble_list.html rename to vrobbler/templates/scrobbles/scrobble_list.html