def change_avatar(request, target_obj, target_type, from_name, extra_context={}, next_override=None, current_app='plus_groups', namespace='groups', **kwargs): # XXX some of this should probably be refactored into the model layer target = target_obj.get_ref() avatars = Avatar.objects.filter(target=target).order_by('-primary') if avatars.count() > 0: avatar = avatars[0] kwargs = {'initial': {'choice': avatar.id}} else: avatar = None kwargs = {} primary_avatar_form = PrimaryAvatarForm(request.POST or None, target=target, **kwargs) if request.method == "POST": if 'avatar' in request.FILES: path = avatar_file_path(target=target, filename=request.FILES['avatar'].name) avatar = Avatar( target=target, primary=True, avatar=path, ) new_file = avatar.avatar.storage.save(path, request.FILES['avatar']) avatar.save() request.user.message_set.create( message=_("Successfully uploaded a new avatar.")) 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() request.user.message_set.create( message=_("Successfully updated your avatar.")) return HttpResponseRedirect(next_override or _get_next(request)) return render_to_response( 'avatar/change.html', extra_context, context_instance=RequestContext( request, { 'avatar': avatar, 'avatars': avatars, 'primary_avatar_form': primary_avatar_form, 'next': next_override or _get_next(request), 'target': target_obj, 'target_type': target_type, 'from_name': from_name, }))
def change_avatar(request, id, extra_context={}, next_override=None): user_edit = get_object_or_404(User, pk=id) try: profile = Profile.objects.get(user=user_edit) except Profile.DoesNotExist: profile = Profile.objects.create_profile(user=user_edit) #if not has_perm(request.user,'profiles.change_profile',profile): raise Http403 if not profile.allow_edit_by(request.user): raise Http403 avatars = Avatar.objects.filter(user=user_edit).order_by('-primary') if avatars.count() > 0: avatar = avatars[0] kwargs = {'initial': {'choice': avatar.id}} else: avatar = None kwargs = {} primary_avatar_form = PrimaryAvatarForm(request.POST or None, user=user_edit, **kwargs) if request.method == "POST": updated = False if 'avatar' in request.FILES: path = avatar_file_path(user=user_edit, filename=request.FILES['avatar'].name) avatar = Avatar( user = user_edit, primary = True, avatar = path, ) new_file = avatar.avatar.storage.save(path, request.FILES['avatar']) avatar.save() updated = True request.user.message_set.create( message=_("Successfully uploaded a new avatar.")) 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() updated = True request.user.message_set.create( message=_("Successfully updated your avatar.")) if updated and notification: notification.send([request.user], "avatar_updated", {"user": user_edit, "avatar": avatar}) #if friends: # notification.send((x['friend'] for x in Friendship.objects.friends_for_user(user_edit)), "avatar_friend_updated", {"user": user_edit, "avatar": avatar}) return HttpResponseRedirect(reverse('profile', args=[user_edit.username])) #return HttpResponseRedirect(next_override or _get_next(request)) return render_to_response( 'profiles/change_avatar.html', extra_context, context_instance = RequestContext( request, {'user_this': user_edit, 'avatar': avatar, 'avatars': avatars, 'primary_avatar_form': primary_avatar_form, 'next': next_override or _get_next(request), } ) )
def change(request, extra_context={}, next_override=None): avatars = Avatar.objects.filter(user=request.user).order_by('-primary') if avatars.count() > 0: avatar = avatars[0] kwargs = {'initial': {'choice': avatar.id}} else: avatar = None kwargs = {} primary_avatar_form = PrimaryAvatarForm(request.POST or None, user=request.user, **kwargs) if request.method == "POST": updated = False if 'avatar' in request.FILES: path = avatar_file_path(user=request.user, filename=request.FILES['avatar'].name) avatar = Avatar( user=request.user, primary=True, avatar=path, ) new_file = avatar.avatar.storage.save(path, request.FILES['avatar']) avatar.save() updated = True request.user.message_set.create( message=_("Successfully uploaded a new avatar.")) 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() updated = True request.user.message_set.create( message=_("Successfully updated your avatar.")) if updated and notification: 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 }) return HttpResponseRedirect(next_override or _get_next(request)) return render_to_response( 'avatar/change.html', extra_context, context_instance=RequestContext( request, { 'avatar': avatar, 'avatars': avatars, 'primary_avatar_form': primary_avatar_form, 'next': next_override or _get_next(request), }))
def change_avatar(request, target_obj, target_type, from_name, extra_context={}, next_override=None, current_app='plus_groups',namespace='groups',**kwargs): # XXX some of this should probably be refactored into the model layer target = target_obj.get_ref() avatars = Avatar.objects.filter(target=target).order_by('-primary') if avatars.count() > 0: avatar = avatars[0] kwargs = {'initial': {'choice': avatar.id}} else: avatar = None kwargs = {} primary_avatar_form = PrimaryAvatarForm(request.POST or None, target=target, **kwargs) if request.method == "POST": if 'avatar' in request.FILES: path = avatar_file_path(target=target, filename=request.FILES['avatar'].name) avatar = Avatar( target = target, primary = True, avatar = path, ) new_file = avatar.avatar.storage.save(path, request.FILES['avatar']) avatar.save() request.user.message_set.create( message=_("Successfully uploaded a new avatar.")) 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() request.user.message_set.create( message=_("Successfully updated your avatar.")) return HttpResponseRedirect(next_override or _get_next(request)) return render_to_response( 'avatar/change.html', extra_context, context_instance = RequestContext( request, { 'avatar': avatar, 'avatars': avatars, 'primary_avatar_form': primary_avatar_form, 'next': next_override or _get_next(request), 'target' : target_obj, 'target_type' : target_type, 'from_name' : from_name, } ) )
def change(request, extra_context={}, next_override=None): avatars = Avatar.objects.filter(user=request.user).order_by('-primary') if avatars.count() > 0: avatar = avatars[0] kwargs = {'initial': {'choice': avatar.id}} else: avatar = None kwargs = {} primary_avatar_form = PrimaryAvatarForm( request.POST or None, user=request.user, **kwargs) if request.method == "POST": updated = False if 'avatar' in request.FILES: path = avatar_file_path(user=request.user, filename=request.FILES['avatar'].name) avatar = Avatar( user=request.user, primary=True, avatar=path, ) new_file = avatar.avatar.storage.save( path, request.FILES['avatar']) avatar.save() updated = True messages.info(request, _("Successfully uploaded a new avatar.")) 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() updated = True messages.info(request, _("Successfully updated your avatar.")) if updated and notification: 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}) return HttpResponseRedirect(next_override or _get_next(request)) return render_to_response( 'avatar/change.html', extra_context, context_instance=RequestContext( request, {'avatar': avatar, 'avatars': avatars, 'primary_avatar_form': primary_avatar_form, 'next': next_override or _get_next(request), } ) )
def change(request, extra_context={}, next_override=None): avatars = Avatar.objects.filter(user=request.user).order_by("-primary") if avatars.count() > 0: avatar = avatars[0] kwargs = {"initial": {"choice": avatar.id}} else: avatar = None kwargs = {} primary_avatar_form = PrimaryAvatarForm(request.POST or None, user=request.user, **kwargs) if request.method == "POST": updated = False if "avatar" in request.FILES: path = avatar_file_path(user=request.user, filename=request.FILES["avatar"].name) avatar = Avatar(user=request.user, primary=True, avatar=path) new_file = avatar.avatar.storage.save(path, request.FILES["avatar"]) avatar.save() updated = True # request.user.message_set.create( # message=_("Successfully uploaded a new avatar.")) 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() updated = True # request.user.message_set.create( # message=_("Successfully updated your avatar.")) if updated and notification: 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}, ) return HttpResponseRedirect(next_override or _get_next(request)) return render_to_response( "avatar/change.html", extra_context, context_instance=RequestContext( request, { "avatar": avatar, "avatars": avatars, "primary_avatar_form": primary_avatar_form, "next": next_override or _get_next(request), }, ), )
def set_primary(request, user_id=None, extra_context=None, avatar_size=128): """changes default uploaded avatar""" user = get_object_or_404(User, pk=user_id) if request.method == "POST": updated = False form = PrimaryAvatarForm( request.POST, user=user, avatars=user.avatar_set.all() ) if 'choice' in request.POST and form.is_valid(): avatar = Avatar.objects.get(id=form.cleaned_data['choice']) avatar.primary = True avatar.save() avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar) user.clear_avatar_cache() return redirect_to_show_list(user_id)
def set_primary(request, user_id=None, extra_context=None, avatar_size=128): """changes default uploaded avatar""" user = get_object_or_404(User, pk=user_id) if request.method == "POST": updated = False form = PrimaryAvatarForm(request.POST, user=user, avatars=user.avatar_set.all()) if 'choice' in request.POST and form.is_valid(): avatar = Avatar.objects.get(id=form.cleaned_data['choice']) avatar.primary = True avatar.save() avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar) user.avatar_type = 'a' user.clear_avatar_urls() user.save() return redirect_to_show_list(user_id)
def change(request, extra_context={}, next_override=None): avatars = Avatar.objects.filter(user=request.user).order_by('-primary') if avatars.count() > 0: avatar = avatars[0] kwargs = {'initial': {'choice': avatar.id}} else: avatar = None kwargs = {} primary_avatar_form = PrimaryAvatarForm(request.POST or None, user=request.user, **kwargs) if request.method == "POST": if 'avatar' in request.FILES: path = avatar_file_path(user=request.user, filename=request.FILES['avatar'].name) avatar = Avatar( user = request.user, primary = True, avatar = path, ) new_file = avatar.avatar.storage.save(path, request.FILES['avatar']) avatar.save() request.user.message_set.create( message=_("Successfully uploaded a new avatar.")) 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() request.user.message_set.create( message=_("Successfully updated your avatar.")) return HttpResponseRedirect(next_override or _get_next(request)) return render_to_response( 'avatar/change.html', extra_context, context_instance = RequestContext( request, { 'avatar': avatar, 'avatars': avatars, 'primary_avatar_form': primary_avatar_form, 'next': next_override or _get_next(request), } ) )
def change(request, extra_context={}, next_override=None): avatars = Avatar.objects.filter(user=request.user).order_by('-primary') if avatars.count() > 0: avatar = avatars[0] kwargs = {'initial': {'choice': avatar.id}} else: avatar = None kwargs = {} primary_avatar_form = PrimaryAvatarForm(request.POST or None, user=request.user, **kwargs) if request.method == "POST": if 'avatar' in request.FILES: path = avatar_file_path(user=request.user, filename=request.FILES['avatar'].name) try: os.makedirs(os.path.join( settings.MEDIA_ROOT, "/".join(path.split('/')[:-1]))) except OSError, e: pass # The dirs already exist. new_file = default_storage.open(path, 'wb') for i, chunk in enumerate(request.FILES['avatar'].chunks()): if i * 16 == MAX_MEGABYTES: raise Http404 # TODO: Is this the right thing to do? # Validation error maybe, instead? new_file.write(chunk) avatar = Avatar( user = request.user, primary = True, avatar = path, ) avatar.save() new_file.close() request.user.message_set.create( message=_("Successfully uploaded a new avatar.")) 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() request.user.message_set.create( message=_("Successfully updated your avatar.")) return HttpResponseRedirect(next_override or _get_next(request))
def post(self, *args, **kwargs): avatar, avatars = _get_avatars(self.request.user) if 'add' in self.request.POST and 'avatar' in self.request.FILES: upload_avatar_form = UploadAvatarForm(self.request.POST, self.request.FILES, user=self.request.user) if upload_avatar_form.is_valid(): avatar = Avatar(user=self.request.user, primary=True) image_file = self.request.FILES['avatar'] avatar.avatar.save(image_file.name, image_file) avatar.save() messages.success(self.request, 'Successfully uploaded a new avatar.') avatar_updated.send(sender=Avatar, user=self.request.user, avatar=avatar) return HttpResponseRedirect(reverse('avatar_change')) elif 'change' in self.request.POST: primary_avatar_form = PrimaryAvatarForm(self.request.POST, user=self.request.user, avatars=avatars) if 'choice' in self.request.POST and primary_avatar_form.is_valid( ): avatar = Avatar.objects.get( id=primary_avatar_form.cleaned_data['choice']) avatar.primary = True avatar.save() avatar_updated.send(sender=Avatar, user=self.request.user, avatar=avatar) messages.success(self.request, 'Successfully updated your avatar.') return HttpResponseRedirect(reverse('avatar_change')) elif 'delete' in self.request.POST: delete_avatar_form = PrimaryAvatarForm(self.request.POST, user=self.request.user, avatars=avatars) if delete_avatar_form.is_valid(): ids = delete_avatar_form.cleaned_data['choice'] if six.text_type( avatar.id) in ids and avatars.count() > len(ids): for a in avatars: if six.text_type(a.id) not in ids: a.primary = True a.save() avatar_updated.send(sender=Avatar, user=self.request.user, avatar=avatar) break Avatar.objects.filter(id__in=ids).delete() messages.success(self.request, 'Selected avatar successfully deleted.') return HttpResponseRedirect(reverse('avatar_change')) return HttpResponseRedirect(reverse('avatar_change'))
def get_context_data(self, **kwargs): context = super(AvatarChangeView, self).get_context_data(**kwargs) avatar, avatars = _get_avatars(self.request.user) if avatar: avatar_kwargs = {'initial': {'choice': avatar.id}} else: avatar_kwargs = {} upload_avatar_form = UploadAvatarForm(None, None, user=self.request.user) primary_avatar_form = PrimaryAvatarForm(None, user=self.request.user, avatars=avatars, **avatar_kwargs) context.update({ 'avatar': avatar, 'avatars': avatars, 'upload_avatar_form': upload_avatar_form, 'primary_avatar_form': primary_avatar_form, }) return context
def change_avatar(request, id, extra_context={}, next_override=None): user_edit = get_object_or_404(User, pk=id) try: profile = Profile.objects.get(user=user_edit) except Profile.DoesNotExist: profile = Profile.objects.create_profile(user=user_edit) #if not has_perm(request.user,'profiles.change_profile',profile): raise Http403 if not profile.allow_edit_by(request.user): raise Http403 avatars = Avatar.objects.filter(user=user_edit).order_by('-primary') if avatars.count() > 0: avatar = avatars[0] kwargs = {'initial': {'choice': avatar.id}} else: avatar = None kwargs = {} primary_avatar_form = PrimaryAvatarForm(request.POST or None, user=user_edit, **kwargs) if request.method == "POST": updated = False if 'avatar' in request.FILES: path = avatar_file_path(user=user_edit, filename=request.FILES['avatar'].name) avatar = Avatar( user=user_edit, primary=True, avatar=path, ) new_file = avatar.avatar.storage.save(path, request.FILES['avatar']) avatar.save() updated = True messages.add_message(request, messages.SUCCESS, _("Successfully uploaded a new avatar.")) 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() updated = True messages.add_message(request, messages.SUCCESS, _("Successfully updated your avatar.")) if updated and notification: notification.send([request.user], "avatar_updated", { "user": user_edit, "avatar": avatar }) #if friends: # notification.send((x['friend'] for x in Friendship.objects.friends_for_user(user_edit)), "avatar_friend_updated", {"user": user_edit, "avatar": avatar}) return HttpResponseRedirect( reverse('profile', args=[user_edit.username])) #return HttpResponseRedirect(next_override or _get_next(request)) return render_to_response( 'profiles/change_avatar.html', extra_context, context_instance=RequestContext( request, { 'user_this': user_edit, 'avatar': avatar, 'avatars': avatars, 'primary_avatar_form': primary_avatar_form, 'next': next_override or _get_next(request), }))
def edit_settings(request): """ Allow user to edit own profile. Requires login. There are 4 buttons on this page: choose avatar, delete avatar, upload avatar, change settings. """ userdata, userdatacreated = \ UserSettings.objects.get_or_create(user=request.user) xfuserdata, xfuserdatacreated = \ xfforms.UserSettings.objects.get_or_create(user=request.user) # ! XXX: Actually, the avatars code was mostly copied from avatar/views.py # Which is problematic if avatar module is updated # but blends in much better. from avatar.models import Avatar, avatar_file_path from avatar.forms import PrimaryAvatarForm, UploadAvatarForm import avatar.views uavatar, avatars = avatar.views._get_avatars(request.user) if uavatar: kwargs = {'initial': {'choice': uavatar.id}} else: kwargs = {} settings_form = xfsettings_form = primary_avatar_form = \ upload_avatar_form = None if request.method == 'POST': if 'updatesettings' in request.POST: # Settings were edited. settings_form = UserSettingsForm(request.POST, instance=userdata, user=request.user) xfsettings_form = xfforms.UserSettingsForm(request.POST, instance=xfuserdata, user=request.user) if settings_form.is_valid(): settings_form.save(commit=True) # ! I don't see very much of a problem if only one is valid. if xfsettings_form.is_valid(): xfsettings_form.save(commit=True) elif 'avatar' in request.FILES: # New avatar upload submitted. return avatar.views.add(request, next_override=request.path) elif 'choice' in request.POST: # This part was rewritten to use the same form for choosing the # default and deleting, i.e. using single choice for both # instead of multiple choice for deleteavatar. primary_avatar_form = PrimaryAvatarForm(request.POST, user=request.user, avatars=avatars, **kwargs) if primary_avatar_form.is_valid(): # Selection / deletion form. savatar = get_object_or_404(Avatar, id=primary_avatar_form.cleaned_data['choice']) if 'defaultavatar' in request.POST: # "choose" was pressed # ! Maybe should check if it's the same avatar. savatar.primary = True savatar.save() avatar.views._notification_updated(request, savatar) request.user.message_set.create( message=_("Successfully updated your avatar.")) # No need for redirect here, seemingly. elif 'deleteavatar' in request.POST: # "delete" was pressed if unicode(uavatar.id) == id and avatars.count() > 1: # Find the next best avatar, and set it as the new # primary for av in avatars: if unicode(av.id) != id: av.primary = True av.save() avatar.views._notification_updated( request, av) break savatar.delete() request.user.message_set.create( message=_("Deletion successful.")) return HttpResponseRedirect(request.path) # (reload) # ! Create what was not created; cannot create all of them always # because most of them will fail validation then. settings_form = settings_form or UserSettingsForm(None, instance=userdata, user=request.user) xfsettings_form = xfsettings_form or xfforms.UserSettingsForm(None, instance=xfuserdata, user=request.user) upload_avatar_form = upload_avatar_form or UploadAvatarForm(None, user=request.user) primary_avatar_form = primary_avatar_form or PrimaryAvatarForm(None, user=request.user, avatars=avatars, **kwargs) return render_to_response( 'snapboard/edit_settings', {'settings_form': settings_form, 'xfsettings_form': xfsettings_form, 'upload_avatar_form': upload_avatar_form, 'primary_avatar_form': primary_avatar_form, 'avatar': avatar, 'avatars': avatars, }, context_instance=RequestContext(request, processors=extra_processors))