示例#1
0
def avatarchoose(request):
    """
    Avatar choose
    """
    profile, created = Profile.objects.get_or_create(user = request.user)
    if not request.method == "POST":
        form = AvatarForm()
    else:
        form = AvatarForm(request.POST, request.FILES)
        if form.is_valid():
            image = form.cleaned_data.get('url') or form.cleaned_data.get('photo')
            avatar = Avatar(user=request.user, image=image, valid=False)
            avatar.image.save("%s.jpg" % request.user.username, image)
            image = Image.open(avatar.image.path)
            image.thumbnail((480, 480), Image.ANTIALIAS)
            image.convert("RGB").save(avatar.image.path, "JPEG")
            avatar.save()
            return HttpResponseRedirect('%scrop/' % request.path_info)

            base, filename = os.path.split(avatar_path)
            generic, extension = os.path.splitext(filename)

    if DEFAULT_AVATAR:
        base, filename = os.path.split(DEFAULT_AVATAR)
        filename, extension = os.path.splitext(filename)
        generic96 = "%s/%s.150%s" % (base, filename, extension)
        generic96 = generic96.replace(settings.MEDIA_ROOT, settings.MEDIA_URL)
    else:
        generic96 = ""

    template = "userprofile/avatar/choose.html"
    data = { 'generic96': generic96, 'form': form,
             'AVATAR_WEBSEARCH': AVATAR_WEBSEARCH, 'section': 'avatar', }
    return render_to_response(template, data, context_instance=RequestContext(request))
示例#2
0
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))
示例#3
0
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))
示例#4
0
def avatarchoose(request):
    """
    Avatar choose
    """
    profile, created = Profile.objects.get_or_create(user = request.user)
    images = dict()

    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')
                avatar = Avatar(user=request.user, image=image, valid=False)
                avatar.image.save("%s.jpg" % request.user.username, image)
                image = Image.open(avatar.image.path)
                image.thumbnail((480, 480), Image.ANTIALIAS)
                image.convert("RGB").save(avatar.image.path, "JPEG")
                avatar.save()
                return HttpResponseRedirect(reverse("profile_avatar_crop"))

                base, filename = os.path.split(avatar_path)
                generic, extension = os.path.splitext(filename)
    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.MEDIA_ROOT, settings.MEDIA_URL)
    else:
        generic96 = ""

    template = "userprofile/avatar/choose.html"
    data = { 'generic96': generic96, 'form': form, "images": images,
             'AVATAR_WEBSEARCH': AVATAR_WEBSEARCH, 'section': 'avatar', }
    return render_to_response(template, data, context_instance=RequestContext(request))
示例#5
0
def avatarchoose(request):
    """
    Avatar choose
    """
    profile, created = Profile.objects.get_or_create(user=request.user)
    if not request.method == "POST":
        form = AvatarForm()
    else:
        form = AvatarForm(request.POST, request.FILES)
        if form.is_valid():
            image = form.cleaned_data.get('url') or form.cleaned_data.get(
                'photo')
            avatar = Avatar(user=request.user, image=image, valid=False)
            avatar.image.save("%s.jpg" % request.user.username, image)
            image = Image.open(avatar.image.path)
            image.thumbnail((480, 480), Image.ANTIALIAS)
            image.convert("RGB").save(avatar.image.path, "JPEG")
            avatar.save()
            return HttpResponseRedirect('%scrop/' % request.path_info)

            base, filename = os.path.split(avatar_path)
            generic, extension = os.path.splitext(filename)

    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.MEDIA_ROOT, settings.MEDIA_URL)
    else:
        generic96 = ""

    template = "userprofile/avatar/choose.html"
    data = {
        'generic96': generic96,
        'form': form,
        'AVATAR_WEBSEARCH': AVATAR_WEBSEARCH,
        'section': 'avatar',
    }
    return render_to_response(template,
                              data,
                              context_instance=RequestContext(request))
示例#6
0
文件: views.py 项目: envoked/Seeker
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))
示例#7
0
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))
示例#8
0
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))
示例#9
0
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))