Importing from webpage--- need to report error and status
svn: r18332
This commit is contained in:
parent
e0bf2acbc6
commit
7b4b4136bf
@ -53,6 +53,12 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
</div>
|
</div>
|
||||||
<div class="content">
|
<div class="content">
|
||||||
|
{% if error %}
|
||||||
|
<font color="red">{{error}}</font>
|
||||||
|
{% endif %}
|
||||||
|
{% if message %}
|
||||||
|
<font color="blue">{{message}}</font>
|
||||||
|
{% endif %}
|
||||||
<div class="grampsweb">
|
<div class="grampsweb">
|
||||||
{% block content %}
|
{% block content %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
@ -130,6 +130,7 @@ class Place(SourceBase, NoteBase, MediaBase, UrlBase, PrimaryObject):
|
|||||||
MediaBase.unserialize(self, media_list)
|
MediaBase.unserialize(self, media_list)
|
||||||
SourceBase.unserialize(self, source_list)
|
SourceBase.unserialize(self, source_list)
|
||||||
NoteBase.unserialize(self, note_list)
|
NoteBase.unserialize(self, note_list)
|
||||||
|
return self
|
||||||
|
|
||||||
def get_text_data_list(self):
|
def get_text_data_list(self):
|
||||||
"""
|
"""
|
||||||
|
@ -52,12 +52,12 @@ class Cursor(object):
|
|||||||
return self.__next__()
|
return self.__next__()
|
||||||
def __next__(self):
|
def __next__(self):
|
||||||
for item in self.model.all():
|
for item in self.model.all():
|
||||||
yield (item.handle, self.func(item))
|
yield (item.handle, self.func(item.handle))
|
||||||
def __exit__(self, *args, **kwargs):
|
def __exit__(self, *args, **kwargs):
|
||||||
pass
|
pass
|
||||||
def iter(self):
|
def iter(self):
|
||||||
for item in self.model.all():
|
for item in self.model.all():
|
||||||
yield (item.handle, self.func(item))
|
yield (item.handle, self.func(item.handle))
|
||||||
yield None
|
yield None
|
||||||
|
|
||||||
class Bookmarks:
|
class Bookmarks:
|
||||||
@ -678,19 +678,19 @@ class DbDjango(DbWriteBase, DbReadBase):
|
|||||||
return self.dji.Repository.count()
|
return self.dji.Repository.count()
|
||||||
|
|
||||||
def get_place_cursor(self):
|
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):
|
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):
|
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):
|
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):
|
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):
|
def has_person_handle(self, handle):
|
||||||
return self.dji.Person.filter(handle=handle).count() == 1
|
return self.dji.Person.filter(handle=handle).count() == 1
|
||||||
|
@ -248,8 +248,7 @@ def send_file(request, filename, mimetype):
|
|||||||
return response
|
return response
|
||||||
|
|
||||||
def process_action(request, view, handle, action):
|
def process_action(request, view, handle, action):
|
||||||
from webapp.reports import import_file
|
from webapp.reports import import_file, export_file, download
|
||||||
from webapp.reports import export_file
|
|
||||||
from cli.plug import run_report
|
from cli.plug import run_report
|
||||||
db = DbDjango()
|
db = DbDjango()
|
||||||
if view == "report":
|
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"])
|
filename = "/tmp/%s-%s.%s" % (str(profile.user.username), str(handle), args["off"])
|
||||||
export_file(db, filename, lambda n: n) # callback
|
export_file(db, filename, lambda n: n) # callback
|
||||||
mimetype = 'text/plain'
|
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:
|
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)
|
return send_file(request, filename, mimetype)
|
||||||
# If failure, just fail for now:
|
# If failure, just fail for now:
|
||||||
context = RequestContext(request)
|
context = RequestContext(request)
|
||||||
|
@ -46,6 +46,32 @@ def import_file(db, filename, callback):
|
|||||||
return True
|
return True
|
||||||
return False
|
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):
|
def export_file(db, filename, callback):
|
||||||
"""
|
"""
|
||||||
Export the db to a file (such as a GEDCOM file).
|
Export the db to a file (such as a GEDCOM file).
|
||||||
|
Loading…
Reference in New Issue
Block a user