Source code for openassembly.pirate_profile.templatetags.profiletags
from django import template
from django import forms
from django.http import HttpResponseRedirect
import datetime
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from tagging.models import Tag, TaggedItem
from pirate_profile.models import Profile
from pirate_core.helpers import clean_html
from pirate_sources.models import IMGSource
import pytz
from pirate_core import HttpRedirectException, namespace_get
from customtags.decorators import block_decorator
register = template.Library()
block = block_decorator(register)
get_namespace = namespace_get('pp_profile')
@block
[docs]def pp_get_user_profile(context, nodelist, *args, **kwargs):
'''
This block tag can create or process forms to get tags.
Usage is as follows:
{% pp_get_user_profile user=requet.object %}
Do stuff with {{ pp_profile.user }} and {{ pp_profile.profile }}.
{% endpp_get_user_profile %}
'''
context.push()
namespace = get_namespace(context)
user = kwargs.get('user', None)
if user is not None and isinstance(user, User):
try:
profile = Profile.objects.get(user=user)
except:
profile = None
elif user is not None and isinstance(user, Profile):
profile = user
user = profile.user
else:
profile = None
user = None
namespace['user'] = user
namespace['profile'] = profile
output = nodelist.render(context)
context.pop()
return output
@block
[docs]def pp_avatar_thumbnail(context, nodelist, *args, **kwargs):
'''
This block tag can create or process forms either to create or to modify arguments.
Usage is as follows:
{% pp_profile_form POST=request.POST object=request.object %}
Do stuff with {{ pp_profile.form }}.
{% endpp_profile_form %}
'''
context.push()
namespace = get_namespace(context)
pk = kwargs.get('pk', None)
try:
img = IMGSource.objects.get(pk=pk)
except:
img = '/static/img/avatar_20x18.jpg'
namespace['avatar_url'] = img.url
output = nodelist.render(context)
context.pop()
return output
@block
@block
[docs]def pp_get_avatar(context, nodelist, *args, **kwargs):
context.push()
namespace = get_namespace(context)
user = kwargs.get('user', None)
try:
img = IMGSource.objects.get(object_pk=user.pk, current=True)
url = img.thumbnail_large.url
thumbnail = img.thumbnail.url
thumbnail_small = img.thumbnail_small.url
has_avatar = True
except:
url = '/static/img/avatar_180x160.jpg'
thumbnail = '/static/img/avatar_70x60.jpg'
thumbnail_small = '/static/img/avatar_20x18.jpg'
has_avatar = False
#get_serving_url will serve up full size images as well as transformed images
namespace['avatar_url'] = url
namespace['thumbnail'] = thumbnail
namespace['thumbnail_small'] = thumbnail_small
namespace['has_avatar'] = has_avatar
output = nodelist.render(context)
context.pop()
return output