Switch to unified view

a/scripts/publicize-neighborhood.py b/scripts/publicize-neighborhood.py
1
import logging
1
import logging
2
import sys
2
import sys
3
3
4
from ming.orm import ThreadLocalORMSession
4
from ming.orm import ThreadLocalORMSession
5
from pylons import g
5
from pylons import c, g
6
6
7
from allura import model as M
7
from allura import model as M
8
from allura.lib import helpers as h
8
from allura.lib import helpers as h
9
from allura.lib import utils
9
from allura.lib import utils
10
11
from sfx.lib.sfx_api import SFXProjectApi
12
10
13
log = logging.getLogger(__name__)
11
log = logging.getLogger(__name__)
14
12
15
def main(options):
13
def main(options):
16
    log.addHandler(logging.StreamHandler(sys.stdout))
14
    log.addHandler(logging.StreamHandler(sys.stdout))
17
    log.setLevel(getattr(logging, options.log_level.upper()))
15
    log.setLevel(getattr(logging, options.log_level.upper()))
18
16
19
    api = SFXProjectApi()
20
    nbhd = M.Neighborhood.query.get(name=options.neighborhood)
17
    nbhd = M.Neighborhood.query.get(name=options.neighborhood)
21
    if not nbhd:
18
    if not nbhd:
22
        return 'Invalid neighborhood "%s".' % options.neighborhood
19
        return 'Invalid neighborhood "%s".' % options.neighborhood
23
    sfx_siteadmin = M.User.query.get(username=api.sfx_siteadmin)
20
    admin_role = M.ProjectRole.by_name('Admin', project=nbhd.neighborhood_project)
24
    if not sfx_siteadmin:
21
    nbhd_admin = admin_role.users_with_role(project=nbhd.neighborhood_project)[0].user
25
        return "Couldn't find sfx_siteadmin with username '%s'" % api.sfx_siteadmin
22
    log.info('Making updates as neighborhood admin "%s"' % nbhd_admin.username)
26
23
27
    q = {'neighborhood_id': nbhd._id,
24
    q = {'neighborhood_id': nbhd._id,
28
            'shortname': {'$ne':'--init--'}, 'deleted':False}
25
            'shortname': {'$ne':'--init--'}, 'deleted':False}
29
    private_count = public_count = 0
26
    private_count = public_count = 0
30
    for projects in utils.chunked_find(M.Project, q):
27
    for projects in utils.chunked_find(M.Project, q):
...
...
35
                if options.test:
32
                if options.test:
36
                    log.info('Would be made public: "%s"' % p.shortname)
33
                    log.info('Would be made public: "%s"' % p.shortname)
37
                else:
34
                else:
38
                    log.info('Making public: "%s"' % p.shortname)
35
                    log.info('Making public: "%s"' % p.shortname)
39
                    p.acl.append(M.ACE.allow(role_anon._id, 'read'))
36
                    p.acl.append(M.ACE.allow(role_anon._id, 'read'))
37
                    with h.push_config(c, project=p, user=nbhd_admin):
40
                    ThreadLocalORMSession.flush_all()
38
                        ThreadLocalORMSession.flush_all()
41
                    try:
39
                        g.post_event('project_updated')
42
                        api.update(sfx_siteadmin, p)
43
                    except Exception, e:
44
                        log.warning('SFX API update failed for project "%s": '
45
                                    '%s' % (p.shortname, e))
46
                private_count += 1
40
                private_count += 1
47
            else:
41
            else:
48
                log.info('Already public: "%s"' % p.shortname)
42
                log.info('Already public: "%s"' % p.shortname)
49
                public_count += 1
43
                public_count += 1
50
44