Serialize (properly) categories with slugs and names.
This commit is contained in:
parent
0f07b37d2d
commit
b92659f98b
|
@ -6,6 +6,10 @@ import re
|
|||
import os
|
||||
import logging
|
||||
|
||||
from slugify import slugify
|
||||
|
||||
from .schema import CategorySchema
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
@ -55,9 +59,17 @@ def split(file_path):
|
|||
return os.path.normpath(file_path).split(os.sep)
|
||||
|
||||
|
||||
class Category():
|
||||
class NamedSlug():
|
||||
"""NamedSlug are mixins with a name and an auto-generated slug."""
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.slug = slugify(self.name)
|
||||
|
||||
|
||||
class Category(NamedSlug):
|
||||
"""Categories are named, have slug and an empty list of articles."""
|
||||
def __init__(self, name):
|
||||
super().__init__(name)
|
||||
self.articles = []
|
||||
|
||||
def append(self, article):
|
||||
|
@ -202,4 +214,6 @@ class Site(object):
|
|||
json_serialized_file.write(to_json({"data": self}, indent))
|
||||
|
||||
with open(os.path.join(self.output_path, "categories.json"), "w") as json_serialized_file:
|
||||
json_serialized_file.write(to_json({"data": self.categories.values()}, indent))
|
||||
schema = CategorySchema(many=True)
|
||||
result = schema.dump(self.categories.values())
|
||||
json_serialized_file.write(to_json({"data": result}, indent))
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
"""Serialization module for models objects."""
|
||||
|
||||
|
||||
from marshmallow import Schema, fields
|
||||
|
||||
|
||||
class ArticleCategorySchema(Schema):
|
||||
title = fields.Str()
|
||||
slug = fields.Str()
|
||||
|
||||
|
||||
class CategorySchema(Schema):
|
||||
name = fields.Str()
|
||||
slug = fields.Str()
|
||||
articles = fields.Nested(ArticleCategorySchema)
|
|
@ -5,4 +5,5 @@ pytest==3.7.2
|
|||
pytest-cov==2.5.1
|
||||
jinja==2.10
|
||||
python-slugify==1.2.5
|
||||
markdown==2.6.11
|
||||
markdown==2.6.11marshmallow==3.6.1
|
||||
python-slugify==4.0.0
|
||||
|
|
|
@ -1,33 +1,50 @@
|
|||
<html>
|
||||
|
||||
<head>
|
||||
<title>Grnx</title>
|
||||
<meta charset="UTF-8">
|
||||
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
|
||||
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
|
||||
|
||||
<!-- UIkit CSS -->
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/uikit@3.5.4/dist/css/uikit.min.css" />
|
||||
|
||||
<!-- UIkit JS -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/uikit@3.5.4/dist/js/uikit.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/uikit@3.5.4/dist/js/uikit-icons.min.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="app">
|
||||
<h1>Grnx</h1>
|
||||
<ul>
|
||||
<li v-for="(category, article_slugs) in categories.data">
|
||||
{{ category }}
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<script>
|
||||
const app = new Vue({
|
||||
|
||||
<nav class="uk-navbar-container" uk-navbar>
|
||||
<div class="uk-navbar-left">Grnx</div>
|
||||
|
||||
<div class="uk-navbar-right">
|
||||
|
||||
<ul class="uk-navbar-nav">
|
||||
<li class="uk-active" v-for="(category, article_slugs) in categories.data">
|
||||
<a href="{{ category.slug }}">{{ category.name }}</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<script>
|
||||
const app = new Vue({
|
||||
el: '#app',
|
||||
data () {
|
||||
return {
|
||||
categories: [],
|
||||
}
|
||||
data() {
|
||||
return {
|
||||
categories: [],
|
||||
}
|
||||
},
|
||||
mounted () {
|
||||
axios
|
||||
mounted() {
|
||||
axios
|
||||
.get('categories.json')
|
||||
.then(response => (this.categories = response.data));
|
||||
}
|
||||
})
|
||||
</script>
|
||||
})
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in New Issue