Skip to content

Commit

Permalink
Merge pull request #4020 from airqo-platform/website-backend-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Baalmart authored Dec 7, 2024
2 parents 964bccc + 9415b2f commit 782e9ff
Show file tree
Hide file tree
Showing 63 changed files with 1,644 additions and 762 deletions.
2 changes: 1 addition & 1 deletion src/website/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Use Python 3.9-slim as the base image
FROM python:3.9-slim
FROM python:3.11-slim

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
Expand Down
7 changes: 4 additions & 3 deletions src/website/apps/africancities/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def flag_preview(self, obj):
width, height = 60, 40
from django.utils.html import format_html

flag_url = obj.get_country_flag_url()
if flag_url:
return format_html(f'<img src="{flag_url}" width="{width}" height="{height}" />')
if obj.country_flag:
return format_html(f'<img src="{obj.get_country_flag_url()}" width="{width}" height="{height}" />')
return '-'

flag_preview.short_description = "Country Flag"
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.1.4 on 2024-12-06 16:34

import cloudinary.models
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('africancities', '0010_alter_africancountry_country_flag_alter_image_image'),
]

operations = [
migrations.AlterField(
model_name='africancountry',
name='country_flag',
field=cloudinary.models.CloudinaryField(blank=True, max_length=255, null=True),
),
migrations.AlterField(
model_name='image',
name='image',
field=cloudinary.models.CloudinaryField(blank=True, max_length=255, null=True),
),
]
55 changes: 20 additions & 35 deletions src/website/apps/africancities/models.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
from django.conf import settings
from django.db import models
from utils.fields import ConditionalImageField
from cloudinary.models import CloudinaryField
from utils.models import BaseModel


class AfricanCountry(BaseModel):
country_name = models.CharField(max_length=100)
country_flag = ConditionalImageField(
local_upload_to='countries/flags/',
cloudinary_folder='website/uploads/countries/flags',
country_flag = CloudinaryField(
folder='website/uploads/africancities/flags',
null=True,
blank=True
blank=True,
resource_type='image'
)

order = models.IntegerField(default=1)

class Meta:
Expand All @@ -21,19 +20,12 @@ class Meta:
def __str__(self):
return self.country_name

def get_country_flag_url(self, request=None):
def get_country_flag_url(self):
"""
Return the full URL for the country flag.
- For Cloudinary, return a secure HTTPS URL.
- For local development, return the absolute URL using request.build_absolute_uri.
Return the secure URL for the country flag.
"""
if self.country_flag:
if not settings.DEBUG:
# Cloudinary secure URL
return self.country_flag.url # Already provides secure URL by default
else:
# Local development, ensure full URL
return request.build_absolute_uri(self.country_flag.url) if request else self.country_flag.url
return self.country_flag.url # Cloudinary already provides a secure URL
return None


Expand All @@ -44,7 +36,7 @@ class City(BaseModel):
AfricanCountry,
null=True,
related_name="city",
on_delete=models.deletion.SET_NULL,
on_delete=models.SET_NULL,
)

class Meta:
Expand All @@ -61,7 +53,7 @@ class Content(BaseModel):
City,
null=True,
related_name="content",
on_delete=models.deletion.SET_NULL,
on_delete=models.SET_NULL,
)

class Meta:
Expand All @@ -79,7 +71,7 @@ class Description(BaseModel):
null=True,
blank=True,
related_name="description",
on_delete=models.deletion.SET_NULL,
on_delete=models.SET_NULL,
)

class Meta:
Expand All @@ -90,19 +82,19 @@ def __str__(self):


class Image(BaseModel):
image = ConditionalImageField(
local_upload_to='content/images/',
cloudinary_folder='website/uploads/content/images',
image = CloudinaryField(
folder='website/uploads/africancities/images',
null=True,
blank=True
blank=True,
resource_type='image'
)
order = models.IntegerField(default=1)
content = models.ForeignKey(
'Content',
Content,
null=True,
blank=True,
related_name="image",
on_delete=models.deletion.SET_NULL,
on_delete=models.SET_NULL,
)

class Meta:
Expand All @@ -111,17 +103,10 @@ class Meta:
def __str__(self):
return f"Image-{self.id}"

def get_image_url(self, request=None):
def get_image_url(self):
"""
Return the full URL for the image.
- For Cloudinary, return a secure HTTPS URL.
- For local development, return the absolute URL using request.build_absolute_uri.
Return the secure URL for the image.
"""
if self.image:
if not settings.DEBUG:
# Cloudinary secure URL
return self.image.url # Cloudinary already provides secure URL
else:
# Local development, ensure full URL
return request.build_absolute_uri(self.image.url) if request else self.image.url
return self.image.url # Cloudinary already provides a secure URL
return None
6 changes: 2 additions & 4 deletions src/website/apps/africancities/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ class ImageSerializer(serializers.ModelSerializer):
image_url = serializers.SerializerMethodField()

def get_image_url(self, obj):
request = self.context.get('request') # Get the request context for absolute URLs
return obj.get_image_url(request)
return obj.get_image_url() # Cloudinary already provides a secure URL

class Meta:
fields = ('id', 'image_url')
Expand Down Expand Up @@ -42,8 +41,7 @@ class AfricanCitySerializer(serializers.ModelSerializer):
country_flag_url = serializers.SerializerMethodField()

def get_country_flag_url(self, obj):
request = self.context.get('request') # Get the request context for absolute URLs
return obj.get_country_flag_url(request)
return obj.get_country_flag_url()

class Meta:
fields = ('id', 'country_name', 'country_flag_url', 'city')
Expand Down
4 changes: 2 additions & 2 deletions src/website/apps/board/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class BoardMemberAdmin(nested_admin.NestedModelAdmin):
inlines = (BoardMemberBiographyInline,)

def image_tag(self, obj):
width, height = 100, 200
width, height = 150, 200
from django.utils.html import format_html

if obj.picture:
return format_html(f'<img src="{obj.get_picture_url()}" height="{height}" />')
return format_html(f'<img src="{obj.get_picture_url()}" height="{height}" width="{width}" />')
return '-'

image_tag.short_description = "Image Preview"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.1.4 on 2024-12-06 16:24

import cloudinary.models
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('board', '0006_alter_boardmember_picture'),
]

operations = [
migrations.AlterField(
model_name='boardmember',
name='picture',
field=cloudinary.models.CloudinaryField(blank=True, max_length=255, null=True),
),
]
13 changes: 6 additions & 7 deletions src/website/apps/board/models.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from django.db import models
from utils.fields import ConditionalImageField
from cloudinary.models import CloudinaryField
from utils.models import BaseModel


class BoardMember(BaseModel):
name = models.CharField(max_length=100)
title = models.CharField(max_length=100)

picture = ConditionalImageField(
local_upload_to='boardmembers/pictures/',
cloudinary_folder='website/uploads/team/board_members',
picture = CloudinaryField(
folder='website/uploads/team/board_members',
null=True,
blank=True
blank=True,
resource_type='image',
)

twitter = models.URLField(max_length=255, null=True, blank=True)
Expand All @@ -26,8 +26,7 @@ def __str__(self):

def get_picture_url(self):
if self.picture:
# Secure URL is handled internally by Cloudinary or the file system
return self.picture.url
return self.picture.url # Cloudinary provides the actual URL of the uploaded image
return None


Expand Down
4 changes: 3 additions & 1 deletion src/website/apps/board/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ class Meta:
fields = '__all__'

def get_picture_url(self, obj):
return obj.get_picture_url() # Secure or local URL is handled inside the model
if obj.picture:
return obj.picture.url # Cloudinary automatically provides the correct URL
return None
Loading

0 comments on commit 782e9ff

Please sign in to comment.