Skip to content

Commit

Permalink
split settings per environment
Browse files Browse the repository at this point in the history
  • Loading branch information
sickelap committed Sep 25, 2023
1 parent 1014132 commit 2935638
Show file tree
Hide file tree
Showing 20 changed files with 110 additions and 167 deletions.
12 changes: 5 additions & 7 deletions api/directory_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import pytz
from constance import config as site_config
from django import db
from django.conf import settings
from django.core.paginator import Paginator
from django.db.models import Q, QuerySet
from django_q.tasks import AsyncTask

import api.models.album_thing
import api.util as util
import ownphotos.settings
from api.batch_jobs import create_batch_job
from api.face_classify import cluster_all_faces
from api.models import File, LongRunningJob, Photo
Expand Down Expand Up @@ -332,12 +332,10 @@ def photo_scanner(user, last_scan, full_scan, path, job_id):


def scan_photos(user, full_scan, job_id, scan_directory="", scan_files=[]):
if not os.path.exists(
os.path.join(ownphotos.settings.MEDIA_ROOT, "thumbnails_big")
):
os.mkdir(os.path.join(ownphotos.settings.MEDIA_ROOT, "square_thumbnails_small"))
os.mkdir(os.path.join(ownphotos.settings.MEDIA_ROOT, "square_thumbnails"))
os.mkdir(os.path.join(ownphotos.settings.MEDIA_ROOT, "thumbnails_big"))
if not os.path.exists(os.path.join(settings.MEDIA_ROOT, "thumbnails_big")):
os.mkdir(os.path.join(settings.MEDIA_ROOT, "square_thumbnails_small"))
os.mkdir(os.path.join(settings.MEDIA_ROOT, "square_thumbnails"))
os.mkdir(os.path.join(settings.MEDIA_ROOT, "thumbnails_big"))
if LongRunningJob.objects.filter(job_id=job_id).exists():
lrj = LongRunningJob.objects.get(job_id=job_id)
lrj.started_at = datetime.datetime.now().replace(tzinfo=pytz.utc)
Expand Down
4 changes: 2 additions & 2 deletions api/im2txt/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

# import matplotlib.pyplot as plt
import torch
from django.conf import settings
from PIL import Image
from torchvision import transforms

import ownphotos.settings
from api.im2txt.model import DecoderRNN, EncoderCNN

embed_size = 256
hidden_size = 512
num_layers = 1
im2txt_models_path = ownphotos.settings.IM2TXT_ROOT
im2txt_models_path = settings.IM2TXT_ROOT

encoder_path = os.path.join(im2txt_models_path, "models", "encoder-10-1000.ckpt")
decoder_path = os.path.join(im2txt_models_path, "models", "decoder-10-1000.ckpt")
Expand Down
8 changes: 4 additions & 4 deletions api/image_similarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import numpy as np
import requests
from django.conf import settings
from django.core.paginator import Paginator
from django.db.models import Q

from api.models import Photo
from api.util import logger
from ownphotos.settings import IMAGE_SIMILARITY_SERVER


def search_similar_embedding(user, emb, result_count=100, threshold=27):
Expand All @@ -24,7 +24,7 @@ def search_similar_embedding(user, emb, result_count=100, threshold=27):
"n": result_count,
"threshold": threshold,
}
res = requests.post(IMAGE_SIMILARITY_SERVER + "/search/", json=post_data)
res = requests.post(settings.IMAGE_SIMILARITY_SERVER + "/search/", json=post_data)
if res.status_code == 200:
return res.json()["result"]
else:
Expand All @@ -48,7 +48,7 @@ def search_similar_image(user, photo, threshold=27):
"image_embedding": image_embedding.tolist(),
"threshold": threshold,
}
res = requests.post(IMAGE_SIMILARITY_SERVER + "/search/", json=post_data)
res = requests.post(settings.IMAGE_SIMILARITY_SERVER + "/search/", json=post_data)
if res.status_code == 200:
return res.json()
else:
Expand Down Expand Up @@ -85,6 +85,6 @@ def build_image_similarity_index(user):
"image_hashes": image_hashes,
"image_embeddings": image_embeddings,
}
requests.post(IMAGE_SIMILARITY_SERVER + "/build/", json=post_data)
requests.post(settings.IMAGE_SIMILARITY_SERVER + "/build/", json=post_data)
elapsed = (datetime.now() - start).total_seconds()
logger.info("building similarity index took %.2f seconds" % elapsed)
4 changes: 2 additions & 2 deletions api/models/user.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import pytz
from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.db import models
from django_cryptography.fields import encrypt

import ownphotos.settings
from api.date_time_extractor import DEFAULT_RULES_JSON


Expand All @@ -29,7 +29,7 @@ class User(AbstractUser):
)

favorite_min_rating = models.IntegerField(
default=ownphotos.settings.DEFAULT_FAVORITE_MIN_RATING, db_index=True
default=settings.DEFAULT_FAVORITE_MIN_RATING, db_index=True
)

class SaveMetadata(models.TextChoices):
Expand Down
4 changes: 2 additions & 2 deletions api/places365/places365.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@

import numpy as np
import torch
from django.conf import settings
from PIL import Image
from torch.autograd import Variable as V
from torch.nn import functional as F
from torchvision import transforms as trn

import ownphotos.settings
import wideresnet
from api.util import logger

# import warnings

torch.nn.Module.dump_patches = True
dir_places365_model = ownphotos.settings.PLACES365_ROOT
dir_places365_model = settings.PLACES365_ROOT


class Places365:
Expand Down
4 changes: 2 additions & 2 deletions api/semantic_search/semantic_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import numpy as np
import PIL
from django.conf import settings
from sentence_transformers import SentenceTransformer

import ownphotos
from api.util import logger

dir_clip_ViT_B_32_model = ownphotos.settings.CLIP_ROOT
dir_clip_ViT_B_32_model = settings.CLIP_ROOT


class SemanticSearch:
Expand Down
22 changes: 9 additions & 13 deletions api/thumbnails.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

import pyvips
import requests
from django.conf import settings

import api.util as util
import ownphotos.settings
from api.models.file import is_raw


Expand All @@ -14,7 +14,7 @@ def createThumbnail(inputPath, outputHeight, outputPath, hash, fileType):
if is_raw(inputPath):
if "thumbnails_big" in outputPath:
completePath = os.path.join(
ownphotos.settings.MEDIA_ROOT, outputPath, hash + fileType
settings.MEDIA_ROOT, outputPath, hash + fileType
).strip()
json = {
"source": inputPath,
Expand All @@ -25,7 +25,7 @@ def createThumbnail(inputPath, outputHeight, outputPath, hash, fileType):
return response["thumbnail"]
else:
bigThumbnailPath = os.path.join(
ownphotos.settings.MEDIA_ROOT, "thumbnails_big", hash + fileType
settings.MEDIA_ROOT, "thumbnails_big", hash + fileType
)
x = pyvips.Image.thumbnail(
bigThumbnailPath,
Expand All @@ -34,7 +34,7 @@ def createThumbnail(inputPath, outputHeight, outputPath, hash, fileType):
size=pyvips.enums.Size.DOWN,
)
completePath = os.path.join(
ownphotos.settings.MEDIA_ROOT, outputPath, hash + fileType
settings.MEDIA_ROOT, outputPath, hash + fileType
).strip()
x.write_to_file(completePath, Q=95)
return completePath
Expand All @@ -43,7 +43,7 @@ def createThumbnail(inputPath, outputHeight, outputPath, hash, fileType):
inputPath, 10000, height=outputHeight, size=pyvips.enums.Size.DOWN
)
completePath = os.path.join(
ownphotos.settings.MEDIA_ROOT, outputPath, hash + fileType
settings.MEDIA_ROOT, outputPath, hash + fileType
).strip()
x.write_to_file(completePath)
return completePath
Expand All @@ -54,9 +54,7 @@ def createThumbnail(inputPath, outputHeight, outputPath, hash, fileType):

def createAnimatedThumbnail(inputPath, outputHeight, outputPath, hash, fileType):
try:
output = os.path.join(
ownphotos.settings.MEDIA_ROOT, outputPath, hash + fileType
).strip()
output = os.path.join(settings.MEDIA_ROOT, outputPath, hash + fileType).strip()
subprocess.call(
[
"ffmpeg",
Expand Down Expand Up @@ -92,9 +90,7 @@ def createThumbnailForVideo(inputPath, outputPath, hash, fileType):
"00:00:00.000",
"-vframes",
"1",
os.path.join(
ownphotos.settings.MEDIA_ROOT, outputPath, hash + fileType
).strip(),
os.path.join(settings.MEDIA_ROOT, outputPath, hash + fileType).strip(),
]
)
except Exception as e:
Expand All @@ -106,11 +102,11 @@ def createThumbnailForVideo(inputPath, outputPath, hash, fileType):

def doesStaticThumbnailExists(outputPath, hash):
return os.path.exists(
os.path.join(ownphotos.settings.MEDIA_ROOT, outputPath, hash + ".webp").strip()
os.path.join(settings.MEDIA_ROOT, outputPath, hash + ".webp").strip()
)


def doesVideoThumbnailExists(outputPath, hash):
return os.path.exists(
os.path.join(ownphotos.settings.MEDIA_ROOT, outputPath, hash + ".mp4").strip()
os.path.join(settings.MEDIA_ROOT, outputPath, hash + ".mp4").strip()
)
6 changes: 2 additions & 4 deletions api/util.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import logging
import logging.handlers
import os
import os.path

import exiftool
import requests
from constance import config as site_config

import ownphotos.settings
from django.conf import settings

logger = logging.getLogger("ownphotos")
formatter = logging.Formatter(
"%(asctime)s : %(filename)s : %(funcName)s : %(lineno)s : %(levelname)s : %(message)s"
)
fileMaxByte = 256 * 1024 * 200 # 100MB
fileHandler = logging.handlers.RotatingFileHandler(
os.path.join(ownphotos.settings.LOGS_ROOT, "ownphotos.log"),
os.path.join(settings.LOGS_ROOT, "ownphotos.log"),
maxBytes=fileMaxByte,
backupCount=10,
)
Expand Down
4 changes: 2 additions & 2 deletions api/views/user.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.conf import settings
from rest_framework import status, viewsets
from rest_framework.permissions import AllowAny, IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

import ownphotos.settings
from api.api_util import path_to_dict
from api.date_time_extractor import DEFAULT_RULES_JSON, PREDEFINED_RULES_JSON
from api.models import User
Expand Down Expand Up @@ -39,7 +39,7 @@ def get(self, request, format=None):
if path:
res = [path_to_dict(path)]
else:
res = [path_to_dict(ownphotos.settings.DATA_ROOT)]
res = [path_to_dict(settings.DATA_ROOT)]
return Response(res)
except Exception as e:
logger.exception(str(e))
Expand Down
6 changes: 2 additions & 4 deletions api/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import jsonschema
import magic
from constance import config as site_config
from django.conf import settings
from django.db.models import Q
from django.http import HttpResponse, HttpResponseForbidden, StreamingHttpResponse
from django.utils.decorators import method_decorator
Expand All @@ -22,7 +23,6 @@
from rest_framework_simplejwt.exceptions import TokenError
from rest_framework_simplejwt.tokens import AccessToken

import ownphotos.settings
from api.api_util import get_search_term_examples
from api.autoalbum import delete_missing_photos
from api.directory_watcher import scan_photos
Expand Down Expand Up @@ -364,9 +364,7 @@ def _generate_response(self, photo, path, fname, transcode_videos):
response = HttpResponse()
response["Content-Type"] = filename
response["X-Accel-Redirect"] = iri_to_uri(
photo.main_file.path.replace(
ownphotos.settings.DATA_ROOT, "/original"
)
photo.main_file.path.replace(settings.DATA_ROOT, "/original")
)
return response
# faces and avatars
Expand Down
Empty file added librephotos/__init__.py
Empty file.
Empty file.
11 changes: 11 additions & 0 deletions librephotos/settings/development.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from .production import * # noqa

DEBUG = True
MIDDLEWARE += ["silk.middleware.SilkyMiddleware"] # noqa
INSTALLED_APPS += ["silk"] # noqa
INSTALLED_APPS += ["drf_spectacular"]
SPECTACULAR_SETTINGS = {
"TITLE": "LibrePhotos",
"DESCRIPTION": "Your project description",
"VERSION": "1.0.0",
}
Loading

0 comments on commit 2935638

Please sign in to comment.