Fix print statements.
[mediagoblin.git] / mediagoblin / gmg_commands / users.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 from __future__ import print_function
18
19 import six
20
21 from mediagoblin.gmg_commands import util as commands_util
22 from mediagoblin import auth
23 from mediagoblin import mg_globals
24
25 def adduser_parser_setup(subparser):
26 subparser.add_argument(
27 '--username','-u',
28 help="Username used to login")
29 subparser.add_argument(
30 '--password','-p',
31 help="Your supersecret word to login, beware of storing it in bash history")
32 subparser.add_argument(
33 '--email','-e',
34 help="Email to receive notifications")
35
36
37 def adduser(args):
38 #TODO: Lets trust admins this do not validate Emails :)
39 commands_util.setup_app(args)
40
41 args.username = commands_util.prompt_if_not_set(args.username, "Username:")
42 args.password = commands_util.prompt_if_not_set(args.password, "Password:",True)
43 args.email = commands_util.prompt_if_not_set(args.email, "Email:")
44
45 db = mg_globals.database
46 users_with_username = \
47 db.User.query.filter_by(
48 username=args.username.lower()
49 ).count()
50
51 if users_with_username:
52 print(u'Sorry, a user with that name already exists.')
53
54 else:
55 # Create the user
56 entry = db.User()
57 entry.username = six.text_type(args.username.lower())
58 entry.email = six.text_type(args.email)
59 entry.pw_hash = auth.gen_password_hash(args.password)
60 default_privileges = [
61 db.Privilege.query.filter(
62 db.Privilege.privilege_name==u'commenter').one(),
63 db.Privilege.query.filter(
64 db.Privilege.privilege_name==u'uploader').one(),
65 db.Privilege.query.filter(
66 db.Privilege.privilege_name==u'reporter').one(),
67 db.Privilege.query.filter(
68 db.Privilege.privilege_name==u'active').one()
69 ]
70 entry.all_privileges = default_privileges
71 entry.save()
72
73 print(u"User created (and email marked as verified)")
74
75
76 def makeadmin_parser_setup(subparser):
77 subparser.add_argument(
78 'username',
79 help="Username to give admin level")
80
81
82 def makeadmin(args):
83 commands_util.setup_app(args)
84
85 db = mg_globals.database
86
87 user = db.User.query.filter_by(
88 username=six.text_type(args.username.lower())).one()
89 if user:
90 user.all_privileges.append(
91 db.Privilege.query.filter(
92 db.Privilege.privilege_name==u'admin').one()
93 )
94 user.save()
95 print(u'The user is now Admin')
96 else:
97 print(u'The user doesn\'t exist')
98
99
100 def changepw_parser_setup(subparser):
101 subparser.add_argument(
102 'username',
103 help="Username used to login")
104 subparser.add_argument(
105 'password',
106 help="Your NEW supersecret word to login")
107
108
109 def changepw(args):
110 commands_util.setup_app(args)
111
112 db = mg_globals.database
113
114 user = db.User.query.filter_by(
115 username=six.text_type(args.username.lower())).one()
116 if user:
117 user.pw_hash = auth.gen_password_hash(args.password)
118 user.save()
119 print(u'Password successfully changed')
120 else:
121 print(u'The user doesn\'t exist')