Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
justquick committed Jan 29, 2010
0 parents commit ba5e9d3
Show file tree
Hide file tree
Showing 440 changed files with 48,357 additions and 0 deletions.
58 changes: 58 additions & 0 deletions README.txt
@@ -0,0 +1,58 @@
GETTING SET UP FOR THE FIRST TIME
---------------------------------

1. Bootstrap your environment. This only needs to be done once:

./setup/_bootstrap.sh


2. Create a virtual environment (it doesn't need to be named EffervescentCollective,
but name it something you'll remember):

mkvirtualenv EffervescentCollective


3. Install the "pip" Python package manager in the new virtual environment:

easy_install pip


4. Upgrade to the latest version of the packages:

./bin/upgrade.sh

5. Link the source directory into the project folder for easier access

ln -s ~/.virtualenvs/EffervescentCollective/src/ externals


WHAT YOU SHOULD DO PERIODICALLY
-------------------------------

1. Switch to the correct virtual environment:

workon EffervescentCollective


2. Upgrade to the latest version of the packages:

./bin/upgrade.sh



WHAT YOU SHOULD DO EVERY DAY
----------------------------

1. Upgrade to the latest code:

git pull


2. Switch to the correct virtual environment:

workon EffervescentCollective


3. Start your local development server:

python manage.py runserver 0.0.0.0:8000
1 change: 1 addition & 0 deletions __init__.py
@@ -0,0 +1 @@

Binary file added __init__.pyc
Binary file not shown.
Empty file added apps/api/__init__.py
Empty file.
Binary file added apps/api/__init__.pyc
Binary file not shown.
31 changes: 31 additions & 0 deletions apps/api/handlers.py
@@ -0,0 +1,31 @@
from piston.handler import BaseHandler
from tagging.models import Tag, TaggedItem
from django.db.models import get_model
from django.core.urlresolvers import reverse
from pprint import pprint

class TagHandler(BaseHandler):
allowed_methods = ('GET',)
model = Tag

def read(self, request):
if 'referer' in request.GET:
bits = filter(None, request.GET['referer'].replace('/admin/','').split('/'))
model = get_model(*bits[:2])

if len(bits) == 2:
r = []
for x in Tag.objects.cloud_for_model(model):
r.append({'size':x.font_size,'name':x.name})
return r
elif len(bits) == 3:
if bits[2] == 'add':
return []
obj = model.objects.get(pk=bits[2])
tags = Tag.objects.get_for_object(obj)
if 'tags' in request.GET:
obj.tags = request.GET['tags']
else:
return tags


Binary file added apps/api/handlers.pyc
Binary file not shown.
9 changes: 9 additions & 0 deletions apps/api/urls.py
@@ -0,0 +1,9 @@
from django.conf.urls.defaults import *
from piston.resource import Resource
from handlers import TagHandler

tag_handler = Resource(TagHandler)

urlpatterns = patterns('',
url(r'^tags/?$', tag_handler),
)
Binary file added apps/api/urls.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions apps/django_ext/__init__.py
@@ -0,0 +1,3 @@
from django.template import add_to_builtins

add_to_builtins('django_ext.templatetags.smart_if')
Binary file added apps/django_ext/__init__.pyc
Binary file not shown.
Empty file added apps/django_ext/admin.py
Empty file.
Binary file added apps/django_ext/admin.pyc
Binary file not shown.
25 changes: 25 additions & 0 deletions apps/django_ext/cache.py
@@ -0,0 +1,25 @@
from django.core.cache import cache
from django.utils.encoding import smart_str
import inspect

# Check if the cache backend supports min_compress_len. If so, add it.
if cache._cache:
if 'min_compress_len' in inspect.getargspec(cache._cache.add)[0] and \
'min_compress_len' in inspect.getargspec(cache._cache.set)[0]:
class CacheClass(cache.__class__):
def add(self, key, value, timeout=None, min_compress_len=150000):
if isinstance(value, unicode):
value = value.encode('utf-8')
# Allow infinite timeouts
if timeout is None:
timeout = self.default_timeout
return self._cache.add(smart_str(key), value, timeout, min_compress_len)

def set(self, key, value, timeout=None, min_compress_len=150000):
if isinstance(value, unicode):
value = value.encode('utf-8')
if timeout is None:
timeout = self.default_timeout
self._cache.set(smart_str(key), value, timeout, min_compress_len)

cache.__class__ = CacheClass
133 changes: 133 additions & 0 deletions apps/django_ext/fields.py
@@ -0,0 +1,133 @@
import functools
from django.db.models.fields.related import ManyToManyField, ReverseManyRelatedObjectsDescriptor, ManyRelatedObjectsDescriptor
from django.db.models.query import QuerySet
from django.db.models import signals
from django_ext.cache import cache
from types import MethodType

CACHE_DURATION = 60 * 30

def invalidate_cache(obj, field):
cache.set(obj._get_cache_key(field=field), None, 5)

def fix_where(where, modified=False):
def wrap_add(f):
@functools.wraps(f)
def add(self, *args, **kwargs):
"""
Wraps django.db.models.sql.where.add to indicate that a new
'where' condition has been added.
"""
self.modified = True
return f(*args, **kwargs)
return add
where.modified = modified
where.add = MethodType(wrap_add(where.add), where, where.__class__)
return where


def get_pk_list_query_set(superclass):
class PKListQuerySet(superclass):
"""
QuerySet that, when unfiltered, fetches objects individually from
the datastore by pk.
The `pk_list` attribute is a list of primary keys for objects that
should be fetched.
"""
def __init__(self, pk_list=[], from_cache=False, *args, **kwargs):
super(PKListQuerySet, self).__init__(*args, **kwargs)
self.pk_list = pk_list
self.from_cache = from_cache
self.query.where = fix_where(self.query.where)

def iterator(self):
if not self.query.where.modified:
for pk in self.pk_list:
yield self.model._default_manager.get(pk=pk)
else:
superiter = super(PKListQuerySet, self).iterator()
while True:
yield superiter.next()

def _clone(self, *args, **kwargs):
c = super(PKListQuerySet, self)._clone(*args, **kwargs)
c.query.where = fix_where(c.query.where, modified=self.query.where.modified)
c.pk_list = self.pk_list
c.from_cache = self.from_cache
return c
return PKListQuerySet


def get_caching_related_manager(superclass, instance, field_name, related_name):
class CachingRelatedManager(superclass):
def all(self):
key = instance._get_cache_key(field=field_name)
qs = super(CachingRelatedManager, self).get_query_set()
PKListQuerySet = get_pk_list_query_set(qs.__class__)
qs = qs._clone(klass=PKListQuerySet)
pk_list = cache.get(key)
if pk_list is None:
pk_list = qs.values_list('pk', flat=True)
cache.add(key, pk_list, CACHE_DURATION)
else:
qs.from_cache = True
qs.pk_list = pk_list
return qs

def add(self, *objs):
super(CachingRelatedManager, self).add(*objs)
for obj in objs:
invalidate_cache(obj, related_name)
invalidate_cache(instance, field_name)

def remove(self, *objs):
super(CachingRelatedManager, self).remove(*objs)
for obj in objs:
invalidate_cache(obj, related_name)
invalidate_cache(instance, field_name)

def clear(self):
objs = list(self.all())
super(CachingRelatedManager, self).clear()
for obj in objs:
invalidate_cache(obj, related_name)
invalidate_cache(instance, field_name)
return CachingRelatedManager


class CachingReverseManyRelatedObjectsDescriptor(ReverseManyRelatedObjectsDescriptor):
def __get__(self, instance, cls=None):
manager = super(CachingReverseManyRelatedObjectsDescriptor, self).__get__(instance, cls)

CachingRelatedManager = get_caching_related_manager(manager.__class__,
instance,
self.field.name,
self.field.rel.related_name)

manager.__class__ = CachingRelatedManager
return manager


class CachingManyRelatedObjectsDescriptor(ManyRelatedObjectsDescriptor):
def __get__(self, instance, cls=None):
manager = super(CachingManyRelatedObjectsDescriptor, self).__get__(instance, cls)

CachingRelatedManager = get_caching_related_manager(manager.__class__,
instance,
self.related.get_accessor_name(),
self.related.field.name)

manager.__class__ = CachingRelatedManager
return manager


class CachingManyToManyField(ManyToManyField):
def contribute_to_class(self, cls, name):
super(CachingManyToManyField, self).contribute_to_class(cls, name)
setattr(cls, self.name, CachingReverseManyRelatedObjectsDescriptor(self))

def contribute_to_related_class(self, cls, related):
super(CachingManyToManyField, self).contribute_to_related_class(cls, related)
setattr(cls, related.get_accessor_name(), CachingManyRelatedObjectsDescriptor(related))
1 change: 1 addition & 0 deletions apps/django_ext/management/__init__.py
@@ -0,0 +1 @@

Binary file added apps/django_ext/management/__init__.pyc
Binary file not shown.
Empty file.
20 changes: 20 additions & 0 deletions apps/django_ext/management/commands/delete_contenttypes.py
@@ -0,0 +1,20 @@
from django.core.management.base import NoArgsCommand
from django.db import transaction

class Command(NoArgsCommand):
help = "Delete contenttypes crapola"

def handle_noargs(self, **options):
from django.db import connection
print "Deleting content types and permissions"
transaction.commit_unless_managed()
transaction.enter_transaction_management()
transaction.managed(True)
cursor = connection.cursor()
cursor.execute("DELETE FROM django_content_type", [])
print cursor.fetchall()
cursor.execute("DELETE FROM auth_permission", [])
print cursor.fetchall()
transaction.commit()
transaction.leave_transaction_management()
print "Contenttypes deleted"
17 changes: 17 additions & 0 deletions apps/django_ext/management/commands/flush_all_memcached.py
@@ -0,0 +1,17 @@
from django.core.management.base import NoArgsCommand
from django.db import transaction
from django.conf import settings

class Command(NoArgsCommand):
help = "Flushes memcached"

def handle_noargs(self, **options):
try:
import memcache
except ImportError:
return
if 'memcached' in settings.CACHE_BACKEND:
servers = settings.CACHE_BACKEND.replace('memcached://',
'').replace('/','').split(';')
mc = memcache.Client(servers, debug=0)
mc.flush_all()

0 comments on commit ba5e9d3

Please sign in to comment.