2008-08-01 09:27:59 +00:00
|
|
|
import os.path
|
|
|
|
|
|
2008-10-20 01:11:10 +00:00
|
|
|
from avatar.models import Avatar, avatar_file_path
|
2010-01-22 10:59:58 +00:00
|
|
|
from avatar.forms import PrimaryAvatarForm, DeleteAvatarForm, UploadAvatarForm
|
2008-10-28 06:43:52 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2008-10-20 01:11:10 +00:00
|
|
|
from django.shortcuts import render_to_response
|
2008-08-01 09:27:59 +00:00
|
|
|
from django.template import RequestContext
|
|
|
|
|
from django.contrib.auth.decorators import login_required
|
2008-08-02 05:36:33 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2008-08-01 09:27:59 +00:00
|
|
|
|
2009-04-21 02:38:40 +00:00
|
|
|
from django.db.models import get_app
|
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
2010-01-21 17:33:03 +00:00
|
|
|
from avatar import AVATAR_MAX_AVATARS_PER_USER
|
|
|
|
|
|
2009-04-21 02:38:40 +00:00
|
|
|
try:
|
|
|
|
|
notification = get_app('notification')
|
|
|
|
|
except ImproperlyConfigured:
|
|
|
|
|
notification = None
|
|
|
|
|
|
2009-10-21 09:45:32 +00:00
|
|
|
friends = False
|
|
|
|
|
if 'friends' in settings.INSTALLED_APPS:
|
2009-04-21 02:38:40 +00:00
|
|
|
friends = True
|
2009-11-09 20:28:03 +00:00
|
|
|
from friends.models import Friendship
|
2009-04-21 02:38:40 +00:00
|
|
|
|
2008-08-01 09:27:59 +00:00
|
|
|
def _get_next(request):
|
|
|
|
|
"""
|
|
|
|
|
The part that's the least straightforward about views in this module is how they
|
|
|
|
|
determine their redirects after they have finished computation.
|
|
|
|
|
|
|
|
|
|
In short, they will try and determine the next place to go in the following order:
|
|
|
|
|
|
|
|
|
|
1. If there is a variable named ``next`` in the *POST* parameters, the view will
|
|
|
|
|
redirect to that variable's value.
|
|
|
|
|
2. If there is a variable named ``next`` in the *GET* parameters, the view will
|
|
|
|
|
redirect to that variable's value.
|
|
|
|
|
3. If Django can determine the previous page from the HTTP headers, the view will
|
|
|
|
|
redirect to that previous page.
|
|
|
|
|
"""
|
|
|
|
|
next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', None)))
|
2008-08-12 08:12:38 +00:00
|
|
|
if not next:
|
|
|
|
|
next = request.path
|
2008-08-01 09:27:59 +00:00
|
|
|
return next
|
2010-01-22 10:59:58 +00:00
|
|
|
|
|
|
|
|
def _notification_updated(request, avatar):
|
|
|
|
|
notification.send([request.user], "avatar_updated", {"user": request.user, "avatar": avatar})
|
|
|
|
|
if friends:
|
|
|
|
|
notification.send((x['friend'] for x in Friendship.objects.friends_for_user(request.user)), "avatar_friend_updated", {"user": request.user, "avatar": avatar})
|
2010-01-22 13:49:53 +00:00
|
|
|
|
|
|
|
|
def _get_avatars(user):
|
|
|
|
|
# Default set. Needs to be sliced, but that's it. Keep the natural order.
|
|
|
|
|
avatars = user.avatar_set.all()
|
2010-01-22 10:59:58 +00:00
|
|
|
|
2010-01-22 13:49:53 +00:00
|
|
|
# Current avatar
|
|
|
|
|
avatar = avatars.filter(primary=True)[:1]
|
2010-01-22 13:54:35 +00:00
|
|
|
if avatar:
|
|
|
|
|
avatar = avatar[0]
|
|
|
|
|
else:
|
|
|
|
|
avatar = None
|
2010-01-22 13:49:53 +00:00
|
|
|
|
|
|
|
|
# Slice the default set now that we used the queryset for the primary avatar
|
2010-01-21 17:33:03 +00:00
|
|
|
avatars = avatars[:AVATAR_MAX_AVATARS_PER_USER]
|
2010-01-22 13:49:53 +00:00
|
|
|
return (avatar, avatars)
|
|
|
|
|
|
|
|
|
|
def add(request, extra_context={}, next_override=None):
|
|
|
|
|
avatar, avatars = _get_avatars(request.user)
|
2010-01-22 10:59:58 +00:00
|
|
|
upload_avatar_form = UploadAvatarForm(request.POST or None,
|
|
|
|
|
request.FILES or None, user=request.user)
|
|
|
|
|
if request.method == "POST" and 'avatar' in request.FILES:
|
|
|
|
|
if upload_avatar_form.is_valid():
|
2008-08-12 08:12:38 +00:00
|
|
|
path = avatar_file_path(user=request.user,
|
|
|
|
|
filename=request.FILES['avatar'].name)
|
|
|
|
|
avatar = Avatar(
|
|
|
|
|
user = request.user,
|
|
|
|
|
primary = True,
|
|
|
|
|
avatar = path,
|
|
|
|
|
)
|
2008-10-28 06:39:21 +00:00
|
|
|
new_file = avatar.avatar.storage.save(path, request.FILES['avatar'])
|
2008-08-12 08:12:38 +00:00
|
|
|
avatar.save()
|
2009-04-21 02:38:40 +00:00
|
|
|
updated = True
|
2008-08-12 08:12:38 +00:00
|
|
|
request.user.message_set.create(
|
|
|
|
|
message=_("Successfully uploaded a new avatar."))
|
2010-01-22 10:59:58 +00:00
|
|
|
if notification:
|
|
|
|
|
_notification_updated(request, avatar)
|
|
|
|
|
else:
|
|
|
|
|
# from IPython.Shell import IPShellEmbed; IPShellEmbed()()
|
|
|
|
|
print upload_avatar_form.errors
|
|
|
|
|
return render_to_response(
|
|
|
|
|
'avatar/add.html',
|
|
|
|
|
extra_context,
|
|
|
|
|
context_instance = RequestContext(
|
|
|
|
|
request,
|
|
|
|
|
{ 'avatar': avatar,
|
|
|
|
|
'avatars': avatars,
|
|
|
|
|
'upload_avatar_form': upload_avatar_form,
|
|
|
|
|
'next': next_override or _get_next(request), }
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
add = login_required(add)
|
|
|
|
|
|
|
|
|
|
def change(request, extra_context={}, next_override=None):
|
2010-01-22 13:49:53 +00:00
|
|
|
avatar, avatars = _get_avatars(request.user)
|
2010-01-22 10:59:58 +00:00
|
|
|
if avatar:
|
|
|
|
|
kwargs = {'initial': {'choice': avatar.id}}
|
|
|
|
|
else:
|
|
|
|
|
kwargs = {}
|
2010-01-22 11:50:56 +00:00
|
|
|
upload_avatar_form = UploadAvatarForm(user=request.user, **kwargs)
|
2010-01-22 10:59:58 +00:00
|
|
|
primary_avatar_form = PrimaryAvatarForm(request.POST or None,
|
|
|
|
|
user=request.user, avatars=avatars, **kwargs)
|
|
|
|
|
if request.method == "POST":
|
|
|
|
|
updated = False
|
2008-08-12 08:12:38 +00:00
|
|
|
if 'choice' in request.POST and primary_avatar_form.is_valid():
|
|
|
|
|
avatar = Avatar.objects.get(id=
|
|
|
|
|
primary_avatar_form.cleaned_data['choice'])
|
|
|
|
|
avatar.primary = True
|
|
|
|
|
avatar.save()
|
2009-04-21 02:38:40 +00:00
|
|
|
updated = True
|
2008-08-12 08:12:38 +00:00
|
|
|
request.user.message_set.create(
|
|
|
|
|
message=_("Successfully updated your avatar."))
|
2009-04-21 02:38:40 +00:00
|
|
|
if updated and notification:
|
2010-01-22 10:59:58 +00:00
|
|
|
_notification_updated(request, avatar)
|
2008-08-01 09:27:59 +00:00
|
|
|
return HttpResponseRedirect(next_override or _get_next(request))
|
|
|
|
|
return render_to_response(
|
|
|
|
|
'avatar/change.html',
|
|
|
|
|
extra_context,
|
|
|
|
|
context_instance = RequestContext(
|
|
|
|
|
request,
|
2008-08-03 22:37:57 +00:00
|
|
|
{ 'avatar': avatar,
|
2008-08-12 08:12:38 +00:00
|
|
|
'avatars': avatars,
|
2010-01-22 11:50:56 +00:00
|
|
|
'upload_avatar_form': upload_avatar_form,
|
2008-08-12 08:12:38 +00:00
|
|
|
'primary_avatar_form': primary_avatar_form,
|
|
|
|
|
'next': next_override or _get_next(request), }
|
2008-08-01 09:27:59 +00:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
change = login_required(change)
|
|
|
|
|
|
|
|
|
|
def delete(request, extra_context={}, next_override=None):
|
2010-01-22 13:49:53 +00:00
|
|
|
avatar, avatars = _get_avatars(request.user)
|
2010-01-21 16:45:27 +00:00
|
|
|
delete_avatar_form = DeleteAvatarForm(request.POST or None,
|
|
|
|
|
user=request.user, avatars=avatars)
|
2008-08-01 09:27:59 +00:00
|
|
|
if request.method == 'POST':
|
2008-08-12 08:12:38 +00:00
|
|
|
if delete_avatar_form.is_valid():
|
|
|
|
|
ids = delete_avatar_form.cleaned_data['choices']
|
2009-04-20 20:31:40 +00:00
|
|
|
if unicode(avatar.id) in ids and avatars.count() > len(ids):
|
|
|
|
|
for a in avatars:
|
|
|
|
|
if unicode(a.id) not in ids:
|
|
|
|
|
a.primary = True
|
|
|
|
|
a.save()
|
2010-01-22 13:31:06 +00:00
|
|
|
if notification:
|
|
|
|
|
_notification_updated(request, a)
|
2009-04-20 20:31:40 +00:00
|
|
|
break
|
2008-08-12 08:12:38 +00:00
|
|
|
Avatar.objects.filter(id__in=ids).delete()
|
|
|
|
|
request.user.message_set.create(
|
|
|
|
|
message=_("Successfully deleted the requested avatars."))
|
|
|
|
|
return HttpResponseRedirect(next_override or _get_next(request))
|
2008-08-01 09:27:59 +00:00
|
|
|
return render_to_response(
|
|
|
|
|
'avatar/confirm_delete.html',
|
|
|
|
|
extra_context,
|
|
|
|
|
context_instance = RequestContext(
|
|
|
|
|
request,
|
2008-08-12 08:12:38 +00:00
|
|
|
{ 'avatar': avatar,
|
|
|
|
|
'avatars': avatars,
|
|
|
|
|
'delete_avatar_form': delete_avatar_form,
|
|
|
|
|
'next': next_override or _get_next(request), }
|
2008-08-01 09:27:59 +00:00
|
|
|
)
|
|
|
|
|
)
|
2010-02-09 00:46:43 +00:00
|
|
|
delete = login_required(delete)
|