def setup_profile(request): """ Displays the profile form. """ if request.is_ajax(): return _get_profile_form(request) # Fields with file uploads are not AJAX requests. if request.method == "POST": form = ProfileForm(request.POST, user=request.user) profile = request.user.get_profile() # print request if form.is_valid(): profile.name = form.cleaned_data["display_name"].strip() if not profile.setup_profile: profile.setup_profile = True profile.add_points(5, datetime.datetime.today(), "Set up profile") profile.save() 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, ) # print "saving avatar to " + path new_file = avatar.avatar.storage.save(path, request.FILES['avatar']) avatar.save() elif form.cleaned_data["use_fb_photo"] and form.cleaned_data[ "facebook_photo"]: # Need to download the image from the url and save it. photo_temp = NamedTemporaryFile(delete=True) fb_url = form.cleaned_data["facebook_photo"] # print fb_url photo_temp.write(urllib2.urlopen(fb_url).read()) photo_temp.flush() path = avatar_file_path(user=request.user, filename="fb_photo.jpg") avatar = Avatar( user=request.user, primary=True, avatar=path, ) # print "saving facebook photo to " + path new_file = avatar.avatar.storage.save(path, File(photo_temp)) avatar.save() return HttpResponseRedirect(reverse("setup_activity")) return _get_profile_form(request, form=form, non_xhr=True) raise Http404
def setup_profile(request): """ Displays the profile form. """ if request.is_ajax(): return _get_profile_form(request) # Fields with file uploads are not AJAX requests. if request.method == "POST": form = ProfileForm(request.POST) profile = request.user.get_profile() # print request if form.is_valid(): profile.name = form.cleaned_data["display_name"] profile.about = form.cleaned_data["about"] if not profile.setup_profile: profile.setup_profile = True profile.add_points(5, datetime.datetime.today()) profile.save() 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, ) # print "saving avatar to " + path new_file = avatar.avatar.storage.save(path, request.FILES['avatar']) avatar.save() elif form.cleaned_data["use_fb_photo"] and form.cleaned_data["facebook_photo"]: # Need to download the image from the url and save it. photo_temp = NamedTemporaryFile(delete=True) fb_url = form.cleaned_data["facebook_photo"] # print fb_url photo_temp.write(urllib2.urlopen(fb_url).read()) photo_temp.flush() path = avatar_file_path(user=request.user, filename="fb_photo.jpg") avatar = Avatar( user = request.user, primary = True, avatar = path, ) # print "saving facebook photo to " + path new_file = avatar.avatar.storage.save(path, File(photo_temp)) avatar.save() return setup_activity(request, non_xhr=True) return _get_profile_form(request, form=form, non_xhr=True) raise Http404
def upload_fb(request): """Uploads the user's picture from Facebook.""" if request.method == "POST": form = FacebookPictureForm(request.POST) if form.is_valid(): # Need to download the image from the url and save it. photo_temp = NamedTemporaryFile(delete=True) fb_url = form.cleaned_data["facebook_photo"] photo_temp.write(urllib2.urlopen(fb_url).read()) photo_temp.flush() # Delete old avatars if they exist # avatars = Avatar.objects.filter(user=request.user) # for avatar in avatars: # avatar.avatar.delete() # avatar.delete() path = avatar_file_path(user=request.user, filename="fb_photo.jpg") avatar = Avatar( user = request.user, primary = True, avatar = path, ) # print "saving facebook photo to " + path new_file = avatar.avatar.storage.save(path, File(photo_temp)) avatar.save() return HttpResponseRedirect(reverse("profile_index") + "?changed_avatar=True") raise Http404
def upload_fb(request): """Uploads the user's picture from Facebook.""" if request.method == "POST": form = FacebookPictureForm(request.POST) if form.is_valid(): # Need to download the image from the url and save it. photo_temp = NamedTemporaryFile(delete=True) fb_url = form.cleaned_data["facebook_photo"] photo_temp.write(urllib2.urlopen(fb_url).read()) photo_temp.flush() # Delete old avatars if they exist # avatars = Avatar.objects.filter(user=request.user) # for avatar in avatars: # avatar.avatar.delete() # avatar.delete() path = avatar_file_path(user=request.user, filename="fb_photo.jpg") avatar = Avatar( user=request.user, primary=True, avatar=path, ) # print "saving facebook photo to " + path new_file = avatar.avatar.storage.save(path, File(photo_temp)) avatar.save() return HttpResponseRedirect( reverse("profile_index") + "?changed_avatar=True") raise Http404
def __handle_uploaded_file(uploaded_file, user): if uploaded_file.size > SIZE_LIMIT: raise Exception("File is too large") # Delete old avatars if they exist # avatars = Avatar.objects.filter(user=user) # for avatar in avatars: # avatar.avatar.delete() # avatar.delete() path = avatar_file_path(user=user, filename=uploaded_file.name) avatar = Avatar( user=user, primary=True, avatar=path, ) new_file = avatar.avatar.storage.save(path, uploaded_file) avatar.save()
def __handle_uploaded_file(uploaded_file, user): if uploaded_file.size > SIZE_LIMIT: raise Exception("File is too large") # Delete old avatars if they exist # avatars = Avatar.objects.filter(user=user) # for avatar in avatars: # avatar.avatar.delete() # avatar.delete() path = avatar_file_path(user=user, filename=uploaded_file.name) avatar = Avatar( user = user, primary = True, avatar = path, ) new_file = avatar.avatar.storage.save(path, uploaded_file) avatar.save()
def testSetProfilePic(self): """ Tests that this predicate is completed when the user sets a profile pic. """ # Need to disconnect create thumbnail signal temporarily for test so that additional image # files don't get created. signals.post_save.disconnect(create_default_thumbnails, Avatar) self.assertFalse(set_profile_pic(self.user), "User should not have their profile pic set.") image_path = os.path.join(settings.PROJECT_ROOT, "fixtures", "test_images", "test.jpg") image = ImageFile(open(image_path, "r")) path = avatar_file_path(user=self.user, filename="test.jpg") avatar = Avatar(user=self.user, avatar=path, primary=True) new_file = avatar.avatar.storage.save(path, image) avatar.save() self.assertTrue(set_profile_pic(self.user), "User should have their profile pic set.") # Test within context of a quest. avatar.delete() self.quest.unlock_conditions = "set_profile_pic()" self.quest.save() self.assertTrue(self.quest not in get_quests(self.user), "User should not be able to participate in this quest.") self.quest.unlock_conditions = "not set_profile_pic()" self.quest.save() self.assertTrue(self.quest in get_quests(self.user)["available_quests"], "User should be able to participate in this quest.") self.quest.accept(self.user) self.quest.completion_conditions = "set_profile_pic()" self.quest.save() self.assertTrue(self.quest not in possibly_completed_quests(self.user), "User should not be able to complete this quest.") avatar = Avatar(user=self.user, avatar=path, primary=True) avatar.save() self.assertTrue(self.quest in possibly_completed_quests(self.user), "User should have completed this quest.") # Be sure to clean up test files and reconnect post_save signal. signals.post_save.connect(create_default_thumbnails, sender=Avatar) for avatar in self.user.avatar_set.all(): avatar.avatar.delete() avatar.delete()
def upload_fb(request): """Uploads the user's picture from Facebook.""" if request.method == "POST": # Need to download the image from the url and save it. photo_temp = NamedTemporaryFile(delete=True) fb_url = "http://graph.facebook.com/%s/picture?type=large" % request.user.facebookprofile.profile_id # print fb_url photo_temp.write(urllib2.urlopen(fb_url).read()) photo_temp.flush() path = avatar_file_path(user=request.user, filename="fb_photo.jpg") avatar = Avatar( user = request.user, primary = True, avatar = path, ) # print "saving facebook photo to " + path new_file = avatar.avatar.storage.save(path, File(photo_temp)) avatar.save() return HttpResponseRedirect(reverse("profile_index")) raise Http404
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}) # 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 HttpResponseRedirect(reverse("profile_index") + "?changed_avatar=True") fb_user = facebook.get_user_from_cookie(request.COOKIES, settings.FACEBOOK_APP_ID, settings.FACEBOOK_SECRET_KEY) fb_id = None if fb_user: try: graph = facebook.GraphAPI(fb_user["access_token"]) graph_profile = graph.get_object("me") fb_id = graph_profile["id"] except facebook.GraphAPIError: pass return render_to_response( 'makahiki_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), 'fb_id': fb_id,} ) )