本文整理汇总了Python中avatar.models.Avatar类的典型用法代码示例。如果您正苦于以下问题:Python Avatar类的具体用法?Python Avatar怎么用?Python Avatar使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Avatar类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_user_avatar
def get_user_avatar(backend, details, response, social_user, uid,\
user, *args, **kwargs):
url = None
if backend.__class__ == FacebookBackend:
url = "http://graph.facebook.com/%s/picture?type=large" % response['id']
print response.keys()
if url:
print url
avtr = Avatar(
user = user,
primary = True,
)
image_request = requests.get(url)
print image_request.headers
content_type=image_request.headers['content-type']
print content_type
filename = user.username
if content_type == 'image/jpeg':
filename = "%s.jpg" % user.username
if content_type == 'image/png':
filename = "%s.png" % user.username
image_file = SimpleUploadedFile(
filename,
image_request.content,
content_type=content_type
)
print image_file.__dict__
avtr.avatar.save(image_file.name, image_file)
avtr.save()
示例2: add
def add(request, extra_context=None, next_override=None,
upload_form=UploadAvatarForm, *args, **kwargs):
if extra_context is None:
extra_context = {}
avatar, avatars = _get_avatars(request.user)
upload_avatar_form = upload_form(request.POST or None,
request.FILES or None, user=request.user)
if request.method == "POST" and 'avatar' in request.FILES:
if upload_avatar_form.is_valid():
avatar = Avatar(
user = request.user,
primary = True,
)
image_file = request.FILES['avatar']
avatar.avatar.save(image_file.name, image_file)
avatar.save()
request.user.message_set.create(
message=_("Successfully uploaded a new avatar."))
avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
return HttpResponseRedirect(next_override or _get_next(request))
return render_to_response(
'avatar/add.html',
extra_context,
context_instance = RequestContext(
request,
{ 'avatar': avatar,
'avatars': avatars,
'upload_avatar_form': upload_avatar_form,
'next': next_override or _get_next(request), }
)
)
示例3: add
def add(request, extra_context=None, next_override=None, upload_form=UploadAvatarForm, *args, **kwargs):
if extra_context is None:
extra_context = {}
avatar, avatars = _get_avatars(request.user)
upload_avatar_form = upload_form(request.POST or None, request.FILES or None, user=request.user)
if request.method == "POST" and "avatar" in request.FILES:
if upload_avatar_form.is_valid():
avatar = Avatar(user=request.user, primary=True)
image_file = request.FILES["avatar"]
avatar.avatar.save(image_file.name, image_file)
avatar.save()
request.user.message_set.create(message=_("Successfully uploaded a new avatar."))
if notification:
_notification_updated(request, avatar)
return HttpResponseRedirect(next_override or _get_next(request))
data = {
"avatar": avatar,
"avatars": avatars,
"upload_avatar_form": upload_avatar_form,
"next": next_override or _get_next(request),
"view_user": request.user,
"page_class": "avatar-page",
}
if extra_context:
data.update(extra_context)
return render(request, "avatar/add.html", data)
示例4: add
def add(request, extra_context=None, next_override=None, upload_form=UploadAvatarForm, *args, **kwargs):
if extra_context is None:
extra_context = {}
avatar, avatars = _get_avatars(request.user)
upload_avatar_form = upload_form(request.POST or None, request.FILES or None, user=request.user)
if request.method == "POST" and "avatar" in request.FILES:
if upload_avatar_form.is_valid():
avatar = Avatar(user=request.user, primary=True)
image_file = request.FILES["avatar"]
avatar.avatar.save(image_file.name, image_file)
avatar.save()
messages.add_message(request, messages.INFO, _("Successfully uploaded a new avatar"))
avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
return HttpResponseRedirect(next_override or _get_next(request))
return render_to_response(
"avatar/add.html",
extra_context,
context_instance=RequestContext(
request,
{
"avatar": avatar,
"avatars": avatars,
"upload_avatar_form": upload_avatar_form,
"next": next_override or _get_next(request),
},
),
)
示例5: upload_avatar_js
def upload_avatar_js(request):
'''
Upload avatar image
'''
response = {}
status = 405
if request.is_ajax() and request.method == 'POST':
from avatar.models import Avatar
from avatar import views as avatarviews
from avatar.forms import UploadAvatarForm
from avatar.signals import avatar_updated
avatar, avatars = avatarviews._get_avatars(request.user)
upload_avatar_form = UploadAvatarForm(None, request.FILES, user=request.user)
if upload_avatar_form.is_valid():
avatar = Avatar(
user = request.user,
primary = True,
)
image_file = request.FILES['avatar']
avatar.avatar.save(image_file.name, image_file)
avatar.save()
avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
response['avatar'] = accountutil.get_user_avatar_url(request.user)
status = 200
return HttpResponse(
simplejson.dumps(response),
content_type = 'application/json; charset=utf8',
status=status
)
示例6: avatar_crop
def avatar_crop(request, id=None):
"""
Avatar management, creates a new avatar and makes it default
"""
if id:
avatar = get_object_or_404(Avatar, id=id, user=request.user)
else:
avatar = get_object_or_404(Avatar, user=request.user, primary=True)
if (avatar.avatar.width<=avatar.avatar.height):
result = "width"
else:
result = "height"
if not request.method == "POST":
form = AvatarCropForm()
else:
try:
orig = avatar.avatar.storage.open(avatar.avatar.name, 'rb').read()
image = Image.open(StringIO(orig))
except IOError:
return
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'))
box = [ left, top, right, bottom ]
(w, h) = image.size
if result=="width":
box = map(lambda x: x*h/AVATAR_CROP_MAX_SIZE, box)
else:
box = map(lambda x: x*w/AVATAR_CROP_MAX_SIZE, box)
image = image.crop(box)
if image.mode != 'RGB':
image = image.convert('RGB')
thumb = StringIO()
image.save(thumb, AVATAR_THUMB_FORMAT, quality=AVATAR_THUMB_QUALITY)
thumb_file = ContentFile(thumb.getvalue())
base_name, ext = os.path.splitext(avatar.avatar.name)
thumb = avatar.avatar.storage.save("%s_cropped%s" % (base_name, ext), thumb_file)
Avatar.objects.filter(id=avatar.id).update(primary=False)
new_avatar = Avatar(user=request.user, primary=True, avatar=thumb)
new_avatar.save()
request.user.message_set.create(
message=ugettext("Your new avatar has been saved successfully."))
return HttpResponseRedirect(reverse("avatar_change"))
return render_to_response("avatar_crop/crop.html", {
'AVATAR_CROP_MAX_SIZE': AVATAR_CROP_MAX_SIZE,
'dim': result,
'avatar': avatar,
'form': form
}, context_instance=RequestContext(request))
示例7: user_signed_up_
def user_signed_up_(request, user, sociallogin=None, **kwargs):
'''
When a social account is created successfully and this signal is received,
django-allauth passes in the sociallogin param, giving access to metadata on the remote account, e.g.:
sociallogin.account.provider # e.g. 'twitter'
sociallogin.account.get_avatar_url()
sociallogin.account.get_profile_url()
sociallogin.account.extra_data['screen_name']
See the socialaccount_socialaccount table for more in the 'extra_data' field.
'''
if sociallogin:
# Extract first / last names from social nets and store on Profile record
if sociallogin.account.provider == 'facebook':
user.profile.first_name = sociallogin.account.extra_data['first_name']
user.profile.last_name = sociallogin.account.extra_data['last_name']
if sociallogin.account.provider == 'google':
user.profile.first_name = sociallogin.account.extra_data['given_name']
user.profile.last_name = sociallogin.account.extra_data['family_name']
user.profile.save()
mage_avatar = user.profile.download_avatar(sociallogin.account.get_avatar_url())
avatar = Avatar(user=user,primary=True, avatar=image_avatar)
avatar.save()
示例8: add_avatar_for_user
def add_avatar_for_user(request, for_user=None, extra_context=None,
next_override=None,
upload_form=UploadAvatarForm, *args, **kwargs):
target_user = get_object_or_404(User, username=for_user, is_active=True)
if not AVATAR_ADMIN_TEST(request, target_user):
raise PermissionDenied
if extra_context is None:
extra_context = {}
avatar, avatars = _get_avatars(target_user)
upload_avatar_form = upload_form(request.POST or None,
request.FILES or None, user=target_user)
if request.method == "POST" and 'avatar' in request.FILES:
if upload_avatar_form.is_valid():
avatar = Avatar(
user = target_user,
primary = True,
)
image_file = request.FILES['avatar']
avatar.avatar.save(image_file.name, image_file)
avatar.save()
messages.add_message(request, messages.INFO, _("Successfully uploaded a new avatar"))
avatar_updated.send(sender=Avatar, user=target_user, avatar=avatar)
return redirect(next_override or _get_next(request))
context = {
'avatar': avatar,
'avatars': avatars,
'target_user': target_user,
'upload_avatar_form': upload_avatar_form,
'next': next_override or _get_next(request),
}
context.update(extra_context)
return render(request, 'avatar/add_for_user.html', context)
示例9: update_from_facebook
def update_from_facebook(user, facebook_obj):
"""docstring for update_from_facebook"""
profile = getattr(facebook_obj, 'profile', None)
if not profile:
facebook_obj.profile = facebook_obj.graph.get_object("me")
profile = facebook_obj.profile
# update django user information
user.email = profile['email']
user.first_name = profile['first_name']
user.last_name = profile['last_name']
user.save()
# update pinax profile information
pinax_profile = Profile.objects.get(user=user)
pinax_profile.name = profile['name']
pinax_profile.save()
# populate email address in emailconfirmation app
email = EmailAddress.objects.create(user=user, email=profile['email'], verified=True)
email.set_as_primary(conditional=True)
email.save()
# save profile picture
picture = urllib2.urlopen("http://graph.facebook.com/%s/picture" % facebook_obj.uid)
picture_file = ContentFile(picture.read())
avatar = Avatar(
user = user,
primary = True,
)
avatar.avatar.save('%s.jpg' % facebook_obj.uid, picture_file)
avatar.save()
示例10: add
def add(request, extra_context=None, next_override=None,
upload_form=UploadAvatarForm, *args, **kwargs):
if extra_context is None:
extra_context = {}
avatar, avatars = _get_avatars(request.user)
upload_avatar_form = upload_form(request.POST or None,
request.FILES or None, user=request.user)
if request.method == "POST" and 'avatar' in request.FILES:
if upload_avatar_form.is_valid():
avatar = Avatar(
user = request.user,
primary = True,
)
image_file = request.FILES['avatar']
avatar.avatar.save(image_file.name, image_file)
avatar.save()
request.user.message_set.create(
message=_("Successfully uploaded a new avatar."))
if notification:
_notification_updated(request, avatar)
return HttpResponseRedirect(next_override or _get_next(request))
data = {
'avatar': avatar,
'avatars': avatars,
'upload_avatar_form': upload_avatar_form,
'next': next_override or _get_next(request),
'view_user': request.user,
'page_class': 'avatar-page',
}
if extra_context:
data.update(extra_context)
return render_into_skin('avatar/add.html', data, request)
示例11: add
def add(request, extra_context=None, next_override=None,
upload_form=UploadAvatarForm, *args, **kwargs):
if extra_context is None:
extra_context = {}
avatar, avatars = _get_avatars(request.user)
upload_avatar_form = upload_form(request.POST or None,
request.FILES or None, user=request.user)
if request.method == "POST" and 'avatar' in request.FILES:
if upload_avatar_form.is_valid():
avatar = Avatar(
user = request.user,
primary = True,
)
image_file = request.FILES['avatar']
avatar.avatar.save(image_file.name, image_file)
avatar.save()
updated = True
request.user.message_set.create(
message=_(u"Zdjęcie zostało pomyślnie dodane."))
if notification:
_notification_updated(request, avatar)
return render_to_response(
'avatar/add.html',
extra_context,
context_instance = RequestContext(
request,
{ 'avatar': avatar,
'avatars': avatars,
'upload_avatar_form': upload_avatar_form,
'next': next_override or _get_next(request), }
)
)
示例12: upload
def upload(request, user_id=None):
user = get_object_or_404(User, pk=user_id)
if request.method == 'POST':
if 'avatar' in request.FILES:
form = UploadAvatarForm(
request.POST,
request.FILES,
user=user)
if form.is_valid():
avatar = Avatar(user=user, primary=True)
image_file = request.FILES['avatar']
avatar.avatar.save(image_file.name, image_file)
avatar.save()
sizes = avatar_settings.AVATAR_AUTO_GENERATE_SIZES
for size in sizes:
avatar.create_thumbnail(size)
avatar_updated.send(sender=Avatar, user=user, avatar=avatar)
user.avatar_type = 'a'
user.clear_avatar_urls()
user.save()
message = _('Avatar uploaded and set as primary')
else:
errors = get_error_list(form)
message = ', '.join(map(lambda v: force_str(v), errors))
else:
message = _('Please choose file to upload')
request.session['askbot_avatar_status_message'] = message
return redirect_to_show_list(user_id)
示例13: upload_avatar_js
def upload_avatar_js(request):
'''
Upload avatar image
'''
response = {}
status = 405
if request.is_ajax() and request.method == 'POST':
avatar, avatars = avatarviews._get_avatars(request.user)
upload_avatar_form = UploadAvatarForm(
None,
request.FILES,
user=request.user
)
if upload_avatar_form.is_valid():
avatar = Avatar(
user=request.user,
primary=True,
)
image_file = request.FILES['avatar']
avatar.avatar.save(image_file.name, image_file)
avatar.save()
avatar_updated.send(
sender=Avatar,
user=request.user,
avatar=avatar
)
response['avatar'] = userutil.get_user_avatar_url(request.user)
status = 200
return JsonResponse(
response,
status=status
)
示例14: add
def add(request, extra_context=None, next_override=None,
upload_form=UploadAvatarForm, *args, **kwargs):
if extra_context is None:
extra_context = {}
avatar, avatars = _get_avatars(request.user)
upload_avatar_form = upload_form(request.POST or None,
request.FILES or None, user=request.user)
if request.method == "POST" and 'avatar' in request.FILES:
if upload_avatar_form.is_valid():
avatar = Avatar(
emailuser = request.user.username,
primary = True,
)
image_file = request.FILES['avatar']
avatar.avatar.save(image_file.name, image_file)
avatar.save()
messages.success(request, _("Successfully uploaded a new avatar."))
avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
return HttpResponseRedirect(next_override or _get_next(request))
else:
messages.error(request, upload_avatar_form.errors['avatar'])
return HttpResponseRedirect(_get_next(request))
else:
# Only allow post request to change avatar.
raise Http404
示例15: add
def add(request, extra_context=None, next_override=None,
upload_form=UploadAvatarForm, *args, **kwargs):
if extra_context is None:
extra_context = {}
avatar, avatars = _get_avatars(request.user)
upload_avatar_form = upload_form(request.POST or None,
request.FILES or None,
user=request.user)
if request.method == "POST" and 'avatar' in request.FILES:
if upload_avatar_form.is_valid():
avatar = Avatar(user=request.user, primary=True)
image_file = request.FILES['avatar']
avatar.avatar.save(image_file.name, image_file)
avatar.save()
# messages.success(request, _("Successfully uploaded a new avatar."))
avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
return redirect(next_override or _get_next(request))
context = {
'avatar': avatar,
'avatars': avatars,
'upload_avatar_form': upload_avatar_form,
'next': next_override or _get_next(request),
}
context.update(extra_context)
return render(request, 'avatar/add.html', context)