add file for schema and continue models.py
This commit is contained in:
parent
f001692714
commit
21611df9b0
|
@ -39,17 +39,33 @@ def json_handler(obj):
|
|||
raise TypeError('Object of type %s with value of %s is not JSON serializable' % (type(obj), repr(obj)))
|
||||
|
||||
|
||||
def split(file_path):
|
||||
return os.path.normpath(file_path).split(os.sep)
|
||||
|
||||
|
||||
class Article(object):
|
||||
|
||||
def __init__(self, path, content, publication_date=None):
|
||||
self.path = path
|
||||
self.content = content
|
||||
|
||||
split_path = os.path.normpath(path).split(os.sep)
|
||||
split_path = split(path)
|
||||
|
||||
self.filename = split_path[-1:] # the last element
|
||||
self.filename = split_path[-1] # the last element
|
||||
self.filename_without_extension = os.path.splitext(self.filename)[0]
|
||||
self.publication_date = get_date_from_article_filename(self.filename)
|
||||
self.slug = slugify(self.filename)
|
||||
|
||||
self.slug = self.filename_without_extension
|
||||
|
||||
if self.publication_date:
|
||||
self.slug = self.slug.replace(
|
||||
self.publication_date.strftime('%Y-%m-%d'), ''
|
||||
)
|
||||
|
||||
if self.slug and self.slug.startswith('-'):
|
||||
self.slug = self.slug[1:]
|
||||
|
||||
self.slug = slugify(self.slug)
|
||||
|
||||
try:
|
||||
self.category = split_path[1] #
|
||||
|
@ -79,13 +95,13 @@ def get_date_from_article_filename(article_filename):
|
|||
2017-02-03-divinity-origin-sin.md -> None
|
||||
lynis.md -> None
|
||||
"""
|
||||
values = article_filename.split('-')
|
||||
values = article_filename[0:10]
|
||||
|
||||
date = None
|
||||
try:
|
||||
date = datetime.date(int(values[0]), int(values[1]), int(values[2]))
|
||||
date = datetime.datetime.strptime(values, '%Y-%m-%d').date()
|
||||
except (ValueError, TypeError):
|
||||
logger.warn('Ignoring file - no publication date found %s', article_filename)
|
||||
logger.warn('No publication date found %s', article_filename)
|
||||
|
||||
return date
|
||||
|
||||
|
@ -105,7 +121,10 @@ class Site(object):
|
|||
|
||||
for root, *_, files in os.walk(root_path):
|
||||
for file in [file for file in files if file.endswith(".md")]:
|
||||
self.build_article(root, file)
|
||||
try:
|
||||
self.build_article(root, file)
|
||||
except UnicodeDecodeError:
|
||||
continue
|
||||
|
||||
def build_article(self, filepath, filename):
|
||||
"""Build a new article from an existing file.
|
||||
|
@ -120,32 +139,36 @@ class Site(object):
|
|||
None if no publication date can be built.
|
||||
An `grnx.models.Article` instance instead.
|
||||
"""
|
||||
publication_date = get_date_from_article_filename(filename)
|
||||
if not publication_date:
|
||||
return
|
||||
|
||||
article_file_path = os.path.join(filepath, filename)
|
||||
|
||||
article = None
|
||||
with open(os.path.join(filepath, filename), encoding="utf8") as f:
|
||||
with open(article_file_path, encoding="utf8") as f:
|
||||
content = f.read()
|
||||
|
||||
article = Article(filepath, content, publication_date)
|
||||
article = Article(article_file_path, content)
|
||||
|
||||
logger.warn('article found in %s: %s', article.category, article)
|
||||
if article:
|
||||
logger.warn('article found in %s: %s', article.category, article)
|
||||
|
||||
self.articles.append(article)
|
||||
self.articles.append(article)
|
||||
|
||||
if article.category not in self.categories:
|
||||
self.categories[article.category] = []
|
||||
if article.category not in self.categories:
|
||||
self.categories[article.category] = []
|
||||
|
||||
self.categories[article.category].append(article)
|
||||
self.categories[article.category].append(article)
|
||||
|
||||
return article
|
||||
|
||||
def to_json(self):
|
||||
"""Serialize the content of the current structure to JSON format."""
|
||||
|
||||
json_dumps = json.dumps(self, default=json_handler, sort_keys=True, indent=4)
|
||||
print('json result: ' + json_dumps)
|
||||
json_dumps = json.dumps(
|
||||
self,
|
||||
default=json_handler,
|
||||
sort_keys=True,
|
||||
indent=4
|
||||
)
|
||||
return json_dumps
|
||||
|
||||
def serialize(self):
|
||||
|
|
1126
index.json
1126
index.json
File diff suppressed because one or more lines are too long
|
@ -4,4 +4,5 @@ clize==4.0.3
|
|||
pytest==3.7.2
|
||||
pytest-cov==2.5.1
|
||||
jinja==2.10
|
||||
python-slugify==1.2.5
|
||||
python-slugify==1.2.5
|
||||
markdown==2.6.11
|
|
@ -2,16 +2,26 @@
|
|||
|
||||
from datetime import date
|
||||
|
||||
from grnx.models import get_date_from_article_filename
|
||||
from grnx.models import get_date_from_article_filename, split
|
||||
from grnx.models import Article
|
||||
|
||||
|
||||
def test_split_path():
|
||||
assert split('articles/sys/2018-01-01 Scaleway Review.md') == ['articles', 'sys', '2018-01-01 Scaleway Review.md']
|
||||
|
||||
|
||||
def test_filename():
|
||||
article = Article("2019-01-01-blabla.md", "")
|
||||
assert "2019-01-01-blabla" == article.filename_without_extension
|
||||
assert "2019-01-01-blabla.md" == article.filename
|
||||
|
||||
def test_article_match_date():
|
||||
"""Checks that a date can be extracted from a filename."""
|
||||
|
||||
assert get_date_from_article_filename('2018-09-01-test.md') == date(2018, 9, 1)
|
||||
assert get_date_from_article_filename('2017-02-30-divinity-origin-sin.md') == None
|
||||
assert get_date_from_article_filename('lynis.md') == None
|
||||
assert get_date_from_article_filename('2018-01-01 scaleway-review.md') == date(2018, 1, 1)
|
||||
|
||||
|
||||
def test_article_slug():
|
||||
|
@ -42,3 +52,12 @@ def test_article_keywords():
|
|||
article = Article('articles/dev/2019-01-01-blabla.md', "")
|
||||
|
||||
assert len(article.keywords) == 0
|
||||
|
||||
|
||||
def test_article_properties():
|
||||
article = Article('articles/dev/python/django/2019-01-01-blabla.md', "")
|
||||
|
||||
assert article.filename == '2019-01-01-blabla.md'
|
||||
assert article.filename_without_extension == '2019-01-01-blabla'
|
||||
assert article.slug == 'blabla'
|
||||
assert article.publication_date == date(2019, 1, 1)
|
Loading…
Reference in New Issue