forked from GoogleChrome/chromium-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blink_handler.py
183 lines (147 loc) · 6.25 KB
/
blink_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__author__ = '[email protected] (Eric Bidelman)'
import collections
import json
import logging
import os
import webapp2
import yaml
# Appengine imports.
from google.appengine.api import memcache
import common
import models
import settings
import util
from schedule import construct_chrome_channels_details
class PopulateSubscribersHandler(common.ContentHandler):
def __populate_subscribers(self):
"""Seeds the database with the team in devrel_team.yaml and adds the team
member to the specified blink components in that file. Should only be ran
if the FeatureOwner database entries have been cleared"""
f = file('%s/data/devrel_team.yaml' % settings.ROOT_DIR, 'r')
for profile in yaml.load_all(f):
blink_components = profile.get('blink_components', [])
blink_components = [models.BlinkComponent.get_by_name(name).key() for name in blink_components]
blink_components = filter(None, blink_components) # Filter out None values
user = models.FeatureOwner(
name=unicode(profile['name']),
email=unicode(profile['email']),
twitter=profile.get('twitter', None),
blink_components=blink_components,
primary_blink_components=blink_components,
watching_all_features=False,
)
user.put()
f.close()
@common.require_whitelisted_user
def get(self):
if settings.PROD:
return self.response.out.write('Handler not allowed in production.')
models.BlinkComponent.update_db()
self.__populate_subscribers()
return self.redirect('/admin/blink')
class BlinkHandler(common.ContentHandler):
def __update_subscribers_list(self, add=True, user_id=None, blink_component=None, primary=False):
if not user_id or not blink_component:
return False
user = models.FeatureOwner.get_by_id(long(user_id))
if not user:
return True
if primary:
if add:
user.add_as_component_owner(blink_component)
else:
user.remove_as_component_owner(blink_component)
else:
if add:
user.add_to_component_subscribers(blink_component)
else:
user.remove_from_component_subscribers(blink_component)
return True
@common.require_whitelisted_user
@common.strip_trailing_slash
def get(self, path):
# key = '%s|blinkcomponentowners' % (settings.MEMCACHE_KEY_PREFIX)
# data = memcache.get(key)
# if data is None:
components = models.BlinkComponent.all().order('name').fetch(None)
subscribers = models.FeatureOwner.all().order('name').fetch(None)
# Format for django template
subscribers = [x.format_for_template() for x in subscribers]
for c in components:
c.primaries = [o.name for o in c.owners]
# wf_component_content = models.BlinkComponent.fetch_wf_content_for_components()
# for c in components:
# c.wf_urls = wf_component_content.get(c.name) or []
data = {
'subscribers': subscribers,
'components': components[1:] # ditch generic "Blink" component
}
# memcache.set(key, data)
self.render(data, template_path=os.path.join('admin/blink.html'))
# Remove user from component subscribers.
def put(self, path):
params = json.loads(self.request.body)
self.__update_subscribers_list(False, user_id=params.get('userId'),
blink_component=params.get('componentName'),
primary=params.get('primary'))
self.response.set_status(200, message='User removed from subscribers')
return self.response.write(json.dumps({'done': True}))
# Add user to component subscribers.
def post(self, path):
params = json.loads(self.request.body)
self.__update_subscribers_list(True, user_id=params.get('userId'),
blink_component=params.get('componentName'),
primary=params.get('primary'))
# memcache.flush_all()
# memcache.delete('%s|blinkcomponentowners' % (settings.MEMCACHE_KEY_PREFIX))
self.response.set_status(200, message='User added to subscribers')
return self.response.write(json.dumps(params))
class SubscribersHandler(common.ContentHandler):
@common.require_whitelisted_user
# @common.strip_trailing_slash
def get(self, path):
users = models.FeatureOwner.all().order('name').fetch(None)
feature_list = models.Feature.get_chronological()
milestone = self.request.get('milestone') or None
if milestone:
milestone = int(milestone)
feature_list = filter(lambda f: (f['shipped_milestone'] or f['shipped_android_milestone']) == milestone, feature_list)
list_features_per_owner = 'showFeatures' in self.request.GET
for user in users:
# user.subscribed_components = [models.BlinkComponent.get(key) for key in user.blink_components]
user.owned_components = [models.BlinkComponent.get(key) for key in user.primary_blink_components]
for component in user.owned_components:
component.features = []
if list_features_per_owner:
component.features = filter(lambda f: component.name in f['blink_components'], feature_list)
details = construct_chrome_channels_details()
data = {
'subscribers': users,
'channels': collections.OrderedDict([
('stable', details['stable']),
('beta', details['beta']),
('dev', details['dev']),
('canary', details['canary']),
]),
'selected_milestone': int(milestone) if milestone else None
}
self.render(data, template_path=os.path.join('admin/subscribers.html'))
app = webapp2.WSGIApplication([
('/admin/blink/populate_subscribers', PopulateSubscribersHandler),
('/admin/subscribers(.*)', SubscribersHandler),
('(.*)', BlinkHandler),
], debug=settings.DEBUG)