<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#     This file is part of CampOS Event,
#     an Odoo module.
#
#     Copyright (c) 2015 Stein & Gabelgaard ApS
#                        http://www.steingabelgaard.dk
#                        <NAME>
#
#     CampOS Event 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.
#
#     CampOS Event 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 CampOS Event.
#     If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import random
from datetime import date, datetime, timedelta
from dateutil.relativedelta import relativedelta
from urlparse import urljoin
import werkzeug

from openerp.addons.base_geoengine import geo_model
from openerp import models, fields, api
from openerp.tools.translate import _

import base64
try:
    import xlwt
except ImportError:
    xlwt = None
import re
from cStringIO import StringIO

import logging
_logger = logging.getLogger(__name__)


def random_token():
    # the token has an entropy of about 120 bits (6 bits/char * 20 chars)
    chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
    return ''.join(random.SystemRandom().choice(chars) for i in xrange(20))

class EventEvent(models.Model):

    '''
    Event ID

    '''
    _inherit = 'event.event'

    survey_id = fields.Many2one('survey.survey', 'Signup survey')
    attachment_id = fields.Many2one('ir.attachment', string="Attachments")
    camp_area_ids = fields.One2many(
        'campos.camp.area',
        'event_id', string="Rooms/Camp Areas")

    regform = fields.Selection([('register_free', 'Std. Free Register'),
                                ('register_meeting', 'Meeting register (with survey)'),
                                ('intl_groups', 'International Groups')], string='Registration Form', default='register_meeting')

    @api.multi
    def action_export_survey(self):
        fields = []
        self.ensure_one()

        # Standard field
        fields = [{'id': "s1", 'fieldname': 'Navn'},
                  {'id': 's1s', 'fieldname': 'Status'},
                  {'id': "s2", 'fieldname': 'Udvalg'},
                  {'id': "s2x", 'fieldname': 'Udvalg Kort'},
                  {'id': "s3", 'fieldname': 'Funktion'},
                  {'id': "s3r", 'fieldname': 'Rum'},
                  {'id': "s4", 'fieldname': 'adresse'},
                  {'id': 's5', 'fieldname': 'Postnr'},
                  {'id': 's6', 'fieldname': 'By'},
                  {'id': 's7', 'fieldname': 'Email'},
                  {'id': 's7p', 'fieldname': 'Privat Email'},
                  {'id': 's8', 'fieldname': 'Tlf'},
                  {'id': 's9', 'fieldname': 'Mobil'},
                  {'id': 's10', 'fieldname': 'Opdateret'},
                  ]
        for page in self.survey_id.page_ids:
            for q in page.question_ids:
                if q.type == 'multiple_choice':
                    for l in q.labels_ids:
                        fields.append({'q': q,
                                       'label': l,
                                       'id': "%d-%d" % (q.id, l.id),
                                       'fieldname': "%s/%s" % (q.question, l.value)})
                else:
                    fields.append({'q': q,
                                   'id': "%d" % (q.id),
                                   'fieldname': "%s" % (q.question)})
                if q.comments_allowed:
                    fields.append({'q': q,
                                   'id': "%d-comm" % (q.id),
                                   'fieldname': "%s" % (q.comments_message)})

        rows = []
        for reg in self.registration_ids:
            # if reg.state == 'cancel':
            #    continue
            row = {}
            row['s1'] = reg.partner_id.name
            row['s3r'] = reg.camp_area_id.name
            row['s4'] = ''.join(filter(None, [reg.partner_id.street, reg.partner_id.street2]))
            row['s5'] = reg.partner_id.zip
            row['s6'] = reg.partner_id.city
            row['s7'] = reg.partner_id.email
            row['s8'] = reg.partner_id.phone
            row['s9'] = reg.partner_id.mobile
            row['s1s'] = reg.state
            participant = self.env['campos.event.participant'].search([('partner_id', '=', reg.partner_id.id)])
            if participant:
                row['s7p'] = participant[0].private_mailaddress
                comm_list = None
                func_list = None
                for j in participant[0].jobfunc_ids:
                    comm_list = '|'.join(filter(None, [comm_list, j.committee_id.display_name]))
                    func_list = '|'.join(filter(None, [func_list, j.function_type_id.name]))
                row['s2'] = comm_list
                row['s3'] = func_list
                if participant[0].jobfunc_ids[0]:
                    row['s2x'] = participant[0].jobfunc_ids[0].committee_id.root_name
            if reg.reg_survey_input_id:
                row['s10'] = reg.reg_survey_input_id.write_date
                for ans in reg.reg_user_input_line_ids:
                    if ans.question_id.comments_allowed and ans.answer_type == 'text':
                        row['%d-comm' % (ans.question_id.id)] = ans.value_text
                    if ans.question_id.type == 'multiple_choice' and ans.value_suggested:
                        row['%d-%d' % (ans.question_id.id, ans.value_suggested.id)] = 'X'
                    elif ans.question_id.type == 'simple_choice' and ans.value_suggested:
                        row['%d' % (ans.question_id.id)] = ans.value_suggested.value
                        _logger.info('%s ROW simple: %d %s', row['s1'], ans.question_id.id, ans.value_suggested.value)
                    elif ans.question_id.type in ['free_text', 'textbox', 'text'] and ans.value_text:
                        row['%d' % (ans.question_id.id)] = ans.value_text
                        _logger.info('%s ROW text: %d %s', row['s1'], ans.question_id.id, ans.value_text)
                    elif ans.question_id.type in ['free_text'] and ans.value_free_text:
                        row['%d' % (ans.question_id.id)] = ans.value_free_text
                        _logger.info('%s ROW FREE text: %d %s', row['s1'], ans.question_id.id, ans.value_free_text)
            rows.append(row)

        data = base64.encodestring(self.from_data(fields, rows))
        attach_vals = {<commit_msg>Fix chrash on missing committee function<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#     This file is part of CampOS Event,
#     an Odoo module.
#
#     Copyright (c) 2015 Stein & Gabelgaard ApS
#                        http://www.steingabelgaard.dk
#                        <NAME>
#
#     CampOS Event 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.
#
#     CampOS Event 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 CampOS Event.
#     If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import random
from datetime import date, datetime, timedelta
from dateutil.relativedelta import relativedelta
from urlparse import urljoin
import werkzeug

from openerp.addons.base_geoengine import geo_model
from openerp import models, fields, api
from openerp.tools.translate import _

import base64
try:
    import xlwt
except ImportError:
    xlwt = None
import re
from cStringIO import StringIO

import logging
_logger = logging.getLogger(__name__)


def random_token():
    # the token has an entropy of about 120 bits (6 bits/char * 20 chars)
    chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
    return ''.join(random.SystemRandom().choice(chars) for i in xrange(20))

class EventEvent(models.Model):

    '''
    Event ID

    '''
    _inherit = 'event.event'

    survey_id = fields.Many2one('survey.survey', 'Signup survey')
    attachment_id = fields.Many2one('ir.attachment', string="Attachments")
    camp_area_ids = fields.One2many(
        'campos.camp.area',
        'event_id', string="Rooms/Camp Areas")

    regform = fields.Selection([('register_free', 'Std. Free Register'),
                                ('register_meeting', 'Meeting register (with survey)'),
                                ('intl_groups', 'International Groups')], string='Registration Form', default='register_meeting')

    @api.multi
    def action_export_survey(self):
        fields = []
        self.ensure_one()

        # Standard field
        fields = [{'id': "s1", 'fieldname': 'Navn'},
                  {'id': 's1s', 'fieldname': 'Status'},
                  {'id': "s2", 'fieldname': 'Udvalg'},
                  {'id': "s2x", 'fieldname': 'Udvalg Kort'},
                  {'id': "s3", 'fieldname': 'Funktion'},
                  {'id': "s3r", 'fieldname': 'Rum'},
                  {'id': "s4", 'fieldname': 'adresse'},
                  {'id': 's5', 'fieldname': 'Postnr'},
                  {'id': 's6', 'fieldname': 'By'},
                  {'id': 's7', 'fieldname': 'Email'},
                  {'id': 's7p', 'fieldname': 'Privat Email'},
                  {'id': 's8', 'fieldname': 'Tlf'},
                  {'id': 's9', 'fieldname': 'Mobil'},
                  {'id': 's10', 'fieldname': 'Opdateret'},
                  ]
        for page in self.survey_id.page_ids:
            for q in page.question_ids:
                if q.type == 'multiple_choice':
                    for l in q.labels_ids:
                        fields.append({'q': q,
                                       'label': l,
                                       'id': "%d-%d" % (q.id, l.id),
                                       'fieldname': "%s/%s" % (q.question, l.value)})
                else:
                    fields.append({'q': q,
                                   'id': "%d" % (q.id),
                                   'fieldname': "%s" % (q.question)})
                if q.comments_allowed:
                    fields.append({'q': q,
                                   'id': "%d-comm" % (q.id),
                                   'fieldname': "%s" % (q.comments_message)})

        rows = []
        for reg in self.registration_ids:
            # if reg.state == 'cancel':
            #    continue
            row = {}
            row['s1'] = reg.partner_id.name
            row['s3r'] = reg.camp_area_id.name
            row['s4'] = ''.join(filter(None, [reg.partner_id.street, reg.partner_id.street2]))
            row['s5'] = reg.partner_id.zip
            row['s6'] = reg.partner_id.city
            row['s7'] = reg.partner_id.email
            row['s8'] = reg.partner_id.phone
            row['s9'] = reg.partner_id.mobile
            row['s1s'] = reg.state
            participant = self.env['campos.event.participant'].search([('partner_id', '=', reg.partner_id.id)])
            if participant:
                row['s7p'] = participant[0].private_mailaddress
                comm_list = None
                func_list = None
                for j in participant[0].jobfunc_ids:
                    comm_list = '|'.join(filter(None, [comm_list, j.committee_id.display_name]))
                    func_list = '|'.join(filter(None, [func_list, j.function_type_id.name]))
                row['s2'] = comm_list
                row['s3'] = func_list
                if participant[0].jobfunc_ids:
                    row['s2x'] = participant[0].jobfunc_ids[0].committee_id.root_name
            if reg.reg_survey_input_id:
                row['s10'] = reg.reg_survey_input_id.write_date
                for ans in reg.reg_user_input_line_ids:
                    if ans.question_id.comments_allowed and ans.answer_type == 'text':
                        row['%d-comm' % (ans.question_id.id)] = ans.value_text
                    if ans.question_id.type == 'multiple_choice' and ans.value_suggested:
                        row['%d-%d' % (ans.question_id.id, ans.value_suggested.id)] = 'X'
                    elif ans.question_id.type == 'simple_choice' and ans.value_suggested:
                        row['%d' % (ans.question_id.id)] = ans.value_suggested.value
                        _logger.info('%s ROW simple: %d %s', row['s1'], ans.question_id.id, ans.value_suggested.value)
                    elif ans.question_id.type in ['free_text', 'textbox', 'text'] and ans.value_text:
                        row['%d' % (ans.question_id.id)] = ans.value_text
                        _logger.info('%s ROW text: %d %s', row['s1'], ans.question_id.id, ans.value_text)
                    elif ans.question_id.type in ['free_text'] and ans.value_free_text:
                        row['%d' % (ans.question_id.id)] = ans.value_free_text
                        _logger.info('%s ROW FREE text: %d %s', row['s1'], ans.question_id.id, ans.value_free_text)
            rows.append(row)

        data = base64.encodestring(self.from_data(fields, rows))
        attach_vals = {
{'fraction_non_alphanumeric': 0.15205343186016768, 'fraction_numerical': 0.010018473781440955, 'mean_word_length': 1.9056564822460775, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3599240', 'n_tokens_mistral': 4319, 'n_tokens_neox': 4076, 'n_words': 977}