def activate_email_user(request, key): user_registration = get_object_or_404(UserRegistration, registration_key=key) if request.method == 'POST': form = EmailUserActivationForm(request.POST, request.FILES) if form.is_valid(): name = form.cleaned_data['name'] password = form.cleaned_data['password'] user_account = user_registration.claim_registration(name, password) avatar = form.cleaned_data['avatar'] if avatar: (root, name, ext) = split_filepath(avatar.name) user_account.avatar.save('avatar.%s' % ext, avatar) user = authenticate(email=user_account.email, password=password) login(request, user) return redirect(settings.LOGIN_REDIRECT_URL) else: form = EmailUserActivationForm() return render(request, 'accounts/registration/registration_activate_email.html', {'form':form})
def view_user_settings_profile(request): user = request.user if request.method == 'POST': form = EditProfileForm(user, request.POST, request.FILES) if form.is_valid(): user.name = form.cleaned_data['name'] user.url_name = form.cleaned_data['url_name'] user.bio = form.cleaned_data['bio'] user.website_url = form.cleaned_data['website_url'] user.facebook_url = form.cleaned_data['facebook_url'] user.twitter_url = form.cleaned_data['twitter_url'] user.save() avatar = form.cleaned_data['avatar'] if avatar: (root, name, ext) = split_filepath(avatar.name) user.avatar.save('avatar.%s' % ext, avatar) messages.success(request, u'บันทึกการเปลี่ยนแปลงเรียบร้อย') return redirect('view_user_settings_profile') else: form = EditProfileForm(user, initial={ 'avatar':user.avatar, 'name':user.name, 'url_name':user.url_name, 'bio':user.bio, 'website_url':user.website_url, 'facebook_url':user.facebook_url, 'twitter_url':user.twitter_url, }) return render(request, 'user/settings/settings_profile.html', {'form':form})
def scale_blog_image(image, size): size = size.split('x') image_path = image.path (root, name, ext) = split_filepath(image_path) filename = scale_image(image_path, (int(size[0]), int(size[1]))) if filename: image_url = image.url.split('/') image_url[-1] = filename return '/'.join(image_url) else: return image.url
def crop(image, size): """ Template filter used to crop an image to make it fill the defined area. {% load image_tags %} {{ profile.picture|crop:"48x48" }} """ size = size.split('x') (root, name, ext) = split_filepath(image.path) filename = scale_image(image.path, (int(size[0]), int(size[1])), 'crop') return '/%s/%s' % (os.path.abspath(root).replace('%s/' % os.path.abspath(settings.BASE_PATH), ''), filename)
def upload_publication(request, uploading_file, organization, shelf): (file_path, file_name, file_ext) = split_filepath(uploading_file.name) publication = Publication.objects.create(organization=organization, title=file_name, original_file_name=file_name, file_ext=file_ext, uploaded_by=request.user) try: publication.uploaded_file.save('%s.%s' % (publication.uid, file_ext), uploading_file.file) except: publication.delete() logger.critical(traceback.format_exc(sys.exc_info()[2])) return None PublicationShelf.objects.create(publication=publication, shelf=shelf, created_by=request.user) return publication
def save_temporary_blog_image(image_file): if not os.path.exists(settings.TEMP_BLOG_IMAGE_ROOT): os.makedirs(settings.TEMP_BLOG_IMAGE_ROOT) (root, file_name, file_ext) = split_filepath(image_file.name) file_name = '%s.%s' % (uuid.uuid4(), file_ext) destination = open('%s%s' % (settings.TEMP_BLOG_IMAGE_ROOT, file_name), 'wd+') for chunk in image_file.chunks(): destination.write(chunk) destination.close() thumbnail_filename = scale_image('%s%s' % (settings.TEMP_BLOG_IMAGE_ROOT, file_name), (settings.BLOG_IMAGE_PREVIEW_WIDTH, settings.BLOG_IMAGE_PREVIEW_HEIGHT)) thumbnail_url = '%s%s%s' % (settings.MEDIA_URL, settings.TEMP_BLOG_IMAGE_URL, thumbnail_filename) return file_name, thumbnail_url
def replace_publication(request, uploading_file, publication): (file_path, file_name, file_ext) = split_filepath(uploading_file.name) try: publication.uploaded_file.delete() publication.uploaded_file.save('%s.%s' % (publication.uid, file_ext), uploading_file.file) except: logger.critical(traceback.format_exc(sys.exc_info()[2])) return None # Change file details publication.original_file_name = file_name publication.file_ext = file_ext publication.replaced = now() publication.replaced_by = request.user publication.save() return publication
def _generate_base_thumbnail(publication, file_type, command='', callback=None): try: (file_path, file_name, file_ext) = split_filepath(publication.uploaded_file.path) temp_file = '%s%s.png' % (settings.THUMBNAIL_TEMP_ROOT, publication.uid) if not os.path.exists(settings.THUMBNAIL_TEMP_ROOT): os.makedirs(settings.THUMBNAIL_TEMP_ROOT) if command: command = command % (temp_file, publication.uploaded_file.path) command = command.split(' ') subprocess.call(command) if callback: callback(temp_file, publication.uploaded_file.path) im = Image.open(temp_file) if im.mode != 'RGB': im = im.convert('RGB') thumbnail_path = '%s%s/' % (settings.THUMBNAIL_ROOT, publication.get_parent_folder()) if not os.path.exists(thumbnail_path): os.makedirs(thumbnail_path) for thumbnail_size in settings.THUMBNAIL_SIZES: thumb_im = im.copy() thumb_im.thumbnail(thumbnail_size[1], Image.ANTIALIAS) fullpath = '%s%s.%s.jpg' % (thumbnail_path, file_name, thumbnail_size[0]) thumb_im.save(fullpath, 'JPEG') try: os.remove(temp_file) except: logger.error(traceback.format_exc(sys.exc_info()[2])) return True except: logger.error('Generating %s thumbnail [%s] - %s' % (file_type, publication.uid, traceback.format_exc(sys.exc_info()[2]))) return False
def _generate_image_thumbnail(publication): try: im = Image.open(publication.uploaded_file.file) if im.mode != 'RGB': im = im.convert('RGB') (file_path, file_name, file_ext) = split_filepath(publication.uploaded_file.path) thumbnail_path = '%s%s/' % (settings.THUMBNAIL_ROOT, publication.get_parent_folder()) if not os.path.exists(thumbnail_path): os.makedirs(thumbnail_path) for thumbnail_size in settings.THUMBNAIL_SIZES: thumb_im = im.copy() thumb_im.thumbnail(thumbnail_size[1], Image.ANTIALIAS) fullpath = '%s%s.%s.jpg' % (thumbnail_path, file_name, thumbnail_size[0]) thumb_im.save(fullpath, 'JPEG') return True except: logger.error('Generating image thumbnail [%s] - %s' % (publication.uid, traceback.format_exc(sys.exc_info()[2]))) return False
def blog_image_url(instance, filename): (root, name, ext) = split_filepath(filename) return './images/blog/%s/%s.%s' % (instance.user.id, name, ext)
def get_image_file_name(self): (root, name ,ext) = split_filepath(self.image.path) return '%s.%s' % (name, ext)
def remove_blog_image(blog): (root, name, ext) = split_filepath(blog.image.path) for f in os.listdir('%s%s/' % (settings.BLOG_IMAGE_ROOT, blog.user.id)): if not f.find('%s.%s' % (name, ext)): os.remove(os.path.join('%s%s/' % (settings.BLOG_IMAGE_ROOT, blog.user.id), f))
def check_blog_image(blog): (root, name, ext) = split_filepath(blog.image.path) return os.path.exists('%s%s/%s.%s' % (settings.BLOG_IMAGE_ROOT, blog.user.id, name, ext))