This repository has been archived by the owner on Jan 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
blog_post.py
199 lines (141 loc) · 6.11 KB
/
blog_post.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
__author__ = 'Flavio'
from django.db import models
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from pelican.utils import slugify
from pelican_admin.models.settings import Settings
import os, datetime, codecs
MARKUPS = (
(0, 'rst'),
(1, 'md'),
)
STATUS = (
('published', _('Published')),
('draft', _('Draft')),
('hidden', _('Hidden')),
)
def _pelican_setting(name):
try:
return Settings.objects.get(name=name).value
except Settings.DoesNotExist:
return None
class BlogPost(models.Model):
title = models.CharField(_('Title'), max_length=255)
markup = models.SmallIntegerField(_('Markup'), choices=MARKUPS, default=0)
category = models.ForeignKey('Category', null=True, blank=True)
# Metadatas
text = models.TextField(_('Text'))
date = models.DateTimeField(_('Date'), default=datetime.datetime.now)
tags = models.CharField(_('Tags'), max_length=255, null=True, blank=True)
slug = models.CharField(_('Slug'), max_length=255, null=True, blank=True)
author = models.CharField(_('Author'), max_length=255, default=_pelican_setting('AUTHOR'))
summary = models.CharField(_('Summary'), max_length=511, null=True, blank=True)
lang = models.CharField(_('Language'), max_length=5, default=_pelican_setting('DEFAULT_LANG'))
status = models.CharField(_('Status'), choices=STATUS, default='published', max_length=127)
file_path = models.CharField(_('File Path'), max_length=255, unique=True, primary_key=True)
def save(self, **kwargs):
self.write()
super(BlogPost, self).save(**kwargs)
def write(self):
if not self.file_path:
filename ='%s.%s' % (self.get_slug(), MARKUPS[self.markup][1])
self.file_path = os.path.join(settings.PELICAN_PATH, 'content', filename)
f = codecs.open(self.file_path, 'w', encoding="utf-8")
meta_format = '%s: %s\n'
title_meta = meta_format % (self.metafy('title'), self.title)
f.write(title_meta)
date_meta = meta_format % (self.metafy('date'), self.date.strftime('%Y-%m-%d %H:%M'))
f.write(date_meta)
author_meta = meta_format % (self.metafy('author'), self.author)
f.write(author_meta)
lang_meta = meta_format % (self.metafy('lang'), self.lang)
f.write(lang_meta)
status_meta = meta_format % (self.metafy('status'), self.status)
f.write(status_meta)
if self.tags:
tags_meta = meta_format % (self.metafy('tags'), self.tags)
f.write(tags_meta)
if self.category:
category_meta = meta_format % (self.metafy('category'), self.category)
f.write(category_meta)
if self.slug:
slug_meta = meta_format % (self.metafy('slug'), self.slug)
f.write(slug_meta)
if self.summary:
summary_meta = meta_format % (self.metafy('summary'), self.summary)
f.write(summary_meta)
f.write(self.text)
f.close()
def delete(self, using=None):
os.remove(self.file_path)
super(BlogPost, self).delete(using)
def metafy(self, meta):
return ':' + meta.lower() if self.markup == 0 else meta.title()
def get_slug(self):
if not self.slug:
self.slug = slugify(self.title)
return self.slug
@classmethod
def get_from_meta(cls, markup, title=None, slug=None):
content_path = os.path.join(settings.PELICAN_PATH, 'content')
if isinstance(markup, int):
markup = MARKUPS[markup][1]
if slug:
file_path = os.path.join(content_path, slug+'.'+markup)
try:
result = BlogPost.objects.get(file_path=file_path)
return result
except BlogPost.DoesNotExist:
pass
if title:
file_path = os.path.join(content_path, slugify(title)+'.'+markup)
try:
result = BlogPost.objects.get(file_path=file_path)
return result
except BlogPost.DoesNotExist:
pass
raise BlogPost.DoesNotExist()
@classmethod
def load_posts(cls):
BlogPost.objects.all().delete()
post_model_list = []
content_path = os.path.join(settings.PELICAN_PATH, 'content')
for file in os.listdir(content_path):
post = BlogPost()
post.file_path = os.path.join(content_path, file)
if file.endswith('.rst'):
post.markup = 0
elif file.endswith('.md'):
post.markup = 1
f = open(post.file_path, 'r+')
for line in f.readlines():
parse = lambda meta: line.split(post.metafy(meta)+':')[1].strip()
if line.startswith(post.metafy('title')):
post.title = parse('title')
elif line.startswith(post.metafy('date')):
date_str = parse('date')
post.date = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M')
elif line.startswith(post.metafy('author')):
post.author = parse('author')
elif line.startswith(post.metafy('lang')):
post.lang = parse('lang')
elif line.startswith(post.metafy('status')):
post.status = parse('status')
elif line.startswith(post.metafy('tags')):
post.tags = parse('tags')
elif line.startswith(post.metafy('category')):
post.category = parse('category')
elif line.startswith(post.metafy('slug')):
post.slug = parse('slug')
elif line.startswith(post.metafy('summary')):
post.summary = parse('summary')
else:
post.text += line
post_model_list.append(post)
BlogPost.objects.bulk_create(post_model_list)
def __unicode__(self):
return unicode(self.title)
class Meta:
app_label = 'pelican_admin'
verbose_name = _('Blog Post')
verbose_name_plural = _('Blog Posts')