mirror of
https://github.com/jazzband/django-avatar.git
synced 2026-04-05 15:51:00 +00:00
51 lines
1.2 KiB
Python
51 lines
1.2 KiB
Python
from avatar.conf import settings
|
|
from html.parser import HTMLParser
|
|
|
|
|
|
class HTMLTagParser(HTMLParser):
|
|
"""
|
|
URL parser for getting (url ,width ,height) from avatar templatetags
|
|
"""
|
|
|
|
def __init__(self, output=None):
|
|
HTMLParser.__init__(self)
|
|
if output is None:
|
|
self.output = {}
|
|
else:
|
|
self.output = output
|
|
|
|
def handle_starttag(self, tag, attrs):
|
|
self.output.update(dict(attrs))
|
|
|
|
|
|
def assign_width_or_height(query_params):
|
|
avatar_default_size = settings.AVATAR_DEFAULT_SIZE
|
|
|
|
width = query_params.get('width', avatar_default_size)
|
|
height = query_params.get('height', avatar_default_size)
|
|
|
|
if width == '':
|
|
width = avatar_default_size
|
|
if height == '':
|
|
height = avatar_default_size
|
|
|
|
if height == avatar_default_size and height != '':
|
|
height = width
|
|
elif width == avatar_default_size and width != '':
|
|
width = height
|
|
|
|
width = int(width)
|
|
height = int(height)
|
|
|
|
context = {
|
|
'width': width,
|
|
'height': height
|
|
}
|
|
return context
|
|
|
|
|
|
def set_new_primary(query_set, instance):
|
|
queryset = query_set.exclude(id=instance.id).first()
|
|
if queryset:
|
|
queryset.primary = True
|
|
queryset.save()
|