Skip to content

Commit 09595b8

Browse files
committed
Merged in [19176] from rjsparks@nostrum.com:
Use a cdn for the oidc profile photo url. - Legacy-Id: 19178 Note: SVN reference [19176] has been migrated to Git commit c906a8e
2 parents 6599881 + c906a8e commit 09595b8

4 files changed

Lines changed: 27 additions & 3 deletions

File tree

ietf/ietfauth/utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from django.db.models import Q
1919
from django.http import HttpResponseRedirect
2020
from django.shortcuts import get_object_or_404
21-
from django.urls import reverse as urlreverse
2221
from django.utils.decorators import available_attrs
2322
from django.utils.http import urlquote
2423

@@ -211,8 +210,7 @@ def openid_userinfo(claims, user):
211210
person = get_object_or_404(Person, user=user)
212211
email = person.email()
213212
if person.photo:
214-
photo_path = urlreverse('ietf.person.views.photo', kwargs={'email_or_name': person.email()})
215-
photo_url = settings.IDTRACKER_BASE_URL + photo_path
213+
photo_url = person.cdn_photo_url()
216214
else:
217215
photo_url = ''
218216
claims.update( {

ietf/person/models.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,20 @@ def available_api_endpoints(self):
244244
from ietf.ietfauth.utils import has_role
245245
return list(set([ (v, n) for (v, n, r) in PERSON_API_KEY_VALUES if r==None or has_role(self.user, r) ]))
246246

247+
def cdn_photo_url(self, size=80):
248+
if self.photo:
249+
if settings.SERVE_CDN_PHOTOS:
250+
source_url = self.photo.url
251+
if source_url.startswith(settings.IETF_HOST_URL):
252+
source_url = source_url[len(settings.IETF_HOST_URL):]
253+
return f'{settings.IETF_HOST_URL}cdn-cgi/image/fit=scale-down,width={size},height={size}{source_url}'
254+
else:
255+
datatracker_photo_path = urlreverse('ietf.person.views.photo', kwargs={'email_or_name': self.email()})
256+
datatracker_photo_url = settings.IDTRACKER_BASE_URL + datatracker_photo_path
257+
return datatracker_photo_url
258+
else:
259+
return ''
260+
247261

248262
class PersonExtResource(models.Model):
249263
person = ForeignKey(Person)

ietf/person/tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212

1313
from django.http import HttpRequest
14+
from django.test import override_settings
1415
from django.urls import reverse as urlreverse
1516
from django.utils.encoding import iri_to_uri
1617

@@ -179,6 +180,16 @@ def test_absolute_url(self):
179180
p = PersonFactory()
180181
self.assertEqual(p.get_absolute_url(), iri_to_uri('/person/%s' % p.name))
181182

183+
@override_settings(SERVE_CDN_PHOTOS=True)
184+
def test_cdn_photo_url_cdn_on(self):
185+
p = PersonFactory(with_bio=True)
186+
self.assertIn('cdn-cgi/image',p.cdn_photo_url())
187+
188+
@override_settings(SERVE_CDN_PHOTOS=False)
189+
def test_cdn_photo_url_cdn_off(self):
190+
p = PersonFactory(with_bio=True)
191+
self.assertNotIn('cdn-cgi/photo',p.cdn_photo_url())
192+
182193
class PersonUtilsTests(TestCase):
183194
def test_determine_merge_order(self):
184195
p1 = get_person_no_user()

ietf/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
# Example: "/var/www/example.com/static/"
153153

154154

155+
SERVE_CDN_PHOTOS = True
155156

156157
SERVE_CDN_FILES_LOCALLY_IN_DEV_MODE = True
157158

0 commit comments

Comments
 (0)