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 characters
# -*- coding: utf-8 -*- | |
from django.contrib import admin | |
from django.http import HttpResponse | |
class OwnerPermissionAdmin(admin.ModelAdmin): | |
def queryset(self, request): | |
qs = super(OwnerPermissionAdmin, self).queryset(request) | |
if request.user.is_superuser: | |
return qs |
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 characters
import sys | |
sys.setdefaultencoding('utf-8') |
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 characters
def handle_bool(value): | |
if value in [True, 'true', 'True', 'TRUE', 1]: | |
return True | |
return False |
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 characters
# -*- coding: utf-8 -*- | |
import hashlib | |
from celery.task import Task | |
from django.core.cache import cache | |
class SingleTask(Task): | |
""" | |
class AddTask(SingleTask): | |
def run_main(self, x, y): |
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 characters
# -*- coding: utf-8 -*- | |
import urllib2 | |
def get_public_ipv4(ec2instance=False): | |
url = 'http://ip.42.pl/raw' | |
if ec2instance: | |
url = 'http://169.254.169.254/latest/meta-data/public-ipv4' | |
return '%s' % urllib2.urlopen(url).read() |
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 characters
# -*- coding: utf-8 -*- | |
import hashlib | |
def make_hash_key(key, key_prefix, version): | |
""" | |
cache.set('key', 'value') デフォルトの make_key では、keyの値がそのまま使用されるため、 | |
場合によっては、オーバーフローを引き起こす。hash化して、固定長にするための関数です。 | |
CACHES の KEY_FUNCTION にこの関数を設定して使用します。 | |
""" |
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 characters
# -*- coding: utf-8 -*- | |
import hashlib | |
try: | |
import cPickle as pickle | |
except ImportError: | |
import pickle | |
class PythonObjectCache(object): | |
@classmethod |
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 characters
for i, v in enumerate(['a', 'b', 'c']): | |
print i, v |
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 characters
# -*- coding: utf-8 -*- | |
from os.path import abspath, dirname, join | |
PROJECT_PATH = abspath(dirname(__file__)) | |
DATABASES = { | |
'default': { | |
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. | |
'NAME': join(PROJECT_PATH, 'django.db'), # Or path to database file if using sqlite3. | |
# The following settings are not used with sqlite3: | |
'USER': '', |
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 characters
def dictsorted(dic, valuesort=False): | |
if valuesort: | |
return [(k, v) for k, v in sorted(d.items(), key=lambda x:x[1])] | |
return [(k, v) for k, v in sorted(d.items())] | |
if __name__ == '__main__': | |
d = {'A':500, 'C':300, 'D':100, 'E':400, 'B':200} | |
print dictsorted(d, False) | |
print dictsorted(d, True) |
OlderNewer