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) 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": 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() photo_temp.seek(0) # Delete old avatars if they exist avatars = Avatar.objects.filter(user=request.user) for avatar in avatars: avatar.avatar.storage.delete(avatar.avatar.name) 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 avatar.avatar.storage.save(path, File(photo_temp)) avatar.save() return HttpResponseRedirect( reverse("profile_index") + "?changed_avatar=True") raise Http404
def setup_profile(request): """Display page 4 (profile) of the first login wizard.""" # Fields with file uploads are not AJAX requests. if request.method == "POST": form = ProfileForm(request.POST, user=request.user) profile = request.user.profile if form.is_valid(): profile.name = form.cleaned_data["display_name"].strip() if not profile.setup_profile: profile.setup_profile = True profile.add_points(score_mgr.setup_points(), datetime.datetime.today(), "Set up profile") profile.save() if form.cleaned_data["pic_method"] == 0: name = request.user for avatar in Avatar.objects.filter(user=name): avatar.delete() elif form.cleaned_data["pic_method"] == 2 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"] photo_temp.write(urllib2.urlopen(fb_url).read()) photo_temp.flush() photo_temp.seek(0) path = avatar_file_path(user=request.user, filename="fb_photo.jpg") avatar = Avatar( user=request.user, primary=True, avatar=path, ) avatar.avatar.storage.save(path, File(photo_temp)) avatar.save() return HttpResponseRedirect(reverse("setup_activity")) return _get_profile_form(request, form=form, non_xhr=False) return _get_profile_form(request)
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.storage.delete(avatar.avatar.name) avatar.avatar.delete() avatar.delete() path = avatar_file_path(user=user, filename=uploaded_file.name) avatar = Avatar( user=user, primary=True, avatar=path, ) avatar.avatar.storage.save(path, uploaded_file) avatar.save()