annotate piecrust/admin/views/sources.py @ 1107:a1c6050c9801

admin: Fix possible crash in the edit page.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 18 Feb 2018 20:31:16 -0800
parents 4c69935ca415
children 0d699f04968c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
959
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
1 import re
587
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 from flask import g, abort, render_template, url_for
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 from flask.ext.login import login_required
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 from piecrust.data.paginator import Paginator
772
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 657
diff changeset
5 from ..blueprint import foodtruck_bp
587
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 from ..views import with_menu_context
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8
772
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 657
diff changeset
9 @foodtruck_bp.route('/list/<source_name>/', defaults={'page_num': 1})
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 657
diff changeset
10 @foodtruck_bp.route('/list/<source_name>/<int:page_num>')
587
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 @login_required
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 def list_source(source_name, page_num):
610
efc1dc916e7c admin: Configuration changes.
Ludovic Chabant <ludovic@chabant.com>
parents: 593
diff changeset
13 site = g.site.piecrust_app
587
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 source = site.getSource(source_name)
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 if source is None:
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 abort(400)
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 i = 0
657
c1a94e1beb9d admin: Show a more classic blog post listing in FoodTruck.
Ludovic Chabant <ludovic@chabant.com>
parents: 610
diff changeset
19 default_author = site.config.get('site/author')
587
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 data = {'title': "List %s" % source_name}
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 data['pages'] = []
886
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 812
diff changeset
22 pgn = Paginator(source, None, sub_num=page_num, items_per_page=20)
587
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 for p in pgn.items:
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 page_data = {
959
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
25 'title': p.get('title') or _get_first_line_title(p),
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
26 'author': p.get('author') or default_author,
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
27 'timestamp': p.get('timestamp'),
812
82509bce94ca internal: PEP8 fixup for admin panel code.
Ludovic Chabant <ludovic@chabant.com>
parents: 778
diff changeset
28 'tags': p.get('tags', []),
82509bce94ca internal: PEP8 fixup for admin panel code.
Ludovic Chabant <ludovic@chabant.com>
parents: 778
diff changeset
29 'category': p.get('category'),
82509bce94ca internal: PEP8 fixup for admin panel code.
Ludovic Chabant <ludovic@chabant.com>
parents: 778
diff changeset
30 'source': source_name,
935
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 886
diff changeset
31 'url': url_for('.edit_page', url=p['rel_url'])
812
82509bce94ca internal: PEP8 fixup for admin panel code.
Ludovic Chabant <ludovic@chabant.com>
parents: 778
diff changeset
32 }
587
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 data['pages'].append(page_data)
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 prev_page_url = None
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 if pgn.prev_page_number:
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 prev_page_url = url_for(
812
82509bce94ca internal: PEP8 fixup for admin panel code.
Ludovic Chabant <ludovic@chabant.com>
parents: 778
diff changeset
38 '.list_source', source_name=source_name,
82509bce94ca internal: PEP8 fixup for admin panel code.
Ludovic Chabant <ludovic@chabant.com>
parents: 778
diff changeset
39 page_num=pgn.prev_page_number)
587
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 next_page_url = None
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 if pgn.next_page_number:
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 next_page_url = url_for(
812
82509bce94ca internal: PEP8 fixup for admin panel code.
Ludovic Chabant <ludovic@chabant.com>
parents: 778
diff changeset
43 '.list_source', source_name=source_name,
82509bce94ca internal: PEP8 fixup for admin panel code.
Ludovic Chabant <ludovic@chabant.com>
parents: 778
diff changeset
44 page_num=pgn.next_page_number)
587
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 page_urls = []
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 for i in pgn.all_page_numbers(7):
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 url = None
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 if i != page_num:
772
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 657
diff changeset
50 url = url_for('.list_source', source_name=source_name, page_num=i)
587
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 page_urls.append({'num': i, 'url': url})
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 data['pagination'] = {
812
82509bce94ca internal: PEP8 fixup for admin panel code.
Ludovic Chabant <ludovic@chabant.com>
parents: 778
diff changeset
54 'prev_page': prev_page_url,
82509bce94ca internal: PEP8 fixup for admin panel code.
Ludovic Chabant <ludovic@chabant.com>
parents: 778
diff changeset
55 'next_page': next_page_url,
82509bce94ca internal: PEP8 fixup for admin panel code.
Ludovic Chabant <ludovic@chabant.com>
parents: 778
diff changeset
56 'nums': page_urls
82509bce94ca internal: PEP8 fixup for admin panel code.
Ludovic Chabant <ludovic@chabant.com>
parents: 778
diff changeset
57 }
587
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 with_menu_context(data)
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 return render_template('list_source.html', **data)
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61
959
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
62
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
63 re_first_line_title = re.compile(r'[\n\r\.\!\?;]')
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
64
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
65
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
66 def _get_first_line_title(pagedata):
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
67 content = pagedata.get('raw_content') or ''
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
68 content = content.content.strip()
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
69 if not content:
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
70 return '<empty page>'
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
71
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
72 m = re_first_line_title.search(content, 1)
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
73 if m:
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
74 content = content[:m.start()]
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
75
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
76 words = content.split(' ')
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
77 title = words[0]
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
78 cur_word_idx = 1
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
79 while len(title) < 60 and cur_word_idx < len(words):
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
80 title += ' ' + words[cur_word_idx]
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
81 cur_word_idx += 1
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
82
4c69935ca415 admin: When there's no post title, make a title from the first few words.
Ludovic Chabant <ludovic@chabant.com>
parents: 935
diff changeset
83 return content