Exemple #1
0
    def insert(self):
        """Creates a new file with the object's attributes"""
        if self.validate():

            helpers.set_max_id(helpers.get_max_id() + 1)
            self.id = helpers.get_max_id()
            self.date = datetime.now()

            hooks = helpers.load_hooks()

            hooks.before_dataobj_create(self)
            data = {
                "type": self.type,
                "title": str(self.title),
                "date": self.date.strftime("%x").replace("/", "-"),
                "tags": self.tags,
                "id": self.id,
                "path": self.path
            }
            if self.type == "bookmark" or self.type == "pocket_bookmark":
                data["url"] = self.url

            # convert to markdown file
            dataobj = frontmatter.Post(self.content)
            dataobj.metadata = data
            self.fullpath = create(
                frontmatter.dumps(dataobj),
                f"{self.id}-{dataobj['title']}",
                path=self.path,
            )

            hooks.on_dataobj_create(self)
            self.index()
            return self.id
        return False
Exemple #2
0
    def insert(self):
        """Creates a new file with the object's attributes"""
        if self.validate():
            helpers.set_max_id(helpers.get_max_id() + 1)
            self.id = helpers.get_max_id()
            self.date = datetime.now()
            data = {
                "type": self.type,
                "desc": self.desc,
                "title": str(self.title),
                "date": self.date.strftime("%x").replace("/", "-"),
                "tags": self.tags,
                "id": self.id,
                "path": self.path
            }
            if self.type == "bookmark" or self.type == "pocket_bookmark":
                data["url"] = self.url

            # convert to markdown file
            dataobj = frontmatter.Post(self.content)
            dataobj.metadata = data
            self.fullpath = create(
                frontmatter.dumps(dataobj),
                str(self.id) + "-" + dataobj["date"] + "-" + dataobj["title"],
                path=self.path,
            )

            add_to_index(current_app.config['INDEX_NAME'], self)
            return self.id
        return False
Exemple #3
0
def test_new_note(test_app, note_fixture):
    """
    Check that a new note is correctly saved into the filesystem
    with the right attributes and the right id.
    """

    with test_app.app_context():
        max_id = get_max_id()
        assert note_fixture.id == max_id

    saved_file = frontmatter.load(note_fixture.fullpath)
    for attr in attributes:
        assert getattr(note_fixture, attr) == saved_file[attr]
Exemple #4
0
def test_bookmark_sanitization(test_app, client, mocked_responses,
                               bookmark_fixture):
    """Test that bookmark content is correctly saved as correct Markdown"""

    with test_app.app_context():
        assert bookmark_fixture.id == get_max_id()

    saved_file = frontmatter.load(bookmark_fixture.fullpath)
    for attr in attributes:
        assert getattr(bookmark_fixture, attr) == saved_file[attr]
    assert bookmark_fixture.url == saved_file["url"] 

    # remove buggy newlines that interfere with checks:
    bookmark_fixture.content = bookmark_fixture.content.replace("\n", "")
    # test script is sanitized 
    assert bookmark_fixture.content.find("<script>") == -1
    # test relative urls in the HTML are remapped to an absolute urls
    assert "example.com/images/image1.png" in bookmark_fixture.content
    assert "example.com/testing-absolute-url" in bookmark_fixture.content

    # check empty link has been cleared
    assert "[](empty-link)" not in bookmark_fixture.content