AvatarServiceHook¶
reviewboard.extensions.hooks.AvatarServiceHook allows extensions to
register new avatar services, which can be used to integrate with databases or
servers to handle the display of user avatars.
Extensions must provide a subclass of
djblets.avatars.services.AvatarService, and pass it as a parameter
to AvatarServiceHook. Each class must provide
avatar_service_id and name attributes, and must implement
get_avatar_urls_uncached() and get_etag_data() methods.
Example¶
This example makes use of a theoretical service that can return image files for a given user based on a hash of their e-mail address and a desired pixel size.
import hashlib
from typing import TYPE_CHECKING
from djblets.avatars.services import AvatarService
from reviewboard.extensions.base import Extension
from reviewboard.extensions.hooks import AvatarServiceHook
if TYPE_CHECKING:
from collections.abc import Mapping, Sequence
from django.contrib.auth.models import User
class SampleAvatarService(AvatarService):
avatar_service_id = 'myvendor_sample_avatar_service'
name = 'Sample Avatar Service'
def get_avatar_urls_uncached(
self,
user: User,
size: int,
) -> Mapping[str, str]:
url = 'https://pictures.example.com/?user=%s&size=%d'
user_hash = hashlib.md5(user.email)
return {
'%dx' % resolution: url % (user_hash, size * resolution)
for resolution in (1, 2, 3)
}
def get_etag_data(
self,
user: User,
) -> Sequence[str]:
return [self.avatar_service_id, user.email]
class SampleExtension(Extension):
def initialize(self) -> None:
AvatarServiceHook(self, SampleAvatarService)