Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Add some type hints, refactor existing code, update docker to use python 3.10 #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
FROM ubuntu:20.04 AS builder
#FROM laurielias/python-3.8.10-dlib:latest AS builder
FROM ubuntu:22.10 AS builder

ENV DEBIAN_FRONTEND noninteractive

Expand All @@ -22,7 +21,7 @@ RUN pip3 install --upgrade pip && \
pip3 wheel --wheel-dir=./wheels/ -r requirements.txt

# Lightweight deployment image this time
FROM python:3.8.10-slim AS deployer
FROM python:3.10.8-slim AS deployer

ENV DEBIAN_FRONTEND noninteractive

Expand All @@ -37,7 +36,7 @@ COPY --from=builder /home/docker/ajapaik/wheels ./wheels

COPY requirements.txt wsgi.py manage.py ./

RUN pip3 install --no-index --find-links=./wheels uwsgi -r requirements.txt && rm -rf ./wheels \
RUN pip3 install --upgrade pip && pip3 install --no-index --find-links=./wheels uwsgi -r requirements.txt && rm -rf ./wheels \
&& rm -rf requirements.txt && rm -rf ajapaik/tests && rm -rf ajapaik/ajapaik/tests

COPY ajapaik ./ajapaik
Expand All @@ -50,6 +49,8 @@ COPY docker/solr ./docker/solr

COPY docker/docker-entrypoint.sh docker/docker-entrypoint-dev.sh /usr/local/bin/

RUN touch c

RUN chmod +x /usr/local/bin/docker-entrypoint.sh /usr/local/bin/docker-entrypoint-dev.sh

EXPOSE 8000
Expand Down
4 changes: 2 additions & 2 deletions ajapaik/ajapaik/account_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
from django.utils.http import is_safe_url


class safeUrlAdapter(DefaultAccountAdapter):
def is_safe_url(self, url):
class SafeUrlAdapter(DefaultAccountAdapter):
def is_safe_url(self, url: str | None):
return is_safe_url(url, allowed_hosts=settings.ALLOWED_HOSTS)
63 changes: 34 additions & 29 deletions ajapaik/ajapaik/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import math

from PIL import Image, ImageOps
from django.contrib import admin
from django.contrib.admin import ModelAdmin
Expand All @@ -16,6 +18,21 @@
Location, LocationPhoto, ApplicationException


def invert_colors(photo_str: str):
photo = Photo.objects.filter(pk=photo_str.split('/')[0]).first()
if photo:
photo_path = f'{settings.MEDIA_ROOT}/{str(photo.image)}'
img = Image.open(photo_path)
inverted_grayscale_image = ImageOps.invert(img).convert('L')
inverted_grayscale_image.save(photo_path)
photo.invert = not photo.invert
sorl_delete(photo.image, delete_file=False)
photo.light_save()
return HttpResponse('Photo inverted!')

return HttpResponse('Failed to invert photo!')


class AlbumPhotoInline(admin.TabularInline):
model = AlbumPhoto
fields = 'album',
Expand All @@ -37,16 +54,12 @@ class DatingConfirmationAdmin(ModelAdmin):
class PhotoAdmin(ModelAdmin):
@staticmethod
def _distance_between_two_points_on_sphere(lon_1, lat_1, lon_2, lat_2):
import math

rad = math.pi / 180.0
equatorial_radius_meters = 6378137
lon_1_rad = lon_1 * rad
lat_1_rad = lat_1 * rad
lon_2_rad = lon_2 * rad
lat_2_rad = lat_2 * rad
cos_angle = math.sin(lat_1_rad) * math.sin(lat_2_rad) + math.cos(lat_1_rad) * math.cos(lat_2_rad) * math.cos(
lon_2_rad - lon_1_rad)

cos_angle = math.sin(lat_1 * rad) * math.sin(lat_2 * rad) + math.cos(lat_1 * rad) * math.cos(
lat_2 * rad) * math.cos(rad * (lon_2 - lon_1))

if cos_angle >= 1:
return 0
Expand All @@ -56,41 +69,33 @@ def _distance_between_two_points_on_sphere(lon_1, lat_1, lon_2, lat_2):

def save_model(self, request, obj, form, change):
if obj.lat and obj.lon and obj.bounding_circle_radius:
# If an administrator sets a bounding circle, invalidate GeoTags outside of it
# If an administrator sets a bounding circle, invalidate outside GeoTags
all_photo_geotags = GeoTag.objects.filter(photo_id=obj.id)
for geotag in all_photo_geotags:
d = self._distance_between_two_points_on_sphere(obj.lon, obj.lat, geotag.lon, geotag.lat)
if d > obj.bounding_circle_radius:
if d > obj.bounding_circle_radius and geotag.is_correct:
geotag.is_correct = False
else:
elif not geotag.is_correct:
geotag.is_correct = True
geotag.save()
obj.save()

def _invertcolors(self, id):
photo = Photo.objects.filter(pk=id.split('/')[0]).first()
if photo:
photo_path = f'{settings.MEDIA_ROOT}/{str(photo.image)}'
img = Image.open(photo_path)
inverted_grayscale_image = ImageOps.invert(img).convert('L')
inverted_grayscale_image.save(photo_path)
photo.invert = not photo.invert
sorl_delete(photo.image, delete_file=False)
photo.light_save()
return HttpResponse('Photo inverted!')
else:
continue

return HttpResponse('Failed to invert photo!')
geotag.save(update_fields=["is_correct"])
obj.save()

extra_buttons = [
{
'url': '_invertcolors',
'url': 'invert_colors',
'textname': _('Invert colors'),
'func': _invertcolors
'func': invert_colors
},
]

def change_view(self, request, object_id, form_url='', extra_context={}):
extra_context['extra_buttons'] = self.extra_buttons
def change_view(self, request, object_id, form_url='', extra_context=None):
if not extra_context:
extra_context = {'extra_buttons': self.extra_buttons}
else:
extra_context['extra_buttons'] = self.extra_buttons
return super(PhotoAdmin, self).change_view(request, object_id, form_url, extra_context=extra_context)

def get_urls(self):
Expand Down
Loading