def do_run(self): obj = json.loads(self.result.uri) note_id = obj['id'] preview = Unity.GenericPreview.new(self.result.title, self.result.comment.strip(), None) image = None for _res in everpad_provider.get_note_resources(note_id): res = Resource.from_tuple(_res) if 'image' in res.mime: image = 'file://%s' % res.file_path if image: preview.props.image_source_uri = image if self.result.metadata: if 'created' in self.result.metadata and self.result.metadata['created'].get_string() != '': preview.add_info(Unity.InfoHint.new("created", _("Created"), None, self.result.metadata['created'].get_string())) if 'last_changed' in self.result.metadata and self.result.metadata['last_changed'].get_string() != '': preview.add_info(Unity.InfoHint.new("last_changed", _("Changed"), None, self.result.metadata['last_changed'].get_string())) if 'tags' in self.result.metadata and self.result.metadata['tags'].get_string() != '': preview.add_info(Unity.InfoHint.new("tags", _("Tags"), None, self.result.metadata['tags'].get_string())) view_action = Unity.PreviewAction.new("view", _("Edit"), None) preview.add_action(view_action) #edit = Unity.PreviewAction.new("edit", "Edit", None) #edit.connect('activated', self.handle_uri) #preview.add_action(edit) return preview
def add_attach(self, name): """Add name as an attachment and return the attachment as a Resource name - a string containing a filename or url that will be attached return: the resource object corresponding to the attached object """ dest = os.path.expanduser('~/.everpad/data/%d/' % self.note.id) try: os.mkdir(dest) except OSError: pass file_name = name.split('/')[-1] file_path = prepare_file_path(dest, file_name) if os.path.isfile(name): shutil.copyfile(name, file_path) else: with open(file_path, 'w') as res_file: res_file.write(urllib.urlopen(name).read()) res = Resource( id=NONE_ID, file_path=file_path, file_name=file_name, mime=self.mime.file(file_path.encode('utf8')), hash=hashlib.md5(open(file_path).read()).hexdigest(), ) self._resources.append(res) self._put(res) self.on_change() return res
def test_note_resources(self): """Test note resources""" notebook = Notebook.from_tuple( self.service.create_notebook('test', None), ) struct = self.service.create_note( Note( id=NONE_ID, title='New note', content="New note content", tags=[], notebook=notebook.id, created=NONE_VAL, updated=NONE_VAL, place='', ).struct) note = Note.from_tuple(self.service.update_note(struct)) resources = [] for i in range(100): resources.append( Resource( id=NONE_ID, file_name="name/%d" % i, file_path="path/%d" % i, mime='image/png', hash='', )) self.service.update_note_resources( note.struct, map(lambda resource: resource.struct, resources), ) received = map(Resource.from_tuple, self.service.get_note_resources(note.id)) self.assertEqual( self._file_names(resources), self._file_names(received), ) received = received[:50] self.service.update_note_resources( note.struct, map(lambda resource: resource.struct, received), ) new_received = map(Resource.from_tuple, self.service.get_note_resources(note.id)) self.assertEqual( self._file_names(new_received), self._file_names(received), )
def preview(self, scope, uri): obj = json.loads(uri) note = Note.from_tuple(provider.get_note(obj['id'])) preview = Unity.GenericPreview.new( note.title, html2text(note.content), None, ) edit = Unity.PreviewAction.new("edit", "Edit", None) image = None for _res in provider.get_note_resources(note.id): res = Resource.from_tuple(_res) if 'image' in res.mime: image = 'file://%s' % res.file_path if image: preview.props.image_source_uri = image edit.connect('activated', self.handle_uri) preview.add_action(edit) return preview
def add_attach(self, name): dest = os.path.expanduser('~/.everpad/data/%d/' % self.note.id) try: os.mkdir(dest) except OSError: pass file_name = name.split('/')[-1] file_path = os.path.join(dest, file_name) if os.path.isfile(name): shutil.copyfile(name, file_path) else: with open(file_path, 'w') as res_file: res_file.write(urllib.urlopen(name).read()) res = Resource( id=NONE_ID, file_path=file_path, file_name=file_name, mime=self.mime.file(file_path.encode('utf8')), hash=hashlib.md5(open(file_path).read()).hexdigest(), ) self._resources.append(res) self._put(res) self.on_change() return res