annotate piecrust/commands/builtin/admin.py @ 1114:8af2ea1f5c34

tasks: Add new `tasks` command and infrastructure, with `mention` task. * The new command lets `chef` run tasks from a queue. * The webmention endpoint now adds a mention task. * Moved mention handling code to a task runner.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 22 Feb 2018 22:12:45 -0800
parents 33a89139c284
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import os
656
dba53f0f7671 admin: run an asset processing loop in the background.
Ludovic Chabant <ludovic@chabant.com>
parents: 653
diff changeset
2 import os.path
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import logging
917
33a89139c284 serve: Add `--admin` option to run the administration panel.
Ludovic Chabant <ludovic@chabant.com>
parents: 886
diff changeset
4 from piecrust import CONFIG_PATH
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 from piecrust.commands.base import ChefCommand
842
a85d08ffe1f6 admin: Fix crash when running `admin run` outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents: 778
diff changeset
6 from piecrust.pathutil import SiteNotFoundError
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 logger = logging.getLogger(__name__)
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 class AdministrationPanelCommand(ChefCommand):
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 def __init__(self):
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 super(AdministrationPanelCommand, self).__init__()
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 self.name = 'admin'
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 self.description = "Manages the PieCrust administration panel."
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 self.requires_website = False
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 def setupParser(self, parser, app):
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 subparsers = parser.add_subparsers()
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 p = subparsers.add_parser(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
23 'init',
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
24 help="Creates a new administration panel website.")
886
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
25 p.set_defaults(sub_func=self._initAdminSite)
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 def checkedRun(self, ctx):
762
c84647485ab2 admin: Fix crash when running the `admin` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 656
diff changeset
28 if ctx.app.root_dir is None:
c84647485ab2 admin: Fix crash when running the `admin` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 656
diff changeset
29 raise SiteNotFoundError(theme=ctx.app.theme_site)
c84647485ab2 admin: Fix crash when running the `admin` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 656
diff changeset
30
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 if not hasattr(ctx.args, 'sub_func'):
762
c84647485ab2 admin: Fix crash when running the `admin` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 656
diff changeset
32 ctx.parser.parse_args(['admin', '--help'])
c84647485ab2 admin: Fix crash when running the `admin` command.
Ludovic Chabant <ludovic@chabant.com>
parents: 656
diff changeset
33 return
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 return ctx.args.sub_func(ctx)
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35
886
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
36 def _initAdminSite(self, ctx):
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
37 import io
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 import getpass
778
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents: 762
diff changeset
39 from piecrust.admin import bcryptfallback as bcrypt
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 secret_key = os.urandom(22)
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 admin_username = input("Admin username (admin): ") or 'admin'
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 admin_password = getpass.getpass("Admin password: ")
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 if not admin_password:
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 logger.warning("No administration password set!")
886
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
46 logger.warning("Don't make this instance of the PieCrust "
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
47 "administration panel public.")
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 logger.info("You can later set an admin password by editing "
886
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
49 "the `admin.cfg` file and using the "
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 "`chef admin genpass` command.")
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 else:
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 binpw = admin_password.encode('utf8')
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 hashpw = bcrypt.hashpw(binpw, bcrypt.gensalt()).decode('utf8')
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 admin_password = hashpw
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 ft_config = """
886
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
57 admin:
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
58 secret_key: %(secret_key)s
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 username: %(username)s
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 # You can generate another hashed password with `chef admin genpass`.
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 password: %(password)s
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 """
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 ft_config = ft_config % {
886
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
64 'secret_key': secret_key,
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
65 'username': admin_username,
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
66 'password': admin_password
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 842
diff changeset
67 }
886
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
68
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
69 config_path = os.path.join(ctx.app.root_dir, CONFIG_PATH)
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
70 with open(config_path, 'a+', encoding='utf8') as fp:
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
71 fp.seek(0, io.SEEK_END)
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
72 fp.write('\n')
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 fp.write(ft_config)
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 def _generatePassword(self, ctx):
778
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents: 762
diff changeset
76 from piecrust.admin import bcryptfallback as bcrypt
588
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 binpw = ctx.args.password.encode('utf8')
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 hashpw = bcrypt.hashpw(binpw, bcrypt.gensalt()).decode('utf8')
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 logger.info(hashpw)
b884bef3e611 admin: New `admin` command to manage FoodTruck-related things.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80