|
a/Allura/allura/controllers/site_admin.py |
|
b/Allura/allura/controllers/site_admin.py |
1 |
import logging
|
1 |
import logging
|
2 |
import os
|
|
|
3 |
import mimetypes
|
|
|
4 |
from datetime import datetime, timedelta
|
2 |
from datetime import datetime, timedelta
|
5 |
from collections import defaultdict
|
3 |
from collections import defaultdict
|
6 |
|
4 |
|
7 |
import pkg_resources
|
|
|
8 |
from tg import expose, redirect, flash, config, validate, request, response
|
5 |
from tg import expose, flash, config, request
|
9 |
from tg.decorators import with_trailing_slash, without_trailing_slash
|
6 |
from tg.decorators import with_trailing_slash, without_trailing_slash
|
10 |
from webob import exc
|
|
|
11 |
from formencode import validators as fev
|
|
|
12 |
from ming.orm import session
|
7 |
from ming.orm import session
|
13 |
import pymongo
|
8 |
import pymongo
|
14 |
from pylons import c, g
|
9 |
from pylons import c
|
15 |
|
10 |
|
16 |
from allura.lib import helpers as h
|
11 |
from allura.lib import helpers as h
|
17 |
from allura.lib.security import require_access
|
12 |
from allura.lib.security import require_access
|
18 |
from allura import model as M
|
13 |
from allura import model as M
|
|
|
14 |
from allura.command.show_models import dfs, build_model_inheritance_graph
|
|
|
15 |
|
|
|
16 |
from urlparse import urlparse
|
19 |
|
17 |
|
20 |
|
18 |
|
21 |
log = logging.getLogger(__name__)
|
19 |
log = logging.getLogger(__name__)
|
|
|
20 |
|
22 |
|
21 |
|
23 |
class SiteAdminController(object):
|
22 |
class SiteAdminController(object):
|
24 |
|
23 |
|
25 |
def _check_security(self):
|
24 |
def _check_security(self):
|
26 |
with h.push_context(config.get('site_admin_project', 'allura'),
|
25 |
with h.push_context(config.get('site_admin_project', 'allura'),
|
|
... |
|
... |
101 |
data = {'expires': datetime.utcnow() + timedelta(days=2)}
|
100 |
data = {'expires': datetime.utcnow() + timedelta(days=2)}
|
102 |
|
101 |
|
103 |
data['token_list'] = M.ApiTicket.query.find().sort('mod_date', pymongo.DESCENDING).all()
|
102 |
data['token_list'] = M.ApiTicket.query.find().sort('mod_date', pymongo.DESCENDING).all()
|
104 |
log.info(data['token_list'])
|
103 |
log.info(data['token_list'])
|
105 |
return data
|
104 |
return data
|
|
|
105 |
|
|
|
106 |
def subscribe_artifact(self, url, user):
|
|
|
107 |
artifact_url = urlparse(url).path[1:-1].split("/")
|
|
|
108 |
neighborhood = M.Neighborhood.query.find({
|
|
|
109 |
"url_prefix": "/" + artifact_url[0] + "/"}).first()
|
|
|
110 |
|
|
|
111 |
if artifact_url[0] == "u":
|
|
|
112 |
project = M.Project.query.find({
|
|
|
113 |
"shortname": artifact_url[0] + "/" + artifact_url[1],
|
|
|
114 |
"neighborhood_id": neighborhood._id}).first()
|
|
|
115 |
else:
|
|
|
116 |
project = M.Project.query.find({
|
|
|
117 |
"shortname": artifact_url[1],
|
|
|
118 |
"neighborhood_id": neighborhood._id}).first()
|
|
|
119 |
|
|
|
120 |
appconf = M.AppConfig.query.find({
|
|
|
121 |
"options.mount_point": artifact_url[2],
|
|
|
122 |
"project_id": project._id}).first()
|
|
|
123 |
|
|
|
124 |
if appconf.url() == urlparse(url).path:
|
|
|
125 |
M.Mailbox.subscribe(
|
|
|
126 |
user_id=user._id,
|
|
|
127 |
app_config_id=appconf._id,
|
|
|
128 |
project_id=project._id)
|
|
|
129 |
return True
|
|
|
130 |
|
|
|
131 |
classes = set()
|
|
|
132 |
for depth, cls in dfs(M.Artifact, build_model_inheritance_graph()):
|
|
|
133 |
classes.add(cls)
|
|
|
134 |
for cls in classes:
|
|
|
135 |
for artifact in cls.query.find({"app_config_id": appconf._id}):
|
|
|
136 |
if artifact.url() == urlparse(url).path:
|
|
|
137 |
M.Mailbox.subscribe(
|
|
|
138 |
user_id=user._id,
|
|
|
139 |
app_config_id=appconf._id,
|
|
|
140 |
project_id=project._id,
|
|
|
141 |
artifact=artifact)
|
|
|
142 |
return True
|
|
|
143 |
return False
|
|
|
144 |
|
|
|
145 |
@expose('jinja:allura:templates/site_admin_add_subscribers.html')
|
|
|
146 |
def add_subscribers(self, **data):
|
|
|
147 |
if request.method == 'POST':
|
|
|
148 |
url = data['artifact_url']
|
|
|
149 |
user = M.User.by_username(data['for_user'])
|
|
|
150 |
if not user or user == M.User.anonymous():
|
|
|
151 |
flash('Invalid login', 'error')
|
|
|
152 |
return data
|
|
|
153 |
|
|
|
154 |
try:
|
|
|
155 |
ok = self.subscribe_artifact(url, user)
|
|
|
156 |
except:
|
|
|
157 |
log.warn("Can't subscribe to artifact", exc_info=True)
|
|
|
158 |
ok = False
|
|
|
159 |
|
|
|
160 |
if ok:
|
|
|
161 |
flash('User successfully subscribed to the artifact')
|
|
|
162 |
return {}
|
|
|
163 |
else:
|
|
|
164 |
flash('Artifact not found', 'error')
|
|
|
165 |
|
|
|
166 |
return data
|