def email_validation_reset(request): """ Resend the validation email """ if request.user.is_authenticated(): try: resend = EmailValidation.objects.exclude(verified=True).get( user=request.user).resend() response = "done" except EmailValidation.DoesNotExist: response = "failed" signal_responses = signals.post_signal.send( sender=email_validation_reset, request=request, extra={'response': response}) return signals.last_response(signal_responses) or HttpResponseRedirect( reverse("email_validation_reset_response", args=[response])) else: if request.method == 'POST': form = ResendEmailValidationForm(request.POST) if form.is_valid(): email = form.cleaned_data.get('email') try: resend = EmailValidation.objects.exclude( verified=True).get(email=email).resend() response = "done" except EmailValidation.DoesNotExist: response = "failed" signal_responses = signals.post_signal.send( sender=email_validation_reset, request=request, extra={'response': response}) return signals.last_response( signal_responses) or HttpResponseRedirect( reverse("email_validation_reset_response", args=[response])) else: form = ResendEmailValidationForm() template = "userprofile/account/email_validation_reset.html" data = { 'form': form, } signals.context_signal.send(sender=email_validation_reset, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def email_validation_process(request, key): """ Verify key and change email """ if EmailValidation.objects.verify(key=key): successful = True else: successful = False signal_responses = signals.post_signal.send( sender=email_validation_process, request=request, extra={ 'key': key, 'successful': successful }) last_response = signals.last_response(signal_responses) if last_response: return last_response template = "userprofile/account/email_validation_done.html" data = { 'successful': successful, } signals.context_signal.send(sender=email_validation_process, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def register(request): if request.method == 'POST': form = _RegistrationForm(request.POST) if form.is_valid(): newuser = form.save() signal_responses = signals.post_signal.send( sender=register, request=request, form=form, extra={'newuser': newuser}) return signals.last_response( signal_responses) or HttpResponseRedirect( reverse('signup_complete')) else: form = _RegistrationForm() template = "userprofile/account/registration.html" data = { 'form': form, } signals.context_signal.send(sender=register, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def location(request): """ Location selection of the user profile """ profile, created = Profile.objects.get_or_create(user=request.user) geoip = hasattr(settings, "GEOIP_PATH") if geoip and request.method == "GET" and request.GET.get('ip') == "1": from django.contrib.gis.utils import GeoIP g = GeoIP() c = g.city(request.META.get("REMOTE_ADDR")) if c and c.get("latitude") and c.get("longitude"): profile.latitude = "%.6f" % c.get("latitude") profile.longitude = "%.6f" % c.get("longitude") profile.country = c.get("country_code") profile.location = unicode(c.get("city"), "latin1") if request.method == "POST": form = LocationForm(request.POST, instance=profile) if form.is_valid(): form.save() request.user.message_set.create(message=_("Your profile information has been updated successfully.")) signal_responses = signals.post_signal.send(sender=location, request=request, form=form) last_reponse = signals.last_response(signal_responses) if last_reponse: return last_response else: form = LocationForm(instance=profile) template = "userprofile/profile/location.html" data = { 'section': 'location', 'GOOGLE_MAPS_API_KEY': GOOGLE_MAPS_API_KEY, 'form': form, 'geoip': geoip } signals.context_signal.send(sender=location, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def personal(request): """ Personal data of the user profile """ profile, created = Profile.objects.get_or_create(user=request.user) if request.method == "POST": old_profile = copy.copy(profile) form = ProfileForm(request.POST, instance=profile) if form.is_valid(): form.save() messages.success(request, _("Your profile information has been updated successfully."), fail_silently=True) signal_responses = signals.post_signal.send(sender=personal, request=request, form=form, extra={'old_profile':old_profile}) last_response = signals.last_response(signal_responses) if last_response: return last_response else: form = ProfileForm(instance=profile) template = "userprofile/profile/personal.html" data = { 'section': 'personal', 'sections': SECTIONS, 'GOOGLE_MAPS_API_KEY': GOOGLE_MAPS_API_KEY, 'form': form, } signals.context_signal.send(sender=personal, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def personal(request): """ Personal data of the user profile """ profile, created = Profile.objects.get_or_create(user=request.user) if request.method == "POST": old_profile = copy.copy(profile) form = ProfileForm(request.POST, instance=profile) if form.is_valid(): form.save() request.user.message_set.create(message=_("Your profile information has been updated successfully.")) signal_responses = signals.post_signal.send( sender=personal, request=request, form=form, extra={"old_profile": old_profile} ) last_reponse = signals.last_response(signal_responses) if last_reponse: return last_response else: form = ProfileForm(instance=profile) template = "userprofile/profile/personal.html" data = {"section": "personal", "GOOGLE_MAPS_API_KEY": GOOGLE_MAPS_API_KEY, "form": form} signals.context_signal.send(sender=personal, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def location(request): """ Location selection of the user profile """ profile, created = Profile.objects.get_or_create(user=request.user) geoip = hasattr(settings, "GEOIP_PATH") if geoip and request.method == "GET" and request.GET.get("ip") == "1": from django.contrib.gis.utils import GeoIP g = GeoIP() c = g.city(request.META.get("REMOTE_ADDR")) if c and c.get("latitude") and c.get("longitude"): profile.latitude = "%.6f" % c.get("latitude") profile.longitude = "%.6f" % c.get("longitude") profile.country = c.get("country_code") profile.location = unicode(c.get("city"), "latin1") if request.method == "POST": form = LocationForm(request.POST, instance=profile) if form.is_valid(): form.save() request.user.message_set.create(message=_("Your profile information has been updated successfully.")) signal_responses = signals.post_signal.send(sender=location, request=request, form=form) last_reponse = signals.last_response(signal_responses) if last_reponse: return last_response else: form = LocationForm(instance=profile) template = "userprofile/profile/location.html" data = {"section": "location", "GOOGLE_MAPS_API_KEY": GOOGLE_MAPS_API_KEY, "form": form, "geoip": geoip} signals.context_signal.send(sender=location, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def delete(request): if request.method == "POST": profile, created = Profile.objects.get_or_create(user=request.user) old_profile = copy.copy(profile) old_user = copy.copy(request.user) # Remove the profile and all the information Profile.objects.filter(user=request.user).delete() EmailValidation.objects.filter(user=request.user).delete() Avatar.objects.filter(user=request.user).delete() # Remove the e-mail of the account too request.user.email = "" request.user.first_name = "" request.user.last_name = "" request.user.save() request.user.message_set.create(message=_("Your profile information has been removed successfully.")) signal_responses = signals.post_signal.send( sender=delete, request=request, extra={"old_profile": old_profile, "old_user": old_user} ) return signals.last_response(signal_responses) or HttpResponseRedirect(reverse("profile_overview")) template = "userprofile/profile/delete.html" data = {"section": "delete"} signals.context_signal.send(sender=delete, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def register(request): if request.method == "POST": form = RegistrationForm(request.POST) if form.is_valid(): username = form.cleaned_data.get("username") password = form.cleaned_data.get("password1") newuser = User.objects.create_user(username=username, email="", password=password) newuser.is_active = ( not hasattr(settings, "REQUIRE_EMAIL_CONFIRMATION") or not settings.REQUIRE_EMAIL_CONFIRMATION ) if form.cleaned_data.get("email"): newuser.email = form.cleaned_data.get("email") EmailValidation.objects.add(user=newuser, email=newuser.email) newuser.save() signal_responses = signals.post_signal.send( sender=register, request=request, form=form, extra={"newuser": newuser} ) return signals.last_response(signal_responses) or HttpResponseRedirect(reverse("signup_complete")) else: form = RegistrationForm() template = "userprofile/account/registration.html" data = {"form": form} signals.context_signal.send(sender=register, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def delete(request): if request.method == "POST": profile, created = Profile.objects.get_or_create(user=request.user) old_profile = copy.copy(profile) old_user = copy.copy(request.user) # Remove the profile and all the information Profile.objects.filter(user=request.user).delete() EmailValidation.objects.filter(user=request.user).delete() Avatar.objects.filter(user=request.user).delete() # Remove the e-mail of the account too request.user.email = '' request.user.first_name = '' request.user.last_name = '' request.user.save() request.user.message_set.create(message=_("Your profile information has been removed successfully.")) signal_responses = signals.post_signal.send(sender=delete, request=request, extra={'old_profile':old_profile, 'old_user': old_user}) return signals.last_response(signal_responses) or HttpResponseRedirect(reverse("profile_overview")) template = "userprofile/profile/delete.html" data = { 'section': 'delete', } signals.context_signal.send(sender=delete, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def email_validation(request): """ E-mail Change form """ if request.method == 'POST': form = EmailValidationForm(request.POST) if form.is_valid(): EmailValidation.objects.add(user=request.user, email=form.cleaned_data.get('email')) signal_responses = signals.post_signal.send( sender=email_validation, request=request, form=form) return signals.last_response( signal_responses) or HttpResponseRedirect( reverse("email_validation_processed")) else: form = EmailValidationForm() template = "userprofile/account/email_validation.html" data = { 'form': form, } signals.context_signal.send(sender=email_validation, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def avatarchoose(request): """ Avatar choose """ profile, created = Profile.objects.get_or_create(user=request.user) images = dict() if hasattr(settings, "AVATAR_QUOTA"): request.upload_handlers.insert(0, QuotaUploadHandler()) if request.method == "POST": form = AvatarForm() if request.POST.get("keyword"): keyword = str(request.POST.get("keyword")) gd_client = gdata.photos.service.PhotosService() feed = gd_client.SearchCommunityPhotos(query="%s&thumbsize=72c" % keyword.split(" ")[0], limit="48") for entry in feed.entry: images[entry.media.thumbnail[0].url] = entry.content.src else: form = AvatarForm(request.POST, request.FILES) if form.is_valid(): image = form.cleaned_data.get("url") or form.cleaned_data.get("photo") thumb = Image.open(ContentFile(image.read())) thumb.thumbnail((480, 480), Image.ANTIALIAS) thumb.convert("RGB") f = StringIO() thumb.save(f, "JPEG") f.seek(0) avatar = Avatar(user=request.user, image="", valid=False) avatar.image.save("%s.jpg" % request.user.username, ContentFile(f.read())) avatar.save() signal_responses = signals.post_signal.send(sender=avatarchoose, request=request, form=form) return signals.last_response(signal_responses) or HttpResponseRedirect(reverse("profile_avatar_crop")) else: form = AvatarForm() if DEFAULT_AVATAR: base, filename = os.path.split(DEFAULT_AVATAR) filename, extension = os.path.splitext(filename) generic96 = "%s/%s.96%s" % (base, filename, extension) generic96 = generic96.replace(settings.STATIC_ROOT, settings.STATIC_URL) else: generic96 = "" template = "userprofile/avatar/choose.html" data = { "generic96": generic96, "form": form, "images": images, "AVATAR_WEBSEARCH": AVATAR_WEBSEARCH, "section": "avatar", } signals.context_signal.send(sender=avatarchoose, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def email_validation_reset(request): """ Resend the validation email """ if request.user.is_authenticated(): try: resend = EmailValidation.objects.get(user=request.user).resend() response = "done" except EmailValidation.DoesNotExist: response = "failed" signal_responses = signals.post_signal.send( sender=email_validation_reset, request=request, extra={"response": response} ) return signals.last_response(signal_responses) or HttpResponseRedirect( reverse("email_validation_reset_response", args=[response]) ) else: if request.method == "POST": form = ResendEmailValidationForm(request.POST) if form.is_valid(): email = form.cleaned_data.get("email") try: resend = EmailValidation.objects.get(email=email).resend() response = "done" except EmailValidation.DoesNotExist: response = "failed" signal_responses = signals.post_signal.send( sender=email_validation_reset, request=request, extra={"response": response} ) return signals.last_response(signal_responses) or HttpResponseRedirect( reverse("email_validation_reset_response", args=[response]) ) else: form = ResendEmailValidationForm() template = "userprofile/account/email_validation_reset.html" data = {"form": form} signals.context_signal.send(sender=email_validation_reset, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def avatarcrop(request): """ Avatar management """ avatar = get_object_or_404(Avatar, user=request.user, valid=False) if not request.method == "POST": form = AvatarCropForm() else: image = Image.open(ContentFile(avatar.image.read())) form = AvatarCropForm(image, request.POST) if form.is_valid(): top = int(form.cleaned_data.get('top')) left = int(form.cleaned_data.get('left')) right = int(form.cleaned_data.get('right')) bottom = int(form.cleaned_data.get('bottom')) if not (top or left or right or bottom): (width, height) = image.size if width > height: diff = (width-height) / 2 left = diff right = width-diff bottom = height else: diff = (height-width) / 2 top = diff right = width bottom = height-diff box = [ left, top, right, bottom ] image = image.crop(box) if hasattr(settings, "AWS_SECRET_ACCESS_KEY"): f = StringIO() image.save(f, image.format) f.seek(0) avatar.image.delete() file_ext = image.content_type.split("/")[1] # "image/gif" => "gif" if file_ext == 'pjpeg': file_ext = 'jpeg' avatar.image.save("%s.%s" % (request.user.username, file_ext), ContentFile(f.read())) else: image.save(avatar.image.path) avatar.valid = True avatar.save() messages.success(request, _("Your new avatar has been saved successfully."), fail_silently=True) signal_responses = signals.post_signal.send(sender=avatarcrop, request=request, form=form) return signals.last_response(signal_responses) or HttpResponseRedirect(reverse("profile_edit_avatar")) template = "userprofile/avatar/crop.html" data = { 'section': 'avatar', 'avatar': avatar, 'form': form, \ 'DEFAULT_AVATAR_SIZE': DEFAULT_AVATAR_SIZE, 'MIN_AVATAR_SIZE': MIN_AVATAR_SIZE } signals.context_signal.send(sender=avatarcrop, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def avatarcrop(request): """ Avatar management """ avatar = get_object_or_404(Avatar, user=request.user, valid=False) if not request.method == "POST": form = AvatarCropForm() else: image = Image.open(ContentFile(avatar.image.read())) form = AvatarCropForm(image, request.POST) if form.is_valid(): top = int(form.cleaned_data.get("top")) left = int(form.cleaned_data.get("left")) right = int(form.cleaned_data.get("right")) bottom = int(form.cleaned_data.get("bottom")) if not (top or left or right or bottom): (width, height) = image.size if width > height: diff = (width - height) / 2 left = diff right = width - diff bottom = height else: diff = (height - width) / 2 top = diff right = width bottom = height - diff box = [left, top, right, bottom] image = image.crop(box) if image.mode not in ("L", "RGB"): image = image.convert("RGB") if hasattr(settings, "AWS_SECRET_ACCESS_KEY"): f = StringIO() image.save(f, "JPEG") f.seek(0) avatar.image.delete() avatar.image.save("%s.jpg" % request.user.username, ContentFile(f.read())) else: image.save(avatar.image.path) avatar.valid = True avatar.save() request.user.message_set.create(message=_("Your new avatar has been saved successfully.")) signal_responses = signals.post_signal.send(sender=avatarcrop, request=request, form=form) return signals.last_response(signal_responses) or HttpResponseRedirect(reverse("profile_edit_avatar")) template = "userprofile/avatar/crop.html" data = {"section": "avatar", "avatar": avatar, "form": form} signals.context_signal.send(sender=avatarcrop, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def avatarcrop(request): """ Avatar management """ avatar = get_object_or_404(Avatar, user=request.user, valid=False) if not request.method == "POST": form = AvatarCropForm() else: image = Image.open(ContentFile(avatar.image.read())) form = AvatarCropForm(image, request.POST) if form.is_valid(): top = int(form.cleaned_data.get('top')) left = int(form.cleaned_data.get('left')) right = int(form.cleaned_data.get('right')) bottom = int(form.cleaned_data.get('bottom')) if not (top or left or right or bottom): (width, height) = image.size if width > height: diff = (width-height) / 2 left = diff right = width-diff bottom = height else: diff = (height-width) / 2 top = diff right = width bottom = height-diff box = [ left, top, right, bottom ] image = image.crop(box) if image.mode not in ('L', 'RGB'): image = image.convert('RGB') if hasattr(settings, "AWS_SECRET_ACCESS_KEY"): f = StringIO() image.save(f, "JPEG") f.seek(0) avatar.image.delete() avatar.image.save("%s.jpg" % request.user.username, ContentFile(f.read())) else: image.save(avatar.image.path) avatar.valid = True avatar.save() request.user.message_set.create(message=_("Your new avatar has been saved successfully.")) signal_responses = signals.post_signal.send(sender=avatarcrop, request=request, form=form) return signals.last_response(signal_responses) or HttpResponseRedirect(reverse("profile_edit_avatar")) template = "userprofile/avatar/crop.html" data = { 'section': 'avatar', 'avatar': avatar, 'form': form, } signals.context_signal.send(sender=avatarcrop, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def avatarchoose(request): """ Avatar choose """ profile, created = Profile.objects.get_or_create(user = request.user) images = dict() if hasattr(settings, "AVATAR_QUOTA"): request.upload_handlers.insert(0, QuotaUploadHandler()) if request.method == "POST": form = AvatarForm() if request.POST.get('keyword'): keyword = str(request.POST.get('keyword')) gd_client = gdata.photos.service.PhotosService() feed = gd_client.SearchCommunityPhotos(query = "%s&thumbsize=72c" % keyword.split(" ")[0], limit='48') for entry in feed.entry: images[entry.media.thumbnail[0].url] = entry.content.src else: form = AvatarForm(request.POST, request.FILES) if form.is_valid(): image = form.cleaned_data.get('url') or form.cleaned_data.get('photo') thumb = Image.open(ContentFile(image.read())) thumb.thumbnail((480, 480), Image.ANTIALIAS) thumb.convert("RGB") f = StringIO() thumb.save(f, "JPEG") f.seek(0) avatar = Avatar(user=request.user, image="", valid=False) avatar.image.save("%s.jpg" % request.user.username, ContentFile(f.read())) avatar.save() signal_responses = signals.post_signal.send(sender=avatarchoose, request=request, form=form) return signals.last_response(signal_responses) or HttpResponseRedirect(reverse("profile_avatar_crop")) else: form = AvatarForm() if DEFAULT_AVATAR: base, filename = os.path.split(DEFAULT_AVATAR) filename, extension = os.path.splitext(filename) generic96 = "%s/%s.96%s" % (base, filename, extension) generic96 = generic96.replace(settings.STATIC_ROOT, settings.STATIC_URL) else: generic96 = "" template = "userprofile/avatar/choose.html" data = { 'generic96': generic96, 'form': form, "images": images, 'AVATAR_WEBSEARCH': AVATAR_WEBSEARCH, 'section': 'avatar', } signals.context_signal.send(sender=avatarchoose, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def register(request): if request.method == 'POST': form = _RegistrationForm(request.POST) if form.is_valid(): newuser = form.save() signal_responses = signals.post_signal.send(sender=register, request=request, form=form, extra={'newuser': newuser}) return signals.last_response(signal_responses) or HttpResponseRedirect(reverse('signup_complete')) else: form = _RegistrationForm() template = "userprofile/account/registration.html" data = { 'form': form, } signals.context_signal.send(sender=register, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def email_validation_process(request, key): """ Verify key and change email """ if EmailValidation.objects.verify(key=key): successful = True else: successful = False signal_responses = signals.post_signal.send(sender=email_validation_process, request=request, extra={"key": key}) last_reponse = signals.last_response(signal_responses) if last_reponse: return last_response template = "userprofile/account/email_validation_done.html" data = {"successful": successful} signals.context_signal.send(sender=email_validation_process, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def email_validation(request): """ E-mail Change form """ if request.method == 'POST': form = EmailValidationForm(request.POST) if form.is_valid(): EmailValidation.objects.add(user=request.user, email=form.cleaned_data.get('email')) signal_responses = signals.post_signal.send(sender=email_validation, request=request, form=form) return signals.last_response(signal_responses) or HttpResponseRedirect(reverse("email_validation_processed")) else: form = EmailValidationForm() template = "userprofile/account/email_validation.html" data = { 'form': form, } signals.context_signal.send(sender=email_validation, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def register(request): if request.method == 'POST': form = RegistrationForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password1') newuser = User.objects.create_user(username=username, email='', password=password) newuser.is_active = not hasattr(settings, "REQUIRE_EMAIL_CONFIRMATION") or not settings.REQUIRE_EMAIL_CONFIRMATION if form.cleaned_data.get('email'): newuser.email = form.cleaned_data.get('email') EmailValidation.objects.add(user=newuser, email=newuser.email) newuser.save() signal_responses = signals.post_signal.send(sender=register, request=request, form=form, extra={'newuser': newuser}) return signals.last_response(signal_responses) or HttpResponseRedirect(reverse('signup_complete')) else: form = RegistrationForm() template = "userprofile/account/registration.html" data = { 'form': form, } signals.context_signal.send(sender=register, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def personal(request): """ Personal data of the user profile """ profile, created = Profile.objects.get_or_create(user=request.user) if request.method == "POST": old_profile = copy.copy(profile) form = ProfileForm(request.POST, instance=profile) if form.is_valid(): form.save() messages.success( request, _("Your profile information has been updated successfully."), fail_silently=True) signal_responses = signals.post_signal.send( sender=personal, request=request, form=form, extra={'old_profile': old_profile}) last_response = signals.last_response(signal_responses) if last_response: return last_response else: form = ProfileForm(instance=profile) template = "userprofile/profile/personal.html" data = { 'section': 'personal', 'GOOGLE_MAPS_API_KEY': GOOGLE_MAPS_API_KEY, 'form': form, } signals.context_signal.send(sender=personal, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def avatarchoose(request): """ Avatar choose """ Profile.objects.get_or_create(user=request.user) images = dict() if AVATAR_QUOTA: from userprofile.uploadhandler import QuotaUploadHandler request.upload_handlers.insert(0, QuotaUploadHandler()) if request.method == "POST": form = AvatarForm() if request.POST.get('keyword'): keyword = iri_to_uri(request.POST.get('keyword')) gd_client = gdata.photos.service.PhotosService() feed = gd_client.SearchCommunityPhotos(query="%s&thumbsize=72c" % keyword.split(" ")[0], limit='48') for entry in feed.entry: images[entry.media.thumbnail[0].url] = entry.content.src else: form = AvatarForm(request.POST, request.FILES) if form.is_valid(): image = form.cleaned_data.get('url') or form.cleaned_data.get( 'photo') try: thumb = Image.open(ContentFile(image.read())) except: messages.error(request, _("This image can't be used as an avatar")) else: if thumb.mode != "RGB": thumb = thumb.convert("RGB") thumb.thumbnail((480, 480), Image.ANTIALIAS) f = File(StringIO(), name=image.name) try: thumb.save(f, thumb.format, **SAVE_IMG_PARAMS.get(thumb.format, {})) except: thumb.save(f, thumb.format) f.seek(0) file_ext = image.content_type.split("/")[ 1] # "image/gif" => "gif" if file_ext == 'pjpeg': file_ext = 'jpeg' try: avatar = Avatar.objects.get(user=request.user, valid=False) avatar.delete_avatar_thumbs() avatar.image.delete() except Avatar.DoesNotExist: avatar = Avatar(user=request.user, image="", valid=False) avatar.image.save( "%s.%s" % (request.user.username, file_ext), ContentFile(f.read())) avatar.save() signal_responses = signals.post_signal.send( sender=avatarchoose, request=request, form=form) return signals.last_response( signal_responses) or HttpResponseRedirect( reverse("profile_avatar_crop")) else: form = AvatarForm() if DEFAULT_AVATAR: generic = Avatar.objects.get_default_avatar().get_resized_image_url( DEFAULT_AVATAR_SIZE) else: generic = "" template = "userprofile/avatar/choose.html" data = { 'generic': generic, 'form': form, "images": images, 'AVATAR_WEBSEARCH': AVATAR_WEBSEARCH, 'section': 'avatar', 'DEFAULT_AVATAR_SIZE': DEFAULT_AVATAR_SIZE, 'MIN_AVATAR_SIZE': MIN_AVATAR_SIZE } signals.context_signal.send(sender=avatarchoose, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def avatarchoose(request): """ Avatar choose """ Profile.objects.get_or_create(user=request.user) images = dict() if AVATAR_QUOTA: from userprofile.uploadhandler import QuotaUploadHandler request.upload_handlers.insert(0, QuotaUploadHandler()) if request.method == "POST": form = AvatarForm() if request.POST.get("keyword"): keyword = iri_to_uri(request.POST.get("keyword")) gd_client = gdata.photos.service.PhotosService() feed = gd_client.SearchCommunityPhotos(query="%s&thumbsize=72c" % keyword.split(" ")[0], limit="48") for entry in feed.entry: images[entry.media.thumbnail[0].url] = entry.content.src else: form = AvatarForm(request.POST, request.FILES) if form.is_valid(): image = form.cleaned_data.get("url") or form.cleaned_data.get("photo") try: thumb = Image.open(ContentFile(image.read())) except: messages.error(request, _("This image can't be used as an avatar")) else: if thumb.mode != "RGB": thumb = thumb.convert("RGB") thumb.thumbnail((480, 480), Image.ANTIALIAS) f = File(StringIO(), name=image.name) try: thumb.save(f, thumb.format, **SAVE_IMG_PARAMS.get(thumb.format, {})) except: thumb.save(f, thumb.format) f.seek(0) file_ext = image.content_type.split("/")[1] # "image/gif" => "gif" if file_ext == "pjpeg": file_ext = "jpeg" try: avatar = Avatar.objects.get(user=request.user, valid=False) avatar.delete_avatar_thumbs() avatar.image.delete() except Avatar.DoesNotExist: avatar = Avatar(user=request.user, image="", valid=False) avatar.image.save("%s.%s" % (request.user.username, file_ext), ContentFile(f.read())) avatar.save() signal_responses = signals.post_signal.send(sender=avatarchoose, request=request, form=form) return signals.last_response(signal_responses) or HttpResponseRedirect( reverse("profile_avatar_crop") ) else: form = AvatarForm() if DEFAULT_AVATAR: generic = Avatar.objects.get_default_avatar().get_resized_image_url(DEFAULT_AVATAR_SIZE) else: generic = "" template = "userprofile/avatar/choose.html" data = { "generic": generic, "form": form, "images": images, "AVATAR_WEBSEARCH": AVATAR_WEBSEARCH, "section": "avatar", "DEFAULT_AVATAR_SIZE": DEFAULT_AVATAR_SIZE, "MIN_AVATAR_SIZE": MIN_AVATAR_SIZE, } signals.context_signal.send(sender=avatarchoose, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def avatarcrop(request): """ Avatar management """ avatar = get_object_or_404(Avatar, user=request.user, valid=False) if not request.method == "POST": form = AvatarCropForm() else: image = Image.open(ContentFile(avatar.image.read())) if image.mode != "RGB": image = image.convert("RGB") form = AvatarCropForm(image, request.POST) if form.is_valid(): top = int(form.cleaned_data.get("top")) left = int(form.cleaned_data.get("left")) right = int(form.cleaned_data.get("right")) bottom = int(form.cleaned_data.get("bottom")) if not (top or left or right or bottom): (width, height) = image.size if width > height: diff = (width - height) / 2 left = diff right = width - diff bottom = height else: diff = (height - width) / 2 top = diff right = width bottom = height - diff box = [left, top, right, bottom] image = image.crop(box) for a in Avatar.objects.filter(user=request.user).exclude(id=avatar.id): a.delete() f = File(StringIO(), name=avatar.image.name) # need if format is empty image.save(f, image.format) f.seek(0) if hasattr(image, "content_type"): file_ext = image.content_type.split("/")[1] # "image/gif" => "gif" else: file_ext = os.path.splitext(avatar.image.name)[1][1:] if file_ext == "pjpeg": file_ext = "jpeg" avatar.image.delete() avatar.image.save("%s.%s" % (request.user.username, file_ext), ContentFile(f.read())) avatar.valid = True avatar.save() messages.success(request, _("Your new avatar has been saved successfully."), fail_silently=True) signal_responses = signals.post_signal.send(sender=avatarcrop, request=request, form=form) return signals.last_response(signal_responses) or HttpResponseRedirect(reverse("profile_edit_avatar")) template = "userprofile/avatar/crop.html" data = { "section": "avatar", "avatar": avatar, "form": form, "DEFAULT_AVATAR_SIZE": DEFAULT_AVATAR_SIZE, "MIN_AVATAR_SIZE": MIN_AVATAR_SIZE, } signals.context_signal.send(sender=avatarcrop, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def avatarchoose(request): """ Avatar choose """ profile, created = Profile.objects.get_or_create(user = request.user) images = dict() if hasattr(settings, "AVATAR_QUOTA"): request.upload_handlers.insert(0, QuotaUploadHandler()) if request.method == "POST": form = AvatarForm() if request.POST.get('keyword'): keyword = iri_to_uri(request.POST.get('keyword')) gd_client = gdata.photos.service.PhotosService() feed = gd_client.SearchCommunityPhotos(query = "%s&thumbsize=72c" % keyword.split(" ")[0], limit='48') for entry in feed.entry: images[entry.media.thumbnail[0].url] = entry.content.src else: form = AvatarForm(request.POST, request.FILES) if form.is_valid(): image = form.cleaned_data.get('url') or form.cleaned_data.get('photo') try: thumb = Image.open(ContentFile(image.read())) except: messages.error(request, _("This image can't be used as an avatar")) else: thumb.thumbnail((480, 480), Image.ANTIALIAS) f = StringIO() save_img_params = SAVE_IMG_PARAMS.get(thumb.format, {}) try: thumb.save(f, thumb.format, **SAVE_IMG_PARAMS.get(thumb.format, {})) except: thumb.save(f, thumb.format) f.seek(0) avatar = Avatar(user=request.user, image="", valid=False) file_ext = image.content_type.split("/")[1] # "image/gif" => "gif" if file_ext == 'pjpeg': file_ext = 'jpeg' avatar.image.save("%s.%s" % (request.user.username, file_ext), ContentFile(f.read())) avatar.save() signal_responses = signals.post_signal.send(sender=avatarchoose, request=request, form=form) return signals.last_response(signal_responses) or HttpResponseRedirect(reverse("profile_avatar_crop")) else: form = AvatarForm() if DEFAULT_AVATAR: base, filename = os.path.split(DEFAULT_AVATAR) filename, extension = os.path.splitext(filename) generic = "%s/%s.%d%s" % (base, filename, DEFAULT_AVATAR_SIZE, extension) generic = generic.replace(settings.MEDIA_ROOT, settings.MEDIA_URL) else: generic = "" template = "userprofile/avatar/choose.html" data = { 'generic': generic, 'form': form, "images": images, 'AVATAR_WEBSEARCH': AVATAR_WEBSEARCH, 'section': 'avatar', 'sections': SECTIONS, 'DEFAULT_AVATAR_SIZE': DEFAULT_AVATAR_SIZE, 'MIN_AVATAR_SIZE': MIN_AVATAR_SIZE } signals.context_signal.send(sender=avatarchoose, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def avatarchoose(request): """ Avatar choose """ profile, created = Profile.objects.get_or_create(user=request.user) images = dict() if hasattr(settings, "AVATAR_QUOTA"): request.upload_handlers.insert(0, QuotaUploadHandler()) if request.method == "POST": form = AvatarForm() if request.POST.get('keyword'): keyword = iri_to_uri(request.POST.get('keyword')) gd_client = gdata.photos.service.PhotosService() feed = gd_client.SearchCommunityPhotos(query="%s&thumbsize=72c" % keyword.split(" ")[0], limit='48') for entry in feed.entry: images[entry.media.thumbnail[0].url] = entry.content.src else: form = AvatarForm(request.POST, request.FILES) if form.is_valid(): image = form.cleaned_data.get('url') or form.cleaned_data.get( 'photo') try: thumb = Image.open(ContentFile(image.read())) except: messages.error(request, _("This image can't be used as an avatar")) else: thumb.thumbnail((480, 480), Image.ANTIALIAS) f = StringIO() save_img_params = SAVE_IMG_PARAMS.get(thumb.format, {}) try: thumb.save(f, thumb.format, **SAVE_IMG_PARAMS.get(thumb.format, {})) except: thumb.save(f, thumb.format) f.seek(0) avatar = Avatar(user=request.user, image="", valid=False) file_ext = image.content_type.split("/")[ 1] # "image/gif" => "gif" if file_ext == 'pjpeg': file_ext = 'jpeg' avatar.image.save( "%s.%s" % (request.user.username, file_ext), ContentFile(f.read())) avatar.save() signal_responses = signals.post_signal.send( sender=avatarchoose, request=request, form=form) return signals.last_response( signal_responses) or HttpResponseRedirect( reverse("profile_avatar_crop")) else: form = AvatarForm() if DEFAULT_AVATAR: base, filename = os.path.split(DEFAULT_AVATAR) filename, extension = os.path.splitext(filename) generic = "%s/%s.%d%s" % (base, filename, DEFAULT_AVATAR_SIZE, extension) generic = generic.replace(settings.MEDIA_ROOT, settings.MEDIA_URL) else: generic = "" template = "userprofile/avatar/choose.html" data = { 'generic': generic, 'form': form, "images": images, 'AVATAR_WEBSEARCH': AVATAR_WEBSEARCH, 'section': 'avatar', 'DEFAULT_AVATAR_SIZE': DEFAULT_AVATAR_SIZE, 'MIN_AVATAR_SIZE': MIN_AVATAR_SIZE } signals.context_signal.send(sender=avatarchoose, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def avatarcrop(request): """ Avatar management """ avatar = get_object_or_404(Avatar, user=request.user, valid=False) if not request.method == "POST": form = AvatarCropForm() else: image = Image.open(ContentFile(avatar.image.read())) form = AvatarCropForm(image, request.POST) if form.is_valid(): top = int(form.cleaned_data.get('top')) left = int(form.cleaned_data.get('left')) right = int(form.cleaned_data.get('right')) bottom = int(form.cleaned_data.get('bottom')) if not (top or left or right or bottom): (width, height) = image.size if width > height: diff = (width - height) / 2 left = diff right = width - diff bottom = height else: diff = (height - width) / 2 top = diff right = width bottom = height - diff box = [left, top, right, bottom] image = image.crop(box) if hasattr(settings, "AWS_SECRET_ACCESS_KEY"): f = StringIO() image.save(f, image.format) f.seek(0) avatar.image.delete() file_ext = image.content_type.split("/")[ 1] # "image/gif" => "gif" if file_ext == 'pjpeg': file_ext = 'jpeg' avatar.image.save("%s.%s" % (request.user.username, file_ext), ContentFile(f.read())) else: image.save(avatar.image.path) avatar.valid = True avatar.save() messages.success(request, _("Your new avatar has been saved successfully."), fail_silently=True) signal_responses = signals.post_signal.send(sender=avatarcrop, request=request, form=form) return signals.last_response( signal_responses) or HttpResponseRedirect( reverse("profile_edit_avatar")) template = "userprofile/avatar/crop.html" data = { 'section': 'avatar', 'avatar': avatar, 'form': form, \ 'DEFAULT_AVATAR_SIZE': DEFAULT_AVATAR_SIZE, 'MIN_AVATAR_SIZE': MIN_AVATAR_SIZE } signals.context_signal.send(sender=avatarcrop, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))