-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask_picocms.py
227 lines (177 loc) · 6.15 KB
/
flask_picocms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"""
flask_picocms
==================
This module provides a lightweight CMS backend for Flask apps.
:copyright: (C) 2017 by John K.E. Wilson.
:license: MIT, see LICENSE for more details.
"""
__version__ = "0.0.6"
import os
import shutil
import toml
import json
from datetime import datetime
from flask import current_app
from peewee import *
# Find the stack on which we want to store the database connection.
# Starting with Flask 0.9, the _app_ctx_stack is the correct one,
# before that we need to use the _request_ctx_stack.
try:
from flask import _app_ctx_stack as stack
except ImportError:
from flask import _request_ctx_stack as stack
database = SqliteDatabase(None)
class BaseModel(Model):
class Meta:
database = database
class Page(BaseModel):
"""Model for CMS page contents: converted from TOML to JSON"""
title = CharField(default="")
directory = CharField(default="/")
name = CharField(null=False)
date = DateTimeField(default=datetime.utcnow)
draft = BooleanField(default=True)
content = TextField(default="")
class Meta:
indexes = (
(("directory","name"), True),
)
class Document(BaseModel):
"""Model for CMS JSON documents"""
directory = CharField(default="/")
name = CharField(null=False)
content = TextField()
class Meta:
indexes = (
(("directory","name"), True),
)
class RepositoryObject(object):
def __init__(self, result):
self.content = result.content
self.meta = {}
self.meta["directory"] = result.directory
self.meta["name"] = result.name
if type(result) is Page:
self.meta["title"] = result.title
self.meta["date"] = result.date
self.meta["draft"] = result.draft
@property
def json(self):
return json.loads(self.content)
class CMS(object):
"""Lytpages CMS"""
def __init__(self, app=None):
self.config = {}
if app is not None:
self.init_app(app)
def init_app(self, app):
self.config = {
"db": app.config["PICOCMS_DATABASE"],
"data": app.config["PICOCMS_SOURCE_DATA"],
"pages": app.config["PICOCMS_SOURCE_PAGES"]
}
# Use the newstyle teardown_appcontext if it's available,
# otherwise fall back to the request context
if hasattr(app, "teardown_appcontext"):
app.teardown_appcontext(self.teardown)
else:
app.teardown_request(self.teardown)
database.init(self.config["db"])
database.connect()
def teardown(self, exception):
try:
database.close()
except Exception as e:
pass
def rebuild(self):
self.clear()
self.parse()
def clear(self):
database.drop_tables([Page, Document], safe=True)
database.create_tables([Page, Document])
def parse(self):
self.__add_content()
self.__add_data()
def get_content(self, path):
directory = os.path.dirname(path)
name = os.path.basename(path)
try:
page = Page.get(
Page.directory == directory,
Page.name == name
)
except Page.DoesNotExist:
raise ValueError("Invalid Content path: %s" % path)
return RepositoryObject(page)
@property
def content_count(self):
return Page.select().count()
def list_content(self, directory, date="asc"):
res = []
if date == "desc":
orderby = Page.date.desc()
else:
orderby = Page.date.asc()
for item in Page.select().where(Page.directory == directory).order_by(orderby):
medata = {
"title": item.title,
"directory": item.directory,
"name": item.name,
"date": item.date,
"draft": item.draft
}
res.append(medata)
return res
def get_data(self, path):
directory = os.path.dirname(path)
name = os.path.basename(path)
try:
doc = Document.get(
Document.directory == directory,
Document.name == name
)
except Document.DoesNotExist:
raise ValueError("Invalid Data path: %s" % path)
return RepositoryObject(doc)
@property
def data_count(self):
return Document.select().count()
def __add_content(self):
_dir = self.config["pages"]
for root, dirs, files in os.walk(_dir):
for item in files:
name, directory, data, data_str = self.__get_file_data(_dir, item, root)
# create repository entry
pg = Page(content=data_str, directory=directory, name=name)
if "title" in data:
pg.title = data["title"]
if "draft" in data:
pg.draft = data["draft"]
if "date" in data:
pg.date = data["date"]
pg.save()
def __add_data(self):
_dir = self.config["data"]
for root, dirs, files in os.walk(_dir):
for item in files:
name, directory, data, data_str = self.__get_file_data(_dir, item, root)
# create repository entry
doc = Document(content=data_str, directory=directory, name=name)
doc.save()
def __get_file_data(self, srcdir, fname, fdir):
abspath = os.path.join(fdir, fname)
# create repository indexing fields
name, ext = os.path.splitext(fname)
directory = fdir.replace(srcdir, "")
if not directory:
directory = "/"
# get file contents and parse
with open(abspath, "r") as fp:
fraw = fp.read()
if ext == ".toml":
data = toml.loads(fraw)
data_str = json.dumps(data)
elif ext == ".json":
data = json.loads(fraw)
data_str = fraw
return name, directory, data, data_str