Mercurial > piecrust2
comparison piecrust/admin/views/micropub.py @ 964:d65838abbd90
admin: Better micropub request handling.
- Use YAML to write the new post's configuration header.
- Add support for automatic microblogging post configs.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 07 Oct 2017 12:31:48 -0700 |
parents | accfe8fc8440 |
children | 5b735229b6fb |
comparison
equal
deleted
inserted
replaced
963:a90541509a41 | 964:d65838abbd90 |
---|---|
1 import re | 1 import re |
2 import os | 2 import os |
3 import os.path | 3 import os.path |
4 import logging | 4 import logging |
5 import datetime | 5 import datetime |
6 import yaml | |
6 from werkzeug.utils import secure_filename | 7 from werkzeug.utils import secure_filename |
7 from flask import g, request, abort, Response | 8 from flask import g, request, abort, Response |
8 from flask_indieauth import requires_indieauth | 9 from flask_indieauth import requires_indieauth |
9 from ..blueprint import foodtruck_bp | 10 from ..blueprint import foodtruck_bp |
11 from piecrust.configuration import merge_dicts | |
10 from piecrust.page import Page | 12 from piecrust.page import Page |
11 | 13 |
12 | 14 |
13 logger = logging.getLogger(__name__) | 15 logger = logging.getLogger(__name__) |
14 | 16 |
30 abort(400) | 32 abort(400) |
31 | 33 |
32 | 34 |
33 def _run_publisher(): | 35 def _run_publisher(): |
34 pcapp = g.site.piecrust_app | 36 pcapp = g.site.piecrust_app |
35 target = pcapp.config.get('micropub/publish_target', 'default') | 37 target = pcapp.config.get('micropub/publish_target') |
36 logger.debug("Running pushing target '%s'." % target) | 38 if target: |
37 g.site.publish(target) | 39 logger.debug("Running pushing target '%s'." % target) |
40 g.site.publish(target) | |
38 | 41 |
39 | 42 |
40 def _get_location_response(uri): | 43 def _get_location_response(uri): |
41 logger.debug("Redirecting to: %s" % uri) | 44 logger.debug("Redirecting to: %s" % uri) |
42 r = Response() | 45 r = Response() |
45 return r | 48 return r |
46 | 49 |
47 | 50 |
48 def _create_hentry(): | 51 def _create_hentry(): |
49 f = request.form | 52 f = request.form |
53 pcapp = g.site.piecrust_app | |
54 | |
50 summary = f.get('summary') | 55 summary = f.get('summary') |
51 categories = f.getlist('category[]') | 56 categories = f.getlist('category[]') |
52 location = f.get('location') | 57 location = f.get('location') |
53 reply_to = f.get('in-reply-to') | 58 reply_to = f.get('in-reply-to') |
54 status = f.get('post-status') | 59 status = f.get('post-status') |
135 photo.save(photo_path) | 140 photo.save(photo_path) |
136 | 141 |
137 fn_no_ext, _ = os.path.splitext(fn) | 142 fn_no_ext, _ = os.path.splitext(fn) |
138 photo_names.append(fn_no_ext) | 143 photo_names.append(fn_no_ext) |
139 | 144 |
145 # Build the config. | |
146 post_config = {} | |
147 if name: | |
148 post_config['title'] = name | |
149 if categories: | |
150 post_config['tags'] = categories | |
151 if location: | |
152 post_config['location'] = location | |
153 if reply_to: | |
154 post_config['reply_to'] = reply_to | |
155 if status: | |
156 post_config['status'] = status | |
157 if post_format: | |
158 post_config['format'] = post_format | |
159 post_config['time'] = '%02d:%02d:%02d' % (now.hour, now.minute, now.second) | |
160 | |
161 # If there's no title, this is a "microblogging" post. | |
162 if not name: | |
163 micro_config = pcapp.config.get('micropub/microblogging') | |
164 if micro_config: | |
165 merge_dicts(post_config, micro_config) | |
166 | |
140 logger.debug("Writing to item: %s" % content_item.spec) | 167 logger.debug("Writing to item: %s" % content_item.spec) |
141 with source.openItem(content_item, mode='w') as fp: | 168 with source.openItem(content_item, mode='w') as fp: |
142 fp.write('---\n') | 169 fp.write('---\n') |
143 if name: | 170 yaml.dump(post_config, fp, |
144 fp.write('title: "%s"\n' % name) | 171 default_flow_style=False, |
145 if categories: | 172 allow_unicode=True) |
146 fp.write('tags: [%s]\n' % ','.join(categories)) | |
147 if location: | |
148 fp.write('location: %s\n' % location) | |
149 if reply_to: | |
150 fp.write('reply_to: "%s"\n' % reply_to) | |
151 if status: | |
152 fp.write('status: %s\n' % status) | |
153 if post_format: | |
154 fp.write('format: %s\n' % post_format) | |
155 fp.write('time: %02d:%02d:%02d\n' % (now.hour, now.minute, now.second)) | |
156 fp.write('---\n') | 173 fp.write('---\n') |
157 | 174 |
158 if summary: | 175 if summary: |
159 fp.write(summary) | 176 fp.write(summary) |
160 fp.write('\n') | 177 fp.write('\n') |
170 fp.write('\n\n') | 187 fp.write('\n\n') |
171 for pn in photo_names: | 188 for pn in photo_names: |
172 fp.write('<img src="{{assets.%s}}" alt="%s"/>\n\n' % | 189 fp.write('<img src="{{assets.%s}}" alt="%s"/>\n\n' % |
173 (pn, pn)) | 190 (pn, pn)) |
174 | 191 |
175 route = pcapp.getSourceRoute(source.name) | |
176 if route is None: | |
177 logger.error("Can't find route for source: %s" % source.name) | |
178 abort(500) | |
179 | |
180 page = Page(source, content_item) | 192 page = Page(source, content_item) |
181 uri = page.getUri() | 193 uri = page.getUri() |
182 return uri | 194 return uri |
183 | 195 |