Skip to content

Commit

Permalink
Add LibRavatar support (#114) (#225)
Browse files Browse the repository at this point in the history
* feat: add libravatar support (#114) and format

* feat: add documentation for libravatar

* fix(gh-actions): remove support for 3.6 and add 3.11

3.6 reached EOL: https://devguide.python.org/versions/

I also refactored the matrix by removing duplicated code

* fix(deps): add missing dnspython

* feat(deps): add requirements.txt

* fix(gh-actions): install deps

* chore(deps): add pyproject.toml 

See pypa/pip#8559

* fix(gh-actions): add fail-fast so all checks run

* fix(deps): bump coverage to 7.1.0

* fix(pre-commit): update versions

* fix(pre-commit): config error

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* style: update code for passing flake

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: 0xMRTT <0xMRTT@evta.fr>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 22, 2023
1 parent 675315b commit 8034665
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 29 deletions.
26 changes: 5 additions & 21 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,13 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11']
django-version: ['3.2', '4.0', '4.1']
include:
- python-version: 3.6
django-version: 3.2
- python-version: 3.7
django-version: 3.2
- python-version: 3.8
django-version: 3.2
- python-version: 3.9
django-version: 3.2
- python-version: '3.10'
django-version: 3.2
- python-version: 3.8
django-version: 4.0
- python-version: 3.9
django-version: 4.0
- python-version: '3.10'
django-version: 4.0
- python-version: 3.8
django-version: 4.1
- python-version: 3.9
django-version: 4.1
- python-version: '3.10'
django-version: 4.1
fail-fast: false

steps:
- uses: actions/checkout@v3
- name: 'Set up Python ${{ matrix.python-version }}'
Expand All @@ -37,7 +21,7 @@ jobs:
cache: 'pip'
- name: Install dependencies
run: |
pip install -e .
pip install -r requirements.txt
pip install -r tests/requirements.txt
pip install "Django~=${{ matrix.django-version }}.0" .
- name: Run Tests
Expand Down
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.4.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/pycqa/isort
rev: "5.10.1"
rev: "5.12.0"
hooks:
- id: isort
args: ["--profile", "black"]

- repo: https://github.com/psf/black
rev: 22.10.0
rev: 23.1.0
hooks:
- id: black
args: [--target-version=py310]

- repo: https://github.com/pycqa/flake8
rev: '5.0.4'
rev: '6.0.0'
hooks:
- id: flake8
additional_dependencies:
Expand Down
1 change: 1 addition & 0 deletions avatar/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class AvatarConf(AppConf):
DELETE_TEMPLATE = ""
PROVIDERS = (
"avatar.providers.PrimaryAvatarProvider",
"avatar.providers.LibRAvatarProvider",
"avatar.providers.GravatarAvatarProvider",
"avatar.providers.DefaultAvatarProvider",
)
Expand Down
1 change: 0 additions & 1 deletion avatar/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


class Migration(migrations.Migration):

dependencies = [
("avatar", "0001_initial"),
]
Expand Down
1 change: 0 additions & 1 deletion avatar/migrations/0003_auto_20170827_1345.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
("avatar", "0002_add_verbose_names_to_avatar_fields"),
]
Expand Down
27 changes: 27 additions & 0 deletions avatar/providers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import hashlib
import re
from urllib.parse import urlencode, urljoin

import dns.resolver
from django.utils.module_loading import import_string

from avatar.conf import settings
Expand Down Expand Up @@ -64,6 +66,31 @@ def get_avatar_url(cls, user, width, _height=None):
return urljoin(settings.AVATAR_GRAVATAR_BASE_URL, path)


class LibRAvatarProvider:
"""
Returns the url of an avatar by the Ravatar service.
"""

@classmethod
def get_avatar_url(cls, user, width, _height=None):
email = getattr(user, settings.AVATAR_GRAVATAR_FIELD).encode("utf-8")
_, domain = email.split(b"@")
try:
answers = dns.resolver.query("_avatars._tcp." + domain, "SRV")
hostname = re.sub(r"\.$", "", str(answers[0].target))
# query returns "example.com." and while http requests are fine with this,
# https most certainly do not consider "example.com." and "example.com" to be the same.
port = str(answers[0].port)
if port == "443":
baseurl = "https://" + hostname + "/avatar/"
else:
baseurl = "http://" + hostname + ":" + port + "/avatar/"
except Exception:
baseurl = "https://seccdn.libravatar.org/avatar/"
hash = hashlib.md5(email.strip().lower()).hexdigest()
return baseurl + hash


class FacebookAvatarProvider(object):
"""
Returns the url of a Facebook profile image.
Expand Down
1 change: 1 addition & 0 deletions docs/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ appear on the site. Listed below are those settings:

(
'avatar.providers.PrimaryAvatarProvider',
'avatar.providers.LibRAvatarProvider',
'avatar.providers.GravatarAvatarProvider',
'avatar.providers.DefaultAvatarProvider',
)
Expand Down
50 changes: 50 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[tool.poetry]
name = "django-avatar"
version = "7.0.1"
description = "A Django app for handling user avatars"
authors = ["Eric Florenzano <floguy@gmail.com>"]
maintainers = ["Johannes Wilm <johannes@fiduswriter.org>"]
license = "BSD-4-Clause"
readme = "README.rst"
packages = [
{ include = "avatar" },
]
exclude = [
{ path = "tests"},
]
homepage = "https://github.com/jazzband/django-avatar"
repository = "https://github.com/jazzband/django-avatar"
documentation = "https://django-avatar.readthedocs.io"
keywords=["avatar", "django"]
classifiers=[
"Development Status :: 5 - Production/Stable",
"Environment :: Web Environment",
"Framework :: Django",
"Intended Audience :: Developers",
"Framework :: Django",
"Framework :: Django :: 3.2",
"Framework :: Django :: 4.0",
"Framework :: Django :: 4.1",
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
]

[tool.poetry.dependencies]
python = "^3.7"
Pillow = "^9.4.0"
django-appconf = "^1.0.5"
dnspython = "^2.3.0"

[tool.poetry.group.dev.dependencies]
coverage = "^7.1.0"
python-magic = "^0.4.27"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Pillow>=8.4.0
django-appconf>=1.0.5
dnspython>=2.3.0
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def find_version(*file_paths):
install_requires=[
"Pillow>=8.4.0",
"django-appconf>=1.0.5",
"dnspython>=2.3.0",
],
zip_safe=False,
)
2 changes: 1 addition & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
coverage==6.2
coverage==7.1.0
django
python-magic

0 comments on commit 8034665

Please sign in to comment.