def render(self): out = StringIO() writer = UnicodeWriter(out, delimiter=';', quoting=csv.QUOTE_ALL) #writer = csv.writer(out) CSV_HEADER = self.csv_headers() # Create CSV file CSV_HEADER_I18N = [self.context.translate(_(x)) for x in CSV_HEADER] writer.writerow(CSV_HEADER_I18N) export_data = self.prepare_export_data() for entry in export_data: result = export_data[entry] answers = [] for r in survey_fields_ordered(): value = result[r] answers.append(value.decode('utf-8')) writer.writerow(answers) data = out.getvalue() prefix = 'surveyresults' timemarker = int(round(time.time() * 1000)) ext = '.csv' filename = "%s-%s%s" % (prefix, timemarker, ext) c_control = "must-revalidate, post-check=0, pre-check=0, public" # Create response self.request.response.setHeader('Content-Length', "%d" % len(data)) self.request.response.setHeader('Pragma', "no-cache") self.request.response.setHeader('Cache-Control', c_control) self.request.response.setHeader('Expires', "0") # Return CSV data self.request.response.setHeader('Content-Type', 'text/csv') self.request.response.setHeader('Content-Disposition', 'attachment; filename=%s' % filename) return out.getvalue()
def render(self): portal = api.portal.get() tool = getUtility(ISurveyTool) tool.destroy() portal_url = portal.absolute_url() api.portal.show_message( message=_(u"Session cleared"), request=self.request) return self.request.response.redirect(portal_url)
def render(self): form = self.request.form data = {} unwanted = ('_authenticator', 'form.button.Submit') for value in form: if value not in unwanted: data[value] = form[value] tool = getUtility(ISurveyTool) now = datetime.now() timestamp = api.portal.get_localized_time(datetime=now, long_format=True) client_ip = self.get_client_ip() if client_ip is None: userinfo = timestamp else: userinfo = client_ip + '-' + timestamp name = 'survey-state' puid = django_random.get_random_string() if not self.has_active_session(): data['puid'] = puid else: session = tool.get() try: current_session = session[name] saved_puid = current_session['puid'] data['puid'] = saved_puid except KeyError: data['puid'] = puid data['pip'] = client_ip tool.add(name, data) time_info = _(u"Autosave %s") % userinfo msg = _(u"Survey state automatically saved") results = {'success': True, 'message': msg, 'timestamp': time_info } self.request.response.setHeader('Content-Type', 'application/json; charset=utf-8') return json.dumps(results)
def prepare_export_data(self): data = {} arrays = survey_fields_multiselect() selections = survey_fields_select() results = self.survey_answers() for r in results: obj = r.getObject() index = obj.participant answers = json.loads(obj.answers) if 'survey-state' in answers: itemdata = answers['survey-state'] else: itemdata = {} flattened = {} for item in survey_fields_datacollection(): if item in arrays: try: values = itemdata[item] except KeyError: values = '' for token in arrays[item]: if token in values: flattened[token] = '1' else: flattened[token] = '0' elif item in selections: try: value = itemdata[item] except KeyError: value = '' try: splitted_value = value.split('.') flattened[item] = splitted_value[-1] except AttributeError: flattened[item] = _(u"Outdated field value") elif item == 'pid': flattened[item] = str(index) else: try: value = safe_unicode(itemdata[item]).encode('utf-8') flattened[item] = str(value) except KeyError: flattened[item] = '' data[index] = flattened return data
def csv_preview(self): out = StringIO() #writer = UnicodeWriter(out, # {'delimiter': ';', # 'quotechar': '"', # 'quoting': csv.QUOTE_ALL}) writer = UnicodeWriter(out, delimiter=';', quoting=csv.QUOTE_ALL) #writer = csv.writer(out) CSV_HEADER = self.csv_headers() # Create CSV file CSV_HEADER_I18N = [self.context.translate(_(x)) for x in CSV_HEADER] writer.writerow(CSV_HEADER_I18N) export_data = self.prepare_export_data() for entry in export_data: result = export_data[entry] answers = [] for r in survey_fields_ordered(): value = result[r] answers.append(value.decode('utf-8')) writer.writerow(answers) data = out.getvalue() return data
def survey_fields_i18n(): fields = { u'pid': _(u"Participant ID"), u'pip': _(u"Participant IP"), u'puid': _(u"Participant Code"), u'favorite1': _(u"Favorite 1"), u'favorite1.likes.one': _(u"Favorite 1 - Like 1"), u'favorite1.likes.two': _(u"Favorite 1 - Like 2"), u'favorite1.likes.three': _(u"Favorite 1 - Like 3"), u'favorite1.dislikes.one': _(u"Favorite 1 - Dislike 1"), u'favorite1.dislikes.two': _(u"Favorite 1 - Dislike 2"), u'favorite1.dislikes.three': _(u"Favorite 1 - Dislike 3"), u'favorite2': _(u"Favorite 2"), u'favorite2.likes.one': _(u"Favorite 2 - Like 1"), u'favorite2.likes.two': _(u"Favorite 2 - Like 2"), u'favorite2.likes.three': _(u"Favorite 2 - Like 3"), u'favorite2.dislikes.one': _(u"Favorite 2 - Dislike 1"), u'favorite2.dislikes.two': _(u"Favorite 2 - Dislike 2"), u'favorite2.dislikes.three': _(u"Favorite 2 - Dislike 3"), u'favorite3': _(u"Favorite 3"), u'favorite3.likes.one': _(u"Favorite 3 - Like 1"), u'favorite3.likes.two': _(u"Favorite 3 - Like 2"), u'favorite3.likes.three': _(u"Favorite 3 - Like 3"), u'favorite3.dislikes.one': _(u"Favorite 3 - Dislike 1"), u'favorite3.dislikes.two': _(u"Favorite 3 - Dislike 2"), u'favorite3.dislikes.three': _(u"Favorite 3 - Dislike 3"), u'dislikes.one': _(u"Dislike One"), u'dislikes.two': _(u"Dislike Two"), u'dislikes.three': _(u"Dislike Three"), u'functionality.1': _(u"Functionality 1"), u'functionality.2': _(u"Functionality 2"), u'functionality.3': _(u"Functionality 3"), u'functionality.4': _(u"Functionality 4"), u'functionality.5': _(u"Functionality 5"), u'functionality.6': _(u"Functionality 6"), u'functionality.7': _(u"Functionality 7"), u'functionality.additional': _(u"Functionality Additional"), u'accessory.1': _(u"Accessory One"), u'accessory.2': _(u"Accessory Two"), u'accessory.3': _(u"Accessory Three"), u'accessory.additional': _(u"Accessory Additional"), u'benefit.1': _(u"Benefit One"), u'benefit.2': _(u"Benefit Two"), u'benefit.3': _(u"Benefit Three"), u'benefit.additional': _(u"Benefit Additional"), u'frequency': _(u"Training Frequency"), u'roadwork': _(u"Roadwork"), u'trainingresource.1': _(u"Training Resource 1"), u'trainingresource.2': _(u"Training Resource 2"), u'trainingresource.3': _(u"Training Resource 3"), u'trainingresource.4': _(u"Training Resource 4"), u'trainingresource.5': _(u"Training Resource 5"), u'trainingresource.6': _(u"Training Resource 6"), u'trainingresource.7': _(u"Training Resource 7"), u'trainingresource.8': _(u"Training Resource 8"), u'trainingresource.9': _(u"Training Resource 9"), u'trainingresource.10': _(u"Training Resource 10"), u'trainingresource.additional': _(u"Training Resource Additional"), u'trainingresource.effect': _(u"Training Resource Effect"), u'training.monitor': _(u"Training Monitor"), u'training.personalization': _(u"Training Personalization"), u'training.maxprice': _(u"Training Maximum Price"), u'participant.age': _(u"Participant Age"), u'participant.gender': _(u"Participant Gender"), u'participant.occupation': _(u"Participant Occupation"), u'investment': _(u"Investment Maximum"), u'participant.investment.1': _(u"Participant Investment 1"), u'participant.investment.2': _(u"Participant Investment 2"), u'participant.investment.3': _(u"Participant Investment 3"), u'participant.investment.4': _(u"Participant Investment 4"), u'participant.investment.5': _(u"Participant Investment 5"), u'price.hometrainer': _(u"Price Hometrainer"), u'price.treadmill': _(u"Price Treadmill"), u'price.crosstrainer': _(u"Price Crosstrainer"), u'price.bike': _(u"Price Bike"), u'price.mountainbike': _(u"Price Mountainbike"), u'price.trainer': _(u"Price Personal Trainer"), u'price.club': _(u"Price Club"), u'purchase': _(u"Purchase"), u'purchase.comment': _(u"Purchase Comment"), u'interest.1': _(u"Interest 1"), u'interest.2': _(u"Interest 2"), u'email': _(u"E-Mail")} return fields