THUMBNAIL_ALIASES = {
'': {
'avatar': {'size': (128, 128), 'crop': True},
},
}
def update_profile(request, add_id):
token = {}
token.update(csrf(request))
if request.user.is_authenticated():
current_user = request.user
if request.POST:
# profilepage = Profile.objects.get(id = add_id)
form = Profile_Form(request.POST or None, request.FILES or None)
if form.is_valid():
avatar = form.cleaned_data['avatar']
avatar = get_thumbnailer(avatar)['avatar']
myself = form.cleaned_data['myself']
myoffer = form.cleaned_data['myoffer']
profile_obj = Profile(user=current_user, avatar=avatar, myself=myself, myoffer = myoffer)
profile_obj.save()
return redirect('/profile'+ add_id, token)
else:
return redirect('/profile'+ add_id, token)
else:
return redirect('/profile'+ add_id, token)
else:
return redirect('/profile'+ add_id, token)
class Profile(models.Model):
avatar = models.ImageField(upload_to='', blank=True, null=True, help_text="Идеальный размер 150 на 150 пикселей", verbose_name="Аватар юзера")
class Profile_Form(forms.Form):
avatar = forms.ImageField(label='Загрузить Аватар', required=False)
from thumbnails import get_thumbnail
def update_profile(request, add_id):
token = {}
token.update(csrf(request))
if request.user.is_authenticated():
current_user = request.user
if request.POST:
form = Profile_Form(request.POST or None, request.FILES or None)
if form.is_valid():
avatar = form.cleaned_data['avatar']
options = {'size': (200, 200), 'crop': True}
thumb_url = get_thumbnailer(avatar, relative_name='avatar').get_thumbnail(options).url
profile_obj = Profile(avatar=thumb_url)
profile_obj.save()
return redirect('/profile'+ add_id, token)
else:
return redirect('/profile'+ add_id, token)
else:
return redirect('/profile'+ add_id, token)
else:
return redirect('/profile'+ add_id, token)
def get_thumbnailer(obj, relative_name=None):
if hasattr(obj, 'easy_thumbnails_thumbnailer'):
return obj.easy_thumbnails_thumbnailer
if isinstance(obj, Thumbnailer):
return obj
elif isinstance(obj, FieldFile):
if not relative_name:
relative_name = obj.name
return ThumbnailerFieldFile(obj.instance, obj.field, relative_name)
source_storage = None
if isinstance(obj, six.string_types):
relative_name = obj
obj = None
if not relative_name:
raise ValueError(
"If object is not a FieldFile or Thumbnailer instance, the "
"relative name must be provided")
if isinstance(obj, File):
obj = obj.file
if isinstance(obj, Storage) or obj == default_storage:
source_storage = obj
obj = None
return Thumbnailer(
file=obj, name=relative_name, source_storage=source_storage,
remote_source=obj is not None)
avatar = get_thumbnailer(avatar, relative_name=avatar.name)