# -*- python -*-
# -*- coding: utf-8 -*-
#
# DDPortfolio service DdportfolioController
# Copyright (c) 2009 Jan Dittberner <jan@dittberner.info>
#
# This file is part of DDPortfolio service.
#
# DDPortfolio service is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# DDPortfolio service is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
import logging
import simplejson

from ddportfolioservice.lib.base import *
from ddportfolioservice.model.form import *
from ddportfolioservice.model.urlbuilder import *

log = logging.getLogger(__name__)

class DdportfolioController(BaseController):
    _LABELS = {
        'overview' : {
            'label' : N_('Overview'),
            'ddpo'  : N_("Debian Developer's Package Overview"),
            'alladdresses' : N_("""Debian Developer's Package Overview
... showing all email addresses"""),
            },
        'bugs' : {
            'label' : N_('Bugs'),
            'received' : N_('''bugs received
(note: co-maintainers not listed, see \
<a href="http://bugs.debian.org/cgi-bin/bugreport.cgi?\
bug=430986">#430986</a>)'''),
            'reported' : N_('bugs reported'),
            'bugstats' : N_('bugstats AKA <em>karma</em>'),
            'usertags' : N_('user tags'),
            'searchall' : N_('all messages (i.e., full text search for \
developer name on all bug logs)'),
            'wnpp' : N_('<a href="http://wiki.debian.org/WNPP">WNPP</a>'),
            },
        'build' : {
            'label' : N_('Build'),
            'buildd' : N_('buildd.d.o (note: co-maintainers \
<em>not</em> listed)'),
            'igloo' : N_('igloo'),
            'svnbuildstat' : N_('svnbuildstat'),
            },
        'qa' : {
            'label' : N_('Quality Assurance'),
            'lintian' : N_('lintian reports'),
            'lintianfull' : N_('full lintian reports (i.e. including \
"info"-level messages)'),
            'dehs' : N_('DEHS (Debian External Health Status)'),
            },
        'upload' : {
            'label' : N_('Upload'),
            'keylog' : N_('''keylog (per-key upload list)
(note: uses key fingerprint)'''),
            },
        'lists' : {
            'label' : N_('Mailing Lists'),
            'dolists' : N_('lists.d.o'),
            'adolists' : N_('lists.a.d.o'),
            'gmane' : N_('gmane'),
            },
        'files' : {
            'label' : N_('Files'),
            'people' : N_('people.d.o'),
            'oldpeople' : N_('oldpeople'),
            'alioth' : N_('Alioth'),
            },
        'membership' : {
            'label' : N_('Membership'),
            'nm' : N_('NM'),
            'db' : N_('DB'),
            'alioth' : N_('Alioth'),
            },
        'miscellaneous' : {
            'label' : N_('Miscellaneous'),
            'debtags' : N_('debtags'),
            'links' : N_('links'),
            },
        }


    def _get_label(self, section, url=None):
        if section in self._LABELS:
            if url:
                if url in self._LABELS[section]:
                    return self._LABELS[section][url]
            elif 'label' in self._LABELS[section]:
                return self._LABELS[section]['label']
        if url:
            return "%s.%s" % (section, url)
        return section


    def index(self):
        # Return a rendered template
        #   return render('/some/template.mako')
        # or, Return a response
        return render('/showform.mako')

    def urllist(self):
        """Handle the actual data."""
        schema = DeveloperData()
        try:
            formencode.api.set_stdtranslation(
                domain="FormEncode",
                languages=[lang[0:2] for lang in request.languages])
            form_result = schema.to_python(request.params)
        except formencode.validators.Invalid, error:
            c.messages = { 'errors' : error.unpack_errors() }
            return render('/showform.mako')
        data = build_urls(form_result)
        if 'mode' in request.params and request.params['mode'] == 'json':
            response.headers['Content-Type'] = 'text/json'
            return simplejson.dumps(
                dict([("%s.%s" % \
                           (entry[1], entry[2]), entry[3]) \
                          for entry in data if entry[0] == 'url']))
        for entry in data:
            if entry[0] in ('url', 'error'):
                entry.append(_(self._get_label(entry[1], entry[2])))
            elif entry[0] == 'section':
                entry.append(_(self._get_label(entry[1])))
        c.urldata = data
        return render('/showurls.mako')