switch from ddportfolio to debianmemberportfolio
[debianmemberportfolio.git] / debianmemberportfolio / model / urlbuilder.py
1 # -*- python -*-
2 # -*- coding: utf8 -*-
3 #
4 # Debian Member Portfolio Service url builder
5 #
6 # Copyright © 2009-2014 Jan Dittberner <jan@dittberner.info>
7 #
8 # This file is part of the Debian Member Portfolio Service.
9 #
10 # Debian Member Portfolio Service is free software: you can redistribute it
11 # and/or modify it under the terms of the GNU Affero General Public License as
12 # published by the Free Software Foundation, either version 3 of the License,
13 # or (at your option) any later version.
14 #
15 # Debian Member Portfolio Service is distributed in the hope that it will be
16 # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero
18 # General Public License for more details.
19 #
20 # You should have received a copy of the GNU Affero General Public License
21 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 #
23 """
24 This module provides the function build_urls to build personalized
25 URLs using the given information and the URL patterns defined in
26 portfolio.ini.
27 """
28
29 from ConfigParser import ConfigParser, InterpolationMissingOptionError
30 import pkg_resources
31 from debianmemberportfolio.model import keyfinder
32 from urllib import quote_plus
33 from pylons.i18n.translation import _, N_
34
35
36 my_config = ConfigParser()
37 my_config.readfp(pkg_resources.resource_stream(__name__, 'portfolio.ini'))
38
39 _FIELDNAMES_MAP = {
40     'email': N_('Email address'),
41     'name': N_('Name'),
42     'gpgfp': N_('GPG fingerprint'),
43     'username': N_('Debian user name'),
44     'nonddemail': N_('Non Debian email address'),
45     'aliothusername': N_('Alioth user name'),
46 }
47
48
49 class DDPortfolioEntry(object):
50     def __init__(self, config, section, key):
51         self.name = key
52         self.optional = config.has_option(section, key + '.optional') and \
53             config.getboolean(section, key + '.optional') or False
54         if config.has_option(section, key + '.type'):
55             self.type = config.get(section, key + '.type')
56         else:
57             self.type = 'url'
58
59
60 def _build_quoted_fields(fields):
61     """
62     Take a dictionary of raw field values and quote the values if required.
63     """
64     qfields = {}
65     for key, value in fields.iteritems():
66         if value is not None:
67             if isinstance(value, unicode):
68                 qfields[key] = quote_plus(value.encode('utf8'))
69             elif isinstance(value, str):
70                 qfields[key] = quote_plus(value)
71             else:
72                 qfields[key] = value
73
74     if 'gpgfp' not in qfields:
75         fpr = keyfinder.getFingerprintByEmail(fields['email'].encode('utf8'))
76         if fpr:
77             qfields['gpgfp'] = fpr[0]
78     qfields['firstchar'] = fields['email'][0].encode('utf8')
79     qfields['emailnoq'] = fields['email'].encode('utf8')
80     return qfields
81
82
83 def build_urls(fields):
84     """Build personalized URLs using the developer information in
85     fields."""
86     data = []
87     qfields = _build_quoted_fields(fields)
88     for section in [section.strip() for section in
89                     my_config.get('DEFAULT',
90                                   'urlbuilder.sections').split(',')]:
91         data.append(['section', section])
92         if my_config.has_option(section, 'urls'):
93             for entry in ([
94                 DDPortfolioEntry(my_config, section, url) for url in
95                     my_config.get(section, 'urls').split(',')]):
96                 try:
97                     data.append(
98                         ['url', section, entry,
99                          my_config.get(section, entry.name + '.pattern',
100                                        False, qfields)])
101                 except InterpolationMissingOptionError, e:
102                     if not entry.optional:
103                         if e.reference in _FIELDNAMES_MAP:
104                             data.append(['error', section, entry,
105                                          _('Missing input: %s') %
106                                          _(_FIELDNAMES_MAP[e.reference])])
107                         else:
108                             data.append(['error', section, entry,
109                                          _('Missing input: %s') % e.reference])
110     return data