Skip to content

Commit

Permalink
removed non-working stuff from export app.
Browse files Browse the repository at this point in the history
  • Loading branch information
adammck committed Jul 28, 2010
1 parent 95f9792 commit d74c022
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 237 deletions.
56 changes: 0 additions & 56 deletions export/static/javascripts/global.js

This file was deleted.

8 changes: 4 additions & 4 deletions export/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4


from django.conf.urls.defaults import *
import views
from . import views


urlpatterns = patterns('',
url(r'^export/database', views.database),
url(r'^export/str$', views.str_to_excel),
url(r'^export/(?P<app_label>.+?)/(?P<model_name>.+?)$', views.model_to_excel),
url(r'^export/database', views.database)
)
24 changes: 0 additions & 24 deletions export/utils.py

This file was deleted.

171 changes: 18 additions & 153 deletions export/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,36 @@
# vim: ai ts=4 sts=4 et sw=4


import os, re
import os
import datetime
from subprocess import *

from subprocess import Popen, PIPE
from django.conf import settings
from django import http
from django.db import models
from django.utils.text import capfirst
from django.core.exceptions import FieldError
from rapidsms.djangoproject import settings


def database(req):
"""Returns a SQL dump of the current database, by reading the settings
from the config file, and calling the relevant dump program. Currently,
only mySQL and SQLite3 are supported."""
"""
Return a SQL dump of the current database, by reading the settings
from settings.py, and calling the relevant dump program. Currently,
only mySQL and SQLite3 are supported.
"""

conf = vars(settings)
if settings.DATABASE_ENGINE == "mysql":
conf.setdefault('DATABASE_HOST', "localhost")
cmd = "mysqldump --host=%(DATABASE_HOST)s --user=%(DATABASE_USER)s --password=%(DATABASE_PASSWORD)s %(DATABASE_NAME)s" % (conf)
cmd = "mysqldump --host=%s --user=%s --password=%s %s" %\
(settings.DATABASE_HOST, settings.DATABASE_USER,
settings.DATABASE_PASSWORD, settings.DATABASE_NAME)

elif settings.DATABASE_ENGINE == "sqlite3":
cmd = "sqlite3 %(DATABASE_NAME)s .dump" % (conf)
cmd = "sqlite3 %s .dump" %\
(settings.DATABASE_NAME)

else:
return HttpResponse(
"Sorry, %(DATABASE_ENGINE)s databases are not supported yet." % (conf),
status=500, content_type="text/plain")
"Sorry, %s databases are not supported yet." %\
(settings.DATABASE_ENGINE),
content_type="text/plain",
status=500)

# execute the dump command, and wait for it to terminate
proc = Popen([cmd], shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
sql = proc.communicate()
Expand All @@ -39,140 +41,3 @@ def database(req):
resp = http.HttpResponse(sql, mimetype="text/plain")
resp["content-disposition"] = "attachment; filename=%s.sql" % (today)
return resp


def _get_model(app_label, model_name):
model = models.get_model(app_label, model_name)

# check that the model is valid
if model is None:
raise http.Http404(
"App %r, model %r, not found."\
% (app_label, model_name))

return model


def str_to_excel(req):
def __table(str):
return "<table>\n%s</table>" % "".join(map(__row, str.split("\n")))

def __row(str):
return " <tr>\n%s </tr>\n" % "".join(map(__col, str.split("|")))

def __col(str):
str, cs = re.match("^(.*?)(?::(\d+)\s*)?$", str).groups()
return " <td colspan='%s'>%s</td>\n" % (cs, str)

# dump it as a simple html table
html = __table(req.POST["data"])

# download as an excel spreadsheet
resp = http.HttpResponse(html, mimetype='application/vnd.ms-excel')
resp["content-disposition"] = "attachment; filename=test.xls"
return resp


def model_to_excel(request, app_label, model_name, req_filters=None):
model = _get_model(app_label, model_name)
max_depth = 8
rows = []

# if no filters were explictly passed,
# then we will look for them in the GET
if req_filters is None:
req_filters = request.GET

# build a dict of filters, to control
# which objects we get. todo: is this
# dangerous? i can't see any way that
# it is, but it seems kind of wrong
filters = {}
for k ,v in req_filters.items():
filters[str(k)] = v

# fetch the data (might raise if any of the
# params couldn't be matched to model fields
try:
export_data = model.objects.filter(**filters)

except FieldError, e:
return http.HttpResponse(e.message,
status=500, mimetype="text/plain")

# sort the records if requested
if "sort" in req_filters:
export_data = export_data.order_by(str(req_filters["sort"]))


# this function builds a flat list of column titles (verbose names)
# recursively, to include as much data as possible in the export
def build_header(model, depth=0, prefix=""):
columns = []

for field in model._meta.fields:
caption = prefix + capfirst(field.verbose_name)

# if this field is a foreign key, then
# we will recurse to fetch it's fields
if (hasattr(field, "rel")) and (field.rel is not None) and (depth < max_depth):
columns.extend(build_header(field.rel.to, depth+1, caption + ": "))

# not a foreign key, so append
# this column in it's raw form
else:
columns.append("<th>%s</th>" % (caption))

return columns


# the first row contains no data, just field names
rows.append("<tr>%s</tr>" % ("".join(build_header(model))))


# this function is *way* too similar to the function
# above to warrant its independance. abstraction!
def build_row(model, instance=None, depth=0):
columns = []

for field in model._meta.fields:

# fetch the value of this cell
if instance is not None:
cell = getattr(instance, field.name)

# the cell is NONE, but we'll still need to
# recurse if it's a foreign key, so the row
# doesn't end up shorter the rest
else: cell = None


# if this field is a foreign key, then
# we will recurse to fetch it's fields
if (hasattr(field, "rel")) and (field.rel is not None) and (depth < max_depth):
columns.extend(build_row(field.rel.to, cell, depth+1))

# if this cell is none, insert a blank column,
# so we don't have "None" all over the place
elif (cell is None):
columns.append("<td></td>")

# not a foreign key, so append
# this column in it's raw form
else: columns.append("<td>%s</td>" % (cell))
return columns


# the matrix of dumped data
for object in export_data:
row = "".join(build_row(model, object))
rows.append("<tr>%s</tr>" % (row))


# dump it as a simple html table
html = "<table>%s</table>" % ("\n".join(rows))

# download as an excel spreadsheet
resp = http.HttpResponse(html, mimetype='application/vnd.ms-excel')
resp["content-disposition"] = "attachment; filename=%s.xls" % model_name
return resp

0 comments on commit d74c022

Please sign in to comment.