Last active
May 13, 2022 13:46
Revisions
-
voluntas revised this gist
Dec 7, 2013 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,8 +2,8 @@ S3 と非同期サムネイル作成 コトハジメ #################################### :更新: 2013-12-08 :バージョン: 0.1.8 :作者: @voluntas :URL: http://voluntas.github.io/ @@ -370,7 +370,7 @@ project/core/views.py @require_GET def home(request): entries = Entry.objects.all() return render(request, 'home.html', {'entries': entries}) @require_http_methods(["GET", "POST"]) @@ -382,7 +382,7 @@ project/core/views.py return redirect('/') else: form = EntryForm() return render(request, 'upload.html', {'form': form}) project/core/models.py -
voluntas revised this gist
Nov 4, 2013 . 1 changed file with 7 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,14 +2,16 @@ S3 と非同期サムネイル作成 コトハジメ #################################### :更新: 2013-11-05 :バージョン: 0.1.7 :作者: @voluntas :URL: http://voluntas.github.io/ 概要 ==== **Django 前提** - 画像のアップロード先を S3 にして欲しい - アップロードと同時にサムネイルを生成して欲しい - サムネイル生成は非同期であって欲しい @@ -21,6 +23,8 @@ S3 と非同期サムネイル作成 コトハジメ ゴール ====== **Django 前提** - S3 アップロードには django-storages を使う - サムネイル生成には django-imagekit を使う - 非同期処理には django-celery を使う @@ -32,7 +36,7 @@ S3 と非同期サムネイル作成 コトハジメ セットアップ ============ 必須(Pillow は後ほど):: $ pip install django django-celery django-imagekit django-storages boto redis celery -
voluntas revised this gist
Nov 4, 2013 . 1 changed file with 66 additions and 30 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ S3 と非同期サムネイル作成 コトハジメ #################################### :更新: 2013-11-04 :バージョン: 0.1.5 :作者: @voluntas :URL: http://voluntas.github.io/ @@ -32,38 +32,29 @@ S3 と非同期サムネイル作成 コトハジメ セットアップ ============ 必須(pillow 以外):: $ pip install django django-celery django-imagekit django-storages boto redis celery オプション:: $ pip install flower flower は Celery でキューを Web UI から見れる便利なツール。ただし簡易的なのであれば celery events で見られる。 Pillow ------ Pillow はイメージ変換ライブラリだが、外部ライブラリに依存するので、要注意。 今回は JPEG のサポートが出来れば良い。 macports:: $ sudo port install libjpeg-turbo $ pip install pillow ... -------------------------------------------------------------------- PIL SETUP SUMMARY @@ -82,18 +73,63 @@ Pillow インストール例:: *** WEBPMUX support not available -------------------------------------------------------------------- ... CentOS 6.4:: $ sudo yum install libjpeg-turbo-devel $ pip install pillow ... -------------------------------------------------------------------- PIL SETUP SUMMARY -------------------------------------------------------------------- version Pillow 2.2.1 platform linux2 2.7.5 (default, Nov 5 2013, 00:30:50) [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] -------------------------------------------------------------------- *** TKINTER support not available --- JPEG support available --- ZLIB (PNG/ZIP) support available *** TIFF G3/G4 (experimental) support not available *** FREETYPE2 support not available *** LITTLECMS support not available *** WEBP support not available *** WEBPMUX support not available -------------------------------------------------------------------- ... aptitude:: $ sudo ... pip freeze ---------- :: $ pip freeze Django==1.5.5 Pillow==2.2.1 amqp==1.0.13 anyjson==0.3.3 billiard==2.7.3.34 boto==2.15.0 celery==3.0.24 django-appconf==0.6 django-celery==3.0.23 django-imagekit==3.0.4 django-storages==1.1.8 kombu==2.5.16 pilkit==1.1.5 python-dateutil==2.2 pytz==2013.7 redis==2.8.0 six==1.4.1 wsgiref==0.1.2 django-storages =============== -
voluntas revised this gist
Nov 4, 2013 . 1 changed file with 46 additions and 46 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ S3 と非同期サムネイル作成 コトハジメ #################################### :更新: 2013-11-04 :バージョン: 0.1.4 :作者: @voluntas :URL: http://voluntas.github.io/ @@ -259,51 +259,51 @@ imagekit.cachefiles.strategies.Optimistic を指定することで処理が保 IMAGEKIT_CACHEFILE_DIR = ... .. モデルやフォームに追加なしでサムネイルだけを作る方法 .. ---------------------------------------------------- .. .. **S3 を使う場合はオススメ出来ません** .. .. モデルやフォームにフィールドを追加するのではなく template 側でサムネイルを生成する方法があります。 .. .. この方法を使うとサムネイル「だけ」を追加することが出来ます。 .. .. 全てデータはキャッシュに生成されます。 .. .. :: .. .. {% load imagekit %} .. .. {% thumbnail '100x50' entry.img %} .. .. settings.py の IMAGEKIT_DEFAULT_CACHEFILE_STRATEGY は JustInTime に指定して下さい。 .. .. .. code-block:: .. .. IMAGEKIT_DEFAULT_CACHEFILE_STRATEGY = 'imagekit.cachefiles.strategies.JustInTime' .. .. イメージをテンプレート側で生成する .. ---------------------------------- .. .. **S3 を使う場合はオススメ出来ません** .. .. **未検証** .. .. サムネイルだけではなくもう少し細かいところでイメージを生成したいときはこちらが使えます。 .. .. ImageSpec を register した名前を指定することで処理も出来ます。 .. .. :: .. .. {% load imagekit %} .. .. {% generateimage 'core:profile:image_thumbnail' source=source_image %} .. .. settings.py の IMAGEKIT_DEFAULT_CACHEFILE_STRATEGY は JustInTime に指定して下さい。 .. .. .. code-block:: .. .. IMAGEKIT_DEFAULT_CACHEFILE_STRATEGY = 'imagekit.cachefiles.strategies.JustInTime' サンプルプロジェクト ==================== -
voluntas revised this gist
Nov 4, 2013 . 1 changed file with 16 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ S3 と非同期サムネイル作成 コトハジメ #################################### :更新: 2013-11-04 :バージョン: 0.1.3 :作者: @voluntas :URL: http://voluntas.github.io/ @@ -262,24 +262,31 @@ imagekit.cachefiles.strategies.Optimistic を指定することで処理が保 モデルやフォームに追加なしでサムネイルだけを作る方法 ---------------------------------------------------- **S3 を使う場合はオススメ出来ません** モデルやフォームにフィールドを追加するのではなく template 側でサムネイルを生成する方法があります。 この方法を使うとサムネイル「だけ」を追加することが出来ます。 全てデータはキャッシュに生成されます。 :: {% load imagekit %} {% thumbnail '100x50' entry.img %} settings.py の IMAGEKIT_DEFAULT_CACHEFILE_STRATEGY は JustInTime に指定して下さい。 .. code-block:: IMAGEKIT_DEFAULT_CACHEFILE_STRATEGY = 'imagekit.cachefiles.strategies.JustInTime' イメージをテンプレート側で生成する ---------------------------------- **S3 を使う場合はオススメ出来ません** **未検証** サムネイルだけではなくもう少し細かいところでイメージを生成したいときはこちらが使えます。 @@ -292,6 +299,11 @@ ImageSpec を register した名前を指定することで処理も出来ます {% generateimage 'core:profile:image_thumbnail' source=source_image %} settings.py の IMAGEKIT_DEFAULT_CACHEFILE_STRATEGY は JustInTime に指定して下さい。 .. code-block:: IMAGEKIT_DEFAULT_CACHEFILE_STRATEGY = 'imagekit.cachefiles.strategies.JustInTime' サンプルプロジェクト ==================== -
voluntas revised this gist
Nov 4, 2013 . 1 changed file with 35 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ S3 と非同期サムネイル作成 コトハジメ #################################### :更新: 2013-11-04 :バージョン: 0.1.1 :作者: @voluntas :URL: http://voluntas.github.io/ @@ -259,6 +259,40 @@ imagekit.cachefiles.strategies.Optimistic を指定することで処理が保 IMAGEKIT_CACHEFILE_DIR = ... モデルやフォームに追加なしでサムネイルだけを作る方法 ---------------------------------------------------- **未検証** モデルやフォームにフィールドを追加するのではなく template 側でサムネイルを生成する方法があります。 この方法を使うとサムネイル「だけ」を追加することが出来ます。 もしかするとこれが最も簡単な django-imagekit の使い方なのかもしれません。 全てデータはキャッシュに生成されます。 :: {% load imagekit %} {% thumbnail '100x50' person.img %} イメージをテンプレート側で生成する ---------------------------------- **未検証** サムネイルだけではなくもう少し細かいところでイメージを生成したいときはこちらが使えます。 ImageSpec を register した名前を指定することで処理も出来ます。 :: {% load imagekit %} {% generateimage 'core:profile:image_thumbnail' source=source_image %} サンプルプロジェクト ==================== -
voluntas revised this gist
Nov 4, 2013 . 1 changed file with 107 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ S3 と非同期サムネイル作成 コトハジメ #################################### :更新: 2013-11-04 :バージョン: 0.1.0 :作者: @voluntas :URL: http://voluntas.github.io/ @@ -151,7 +151,113 @@ settings.py で s3.py に設定した値を読み込むようにしましょう django-imagekit =============== django-imagekit は画像処理ライブラリですが、よく出来ているので簡単に使えます。 モデルフォームにサムネイルフィールドを追加する ---------------------------------------------- models.py に追加する場合は ImageSpecField を使います。 .. code-block:: python from django.db import models from imagekit.models import ImageSpecField from imagekit.processors import ResizeToFill class Entry(models.Model): title = models.CharField(max_length=255) img = models.ImageField(upload_to='entry/%Y%m%d') img_thumbnail = ImageSpecField(source='img', processors=[ResizeToFill(100, 50)], format='JPEG', options={'quality': 60}) - source は **サムネイル変換** 対象フィールドを指定します - processors は **実際に変換する処理** を指定します - format は **画像の形式** を指定します - options はそれ以外の設定を指定します この書き方は ImageSpec と呼ばれる「変換処理」を直接フィールドに指定する方法ですが、 やはり変換処理自体は色々まとめて色々な場面で呼べるようにしたいと考えると思います。 汎用化する場合は ImageSpec を継承したクラスを作ります。 この場合は ImageSpecField には id= で登録した名前を指定します。 .. code-block:: python from django.db import models from imagekit import ImageSpec, register from imagekit.models import ImageSpecField from imagekit.processors import ResizeToFill class Entry(models.Model): title = models.CharField(max_length=255) img = models.ImageField(upload_to='entry/%Y%m%d') img_thumbnail = ImageSpecField(source='img', id='core:profile:image_thumbnail') class ImageThumbnail(ImageSpec): processors = [ResizeToFill(100, 50)] format = 'JPEG' options = {'quality': 60} register.generator('core:profile:image_thumbnail', ImageThumbnail) これを使う事で画像変換処理を綺麗にまとめておくことが出来る用になります。 非同期処理に切り換える ---------------------- django-imagekit はデフォルトで Celery に対応しています。 settings.py の IMAGEKIT_DEFAULT_CACHEFILE_BACKEND を imagekit.cachefiles.backends.Async に切り換えるだけで対応が可能です。 .. code-block:: # デフォルトが imagekit.cachefiles.backends.Simple なので Async に切り換えます IMAGEKIT_DEFAULT_CACHEFILE_BACKEND = 'imagekit.cachefiles.backends.Async' import djcelery djcelery.setup_loader() # ローカルの redis をブローカーに使った例 BROKER_URL = 'redis://localhost:6379/0' あとは celery worker を起動すれば動きます。 セーブ時にサムネイルを保存する ------------------------------ デフォルトのキャッシュファイル戦略が imagekit.cachefiles.strategies.JustInTime となっているため、画像を表示されたタイミングに「いつも」生成します。もちろん既存のデータがあれば生成はしませんが「既存のデータがあるかどうか」も確認が発生します。 そこで変換処理をファイルのアップロード時限定にする戦略に切り換えることが出来ます。 imagekit.cachefiles.strategies.Optimistic を指定することで処理が保存時のみにすることが可能です。 .. code-block:: IMAGEKIT_DEFAULT_CACHEFILE_STRATEGY = 'imagekit.cachefiles.strategies.Optimistic' キャッシュディレクトリの Prefix を返る -------------------------------------- キャッシュディレクトリはデフォルトでは /CACHE/images/ が指定されます。 何か意図的に変更したい場合に使います。 .. code-block:: IMAGEKIT_CACHEFILE_DIR = ... サンプルプロジェクト ==================== -
voluntas revised this gist
Nov 4, 2013 . 1 changed file with 18 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ S3 と非同期サムネイル作成 コトハジメ #################################### :更新: 2013-11-04 :バージョン: 0.0.5 :作者: @voluntas :URL: http://voluntas.github.io/ @@ -129,7 +129,24 @@ media と static メディアファイルは /media/ で、static ファイルは /static/ から始まるようにしたい場合は以下のようにします。 s3.py というファイルを作り location を切り換えるようにします。 .. code-block:: from storages.backends.s3boto import S3BotoStorage StaticRootS3BotoStorage = lambda: S3BotoStorage(location='static') MediaRootS3BotoStorage = lambda: S3BotoStorage(location='media') settings.py で s3.py に設定した値を読み込むようにしましょう。 .. code-block:: DEFAULT_FILE_STORAGE = 'app.s3.MediaRootS3BotoStorage' STATICFILES_STORAGE = 'app.s3.StaticRootS3BotoStorage' この設定をすることで、アップロードした画像は media へ、 元々用意してた静的ファイルは /static/ から呼ばれるようになります。 django-imagekit =============== -
voluntas revised this gist
Nov 4, 2013 . 1 changed file with 45 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ S3 と非同期サムネイル作成 コトハジメ #################################### :更新: 2013-11-04 :バージョン: 0.0.4 :作者: @voluntas :URL: http://voluntas.github.io/ @@ -94,6 +94,48 @@ aptitude:: $ sudo ... django-storages =============== まずは django-storages を使って ImageField や FileField を使ったデータを S3 に上がるようにします。 settings.py に以下の設定はが必要になります。 .. code-block:: python # ストレージを boto を使った S3 に指定します DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage' # https を有効にします AWS_S3_SECURE_URLS = True # 認証クエリーを無効にします AWS_QUERYSTRING_AUTH = False # アクセスキーを指定します AWS_ACCESS_KEY_ID = '' # シークレットキーを指定します AWS_SECRET_ACCESS_KEY = '' # バケット名を指定します AWS_STORAGE_BUCKET_NAME = '' この設定をすることで後は普通にアップロードすることで S3 にファイルが置かれるようになります。 media と static --------------- メディアファイルは /media/ で、static ファイルは /static/ から始まるようにしたい場合は以下のようにします。 django-imagekit =============== サンプルプロジェクト ==================== @@ -151,6 +193,7 @@ project/core/models.py img = models.ImageField(upload_to='entry/%Y%m%d') img_thumbnail = ImageSpecField(source='img', id='core:profile:image_thumbnail') class Meta: ordering = ('title', ) @@ -230,6 +273,7 @@ project/core/settings.py IMAGEKIT_DEFAULT_FILE_STORAGE = DEFAULT_FILE_STORAGE IMAGEKIT_DEFAULT_CACHEFILE_BACKEND = 'imagekit.cachefiles.backends.Async' IMAGEKIT_DEFAULT_CACHEFILE_STRATEGY = 'imagekit.cachefiles.strategies.Optimistic' import djcelery djcelery.setup_loader() -
voluntas revised this gist
Nov 4, 2013 . 1 changed file with 13 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ S3 と非同期サムネイル作成 コトハジメ #################################### :更新: 2013-11-04 :バージョン: 0.0.3 :作者: @voluntas :URL: http://voluntas.github.io/ @@ -141,24 +141,31 @@ project/core/models.py from django.db import models from imagekit import ImageSpec, register from imagekit.models import ImageSpecField from imagekit.processors import ResizeToFill class Entry(models.Model): title = models.CharField(max_length=255) img = models.ImageField(upload_to='entry/%Y%m%d') img_thumbnail = ImageSpecField(source='img', id='core:profile:image_thumbnail') class Meta: ordering = ('title', ) def __unicode__(self): return self.title class ImageThumbnail(ImageSpec): processors = [ResizeToFill(100, 50)] format = 'JPEG' options = {'quality': 60} register.generator('core:profile:image_thumbnail', ImageThumbnail) project/core/forms.py .. code-block:: python -
voluntas revised this gist
Nov 4, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ S3 と非同期サムネイル作成 コトハジメ #################################### :更新: 2013-11-04 :バージョン: 0.0.2 :作者: @voluntas :URL: http://voluntas.github.io/ @@ -116,7 +116,7 @@ project/core/views.py from .forms import EntryForm @require_GET def home(request): entries = Entry.objects.all() return render(request, 'core.html', {'entries': entries}) -
voluntas revised this gist
Nov 4, 2013 . 1 changed file with 23 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ S3 と非同期サムネイル作成 コトハジメ #################################### :更新: 2013-11-04 :バージョン: 0.0.1 :作者: @voluntas :URL: http://voluntas.github.io/ @@ -34,7 +34,28 @@ S3 と非同期サムネイル作成 コトハジメ 必須:: $ pip install django django-celery django-imagekit django-storages boto redis Pillow celery pip freeze:: Django==1.5.5 Pillow==2.2.1 amqp==1.0.13 anyjson==0.3.3 billiard==2.7.3.34 boto==2.15.0 celery==3.0.24 django-appconf==0.6 django-celery==3.0.23 django-imagekit==3.0.4 django-storages==1.1.8 kombu==2.5.16 pilkit==1.1.5 python-dateutil==2.2 pytz==2013.7 redis==2.8.0 six==1.4.1 wsgiref==0.1.2 おまけ:: -
voluntas revised this gist
Nov 4, 2013 . 1 changed file with 43 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -42,6 +42,25 @@ S3 と非同期サムネイル作成 コトハジメ Pillow はイメージ変換ライブラリだが、libjpeg 等が必要なのでなんとか動くようにすること Pillow インストール例:: -------------------------------------------------------------------- PIL SETUP SUMMARY -------------------------------------------------------------------- version Pillow 2.2.1 platform darwin 2.7.5 (default, Aug 1 2013, 01:01:17) [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] -------------------------------------------------------------------- --- TKINTER support available --- JPEG support available --- ZLIB (PNG/ZIP) support available *** TIFF G3/G4 (experimental) support not available --- FREETYPE2 support available *** LITTLECMS support not available *** WEBP support not available *** WEBPMUX support not available -------------------------------------------------------------------- macports:: $ sudo port install libjpeg-turbo @@ -54,12 +73,15 @@ aptitude:: $ sudo ... サンプルプロジェクト ==================== - ジェネリックビューは使ってない - HTML は凄く適当 ソースコード ------------ project/core/views.py .. code-block:: python @@ -108,6 +130,7 @@ project/core/models.py processors=[ResizeToFill(100, 50)], format='JPEG', options={'quality': 60}) class Meta: ordering = ('title', ) @@ -208,3 +231,22 @@ project/core/urls.py url(r'^$', 'core.views.home', name='home'), url(r'^upload$', 'core.views.upload', name='upload'), ) 起動 ---- アプリを起動する:: $ python manage.py runserver redis サーバを立てる:: $ redis-server celery ワーカを起動する:: $ python manage.py celery worker -E flower をインストールしている場合:: $ python manage.py celery flower -
voluntas revised this gist
Nov 4, 2013 . 1 changed file with 9 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -60,21 +60,26 @@ aptitude:: - ジェネリックビューは使ってない - HTML は凄く適当 project/core/views.py .. code-block:: python # coding=utf8 from django.shortcuts import render, redirect from django.views.decorators.http import require_GET, require_http_methods from .models import Entry from .forms import EntryForm @require_GET() def home(request): entries = Entry.objects.all() return render(request, 'core.html', {'entries': entries}) @require_http_methods(["GET", "POST"]) def upload(request): if request.method == 'POST': form = EntryForm(request.POST, request.FILES) @@ -83,8 +88,7 @@ aptitude:: return redirect('/') else: form = EntryForm() return render(request, 'core.html', {'form': form}) project/core/models.py @@ -201,6 +205,6 @@ project/core/urls.py from django.conf.urls import patterns, url urlpatterns = patterns('', url(r'^$', 'core.views.home', name='home'), url(r'^upload$', 'core.views.upload', name='upload'), ) -
voluntas revised this gist
Nov 4, 2013 . 1 changed file with 6 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,14 +15,16 @@ S3 と非同期サムネイル作成 コトハジメ - サムネイル生成は非同期であって欲しい この 3 つの願いはよくある話なのではないでしょうか。 この辺の処理がまとまってるのが見つけられなかったのでまとめてみました。 ゴール ====== - S3 アップロードには django-storages を使う - サムネイル生成には django-imagekit を使う - 非同期処理には django-celery を使う - Celery のキューには Redis を使う これらの 3 つを組み合わせることで画像を S3 にアップロードし、 非同期にサムネイルを生成するという処理を実現させます。 -
voluntas revised this gist
Nov 4, 2013 . 1 changed file with 72 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,62 @@ #################################### S3 と非同期サムネイル作成 コトハジメ #################################### :更新: 2013-11-04 :バージョン: 0.0.0 :作者: @voluntas :URL: http://voluntas.github.io/ 概要 ==== - 画像のアップロード先を S3 にして欲しい - アップロードと同時にサムネイルを生成して欲しい - サムネイル生成は非同期であって欲しい この 3 つの願いはよくある話なのではないでしょうか。 王道なのかも知れませんがこの辺の処理がまとまっていなかったのでまとめてみました。 ゴール ====== - S3 アップロードには django-storages - サムネイル生成には django-imagekit - 非同期処理には django-celery これらの 3 つを組み合わせることで画像を S3 にアップロードし、 非同期にサムネイルを生成するという処理を実現させます。 セットアップ ============ 必須:: $ pip install django django-celery django-magekit django-storages boto redis Pillow celery おまけ:: $ pip install flower Pillow はイメージ変換ライブラリだが、libjpeg 等が必要なのでなんとか動くようにすること macports:: $ sudo port install libjpeg-turbo yum:: $ sudo ... aptitude:: $ sudo ... サンプルソースコード ==================== - ジェネリックビューは使ってない - HTML は凄く適当 .. code-block:: python @@ -129,7 +183,22 @@ project/core/s3.py .. code-block:: python # coding=utf8 from storages.backends.s3boto import S3BotoStorage StaticRootS3BotoStorage = lambda: S3BotoStorage(location='static') MediaRootS3BotoStorage = lambda: S3BotoStorage(location='media') project/core/urls.py .. code-block:: python # coding=utf8 from django.conf.urls import patterns, url urlpatterns = patterns('', url(r'^$', 'rime.views.home', name='home'), url(r'^upload$', 'rime.views.upload', name='upload'), ) -
voluntas revised this gist
Nov 4, 2013 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ ######################################## AWS S3 + 非同期サムネイル作成 コトハジメ ######################################## -
voluntas revised this gist
Nov 4, 2013 . No changes.There are no files selected for viewing
-
voluntas revised this gist
Nov 4, 2013 . 2 changed files with 135 additions and 120 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,135 @@ ########################################### S3 アップロードで非同期画像処理のススメ(仮) ########################################### .. code-block:: python # coding=utf8 from django.shortcuts import render, redirect from .models import Entry from .forms import EntryForm def home(request): entries = Entry.objects.all() return render(request, 'rime/home.html', {'entries': entries}) def upload(request): if request.method == 'POST': form = EntryForm(request.POST, request.FILES) if form.is_valid(): form.save() return redirect('/') else: form = EntryForm() return render(request, 'rime/upload.html', {'form': form}) project/core/models.py .. code-block:: python # coding=utf8 from django.db import models from imagekit.models import ImageSpecField from imagekit.processors import ResizeToFill class Entry(models.Model): title = models.CharField(max_length=255) img = models.ImageField(upload_to='entry/%Y/%m/%d') img_thumbnail = ImageSpecField(source='img', processors=[ResizeToFill(100, 50)], format='JPEG', options={'quality': 60}) class Meta: ordering = ('title', ) def __unicode__(self): return self.title project/core/forms.py .. code-block:: python # coding=utf8 from django import forms from .models import Entry class EntryForm(forms.ModelForm): class Meta: model = Entry project/core/templates/core/home.html .. code-block:: html {% for entry in entries %} <div><img src="{{ entry.img_thumbnail.url }}"><br></div> {% endfor %} project/core/templates/core/upload.html .. code-block:: html <form action="/upload" method="post" enctype="multipart/form-data">{% csrf_token %} {% for field in form %} <div > {{ field.errors }} {{ field.label_tag }}: {{ field }} </div> {% endfor %} <p><input type="submit" value="アップロード" /></p> </form> project/core/settings.py .. code-block:: python INSTALLED_APPS = ( ... 'djcelery', 'imagekit', 'storages', 'core', ) DEFAULT_FILE_STORAGE = 'core.s3.MediaRootS3BotoStorage' STATICFILES_STORAGE = 'core.s3.StaticRootS3BotoStorage' AWS_S3_SECURE_URLS = True AWS_QUERYSTRING_AUTH = False AWS_ACCESS_KEY_ID = '' AWS_SECRET_ACCESS_KEY = '' AWS_STORAGE_BUCKET_NAME = '' IMAGEKIT_DEFAULT_FILE_STORAGE = DEFAULT_FILE_STORAGE IMAGEKIT_DEFAULT_CACHEFILE_BACKEND = 'imagekit.cachefiles.backends.Async' import djcelery djcelery.setup_loader() BROKER_URL = 'redis://localhost:6379/0' project/core/s3.py .. code-block:: python from storages.backends.s3boto import S3BotoStorage StaticRootS3BotoStorage = lambda: S3BotoStorage(location='static') MediaRootS3BotoStorage = lambda: S3BotoStorage(location='media') This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,120 +0,0 @@ -
voluntas revised this gist
Oct 31, 2013 . 1 changed file with 93 additions and 51 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,78 +1,120 @@ ################################################################ django-storages + easy-thumbnails + django-celery コトハジメ(仮) ################################################################ :更新: 2013-10-16 :バージョン: 0.0.0 :作者: @voluntas :URL: http://voluntas.github.io/ - `Django トラノマキ <https://gist.github.com/voluntas/6855579>`_ 概要 ==== Django の ImageField を使って S3 に画像をアップロードしたとき自動でサムネイル画像も生成して欲しい、 さらにそのサムネイル画像の生成は非同期で行って欲しいという処理が必要になったので、どうやって実現するべきなのかをまとめたメモ。 セットアップ ============ インストール:: $ pip install django-storages easy-thumbnails django-celery boto - celery のキューには Redis を使用する - django-storages は S3 を使用する settings.py に以下の設定をする .. code-block:: python AWS_ACCESS_KEY_ID = '' AWS_SECRET_ACCESS_KEY = '' AWS_STORAGE_BUCKET_NAME = '' DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage' THUMBNAIL_DEFAULT_STORAGE = 'storages.backends.s3boto.S3BotoStorage' models.py はこんな感じ .. code-block:: python from django.db import models from django.dispatch import receiver from easy_thumbnails.signals import saved_file from easy_thumbnails.files import get_thumbnailer from .tasks import generate_thumbnails class Photo(models.Model): title = models.CharField(max_length=255) photo = models.ImageField(upload_to='img/%Y/%m/%d') class Meta: ordering = ('title', ) def __unicode__(self): return self.name def get_photo_url(self, alias='original'): if self.photo: if alias == 'original': return self.photo.url else: return get_thumbnailer(self.photo)[alias].url else: return None @receiver(saved_file) def generate_thumbnails_async(sender, fieldfile, **kwargs): generate_thumbnails.delay( model=sender, pk=fieldfile.instance.pk, field=fieldfile.field.name ) .. code-block:: python # tasks.py from celery import task from easy_thumbnails.files import generate_all_aliases @task def generate_thumbnails(model, pk, field): instance = model._default_manager.get(pk=pk) fieldfile = getattr(instance, field) generate_all_aliases(fieldfile, include_global=True) urls.py .. code-block:: python views.py .. code-block:: python forms.py .. code-block:: python 参照 ==== Easy Thumbnails — easy-thumbnails 1.4 documentation http://easy-thumbnails.readthedocs.org/en/latest/ Asynchronous Pregeneration http://easy-thumbnails.readthedocs.org/en/latest/usage/#asynchronous-pregeneration Amazon S3 — django-storages 1.1.8 documentation http://django-storages.readthedocs.org/en/latest/backends/amazon-S3.html Homepage | Celery: Distributed Task Queue http://www.celeryproject.org/ -
voluntas revised this gist
Oct 31, 2013 . No changes.There are no files selected for viewing
-
voluntas revised this gist
Oct 31, 2013 . 1 changed file with 51 additions and 93 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,120 +1,78 @@ ################### Trello でタスク管理 ################### :更新: 2013-11-01 :バージョン: 0.0.0 :作者: @voluntas :URL: http://voluntas.github.io/ 概要 ==== 関わっている全てのプロジェクトが Trello を使っている。さらに大規模な Trello 運用に関わる機会を得たのでそこで得たノウハウをまとめることにした。 ただし、運用というのはそれぞれの組織にあったものを「探していく」べきものであり、さらに必要があれば「変化」していくものだと考えている。 つまり銀の弾丸なんて無い。これはまぁうまくいってるんじゃないか?という例である。 十人十色のタスク管理があって問題ないと思っている。 書き出し ======== - markdown を覚えて活用する - タスク管理は運用が全て - チャットを減らす - 打ち合わせを減らす - 明確にアサイン - 週1のレビュー - Done から Waiting 、 Doing へ上って「漏れタスク探すレビュー」を行う - レビュー時間を決める - To Do はレビューしない - trello のボードは増やさない - 出来るだけプロジェクト単位で増やす - 組織単位であれば 部署〜課単位で止める - 部署全体のボード、課単位のボード、あとは全てプロジェクト単位のボード - 可能であれば課単位のリストでボードをできる限り増やさないようにする - trello のカードのタスクは細かくしない - 細かいタスクは全て checklist レベルに落とし込む - リストはできる限りシンプルに (To do, Doing, Waiting, Done, Pending) 程度にする - 出来ないカードは出来ない理由を書いて Done へ持っていく - いつかやりたいならその旨をおいて To Do へ - ただいつかやるはやらないので、本当に残しておきたい物だけ残す - 管理を継続するには複雑にしない事が大事 - タスクの見える化が目的であって「人の管理が目的」ではないことを理解する - 気持ちはカードにせず、コメントへ - カードで議論しない F2F で議論する - 複数人数での議論が必要な場合は「必要な人だけ」を招集する - 偉い人専用リストを追加する (~ だけど聞きたいことある?) のようなもの - わからないことはここに書いて、非同期にする - 全てをプロジェクト単位で考える 緊急タスク ---------- 事前に To Do に存在するのでは無くいきなり Doing になるタスクというのが存在します。 それが緊急タスクです。緊急タスクは全てのタスクより優先順位を高くします。 このタスクによって他のタスクに影響が発生することも考えられるためあまりあって欲しくないタスクです。 ただ障害対応などの「対応しないとまずい」場合はこのタスクになります。 - 赤のラベルを使う - 赤のラベルに「緊急対応」と書く - Doing の一番上に置く - その部門の責任者、主担当者、副担当者の 3 名をアサインする - 時間事に経過をコメントに張っていく - チャットでの対応であればチャットのログをベタベタ張っていく かなり特別のタスクとして扱うことが重要です。 -
voluntas revised this gist
Oct 16, 2013 . No changes.There are no files selected for viewing
-
voluntas revised this gist
Oct 16, 2013 . 1 changed file with 25 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -42,15 +42,19 @@ models.py はこんな感じ .. code-block:: python from django.db import models from django.dispatch import receiver from easy_thumbnails.signals import saved_file from easy_thumbnails.files import get_thumbnailer from .tasks import generate_thumbnails class Photo(models.Model): title = models.CharField(max_length=255) photo = models.ImageField(upload_to='img/%Y/%m/%d') class Meta: ordering = ('title', ) def __unicode__(self): return self.name @@ -64,19 +68,14 @@ models.py はこんな感じ else: return None @receiver(saved_file) def generate_thumbnails_async(sender, fieldfile, **kwargs): generate_thumbnails.delay( model=sender, pk=fieldfile.instance.pk, field=fieldfile.field.name ) .. code-block:: python # tasks.py @@ -90,6 +89,21 @@ models.py はこんな感じ generate_all_aliases(fieldfile, include_global=True) urls.py .. code-block:: python views.py .. code-block:: python forms.py .. code-block:: python 参照 ==== -
voluntas revised this gist
Oct 16, 2013 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,10 +18,13 @@ Django の ImageField を使って S3 に画像をアップロードしたとき セットアップ ============ インストール:: $ pip install django-storages easy-thumbnails django-celery boto - celery のキューには Redis を使用する - django-storages は S3 を使用する settings.py に以下の設定をする -
voluntas revised this gist
Oct 16, 2013 . 1 changed file with 13 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,9 +2,21 @@ django-storages + easy-thumbnails + django-celery コトハジメ(仮) ################################################################ :更新: 2013-10-16 :バージョン: 0.0.0 :作者: @voluntas :URL: http://voluntas.github.io/ - `Django トラノマキ <https://gist.github.com/voluntas/6855579>`_ 概要 ==== Django の ImageField を使って S3 に画像をアップロードしたとき自動でサムネイル画像も生成して欲しい、 さらにそのサムネイル画像の生成は非同期で行って欲しいという処理が必要になったので、どうやって実現するべきなのかをまとめたメモ。 セットアップ ============ :: -
voluntas created this gist
Oct 16, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,91 @@ ################################################################ django-storages + easy-thumbnails + django-celery コトハジメ(仮) ################################################################ Django の ImageField を使って S3 に画像をアップロードしたとき自動でサムネイル画像も生成して欲しい、 さらにそのサムネイル画像の生成は非同期で行って欲しいという処理のメモ。 :: $ pip install django-storages easy-thumbnails django-celery boto settings.py に以下の設定をする .. code-block:: python AWS_ACCESS_KEY_ID = '' AWS_SECRET_ACCESS_KEY = '' AWS_STORAGE_BUCKET_NAME = '' DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage' THUMBNAIL_DEFAULT_STORAGE = 'storages.backends.s3boto.S3BotoStorage' models.py はこんな感じ .. code-block:: python from django.db import models from easy_thumbnails.files import get_thumbnailer class Photo(models.Model): title = models.CharField(max_length=255) photo = models.ImageField(upload_to='img/%Y/%m/%d') class Meta: ordering = ('name', ) def __unicode__(self): return self.name def get_photo_url(self, alias='original'): if self.photo: if alias == 'original': return self.photo.url else: return get_thumbnailer(self.photo)[alias].url else: return None .. code-block:: python # models.py from django.dispatch import receiver from easy_thumbnails.signals import saved_file from myapp import tasks @receiver(saved_file) def generate_thumbnails_async(sender, fieldfile, **kwargs): tasks.generate_thumbnails.delay( model=sender, pk=fieldfile.instance.pk, field=fieldfile.field.name) # tasks.py from celery import task from easy_thumbnails.files import generate_all_aliases @task def generate_thumbnails(model, pk, field): instance = model._default_manager.get(pk=pk) fieldfile = getattr(instance, field) generate_all_aliases(fieldfile, include_global=True) 参照 ==== Easy Thumbnails — easy-thumbnails 1.4 documentation http://easy-thumbnails.readthedocs.org/en/latest/ Asynchronous Pregeneration http://easy-thumbnails.readthedocs.org/en/latest/usage/#asynchronous-pregeneration Amazon S3 — django-storages 1.1.8 documentation http://django-storages.readthedocs.org/en/latest/backends/amazon-S3.html Homepage | Celery: Distributed Task Queue http://www.celeryproject.org/