Adding files to repo.

This commit is contained in:
Brandon Taylor
2011-08-31 19:51:02 -05:00
parent 41b83e155f
commit 4e38d8fbe1
79 changed files with 9612 additions and 0 deletions
File diff suppressed because it is too large Load Diff
+2
View File
@@ -0,0 +1,2 @@
VERSION = (1, 0, 1)
__version__ = '1.0.1'
Binary file not shown.
+15
View File
@@ -0,0 +1,15 @@
import os, os.path as osp
from django.conf import settings
from django.db.models import get_apps
def get_app_resource(app, path):
apps = get_apps()
for x in apps:
app_dir = osp.dirname(x.__file__)
if app == x.__name__.split('.')[-2]:
resource_dir = osp.join(app_dir, "resources")
asset = osp.join(resource_dir, path)
if osp.exists(asset):
return asset
continue
Binary file not shown.
@@ -0,0 +1,35 @@
from django.core.management.base import LabelCommand, CommandError
from django.db.models import get_apps
import os, os.path as osp
import shutil
class Command(LabelCommand):
args = ''
label = 'directory'
def handle(self, *labels, **options):
if not labels or len(labels) > 1:
raise CommandError('Enter one directory name.')
label = labels[0]
final_dest = osp.join(os.getcwd(), label)
if osp.exists(final_dest):
raise CommandError('Directory already exists')
os.mkdir(final_dest)
apps = get_apps()
for x in apps:
app_dir = osp.dirname(x.__file__)
module = x.__name__
app = module.split('.')[-2]
if app == 'admin': continue
media_dir = osp.join(app_dir, "media", app)
if not osp.isdir(media_dir):
media_dir = osp.join(app_dir, "media")
if osp.exists(media_dir):
print "copy", media_dir, '->', osp.join(final_dest, app)
shutil.copytree(media_dir, osp.join(final_dest, app))
@@ -0,0 +1,53 @@
from django.core.management.base import NoArgsCommand
from django.db.models import get_apps
class Command(NoArgsCommand):
help = """
Removes all symlinks in MEDIA_ROOT and then scans all installed applications for a media folder to symlink to MEDIA_ROOT.
If installed app has a media folder, it first attempts to symlink the contents
ie: app/media/app_name -> MEDIA_ROOT/app_name
If the symlink name already exists, it assumes the media directory is not subfoldered and attempts:
ie: app/media -> MEDIA_ROOT/app_name"""
def handle_noargs(self, **options):
from django.conf import settings
import os
media_path = settings.MEDIA_ROOT
print "creating symlinks for app media under %s" % media_path
for d in os.listdir(media_path):
path = os.path.join(media_path, d)
if os.path.islink(path):
os.remove(os.path.join(path))
print " - removed %s" % path
apps = get_apps()
for app in apps:
app_file = app.__file__
if os.path.splitext(app_file)[0].endswith('/__init__'):
# models are an folder, go one level up
app_file = os.path.dirname(app_file)
app_path = os.path.dirname(app_file)
if 'media' in os.listdir(app_path) and os.path.isdir(os.path.join(app_path,'media')):
module = app.__name__
app_name = module.split('.')[-2]
app_media = os.path.join(app_path, "media", app_name)
if not os.path.isdir(app_media):
app_media = os.path.join(app_path, "media")
try:
os.symlink(app_media, os.path.join(media_path, app_name))
print " + added %s as %s" % (os.path.join(app_media), os.path.join(media_path, app_name))
except OSError, e:
if e.errno == 17:
pass
print " o skipping %s" % app_media
else:
raise
# try:
# os.symlink(app_media, os.path.join(media_path,app.split('.')[-1]))
# print " + added %s as %s" % (app_media, os.path.join(media_path, app.split('.')[-1]))
+38
View File
@@ -0,0 +1,38 @@
from md5 import md5
from django.conf import settings
from django.core.cache import cache
from django.core.urlresolvers import reverse
from appmedia.BeautifulSoup import BeautifulSoup, Tag
boundary = '*#*'
class ReplaceAssets(object):
def process_response(self, request, response):
if response['Content-Type'].startswith('text/html') and settings.CACHE_BACKEND not in ['', None, 'dummy:///']:
soup = BeautifulSoup(response.content)
head = soup.head
#[script.extract() for script in head.findAll(lambda x: x.name == 'script' and 'src' in dict(x.attrs) and x['src'].startswith(settings.MEDIA_URL) )]
#[css.extract() for css in head.findAll(lambda x: x.name == 'link' and 'href' in dict(x.attrs) and x['href'].startswith(settings.MEDIA_URL) )]
scripts = head.findAll(lambda x: x.name == 'script' and 'src' in dict(x.attrs) and x['src'].startswith(settings.MEDIA_URL) )
css = head.findAll(lambda x: x.name == 'link' and 'href' in dict(x.attrs) and x['href'].startswith(settings.MEDIA_URL) )
script_sources = [x['src'] for x in scripts]
new_script = md5(boundary.join(script_sources)).hexdigest()
cache.set(new_script, script_sources)
[x.extract() for x in scripts]
css_sources = [x['href'] for x in css]
new_css = md5(boundary.join(css_sources)).hexdigest()
cache.set(new_css, css_sources)
[x.extract() for x in css]
tag = Tag(soup, "script", [("type", "text/javascript"), ("src", reverse('cached_asset', kwargs={'asset':new_script+".js"}) )])
head.insert(0, tag)
tag = Tag(soup, "link", [("type", "text/css"), ("href", reverse('cached_asset', kwargs={'asset':new_css+".css"})), ('rel', 'stylesheet')])
head.insert(0, tag)
response.content = soup.prettify()
return response
+3
View File
@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.
Binary file not shown.
+7
View File
@@ -0,0 +1,7 @@
from django.conf.urls.defaults import *
from django.conf import settings
urlpatterns = patterns('',
url(r'^cached-asset/(?P<asset>.*)$', 'appmedia.views.serve_cached_asset', name="cached_asset"),
(r'^(?P<app>[^/]*)/(?P<path>.*)$', 'appmedia.views.serve'),
)
Binary file not shown.
+81
View File
@@ -0,0 +1,81 @@
import os, os.path as osp
from django.conf import settings
from django.views.static import serve as django_serve
from django.views.decorators.cache import cache_page
from django.db.models import get_apps
from django.core.cache import cache
from django.http import Http404, HttpResponse
def serve(request, app, path, show_indexes=True):
if request.method == 'GET':
apps = get_apps()
for x in apps:
app_dir = osp.dirname(x.__file__)
module = x.__name__
if app == module.split('.')[-2]: #we get the models module here
if app_dir.endswith("models"):
# this can happen only in case when models are an directory
app_dir = osp.split(app_dir)[0]
media_dir = osp.join(app_dir, "media", app)
if not osp.isdir(media_dir):
media_dir = osp.join(app_dir, "media")
asset = osp.join(media_dir, path)
if osp.exists(asset):
return django_serve(request, path, document_root=media_dir, show_indexes=show_indexes)
#continue
return django_serve(request, app+"/"+path, document_root=settings.MEDIA_ROOT, show_indexes=show_indexes)
elif request.method == 'POST':
data = request.POST.get("data", "")
apps = get_apps()
for x in apps:
app_dir = osp.dirname(x.__file__)
module = x.__name__
if app == module.split('.')[-2]: #we get the models module here
media_dir = osp.join(app_dir, "media")
asset = osp.join(media_dir, path)
if osp.exists(asset):
f = file(asset, 'w')
for line in data.split('\n'):
line.strip()
line = line[:-1]
if line:
selector, datap = line.split('{')
print >>f, selector, '{'
datap.strip()
lines = datap.split(';')
if lines:
print >>f, " "+";\n ".join(lines)
print >>f, '}\n'
f.close()
return django_serve(request, path, document_root=media_dir, show_indexes=show_indexes)
continue
def get_file(path):
app = path.split('/')[2]
path = "/".join(path.split('/')[3:])
apps = get_apps()
for x in apps:
app_dir = osp.dirname(x.__file__)
module = x.__name__
if app == module.split('.')[-2]: #we get the models module here
media_dir = osp.join(app_dir, "media")
asset = osp.join(media_dir, path)
if osp.exists(asset):
print osp.join(media_dir, path)
return osp.join(media_dir, path)
return osp.join(settings.MEDIA_ROOT, app+"/"+path)
@cache_page(24*60*60)
def serve_cached_asset(request, asset):
name, ext = asset.split('.')
files = cache.get(name)
if ext == 'js':
response = HttpResponse("\n".join([file(get_file(path)).read() for path in files]), mimetype="text/javascript")
return response
elif ext == 'css':
response = HttpResponse("\n".join([file(get_file(path)).read() for path in files]), mimetype="text/css")
return response
raise Http404()
Binary file not shown.