Importing from webpage--- need to report error and status

svn: r18332
This commit is contained in:
Doug Blank 2011-10-16 01:46:28 +00:00
parent e0bf2acbc6
commit 7b4b4136bf
5 changed files with 57 additions and 10 deletions

View File

@ -53,6 +53,12 @@
{% endblock %}
</div>
<div class="content">
{% if error %}
<font color="red">{{error}}</font>
{% endif %}
{% if message %}
<font color="blue">{{message}}</font>
{% endif %}
<div class="grampsweb">
{% block content %}
{% endblock %}

View File

@ -130,6 +130,7 @@ class Place(SourceBase, NoteBase, MediaBase, UrlBase, PrimaryObject):
MediaBase.unserialize(self, media_list)
SourceBase.unserialize(self, source_list)
NoteBase.unserialize(self, note_list)
return self
def get_text_data_list(self):
"""

View File

@ -52,12 +52,12 @@ class Cursor(object):
return self.__next__()
def __next__(self):
for item in self.model.all():
yield (item.handle, self.func(item))
yield (item.handle, self.func(item.handle))
def __exit__(self, *args, **kwargs):
pass
def iter(self):
for item in self.model.all():
yield (item.handle, self.func(item))
yield (item.handle, self.func(item.handle))
yield None
class Bookmarks:
@ -678,19 +678,19 @@ class DbDjango(DbWriteBase, DbReadBase):
return self.dji.Repository.count()
def get_place_cursor(self):
return Cursor(self.dji.Place, self.make_place).iter()
return Cursor(self.dji.Place, self.get_raw_place_data).iter()
def get_person_cursor(self):
return Cursor(self.dji.Person, self.make_person).iter()
return Cursor(self.dji.Person, self.get_raw_person_data).iter()
def get_family_cursor(self):
return Cursor(self.dji.Family, self.make_family).iter()
return Cursor(self.dji.Family, self.get_raw_family_data).iter()
def get_events_cursor(self):
return Cursor(self.dji.Event, self.make_event).iter()
return Cursor(self.dji.Event, self.get_raw_event_data).iter()
def get_source_cursor(self):
return Cursor(self.dji.Source, self.make_source).iter()
return Cursor(self.dji.Source, self.get_raw_source_data).iter()
def has_person_handle(self, handle):
return self.dji.Person.filter(handle=handle).count() == 1

View File

@ -248,8 +248,7 @@ def send_file(request, filename, mimetype):
return response
def process_action(request, view, handle, action):
from webapp.reports import import_file
from webapp.reports import export_file
from webapp.reports import import_file, export_file, download
from cli.plug import run_report
db = DbDjango()
if view == "report":
@ -280,8 +279,23 @@ def process_action(request, view, handle, action):
filename = "/tmp/%s-%s.%s" % (str(profile.user.username), str(handle), args["off"])
export_file(db, filename, lambda n: n) # callback
mimetype = 'text/plain'
elif report.report_type == "import":
context = RequestContext(request)
filename = download(args["i"], "/tmp/%s-%s.ged" % (str(profile.user.username), str(handle)))
if filename is not None:
import threading
def background():
import_file(db, filename, lambda n: n) # callback
threading.Thread(target=background).start()
context["message"] = "Your data is now being imported..."
return redirect("/report/", context)
else:
context["error"] = "No filename was provided or found."
return redirect("/report/", context)
else:
pass # FIXME: error
context = RequestContext(request)
context["error"] = "Invalid report type '%s'" % report.report_type
return redirect("/report/", context)
return send_file(request, filename, mimetype)
# If failure, just fail for now:
context = RequestContext(request)

View File

@ -46,6 +46,32 @@ def import_file(db, filename, callback):
return True
return False
def download(url, filename=None):
import urllib2
import shutil
import urlparse
def getFilename(url,openUrl):
if 'Content-Disposition' in openUrl.info():
# If the response has Content-Disposition, try to get filename from it
cd = dict(map(
lambda x: x.strip().split('=') if '=' in x else (x.strip(),''),
openUrl.info().split(';')))
if 'filename' in cd:
fname = cd['filename'].strip("\"'")
if fname: return fname
# if no filename was found above, parse it out of the final URL.
return os.path.basename(urlparse.urlsplit(openUrl.url)[2])
r = urllib2.urlopen(urllib2.Request(url))
success = None
try:
filename = filename or "/tmp/%s" % getFilename(url,r)
with open(filename, 'wb') as f:
shutil.copyfileobj(r,f)
success = filename
finally:
r.close()
return success
def export_file(db, filename, callback):
"""
Export the db to a file (such as a GEDCOM file).