allura
リビジョン | 948611f5db9460c77cf5cfe5195ce99f09ee6ef8 (tree) |
---|---|
日時 | 2012-07-19 03:59:40 |
作者 | Tim Van Steenburgh <tvansteenburgh@gmai...> |
コミッター | Tim Van Steenburgh |
Script to add a user to a group on a project.
Signed-off-by: Tim Van Steenburgh <tvansteenburgh@gmail.com>
@@ -0,0 +1,61 @@ | ||
1 | +""" | |
2 | +Add a user to group on a project. | |
3 | + | |
4 | +Especially useful for making admin1 a neighborhood admin after loading a | |
5 | +production dataset. | |
6 | + | |
7 | +Example: | |
8 | + # Add admin1 to Admin group for the entire /p/ neighborhood: | |
9 | + $ paster script production.ini ../scripts/add_user_to_group.py -- admin1 Admin | |
10 | + | |
11 | + # Add admin1 to Member group on project /p/allura: | |
12 | + $ paster script production.ini ../scripts/add_user_to_group.py -- admin1 Member allura | |
13 | + | |
14 | + # Add admin1 to Developer group on project /berlios/codeblocks: | |
15 | + $ paster script production.ini ../scripts/add_user_to_group.py -- admin1 Developer codeblocks --nbhd=/berlios/ | |
16 | + | |
17 | + # Add admin1 to Admin group for entire berlios neighborhood: | |
18 | + $ paster script production.ini ../scripts/add_user_to_group.py -- admin1 Admin --nbhd=/berlios/ | |
19 | + | |
20 | +""" | |
21 | + | |
22 | +from allura import model as M | |
23 | +from ming.orm import ThreadLocalORMSession | |
24 | + | |
25 | +def main(options): | |
26 | + nbhd = M.Neighborhood.query.get(url_prefix=options.nbhd) | |
27 | + if not nbhd: | |
28 | + return "Couldn't find neighborhood with url_prefix '%s'" % options.nbhd | |
29 | + project = M.Project.query.get(neighborhood_id=nbhd._id, | |
30 | + shortname=options.project) | |
31 | + if not project: | |
32 | + return "Couldn't find project with shortname '%s'" % options.project | |
33 | + user = M.User.by_username(options.user) | |
34 | + if not user: | |
35 | + return "Couldn't find user with username '%s'" % options.user | |
36 | + project_role = M.ProjectRole.by_name(options.group, project=project) | |
37 | + if not project_role: | |
38 | + return "Couldn't find group (ProjectRole) with name '%s'" % options.group | |
39 | + user_roles = user.project_role(project=project).roles | |
40 | + if project_role._id not in user_roles: | |
41 | + user_roles.append(project_role._id) | |
42 | + ThreadLocalORMSession.flush_all() | |
43 | + | |
44 | + | |
45 | +def parse_options(): | |
46 | + import argparse | |
47 | + parser = argparse.ArgumentParser(description=__doc__, | |
48 | + formatter_class=argparse.RawDescriptionHelpFormatter) | |
49 | + parser.add_argument('user', help='Username') | |
50 | + parser.add_argument('group', help='Group (ProjectRole) name, e.g. Admin, ' | |
51 | + 'Member, Developer, etc.') | |
52 | + parser.add_argument('project', nargs='?', default='--init--', | |
53 | + help='Project shortname. Default is --init--.') | |
54 | + parser.add_argument('--nbhd', default='/p/', help='Neighborhood ' | |
55 | + 'url_prefix. Default is /p/.') | |
56 | + return parser.parse_args() | |
57 | + | |
58 | + | |
59 | +if __name__ == '__main__': | |
60 | + import sys | |
61 | + sys.exit(main(parse_options())) |