e1dad66c820d4b768e5b72e0c90b97f4e32c2cba
[debianmemberportfolio.git] / debianmemberportfolio / controllers / portfolio.py
1 # -*- python -*-
2 # -*- coding: utf-8 -*-
3 #
4 # Debian Member Portfolio Service DdportfolioController
5 # Copyright © 2009-2014 Jan Dittberner <jan@dittberner.info>
6 #
7 # This file is part of Debian Member Portfolio Service.
8 #
9 # Debian Member Portfolio Service is free software: you can redistribute it
10 # and/or modify it under the terms of the GNU Affero General Public License as
11 # published by the Free Software Foundation, either version 3 of the License,
12 # or (at your option) any later version.
13 #
14 # Debian Member Portfolio Service is distributed in the hope that it will be
15 # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero
17 # General Public License for more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22 import logging
23 import simplejson
24
25 from pylons import request, response, tmpl_context as c
26 from pylons.i18n import N_, _
27 import formencode.api
28 import formencode.validators
29
30 from ddportfolioservice.lib.base import BaseController, render
31 from ddportfolioservice.model.form import DDDataRequest, DeveloperData
32 from ddportfolioservice.model.urlbuilder import build_urls
33 from ddportfolioservice.model import dddatabuilder
34
35 log = logging.getLogger(__name__)
36
37
38 class DdportfolioController(BaseController):
39     """
40     Main controller for the Debian Member portfolio service.
41     """
42     _LABELS = {
43         'overview': {
44             'label': N_('Overview'),
45             'ddpo': N_("Debian Member's Package Overview"),
46             'alladdresses': N_("""Debian Member's Package Overview
47 ... showing all email addresses"""),
48         },
49         'bugs': {
50             'label': N_('Bugs'),
51             'received': N_('''bugs received
52 (note: co-maintainers not listed, see \
53 <a href="http://bugs.debian.org/cgi-bin/bugreport.cgi?\
54 bug=430986">#430986</a>)'''),
55             'reported': N_('bugs reported'),
56             'usertags': N_('user tags'),
57             'searchall': N_('all messages (i.e., full text search for \
58 developer name on all bug logs)'),
59             'wnpp': N_('<a href="http://wiki.debian.org/WNPP">WNPP</a>'),
60             'correspondent': N_('correspondent for bugs'),
61             'graph': N_('one year open bug history graph'),
62         },
63         'build': {
64             'label': N_('Build'),
65             'buildd': N_('buildd.d.o'),
66             'igloo': N_('igloo'),
67         },
68         'qa': {
69             'label': N_('Quality Assurance'),
70             'dmd': N_('maintainer dashboard'),
71             'lintian': N_('lintian reports'),
72             'lintianfull': N_('full lintian reports (i.e. including \
73 "info"-level messages)'),
74             'piuparts': N_('piuparts'),
75             'patchtracker': N_('Debian patch tracking system'),
76             'duck': N_('Debian Url ChecKer'),
77         },
78         'lists': {
79             'label': N_('Mailing Lists'),
80             'dolists': N_('lists.d.o'),
81             'adolists': N_('lists.a.d.o'),
82             'gmane': N_('gmane'),
83         },
84         'files': {
85             'label': N_('Files'),
86             'people': N_('people.d.o'),
87             'oldpeople': N_('oldpeople'),
88             'alioth': N_('Alioth'),
89         },
90         'membership': {
91             'label': N_('Membership'),
92             'nm': N_('NM'),
93             'dbfinger': N_('DB information via finger'),
94             'db': N_('DB information via HTTP'),
95             'webid': N_('FOAF profile'),
96             'alioth': N_('Alioth'),
97             'wiki': N_('Wiki'),
98             'forum': N_('Forum'),
99         },
100         'miscellaneous': {
101             'label': N_('Miscellaneous'),
102             'debtags': N_('debtags'),
103             'planetname': N_('Planet Debian (name)'),
104             'planetuser': N_('Planet Debian (username)'),
105             'links': N_('links'),
106             'website': N_('Debian website'),
107             'search': N_('Debian search'),
108             'gpgfinger': N_('GPG public key via finger'),
109             'gpgweb': N_('GPG public key via HTTP'),
110             'nm': N_('NM, AM participation'),
111             'contrib': N_('Contribution information'),
112         },
113         'ssh': {
114             'label': N_('Information reachable via ssh (for Debian Members)'),
115             'owndndoms': N_('owned debian.net domains'),
116             'miainfo': N_('<a href="http://wiki.debian.org/qa.debian.org/'
117                           'MIATeam">MIA</a> database information'),
118             'groupinfo': N_('Group membership information'),
119         },
120         'ubuntu': {
121             'label': N_('Ubuntu'),
122             'ubuntudiff': N_('Available patches from Ubuntu'),
123         },
124     }
125
126     def _get_label(self, section, url=None):
127         if section in self._LABELS:
128             if url:
129                 if url in self._LABELS[section]:
130                     return self._LABELS[section][url]
131             elif 'label' in self._LABELS[section]:
132                 return self._LABELS[section]['label']
133         if url:
134             return "%s.%s" % (section, url)
135         return section
136
137     def index(self):
138         """
139         Render the input form.
140         """
141         return render('/showform.mako')
142
143     def urllist(self):
144         """Handle the actual data."""
145         schema = DDDataRequest()
146         try:
147             formencode.api.set_stdtranslation(
148                 domain="FormEncode",
149                 languages=[lang[0:2] for lang in request.languages])
150             form_result = schema.to_python(request.params)
151         except formencode.validators.Invalid, error:
152             c.messages = {'errors': error.unpack_errors()}
153             return render('/showform.mako')
154         fields = dddatabuilder.build_data(form_result['email'])
155         rp = request.params.copy()
156
157         DM_TUPLES = (('name', 'name'),
158                      ('gpgfp', 'gpgfp'),
159                      ('nonddemail', 'email'))
160         DD_TUPLES = (('username', 'username'),
161                      ('aliothusername', 'username'))
162
163         if fields['type'] in (dddatabuilder.TYPE_DD, dddatabuilder.TYPE_DM):
164             for tuple in DM_TUPLES:
165                 if not tuple[0] in rp or not rp[tuple[0]]:
166                     rp[tuple[0]] = fields[tuple[1]]
167         if fields['type'] == dddatabuilder.TYPE_DD:
168             for tuple in DD_TUPLES:
169                 if not tuple[0] in rp or not rp[tuple[0]]:
170                     rp[tuple[0]] = fields[tuple[1]]
171
172         schema = DeveloperData()
173         try:
174             formencode.api.set_stdtranslation(
175                 domain="FormEncode",
176                 languages=[lang[0:2] for lang in request.languages])
177             form_result = schema.to_python(rp)
178         except formencode.validators.Invalid, error:
179             c.messages = {'errors': error.unpack_errors()}
180             return render('/showform.mako')
181         if form_result['wikihomepage'] is None:
182             log.debug('generate wikihomepage from name')
183             form_result['wikihomepage'] = "".join(
184                 [part.capitalize() for part in form_result['name'].split()])
185         data = build_urls(form_result)
186         if form_result['mode'] == 'json':
187             response.headers['Content-Type'] = 'text/javascript'
188             return simplejson.dumps(
189                 dict([("%s.%s" % (entry[1], entry[2].name), entry[3])
190                       for entry in data if entry[0] == 'url']))
191         for entry in data:
192             if entry[0] in ('url', 'error'):
193                 entry.append(_(self._get_label(entry[1], entry[2].name)))
194             elif entry[0] == 'section':
195                 entry.append(_(self._get_label(entry[1])))
196         c.urldata = data
197         return render('/showurls.mako')