def drag_data_received(self, widget, context, x, y, sel_data, info, time): """ Handle the standard gtk interface for drag_data_received. If the selection data is define, extract the value from sel_data.data, and decide if this is a move or a reorder. The only data we accept on mediaview is dropping a file, so URI_LIST. We assume this is what we obtain """ if not sel_data: return files = sel_data.get_uris() for file in files: protocol, site, mfile, j, k, l = urlparse(file) if protocol == "file": name = url2pathname(mfile) mime = get_type(name) if not is_valid_type(mime): return photo = Media() self.uistate.set_busy_cursor(True) photo.set_checksum(create_checksum(name)) self.uistate.set_busy_cursor(False) base_dir = str(media_path(self.dbstate.db)) if os.path.exists(base_dir): name = relative_path(name, base_dir) photo.set_path(name) photo.set_mime_type(mime) basename = os.path.basename(name) (root, ext) = os.path.splitext(basename) photo.set_description(root) with DbTxn(_("Drag Media Object"), self.dbstate.db) as trans: self.dbstate.db.add_media(photo, trans) widget.emit_stop_by_name('drag_data_received')
def _run(self): """ Go through directories that are mentioned in the database via media files, and include all images that are not all ready included. """ if not self.prepared: self.prepare() self.set_total(len(self.dir_list)) for directory in self.dir_list: for (dirpath, dirnames, filenames) in os.walk(directory): if ".git" in dirnames: dirnames.remove('.git') # don't visit .git directories for filename in filenames: media_full_path = os.path.join(dirpath, filename) if media_full_path not in self.path_list: self.path_list.append(media_full_path) mime_type = get_type(media_full_path) if is_image_type(mime_type): obj = Media() obj.set_path(media_full_path) obj.set_mime_type(mime_type) (root, ext) = os.path.splitext(filename) obj.set_description(root) self.db.add_media(obj, self.trans) self.update() return True
def add_button_clicked(self, obj): try: from .. import EditMediaRef EditMediaRef(self.dbstate, self.uistate, self.track, Media(), MediaRef(), self.add_callback) except WindowActiveError: pass
def sort_date(self,data): obj = Media() obj.unserialize(data) d = obj.get_date_object() if d: return "%09d" % d.get_sort_value() else: return ''
def _prepare(self): self.set_total(self.db.get_number_of_media()) with self.db.get_media_cursor() as cursor: for handle, data in cursor: obj = Media() obj.unserialize(data) if os.path.isabs(obj.path): self.handle_list.append(handle) self.path_list.append(obj.path) self.update() self.reset()
def call_editor(self, obj=None): if obj is None: object = Media() func = self.obj_added else: object = obj func = self.after_edit try: EditMedia(self.dbstate, self.uistate, self.track, object, func) except WindowActiveError: pass
def _prepare(self): from_text = str(self.from_entry.get_text()) self.set_total(self.db.get_number_of_media()) with self.db.get_media_cursor() as cursor: for handle, data in cursor: obj = Media() obj.unserialize(data) if obj.get_path().find(from_text) != -1: self.handle_list.append(handle) self.path_list.append(obj.path) self.update() self.reset() self.prepared = True
def post(self, path): if "/" in path: handle, action = path.split("/") else: handle, action = path, "view" if handle == "add": media = Media() media.handle = handle = create_id() else: media = self.database.get_media_from_handle(handle) form = MediaForm(self.database, _, instance=media) form.save(handler=self) self.redirect("/media/%(handle)s" % {"handle": handle})
def get(self, path=""): """ HANDLE HANDLE/edit|delete /add b2cfa6ca1e174b1f63d/remove/eventref/1 """ page = int(self.get_argument("page", 1)) search = self.get_argument("search", "") if "/" in path: handle, action = path.split("/", 1) else: handle, action = path, "view" if handle: if handle == "add": media = Media() action = "edit" else: media = self.database.get_media_from_handle(handle) if media: self.render( "media.html", **self.get_template_dict(tview=_("media detail"), action=action, page=page, search=search, form=MediaForm(self.database, _, instance=media), logform=None)) return else: self.clear() self.set_status(404) self.finish("<html><body>No such media</body></html>") return self.render( "page_view.html", **self.get_template_dict( tview=_("media view"), page=page, search=search, form=MediaForm(self.database, _, table="Media"), ))
def _prepare(self): """ Get all of the fullpaths, and the directories of media objects in the database. """ self.dir_list = set() self.set_total(self.db.get_number_of_media()) with self.db.get_media_cursor() as cursor: for handle, data in cursor: obj = Media() obj.unserialize(data) self.handle_list.append(handle) full_path = media_path_full(self.db, obj.path) self.path_list.append(full_path) directory, filename = os.path.split(full_path) if directory not in self.dir_list: self.dir_list.add(directory) self.update() self.reset()
def post(self) -> Response: """Post a new object.""" require_permissions([PERM_ADD_OBJ]) mime = request.content_type if not mime: abort(HTTPStatus.NOT_ACCEPTABLE) checksum, f = process_file(request.stream) base_dir = current_app.config.get("MEDIA_BASE_DIR", "") media_handler = MediaHandler(base_dir) media_handler.upload_file(f, checksum, mime) path = media_handler.get_default_filename(checksum, mime) db_handle = self.db_handle_writable obj = Media() obj.set_checksum(checksum) obj.set_path(path) obj.set_mime_type(mime) with DbTxn("Add object", db_handle) as trans: try: add_object(db_handle, obj, trans) except ValueError: abort(400) trans_dict = transaction_to_json(trans) return self.response(201, trans_dict, total_items=len(trans_dict))
def empty_object(self): return Media()
def add(self, *obj): """Add a new media object to the media list""" try: EditMedia(self.dbstate, self.uistate, [], Media()) except WindowActiveError: pass
def _process(self, count, total, sql): # --------------------------------- # Process note # --------------------------------- notes = sql.query("""select * from note;""") for note in notes: (handle, gid, text, _format, note_type1, note_type2, change, private) = note styled_text = [text, []] markups = sql.query( """select * from link where from_handle = ? """ """and to_type = 'markup';""", handle) for markup_link in markups: _from_type, _from_handle, _to_type, to_handle = markup_link markup_detail = sql.query( """select * from markup where handle = ?;""", to_handle) for markup in markup_detail: (_mhandle, markup0, markup1, value, start_stop_list) = markup ss_list = eval(start_stop_list) styled_text[1] += [((markup0, markup1), value, ss_list)] tags = self.get_links(sql, "note", handle, "tag") g_note = Note() g_note.unserialize( (handle, gid, styled_text, _format, (note_type1, note_type2), change, tags, bool(private))) self.db.add_note(g_note, self.trans) count += 1 self.callback(100 * count / total) # --------------------------------- # Process event # --------------------------------- events = sql.query("""select * from event;""") for event in events: (handle, gid, the_type0, the_type1, description, change, private) = event note_list = self.get_note_list(sql, "event", handle) citation_list = self.get_citation_list(sql, "event", handle) media_list = self.get_media_list(sql, "event", handle) attribute_list = self.get_attribute_list(sql, "event", handle) date_handle = self.get_link(sql, "event", handle, "date") date = self.get_date(sql, date_handle) place_handle = self.get_link(sql, "event", handle, "place") place = self.get_place_from_handle(sql, place_handle) tags = self.get_links(sql, "event", handle, "tag") data = (handle, gid, (the_type0, the_type1), date, description, place, citation_list, note_list, media_list, attribute_list, change, tags, bool(private)) g_event = Event() g_event.unserialize(data) self.db.add_event(g_event, self.trans) count += 1 self.callback(100 * count / total) # --------------------------------- # Process person # --------------------------------- people = sql.query("""select * from person;""") for person in people: if person is None: continue ( handle, # 0 gid, # 1 gender, # 2 death_ref_handle, # 5 birth_ref_handle, # 6 change, # 17 private, # 19 ) = person primary_name = self.get_names(sql, "person", handle, True) # one alternate_names = self.get_names(sql, "person", handle, False) event_ref_list = self.get_event_ref_list(sql, "person", handle) family_list = self.get_family_list(sql, "person", handle) parent_family_list = self.get_parent_family_list( sql, "person", handle) media_list = self.get_media_list(sql, "person", handle) address_list = self.get_address_list(sql, "person", handle, with_parish=False) attribute_list = self.get_attribute_list(sql, "person", handle) urls = self.get_url_list(sql, "person", handle) lds_ord_list = self.get_lds_list(sql, "person", handle) pcitation_list = self.get_citation_list(sql, "person", handle) pnote_list = self.get_note_list(sql, "person", handle) person_ref_list = self.get_person_ref_list(sql, "person", handle) death_ref_index = lookup(death_ref_handle, event_ref_list) birth_ref_index = lookup(birth_ref_handle, event_ref_list) tags = self.get_links(sql, "person", handle, "tag") data = ( handle, # 0 gid, # 1 gender, # 2 primary_name, # 3 alternate_names, # 4 death_ref_index, # 5 birth_ref_index, # 6 event_ref_list, # 7 family_list, # 8 parent_family_list, # 9 media_list, # 10 address_list, # 11 attribute_list, # 12 urls, # 13 lds_ord_list, # 14 pcitation_list, # 15 pnote_list, # 16 change, # 17 tags, bool(private), # 19 person_ref_list, ) # 20 g_pers = Person() g_pers.unserialize(data) self.db.add_person(g_pers, self.trans) count += 1 self.callback(100 * count / total) # --------------------------------- # Process family # --------------------------------- families = sql.query("""select * from family;""") for family in families: (handle, gid, father_handle, mother_handle, the_type0, the_type1, change, private) = family child_ref_list = self.get_child_ref_list(sql, "family", handle) event_ref_list = self.get_event_ref_list(sql, "family", handle) media_list = self.get_media_list(sql, "family", handle) attribute_list = self.get_attribute_list(sql, "family", handle) lds_seal_list = self.get_lds_list(sql, "family", handle) citation_list = self.get_citation_list(sql, "family", handle) note_list = self.get_note_list(sql, "family", handle) tags = self.get_links(sql, "family", handle, "tag") data = (handle, gid, father_handle, mother_handle, child_ref_list, (the_type0, the_type1), event_ref_list, media_list, attribute_list, lds_seal_list, citation_list, note_list, change, tags, private) g_fam = Family() g_fam.unserialize(data) self.db.add_family(g_fam, self.trans) count += 1 self.callback(100 * count / total) # --------------------------------- # Process repository # --------------------------------- repositories = sql.query("""select * from repository;""") for repo in repositories: (handle, gid, the_type0, the_type1, name, change, private) = repo note_list = self.get_note_list(sql, "repository", handle) address_list = self.get_address_list(sql, "repository", handle, with_parish=False) urls = self.get_url_list(sql, "repository", handle) tags = self.get_links(sql, "repository", handle, "tag") data = (handle, gid, (the_type0, the_type1), name, note_list, address_list, urls, change, tags, private) g_rep = Repository() g_rep.unserialize(data) self.db.add_repository(g_rep, self.trans) count += 1 self.callback(100 * count / total) # --------------------------------- # Process place # --------------------------------- places = sql.query("""select * from place;""") for place in places: count += 1 (handle, gid, title, value, the_type0, the_type1, code, long, lat, lang, change, private) = place # We could look this up by "place_main", but we have the handle: #main_loc = self.get_main_location(sql, handle, with_parish=True) alt_loc_list = self.get_location_list(sql, "place_alt", handle, with_parish=True) urls = self.get_url_list(sql, "place", handle) media_list = self.get_media_list(sql, "place", handle) citation_list = self.get_citation_list(sql, "place", handle) note_list = self.get_note_list(sql, "place", handle) tags = self.get_links(sql, "place", handle, "tag") place_type = (the_type0, the_type1) alt_place_name_list = self.get_alt_place_name_list(sql, handle) place_ref_list = self.get_place_ref_list(sql, handle) data = (handle, gid, title, long, lat, place_ref_list, PlaceName(value=value, lang=lang).serialize(), alt_place_name_list, place_type, code, alt_loc_list, urls, media_list, citation_list, note_list, change, tags, private) g_plac = Place() g_plac.unserialize(data) self.db.commit_place(g_plac, self.trans) self.callback(100 * count / total) # --------------------------------- # Process citation # --------------------------------- citations = sql.query("""select * from citation;""") for citation in citations: (handle, gid, confidence, page, source_handle, change, private) = citation date_handle = self.get_link(sql, "citation", handle, "date") date = self.get_date(sql, date_handle) note_list = self.get_note_list(sql, "citation", handle) media_list = self.get_media_list(sql, "citation", handle) datamap = self.get_datamap_list(sql, "citation", handle) tags = self.get_links(sql, "citation", handle, "tag") data = (handle, gid, date, page, confidence, source_handle, note_list, media_list, datamap, change, tags, private) g_cit = Citation() g_cit.unserialize(data) self.db.commit_citation(g_cit, self.trans) count += 1 self.callback(100 * count / total) # --------------------------------- # Process source # --------------------------------- sources = sql.query("""select * from source;""") for source in sources: (handle, gid, title, author, pubinfo, abbrev, change, private) = source note_list = self.get_note_list(sql, "source", handle) media_list = self.get_media_list(sql, "source", handle) datamap = self.get_datamap_list(sql, "source", handle) reporef_list = self.get_repository_ref_list(sql, "source", handle) tags = self.get_links(sql, "source", handle, "tag") data = (handle, gid, title, author, pubinfo, note_list, media_list, abbrev, change, datamap, reporef_list, tags, private) g_src = Source() g_src.unserialize(data) self.db.commit_source(g_src, self.trans) count += 1 self.callback(100 * count / total) # --------------------------------- # Process media # --------------------------------- media = sql.query("""select * from media;""") for med in media: (handle, gid, path, mime, desc, checksum, change, private) = med attribute_list = self.get_attribute_list(sql, "media", handle) citation_list = self.get_citation_list(sql, "media", handle) note_list = self.get_note_list(sql, "media", handle) date_handle = self.get_link(sql, "media", handle, "date") date = self.get_date(sql, date_handle) tags = self.get_links(sql, "media", handle, "tag") data = (handle, gid, path, mime, desc, checksum, attribute_list, citation_list, note_list, change, date, tags, private) g_med = Media() g_med.unserialize(data) self.db.commit_media(g_med, self.trans) count += 1 self.callback(100 * count / total) # --------------------------------- # Process tag # --------------------------------- tags = sql.query("""select * from tag;""") for tag in tags: (handle, name, color, priority, change) = tag data = (handle, name, color, priority, change) g_tag = Tag() g_tag.unserialize(data) self.db.commit_tag(g_tag, self.trans) count += 1 self.callback(100 * count / total)
def drag_data_received(self, widget, context, x, y, sel_data, info, time): """ Handle the standard gtk interface for drag_data_received. If the selection data is define, extract the value from sel_data.data, and decide if this is a move or a reorder. """ if sel_data and sel_data.get_data(): sel_list = [] try: rets = pickle.loads(sel_data.get_data()) # single dnd item if isinstance(rets, tuple): sel_list.append(rets) # multiple dnd items elif isinstance(rets, list): for ret in rets: sel_list.append(pickle.loads(ret)) for sel in sel_list: (mytype, selfid, obj, row_from) = sel # make sure this is the correct DND type for this object if mytype == self._DND_TYPE.drag_type: # determine the destination row data = self.iconlist.get_dest_item_at_pos(x, y) if data: (path, pos) = data row = path.get_indices()[0] if pos == Gtk.IconViewDropPosition.DROP_LEFT: row = max(row, 0) elif pos == Gtk.IconViewDropPosition.DROP_RIGHT: row = min(row, len(self.get_data())) elif pos == Gtk.IconViewDropPosition.DROP_INTO: row = min(row + 1, len(self.get_data())) else: row = len(self.get_data()) # if the is same object, we have a move, otherwise, # it is a standard drag-n-drop if id(self) == selfid: self._move(row_from, row, obj) else: self._handle_drag(row, obj) self.rebuild() elif mytype == DdTargets.MEDIAOBJ.drag_type: oref = MediaRef() oref.set_reference_handle(obj) self.get_data().append(oref) self.changed = True self.rebuild() elif self._DND_EXTRA and mytype == self._DND_EXTRA.drag_type: self.handle_extra_type(mytype, obj) except pickle.UnpicklingError: files = sel_data.get_uris() for file in files: protocol, site, mfile, j, k, l = urlparse(file) if protocol == "file": name = url2pathname(mfile) mime = get_type(name) if not is_valid_type(mime): return photo = Media() self.uistate.set_busy_cursor(True) photo.set_checksum(create_checksum(name)) self.uistate.set_busy_cursor(False) base_dir = str(media_path(self.dbstate.db)) if os.path.exists(base_dir): name = relative_path(name, base_dir) photo.set_path(name) photo.set_mime_type(mime) basename = os.path.basename(name) (root, ext) = os.path.splitext(basename) photo.set_description(root) with DbTxn(_("Drag Media Object"), self.dbstate.db) as trans: self.dbstate.db.add_media(photo, trans) oref = MediaRef() oref.set_reference_handle(photo.get_handle()) self.get_data().append(oref) self.changed = True self.rebuild()
def empty_media(self): return Media()