first comit
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
24
venv/lib/python3.10/site-packages/django_gravatar/compat.py
Normal file
24
venv/lib/python3.10/site-packages/django_gravatar/compat.py
Normal file
@@ -0,0 +1,24 @@
|
||||
"""
|
||||
This module provides compatibility with older versions of Django and Python
|
||||
"""
|
||||
|
||||
try:
|
||||
from urllib.parse import urlparse, parse_qs, quote_plus, urlencode
|
||||
except ImportError: # Python 2
|
||||
from urlparse import urlparse, parse_qs
|
||||
from urllib import quote_plus, urlencode
|
||||
|
||||
try:
|
||||
from urllib.request import Request, urlopen
|
||||
except ImportError: # Python 2
|
||||
from urllib2 import Request, urlopen
|
||||
|
||||
try:
|
||||
from urllib.error import HTTPError
|
||||
except ImportError: # Python 2
|
||||
from urllib2 import HTTPError
|
||||
|
||||
try:
|
||||
from urllib.error import URLError
|
||||
except ImportError: # Python 2
|
||||
from urllib2 import URLError
|
||||
108
venv/lib/python3.10/site-packages/django_gravatar/helpers.py
Normal file
108
venv/lib/python3.10/site-packages/django_gravatar/helpers.py
Normal file
@@ -0,0 +1,108 @@
|
||||
import hashlib
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from .compat import urlencode, urlopen, Request, HTTPError, URLError
|
||||
|
||||
# These options can be used to change the default image if no gravatar is found
|
||||
GRAVATAR_DEFAULT_IMAGE_404 = '404'
|
||||
GRAVATAR_DEFAULT_IMAGE_MYSTERY_MAN = 'mm'
|
||||
GRAVATAR_DEFAULT_IMAGE_IDENTICON = 'identicon'
|
||||
GRAVATAR_DEFAULT_IMAGE_MONSTER = 'monsterid'
|
||||
GRAVATAR_DEFAULT_IMAGE_WAVATAR = 'wavatar'
|
||||
GRAVATAR_DEFAULT_IMAGE_RETRO = 'retro'
|
||||
|
||||
# These options can be used to restrict gravatar content
|
||||
GRAVATAR_RATING_G = 'g'
|
||||
GRAVATAR_RATING_PG = 'pg'
|
||||
GRAVATAR_RATING_R = 'r'
|
||||
GRAVATAR_RATING_X = 'x'
|
||||
|
||||
# Get Gravatar base url from settings.py
|
||||
GRAVATAR_URL = getattr(settings, 'GRAVATAR_URL', 'http://www.gravatar.com/')
|
||||
GRAVATAR_SECURE_URL = getattr(settings, 'GRAVATAR_SECURE_URL', 'https://secure.gravatar.com/')
|
||||
|
||||
# Get user defaults from settings.py
|
||||
GRAVATAR_DEFAULT_SIZE = getattr(settings, 'GRAVATAR_DEFAULT_SIZE', 80)
|
||||
GRAVATAR_DEFAULT_IMAGE = getattr(settings, 'GRAVATAR_DEFAULT_IMAGE',
|
||||
GRAVATAR_DEFAULT_IMAGE_MYSTERY_MAN)
|
||||
GRAVATAR_DEFAULT_RATING = getattr(settings, 'GRAVATAR_DEFAULT_RATING',
|
||||
GRAVATAR_RATING_G)
|
||||
GRAVATAR_DEFAULT_SECURE = getattr(settings, 'GRAVATAR_DEFAULT_SECURE', True)
|
||||
|
||||
|
||||
def calculate_gravatar_hash(email):
|
||||
# Calculate the email hash
|
||||
enc_email = email.strip().lower().encode("utf-8")
|
||||
email_hash = hashlib.md5(enc_email).hexdigest()
|
||||
return email_hash
|
||||
|
||||
|
||||
def get_gravatar_url(email, size=GRAVATAR_DEFAULT_SIZE, default=GRAVATAR_DEFAULT_IMAGE,
|
||||
rating=GRAVATAR_DEFAULT_RATING, secure=GRAVATAR_DEFAULT_SECURE):
|
||||
"""
|
||||
Builds a url to a gravatar from an email address.
|
||||
|
||||
:param email: The email to fetch the gravatar for
|
||||
:param size: The size (in pixels) of the gravatar to fetch
|
||||
:param default: What type of default image to use if the gravatar does not exist
|
||||
:param rating: Used to filter the allowed gravatar ratings
|
||||
:param secure: If True use https, otherwise plain http
|
||||
"""
|
||||
if secure:
|
||||
url_base = GRAVATAR_SECURE_URL
|
||||
else:
|
||||
url_base = GRAVATAR_URL
|
||||
|
||||
# Calculate the email hash
|
||||
email_hash = calculate_gravatar_hash(email)
|
||||
|
||||
# Build querystring
|
||||
query_string = urlencode({
|
||||
's': str(size),
|
||||
'd': default,
|
||||
'r': rating,
|
||||
})
|
||||
|
||||
# Build url
|
||||
url = '{base}avatar/{hash}.jpg?{qs}'.format(base=url_base,
|
||||
hash=email_hash, qs=query_string)
|
||||
|
||||
return url
|
||||
|
||||
|
||||
def has_gravatar(email):
|
||||
"""
|
||||
Returns True if the user has a gravatar, False if otherwise
|
||||
"""
|
||||
# Request a 404 response if the gravatar does not exist
|
||||
url = get_gravatar_url(email, default=GRAVATAR_DEFAULT_IMAGE_404)
|
||||
|
||||
# Verify an OK response was received
|
||||
try:
|
||||
request = Request(url)
|
||||
request.get_method = lambda: 'HEAD'
|
||||
return 200 == urlopen(request).code
|
||||
except (HTTPError, URLError):
|
||||
return False
|
||||
|
||||
|
||||
def get_gravatar_profile_url(email, secure=GRAVATAR_DEFAULT_SECURE):
|
||||
"""
|
||||
Builds a url to a gravatar profile from an email address.
|
||||
|
||||
:param email: The email to fetch the gravatar for
|
||||
:param secure: If True use https, otherwise plain http
|
||||
"""
|
||||
if secure:
|
||||
url_base = GRAVATAR_SECURE_URL
|
||||
else:
|
||||
url_base = GRAVATAR_URL
|
||||
|
||||
# Calculate the email hash
|
||||
email_hash = calculate_gravatar_hash(email)
|
||||
|
||||
# Build url
|
||||
url = '{base}{hash}'.format(base=url_base, hash=email_hash)
|
||||
|
||||
return url
|
||||
@@ -0,0 +1 @@
|
||||
"""Stub models.py"""
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,57 @@
|
||||
from django import template
|
||||
from django.utils.html import escape
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
from ..helpers import GRAVATAR_DEFAULT_SIZE, get_gravatar_profile_url, get_gravatar_url
|
||||
|
||||
# Get template.Library instance
|
||||
register = template.Library()
|
||||
|
||||
|
||||
def gravatar_url(user_or_email, size=GRAVATAR_DEFAULT_SIZE):
|
||||
""" Builds a gravatar url from an user or email """
|
||||
if hasattr(user_or_email, 'email'):
|
||||
email = user_or_email.email
|
||||
else:
|
||||
email = user_or_email
|
||||
|
||||
try:
|
||||
return escape(get_gravatar_url(email=email, size=size))
|
||||
except:
|
||||
return ''
|
||||
|
||||
|
||||
def gravatar(user_or_email, size=GRAVATAR_DEFAULT_SIZE, alt_text='', css_class='gravatar'):
|
||||
""" Builds an gravatar <img> tag from an user or email """
|
||||
if hasattr(user_or_email, 'email'):
|
||||
email = user_or_email.email
|
||||
else:
|
||||
email = user_or_email
|
||||
|
||||
try:
|
||||
url = escape(get_gravatar_url(email=email, size=size))
|
||||
except:
|
||||
return ''
|
||||
|
||||
return mark_safe(
|
||||
'<img class="{css_class}" src="{src}" width="{width}"'
|
||||
' height="{height}" alt="{alt}" />'.format(
|
||||
css_class=css_class, src=url, width=size, height=size, alt=alt_text
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def gravatar_profile_url(user_or_email):
|
||||
if hasattr(user_or_email, 'email'):
|
||||
email = user_or_email.email
|
||||
else:
|
||||
email = user_or_email
|
||||
|
||||
try:
|
||||
return escape(get_gravatar_profile_url(email))
|
||||
except:
|
||||
return ''
|
||||
|
||||
register.simple_tag(gravatar_url)
|
||||
register.simple_tag(gravatar)
|
||||
register.simple_tag(gravatar_profile_url)
|
||||
171
venv/lib/python3.10/site-packages/django_gravatar/tests.py
Normal file
171
venv/lib/python3.10/site-packages/django_gravatar/tests.py
Normal file
@@ -0,0 +1,171 @@
|
||||
from django.template import Context, Template
|
||||
from django.test import TestCase
|
||||
from django.utils.html import escape
|
||||
|
||||
from .compat import parse_qs, quote_plus, urlparse
|
||||
from .helpers import *
|
||||
|
||||
|
||||
class TestGravatarHelperMethods(TestCase):
|
||||
|
||||
def test_gravatar_hash_generation(self):
|
||||
"""
|
||||
Verify the generation of hash from email string.
|
||||
"""
|
||||
email = "MyEmailAddress@example.com"
|
||||
email_hash = "0bc83cb571cd1c50ba6f3e8a78ef1346"
|
||||
|
||||
self.assertEqual(calculate_gravatar_hash(email), email_hash)
|
||||
self.assertEqual(calculate_gravatar_hash(email), calculate_gravatar_hash(email.lower()))
|
||||
|
||||
def test_gravatar_url(self):
|
||||
"""
|
||||
Verify that the gravatar_url method returns the expected output.
|
||||
"""
|
||||
email = "joe@example.com"
|
||||
email_upper = "JOE@example.com"
|
||||
email_strip = " JOE@example.com "
|
||||
|
||||
# Construct the url
|
||||
url = get_gravatar_url(email)
|
||||
|
||||
# Verify email is properly sanitized
|
||||
self.assertEqual(url, get_gravatar_url(email_upper))
|
||||
self.assertEqual(url, get_gravatar_url(email_strip))
|
||||
|
||||
# Parse query string from url
|
||||
urlp = urlparse(url)
|
||||
qs = parse_qs(urlp.query)
|
||||
|
||||
# Verify the correct query arguments are included with the proper defaults
|
||||
self.assertTrue('s' in qs)
|
||||
self.assertTrue('d' in qs)
|
||||
self.assertTrue('r' in qs)
|
||||
|
||||
self.assertEqual(qs.get('s').pop(), str(GRAVATAR_DEFAULT_SIZE))
|
||||
self.assertEqual(qs.get('d').pop(), GRAVATAR_DEFAULT_IMAGE)
|
||||
self.assertEqual(qs.get('r').pop(), GRAVATAR_DEFAULT_RATING)
|
||||
|
||||
# Verify the correct protocol is used
|
||||
if GRAVATAR_DEFAULT_SECURE:
|
||||
self.assertTrue(GRAVATAR_SECURE_URL in url)
|
||||
else:
|
||||
self.assertTrue(GRAVATAR_URL in url)
|
||||
|
||||
# Verify that a url value for default is urlencoded
|
||||
default_url = 'https://www.example.com/default.jpg'
|
||||
url = get_gravatar_url(email, default=default_url)
|
||||
|
||||
# Verify urlencoding
|
||||
self.assertTrue(quote_plus(default_url) in url)
|
||||
|
||||
def test_has_gravatar(self):
|
||||
"""
|
||||
Verify that the has_gravatar helper method correctly
|
||||
determines if a user has a gravatar or not.
|
||||
"""
|
||||
bad_email = 'eve@example.com'
|
||||
good_email = 'matt@automattic.com'
|
||||
|
||||
self.assertFalse(has_gravatar(bad_email))
|
||||
self.assertTrue(has_gravatar(good_email))
|
||||
|
||||
def test_gravatar_profile_url(self):
|
||||
"""
|
||||
Verify that the get_gravatar_profile_url helper method correctly
|
||||
generates a profile url for gravatar user.
|
||||
"""
|
||||
email = 'joe@example.com'
|
||||
profile_url = get_gravatar_profile_url(email)
|
||||
email_hash = calculate_gravatar_hash(email)
|
||||
|
||||
self.assertTrue(profile_url.endswith(email_hash))
|
||||
|
||||
|
||||
class TestGravatarTemplateTags(TestCase):
|
||||
def test_gravatar_url(self):
|
||||
email = 'matt@automattic.com'
|
||||
context = Context({'email': email})
|
||||
|
||||
t = Template("{% load gravatar %}{% gravatar_url email %}")
|
||||
rendered = t.render(context)
|
||||
|
||||
self.assertEqual(rendered, escape(get_gravatar_url(email)))
|
||||
|
||||
def test_gravatar_img(self):
|
||||
# Some defaults for testing
|
||||
email = 'matt@automattic.com'
|
||||
alt_text = 'some alt text'
|
||||
css_class = 'gravatar-thumb'
|
||||
size = 250
|
||||
|
||||
# Build context
|
||||
context = Context({
|
||||
'email': email,
|
||||
'size': size,
|
||||
'alt_text': alt_text,
|
||||
'css_class': css_class,
|
||||
})
|
||||
|
||||
# Default behavior
|
||||
t = Template("{% load gravatar %}{% gravatar email %}")
|
||||
rendered = t.render(context)
|
||||
|
||||
self.assertTrue(escape(get_gravatar_url(email)) in rendered)
|
||||
self.assertTrue('class="gravatar"' in rendered)
|
||||
self.assertTrue('alt=""' in rendered)
|
||||
|
||||
t = Template("{% load gravatar %}{% gravatar email size alt_text css_class %}")
|
||||
rendered = t.render(context)
|
||||
|
||||
self.assertTrue('width="%s"' % (size,) in rendered)
|
||||
self.assertTrue('height="%s"' % (size,) in rendered)
|
||||
self.assertTrue('alt="%s"' % (alt_text,) in rendered)
|
||||
self.assertTrue('class="%s"' % (css_class,) in rendered)
|
||||
|
||||
def test_gravatar_user_url(self):
|
||||
# class with email attribute
|
||||
class user:
|
||||
email = 'bouke@webatoom.nl'
|
||||
|
||||
context = Context({'user': user})
|
||||
|
||||
t = Template("{% load gravatar %}{% gravatar_url user %}")
|
||||
rendered = t.render(context)
|
||||
|
||||
self.assertEqual(rendered, escape(get_gravatar_url(user.email)))
|
||||
|
||||
def test_gravatar_user_img(self):
|
||||
# class with email attribute
|
||||
class user:
|
||||
email = 'bouke@webatoom.nl'
|
||||
|
||||
context = Context({'user': user})
|
||||
|
||||
t = Template("{% load gravatar %}{% gravatar user %}")
|
||||
rendered = t.render(context)
|
||||
|
||||
self.assertTrue(escape(get_gravatar_url(user.email)) in rendered)
|
||||
|
||||
def test_invalid_input(self):
|
||||
context = Context({'email': None})
|
||||
|
||||
t = Template("{% load gravatar %}{% gravatar email %}")
|
||||
rendered = t.render(context)
|
||||
|
||||
self.assertEqual("", rendered, "Invalid input should return empty result")
|
||||
|
||||
def test_gravatar_profile_url(self):
|
||||
"""
|
||||
Verify the profile url generated from template gravatar_profile_url tag.
|
||||
"""
|
||||
# class with email attribute
|
||||
class user:
|
||||
email = 'bouke@webatoom.nl'
|
||||
|
||||
context = Context({'user': user})
|
||||
|
||||
t = Template("{% load gravatar %}{% gravatar_profile_url user %}")
|
||||
rendered = t.render(context)
|
||||
|
||||
self.assertEqual(rendered, escape(get_gravatar_profile_url(user.email)))
|
||||
@@ -0,0 +1 @@
|
||||
"""Stub views.py"""
|
||||
Reference in New Issue
Block a user