Export from webapp is complete; working on import into webapp

svn: r18326
This commit is contained in:
Doug Blank 2011-10-15 20:12:11 +00:00
parent 382336e9b5
commit f492515494
6 changed files with 80 additions and 78 deletions

View File

@ -18,7 +18,7 @@
</tr>
<td class="ColumnAttribute">Options:</td>
<td class="ColumnValue" id="data">
<input autocomplete="off" name="options" id="get_focus" type="text" size="50" value="{{report.options}}"></input><br>
<input autocomplete="off" name="options" id="get_focus" type="text" size="50" value="{% if report.options %}{{report.options}}{% else %}{% endif %}"></input><br>
<i>Hint</i>: use Gramps CLI options such as output file format <b>off=pdf</b>, <b>off=ged</b>, or <b>off=gramps</b>
</td>
</tr>

View File

@ -28,7 +28,6 @@
#------------------------------------------------------------------------
import cPickle
import base64
import web
import gen
import re
from gen.db import DbReadBase, DbWriteBase, DbTxn
@ -43,79 +42,6 @@ from gen.db import (PERSON_KEY,
import Utils
from web.libdjango import DjangoInterface
# Example for running a report:
# ------------------------------
# from cli.plug import run_report
# from django.conf import settings
# import web.settings as default_settings
# try:
# settings.configure(default_settings)
# except:
# pass
# import dbdjango
# db = dbdjango.DbDjango()
# run_report(db, "ancestor_report", off="txt", of="ar.txt", pid="I0363")
# Imports for importing a file:
import DbState
from cli.grampscli import CLIManager
from gen.plug import BasePluginManager
import os
def import_file(db, filename, callback):
"""
Import a file (such as a GEDCOM file) into the given db.
>>> import_file(DbDjango(), "/home/user/Untitled_1.ged", lambda a: a)
"""
dbstate = DbState.DbState()
climanager = CLIManager(dbstate, False) # do not load db_loader
climanager.do_reg_plugins(dbstate, None)
pmgr = BasePluginManager.get_instance()
(name, ext) = os.path.splitext(os.path.basename(filename))
format = ext[1:].lower()
import_list = pmgr.get_reg_importers()
for pdata in import_list:
if format == pdata.extension:
mod = pmgr.load_plugin(pdata)
if not mod:
for name, error_tuple in pmgr.get_fail_list():
etype, exception, traceback = error_tuple
print "ERROR:", name, exception
return False
import_function = getattr(mod, pdata.import_function)
db.prepare_import()
import_function(db, filename, callback)
db.commit_import()
return True
return False
def export_file(db, filename, callback):
"""
Export the db to a file (such as a GEDCOM file).
>>> export_file(DbDjango(), "/home/user/Untitled_1.ged", lambda a: a)
"""
dbstate = DbState.DbState()
climanager = CLIManager(dbstate, False) # do not load db_loader
climanager.do_reg_plugins(dbstate, None)
pmgr = BasePluginManager.get_instance()
(name, ext) = os.path.splitext(os.path.basename(filename))
format = ext[1:].lower()
export_list = pmgr.get_reg_exporters()
for pdata in export_list:
if format == pdata.extension:
mod = pmgr.load_plugin(pdata)
if not mod:
for name, error_tuple in pmgr.get_fail_list():
etype, exception, traceback = error_tuple
print "ERROR:", name, exception
return False
export_function = getattr(mod, pdata.export_function)
export_function(db, filename, callback)
return True
return False
class Cursor(object):
def __init__(self, model, func):
self.model = model
@ -586,7 +512,6 @@ class DbDjango(DbWriteBase, DbReadBase):
if handle in self.import_cache:
return self.import_cache[handle]
try:
#person = self.dji.Person.select_related().get(handle=handle)
person = self.dji.Person.get(handle=handle)
except:
return None
@ -596,7 +521,7 @@ class DbDjango(DbWriteBase, DbReadBase):
if self.use_db_cache and repository.cache:
data = cPickle.loads(base64.decodestring(repository.cache))
else:
data = self.dji.get_family(family)
data = self.dji.get_repository(repository)
return gen.lib.Repository.create(data)
def make_source(self, source):

View File

@ -884,6 +884,7 @@ def clear_tables(*categories):
Clear the entries of categories of tables. Category is:
"abstract", "type", "ref", "system", "primary" and "secondary".
"""
# FIXME: I don't think this works anymore...
from django.db import connection, transaction
cursor = connection.cursor()
flush_tables = []

View File

@ -248,8 +248,9 @@ def send_file(request, filename, mimetype):
return response
def process_action(request, view, handle, action):
from web.reports import import_file
from web.reports import export_file
from cli.plug import run_report
from web.dbdjango import export_file
db = DbDjango()
if view == "report":
if request.user.is_authenticated():

74
src/web/reports.py Normal file
View File

@ -0,0 +1,74 @@
# imports for import/export:
import DbState
from cli.grampscli import CLIManager
from gen.plug import BasePluginManager
import os
# Example for running a report:
# ------------------------------
# from cli.plug import run_report
# from django.conf import settings
# import web.settings as default_settings
# try:
# settings.configure(default_settings)
# except:
# pass
# import dbdjango
# db = dbdjango.DbDjango()
# run_report(db, "ancestor_report", off="txt", of="ar.txt", pid="I0363")
def import_file(db, filename, callback):
"""
Import a file (such as a GEDCOM file) into the given db.
>>> import_file(DbDjango(), "/home/user/Untitled_1.ged", lambda a: a)
"""
dbstate = DbState.DbState()
climanager = CLIManager(dbstate, False) # do not load db_loader
climanager.do_reg_plugins(dbstate, None)
pmgr = BasePluginManager.get_instance()
(name, ext) = os.path.splitext(os.path.basename(filename))
format = ext[1:].lower()
import_list = pmgr.get_reg_importers()
for pdata in import_list:
if format == pdata.extension:
mod = pmgr.load_plugin(pdata)
if not mod:
for name, error_tuple in pmgr.get_fail_list():
etype, exception, traceback = error_tuple
print "ERROR:", name, exception
return False
import_function = getattr(mod, pdata.import_function)
db.prepare_import()
import_function(db, filename, callback)
db.commit_import()
return True
return False
def export_file(db, filename, callback):
"""
Export the db to a file (such as a GEDCOM file).
>>> export_file(DbDjango(), "/home/user/Untitled_1.ged", lambda a: a)
"""
dbstate = DbState.DbState()
climanager = CLIManager(dbstate, False) # do not load db_loader
climanager.do_reg_plugins(dbstate, None)
pmgr = BasePluginManager.get_instance()
(name, ext) = os.path.splitext(os.path.basename(filename))
format = ext[1:].lower()
export_list = pmgr.get_reg_exporters()
for pdata in export_list:
if format == pdata.extension:
mod = pmgr.load_plugin(pdata)
if not mod:
for name, error_tuple in pmgr.get_fail_list():
etype, exception, traceback = error_tuple
print "ERROR:", name, exception
return False
export_function = getattr(mod, pdata.export_function)
export_function(db, filename, callback)
return True
return False

View File

@ -609,3 +609,4 @@ register_plugins()
import HtmlDoc
from libhtmlbackend import HtmlBackend
from libhtml import Html