def _load(self): logger.debug('finding eq_session_id in database', eq_session_id=self.eq_session_id) self._eq_session = data_access.get_by_key(EQSession, self.eq_session_id) if self._eq_session: self.user_id = self._eq_session.user_id if self._eq_session.session_data: encrypted_session_data = self._eq_session.session_data session_data = StorageEncryption(self.user_id, self.user_ik, self.pepper)\ .decrypt_data(encrypted_session_data) # for backwards compatibility # session data used to be base64 encoded before encryption try: session_data = base64url_decode( session_data.decode()).decode() except ValueError: pass self.session_data = json.loads( session_data, object_hook=lambda d: SessionData(**d)) logger.debug( 'found matching eq_session for eq_session_id in database', session_id=self._eq_session.eq_session_id, user_id=self._eq_session.user_id) else: logger.debug('eq_session_id not found in database', eq_session_id=self.eq_session_id) return self._eq_session
def download_pdf(schema, eq_id, form_type): session_data = get_session_store().session_data if _is_submission_viewable(schema.json, session_data.submitted_time): submitted_data = data_access.get_by_key(SubmittedResponse, session_data.tx_id) if submitted_data: filename = '{}_{}.pdf'.format(eq_id, form_type) html = _render_submission_page(session_data, submitted_data, schema, eq_id, form_type) css = """ .header__main { background: none !important; } .header__title { color: black !important; } """ return render_pdf(HTML(string=html), [CSS(string=css)], filename) return redirect( url_for('post_submission.get_thank_you', eq_id=eq_id, form_type=form_type))
def get_view_submission(schema, eq_id, form_type): # pylint: disable=unused-argument session_data = get_session_store().session_data if _is_submission_viewable(schema.json, session_data.submitted_time): submitted_data = data_access.get_by_key(SubmittedResponse, session_data.tx_id) if submitted_data: metadata_context = build_metadata_context_for_survey_completed( session_data) pepper = current_app.eq['secret_store'].get_secret_by_name( 'EQ_SERVER_SIDE_STORAGE_ENCRYPTION_USER_PEPPER') encrypter = StorageEncryption(current_user.user_id, current_user.user_ik, pepper) submitted_data = json.loads( encrypter.decrypt_data(submitted_data.data)) answer_store = AnswerStore( existing_answers=submitted_data.get('answers')) metadata = submitted_data.get('metadata') routing_path = PathFinder(schema, answer_store, metadata, []).get_full_routing_path() schema_context = _get_schema_context(routing_path, 0, metadata, answer_store, schema) rendered_schema = renderer.render(schema.json, **schema_context) summary_rendered_context = build_summary_rendering_context( schema, rendered_schema['sections'], answer_store, metadata) context = { 'summary': { 'groups': summary_rendered_context, 'answers_are_editable': False, 'is_view_submission_response_enabled': is_view_submitted_response_enabled(schema.json), }, 'variables': None, } return render_theme_template( schema.json['theme'], template_name='view-submission.html', metadata=metadata_context, analytics_ua_id=current_app.config['EQ_UA_ID'], survey_id=schema.json['survey_id'], survey_title=TemplateRenderer.safe_content( schema.json['title']), content=context) return redirect( url_for('post_submission.get_thank_you', eq_id=eq_id, form_type=form_type))
def _load(self): logger.debug('finding eq_session_id in database', eq_session_id=self.eq_session_id) self._eq_session = data_access.get_by_key(EQSession, self.eq_session_id) if self._eq_session: self.user_id = self._eq_session.user_id if self._eq_session.session_data: encrypted_session_data = self._eq_session.session_data session_data = StorageEncryption(self.user_id, self.user_ik, self.pepper)\ .decrypt_data(encrypted_session_data) self.session_data = json.loads( session_data, object_hook=lambda d: SessionData(**d)) logger.debug( 'found matching eq_session for eq_session_id in database', session_id=self._eq_session.eq_session_id, user_id=self._eq_session.user_id) else: logger.debug('eq_session_id not found in database', eq_session_id=self.eq_session_id) return self._eq_session
def test_get_by_key(self): dynamo_item = QuestionnaireState(USER_ID, STATE_DATA, VERSION) with mock.patch('app.storage.dynamodb_api.get_by_key', return_value=dynamo_item) as get_by_key: model = data_access.get_by_key(QuestionnaireState, USER_ID) self.assertEqual(get_by_key.call_args[0][0], QuestionnaireState) self.assertEqual(get_by_key.call_args[0][1], USER_ID) self.assertEqual(model.user_id, USER_ID) self.assertEqual(model.state_data, STATE_DATA) self.assertEqual(model.version, VERSION)
def test_get_by_key_rds_fallback(self): rds_model = models.QuestionnaireState(USER_ID, STATE_DATA, VERSION) with mock.patch('app.storage.dynamo_api.get_item', return_value=None), \ mock.patch.object(models.QuestionnaireState, 'query') as query: query.filter_by.return_value.first.return_value = rds_model model = data_access.get_by_key(QuestionnaireState, USER_ID) self.assertTrue(query.filter_by.return_value.first.called) self.assertEqual(model.user_id, USER_ID) self.assertEqual(model.state_data, STATE_DATA) self.assertEqual(model.version, VERSION) self.assertFalse(getattr(model, '_use_dynamo'))
def get_view_submission(schema, eq_id, form_type): # pylint: disable=unused-argument, too-many-locals session_data = get_session_store().session_data if _is_submission_viewable(schema.json, session_data.submitted_time): submitted_data = data_access.get_by_key(SubmittedResponse, session_data.tx_id) if submitted_data: return _render_submission_page(session_data, submitted_data, schema, eq_id, form_type) return redirect( url_for('post_submission.get_thank_you', eq_id=eq_id, form_type=form_type))
def test_get_by_key(self): dynamo_item = { 'user_id': USER_ID, 'state_data': STATE_DATA, 'version': VERSION } with mock.patch('app.storage.dynamo_api.get_item', return_value=dynamo_item) as get_item: model = data_access.get_by_key(QuestionnaireState, USER_ID) self.assertEqual(get_item.call_args[0][1], {'user_id': USER_ID}) self.assertEqual(model.user_id, USER_ID) self.assertEqual(model.state_data, STATE_DATA) self.assertEqual(model.version, VERSION) self.assertTrue(getattr(model, '_use_dynamo'))
def _find_questionnaire_state(self): logger.debug('getting questionnaire data', user_id=self._user_id) return data_access.get_by_key(QuestionnaireState, self._user_id)
def test_by_key_no_sql(self): model = data_access.get_by_key(SubmittedResponse, USER_ID) self.assertIsNone(model)
def get_view_submission(schema, eq_id, form_type): # pylint: disable=unused-argument, too-many-locals session_data = get_session_store().session_data if _is_submission_viewable(schema.json, session_data.submitted_time): submitted_data = data_access.get_by_key(SubmittedResponse, session_data.tx_id) if submitted_data: metadata_context = build_metadata_context_for_survey_completed( session_data) pepper = current_app.eq['secret_store'].get_secret_by_name( 'EQ_SERVER_SIDE_STORAGE_ENCRYPTION_USER_PEPPER') encrypter = StorageEncryption(current_user.user_id, current_user.user_ik, pepper) submitted_data = encrypter.decrypt_data(submitted_data.data) # for backwards compatibility # submitted data used to be base64 encoded before encryption try: submitted_data = base64url_decode( submitted_data.decode()).decode() except ValueError: pass submitted_data = json.loads(submitted_data) answer_store = AnswerStore(submitted_data.get('answers')) metadata = submitted_data.get('metadata') collection_metadata = submitted_data.get('collection_metadata') routing_path = PathFinder(schema, answer_store, metadata, []).get_full_routing_path() schema_context = _get_schema_context(routing_path, None, metadata, collection_metadata, answer_store, schema) section_list = schema.json['sections'] summary_rendered_context = build_summary_rendering_context( schema, section_list, answer_store, metadata, schema_context) context = { 'summary': { 'groups': summary_rendered_context, 'answers_are_editable': False, 'is_view_submission_response_enabled': is_view_submitted_response_enabled(schema.json), }, 'variables': None, } return render_theme_template( schema.json['theme'], template_name='view-submission.html', metadata=metadata_context, analytics_ua_id=current_app.config['EQ_UA_ID'], survey_id=schema.json['survey_id'], survey_title=TemplateRenderer.safe_content( schema.json['title']), account_service_url=cookie_session.get('account_service_url'), account_service_log_out_url=cookie_session.get( 'account_service_log_out_url'), content=context) return redirect( url_for('post_submission.get_thank_you', eq_id=eq_id, form_type=form_type))