Example #1
0
def serialize_receiver(session, receiver, language, data=None):
    """
    Serialize a receiver description

    :param receiver: the receiver to be serialized
    :param language: the language in which to localize data
    :return: a serializtion of the object
    """
    if data is None:
        data = db_prepare_receivers_serialization(session, [receiver])

    user = data['users'][receiver.id]

    ret_dict = {
        'id': receiver.id,
        'username': user.username,
        'name': user.name,
        'state': user.state,
        'configuration': receiver.configuration,
        'can_delete_submission': receiver.can_delete_submission,
        'can_postpone_expiration': receiver.can_postpone_expiration,
        'can_grant_permissions': receiver.can_grant_permissions,
        'picture': data['imgs'].get(user.id, '')
    }

    # description and eventually other localized strings should be taken from user model
    get_localized_values(ret_dict, user, ['description'], language)

    return get_localized_values(ret_dict, receiver, receiver.localized_keys,
                                language)
Example #2
0
def db_serialize_archived_questionnaire_schema(questionnaire_schema, language):
    questionnaire = copy.deepcopy(questionnaire_schema)

    for step in questionnaire:
        for field in step['children']:
            _db_serialize_archived_field_recursively(field, language)

        get_localized_values(step, step, models.Step.localized_keys, language)

    return questionnaire
Example #3
0
def db_serialize_archived_questionnaire_schema(questionnaire_schema, language):
    questionnaire = copy.deepcopy(questionnaire_schema)

    for step in questionnaire:
        for field in step['children']:
            _db_serialize_archived_field_recursively(field, language)

        get_localized_values(step, step, models.Step.localized_keys, language)

    return questionnaire
Example #4
0
def serialize_field_attr(attr, language):
    """
    Serialize a field attribute.
    """
    ret_dict = {
        'id': attr.id,
        'name': attr.name,
        'type': attr.type,
        'value': attr.value
    }

    if attr.type == u'localized':
        get_localized_values(ret_dict, ret_dict, ['value'], language)

    return ret_dict
Example #5
0
def serialize_field_attr(attr, language):
    """
    Serialize a field attribute.
    """
    ret_dict = {
        'id': attr.id,
        'name': attr.name,
        'type': attr.type,
        'value': attr.value
    }

    if attr.type == u'localized':
        get_localized_values(ret_dict, ret_dict, ['value'], language)

    return ret_dict
Example #6
0
def receiver_serialize_receiver(session, tid, receiver, user, language):
    # take only contexts for the current tenant
    contexts = [x[0] for x in session.query(models.ReceiverContext.context_id) \
                                     .filter(models.ReceiverContext.receiver_id == receiver.id,
                                             models.UserTenant.user_id == receiver.id,
                                             models.UserTenant.tenant_id == tid)]

    ret_dict = user_serialize_user(session, user, language)

    ret_dict.update({
        'can_postpone_expiration':
        State.tenant_cache[tid].can_postpone_expiration
        or receiver.can_postpone_expiration,
        'can_delete_submission':
        State.tenant_cache[tid].can_delete_submission
        or receiver.can_delete_submission,
        'can_grant_permissions':
        State.tenant_cache[tid].can_grant_permissions
        or receiver.can_grant_permissions,
        'notification':
        user.notification,
        'contexts':
        contexts
    })

    return get_localized_values(ret_dict, receiver, receiver.localized_keys,
                                language)
Example #7
0
def serialize_questionnaire(session,
                            tid,
                            questionnaire,
                            language,
                            serialize_templates=True):
    """
    Serialize a questionnaire.
    """
    steps = session.query(models.Step).filter(
        models.Step.questionnaire_id == questionnaire.id,
        models.Questionnaire.id == questionnaire.id)

    ret_dict = {
        'id':
        questionnaire.id,
        'editable':
        questionnaire.editable and questionnaire.tid == tid,
        'name':
        questionnaire.name,
        'steps':
        sorted([
            serialize_step(session,
                           tid,
                           s,
                           language,
                           serialize_templates=serialize_templates)
            for s in steps
        ],
               key=lambda x: x['presentation_order'])
    }

    return get_localized_values(ret_dict, questionnaire,
                                questionnaire.localized_keys, language)
Example #8
0
def serialize_context(session, context, language, data=None):
    """
    Serialize context description

    @param context: a valid Storm object
    @return: a dict describing the contexts available for submission,
        (e.g. checks if almost one receiver is associated)
    """
    ret_dict = {
        'id': context.id,
        'presentation_order': context.presentation_order,
        'tip_timetolive': context.tip_timetolive,
        'select_all_receivers': context.select_all_receivers,
        'maximum_selectable_receivers': context.maximum_selectable_receivers,
        'show_context': context.show_context,
        'show_recipients_details': context.show_recipients_details,
        'allow_recipients_selection': context.allow_recipients_selection,
        'show_small_receiver_cards': context.show_small_receiver_cards,
        'enable_comments': context.enable_comments,
        'enable_messages': context.enable_messages,
        'enable_two_way_comments': context.enable_two_way_comments,
        'enable_two_way_messages': context.enable_two_way_messages,
        'enable_attachments': context.enable_attachments,
        'enable_rc_to_wb_files': context.enable_rc_to_wb_files,
        'show_receivers_in_alphabetical_order':
        context.show_receivers_in_alphabetical_order,
        'questionnaire_id': context.questionnaire_id,
        'additional_questionnaire_id': context.additional_questionnaire_id,
        'receivers': data['receivers'].get(context.id, []),
        'picture': data['imgs'].get(context.id, '')
    }

    return get_localized_values(ret_dict, context, context.localized_keys,
                                language)
Example #9
0
def serialize_submission_status(session, status, language):
    """
    Transaction to serialize a submission status

    :param session: An ORM session
    :param status: The status to be serialized
    :param language: The language to be used in the serialization
    :return: The serialized descriptor of the specified status
    """
    submission_status = {
        'id': status.id,
        'order': status.order,
        'substatuses': []
    }

    # See if we have any substatuses we need to serialize
    substatuses = session.query(models.SubmissionSubStatus) \
                         .filter(models.SubmissionSubStatus.tid == status.tid,
                                 models.SubmissionSubStatus.submissionstatus_id == status.id) \
                         .order_by(models.SubmissionSubStatus.order)

    for substatus in substatuses:
        submission_status['substatuses'].append(
            serialize_submission_substatus(substatus, language))

    return get_localized_values(submission_status, status,
                                status.localized_keys, language)
Example #10
0
def serialize_receiver(session, user, language, data=None):
    """
    Serialize a receiver.

    :param session: An ORM session
    :param user: The model to be serialized
    :param language: The language to be used during serialization
    :param data: The dictionary of prefetched resources
    :return: The serialized resource
    """
    if data is None:
        data = db_prepare_receivers_serialization(session, [user])

    ret_dict = {
        'id': user.id,
        'username': user.username,
        'name': user.public_name,
        'state': user.state,
        'encryption': user.crypto_pub_key != '',
        'recipient_configuration': user.recipient_configuration,
        'can_delete_submission': user.can_delete_submission,
        'can_postpone_expiration': user.can_postpone_expiration,
        'can_grant_permissions': user.can_grant_permissions,
        'picture': data['imgs'].get(user.id, '')
    }

    return get_localized_values(ret_dict, user, user.localized_keys, language)
Example #11
0
def serialize_step(session, tid, step, language, serialize_templates=True):
    """
    Serialize a step, localizing its content depending on the language.

    :param step: the step to be serialized.
    :param language: the language in which to localize data
    :return: a serialization of the object
    """
    triggered_by_options = []
    _triggered_by_options = session.query(models.FieldOption).filter(models.FieldOption.trigger_step == step.id)
    for trigger in _triggered_by_options:
        triggered_by_options.append({
            'field': trigger.field_id,
            'option': trigger.id
        })

    children = session.query(models.Field).filter(models.Field.step_id == step.id)

    data = db_prepare_fields_serialization(session, children)

    ret_dict = {
        'id': step.id,
        'questionnaire_id': step.questionnaire_id,
        'presentation_order': step.presentation_order,
        'triggered_by_score': step.triggered_by_score,
        'triggered_by_options': triggered_by_options,
        'children': [serialize_field(
            session, tid, f, language, data, serialize_templates=serialize_templates
        ) for f in children]
    }

    return get_localized_values(ret_dict, step, step.localized_keys, language)
Example #12
0
def serialize_step(session, tid, step, language, serialize_templates=True):
    """
    Serialize a step.
    """
    children = session.query(
        models.Field).filter(models.Field.step_id == step.id)

    data = db_prepare_fields_serialization(session, children)

    children = [
        serialize_field(session,
                        tid,
                        f,
                        language,
                        data,
                        serialize_templates=serialize_templates)
        for f in children
    ]
    children.sort(key=lambda f: (f['y'], f['x']))

    ret_dict = {
        'id': step.id,
        'questionnaire_id': step.questionnaire_id,
        'presentation_order': step.presentation_order,
        'triggered_by_score': step.triggered_by_score,
        'triggered_by_options':
        db_get_triggers_by_type(session, 'step', step.id),
        'children': children
    }

    return get_localized_values(ret_dict, step, step.localized_keys, language)
Example #13
0
def user_serialize_user(session, user, language):
    """
    Serialize user model

    :param user: the user object
    :param language: the language of the data
    :param session: the session on which perform queries.
    :return: a serialization of the object
    """
    picture = db_get_model_img(session, 'users', user.id)

    # take only contexts for the current tenant
    contexts = [x[0] for x in session.query(models.ReceiverContext.context_id)
                                     .filter(models.ReceiverContext.receiver_id == user.id)]
    ret_dict = {
        'id': user.id,
        'username': user.username,
        'password': '',
        'old_password': '',
        'salt': '',
        'role': user.role,
        'state': user.state,
        'last_login': datetime_to_ISO8601(user.last_login),
        'name': user.name,
        'description': user.description,
        'public_name': user.public_name,
        'mail_address': user.mail_address,
        'change_email_address': user.change_email_address,
        'language': user.language,
        'password_change_needed': user.password_change_needed,
        'password_change_date': datetime_to_ISO8601(user.password_change_date),
        'pgp_key_fingerprint': user.pgp_key_fingerprint,
        'pgp_key_public': user.pgp_key_public,
        'pgp_key_expiration': datetime_to_ISO8601(user.pgp_key_expiration),
        'pgp_key_remove': False,
        'picture': picture,
        'can_edit_general_settings': user.can_edit_general_settings,
        'tid': user.tid,
        'notification': user.notification,
        'encryption': user.crypto_pub_key != '',
        'escrow': user.crypto_escrow_prv_key != '',
        'two_factor_enable': user.two_factor_enable,
        'recipient_configuration': user.recipient_configuration,
        'can_postpone_expiration': user.can_postpone_expiration,
        'can_delete_submission': user.can_delete_submission,
        'can_grant_permissions': user.can_grant_permissions,
        'contexts': contexts,
        'send_account_activation_link': False
    }

    if user.tid in State.tenant_cache:
        ret_dict.update({
            'can_postpone_expiration': State.tenant_cache[user.tid].can_postpone_expiration or user.can_postpone_expiration,
            'can_delete_submission': State.tenant_cache[user.tid].can_delete_submission or user.can_delete_submission,
            'can_grant_permissions': State.tenant_cache[user.tid].can_grant_permissions or user.can_grant_permissions
        })

    return get_localized_values(ret_dict, user, user.localized_keys, language)
Example #14
0
def serialize_submission_substatus(row, language):
    """Serializes the submission's substatuses"""
    submission_substatus = {
        'id': row.id,
        'submissionstatus_id': row.submissionstatus_id,
        'presentation_order': row.presentation_order
    }

    return get_localized_values(submission_substatus, row, row.localized_keys, language)
Example #15
0
def serialize_field_attr(attr, language):
    """
    Serialize a field attribute, localizing its content depending on the language.

    :param option: the field attribute object to be serialized
    :param language: the language in which to localize data
    :return: a serialization of the object
    """
    ret_dict = {
        'id': attr.id,
        'name': attr.name,
        'type': attr.type,
        'value': attr.value
    }

    if attr.type == u'localized':
        get_localized_values(ret_dict, ret_dict, ['value'], language)

    return ret_dict
Example #16
0
def serialize_submission_substatus(substatus, language):
    """Serializes the submission's substatuses"""
    submission_substatus = {
        'id': substatus.id,
        'submissionstatus_id': substatus.submissionstatus_id,
        'presentation_order': substatus.presentation_order
    }

    return get_localized_values(submission_substatus, substatus,
                                substatus.localized_keys, language)
Example #17
0
def serialize_field_attr(attr, language):
    """
    Serialize a field attribute.

    :param attr: The option to be serialized
    :param language: The language to be used during serialization
    :return: The serialized resource
    """
    ret_dict = {
        'id': attr.id,
        'name': attr.name,
        'type': attr.type,
        'value': attr.value
    }

    if attr.type == 'localized':
        get_localized_values(ret_dict, ret_dict, ['value'], language)

    return ret_dict
Example #18
0
def serialize_field(session, tid, field, language, data=None, serialize_templates=True):
    """
    Serialize a field, localizing its content depending on the language.

    :param field: the field object to be serialized
    :param language: the language in which to localize data
    :return: a serialization of the object
    """
    if data is None:
        data = db_prepare_fields_serialization(session, [field])

    f_to_serialize = field
    if field.template_override_id is not None and serialize_templates is True:
        f_to_serialize = session.query(models.Field).filter(models.Field.id == field.template_override_id).one_or_none()
    elif field.template_id is not None and serialize_templates is True:
        f_to_serialize = session.query(models.Field).filter(models.Field.id == field.template_id).one_or_none()

    attrs = {}
    for attr in data['attrs'].get(field.id, {}):
        attrs[attr.name] = serialize_field_attr(attr, language)

    triggered_by_options = []
    _triggered_by_options = session.query(models.FieldOption).filter(models.FieldOption.trigger_field == field.id)
    for trigger in _triggered_by_options:
        triggered_by_options.append({
            'field': trigger.field_id,
            'option': trigger.id
        })

    ret_dict = {
        'id': field.id,
        'instance': field.instance,
        'editable': field.editable and field.tid == tid,
        'type': f_to_serialize.type,
        'template_id': field.template_id if field.template_id else '',
        'template_override_id': field.template_override_id if field.template_override_id else '',
        'step_id': field.step_id if field.step_id else '',
        'fieldgroup_id': field.fieldgroup_id if field.fieldgroup_id else '',
        'multi_entry': f_to_serialize.multi_entry,
        'required': field.required,
        'preview': field.preview,
        'encrypt': field.encrypt,
        'attrs': attrs,
        'x': field.x,
        'y': field.y,
        'width': field.width,
        'triggered_by_score': field.triggered_by_score,
        'triggered_by_options': triggered_by_options,
        'options': [serialize_field_option(o, language) for o in data['options'].get(f_to_serialize.id, [])],
        'children': [serialize_field(session, tid, f, language) for f in data['fields'].get(f_to_serialize.id, [])]
    }

    return get_localized_values(ret_dict, field, field.localized_keys, language)
Example #19
0
def admin_serialize_context(session, context, language):
    """
    Serialize the specified context

    :param session: the session on which perform queries
    :param context: The object to be serialized
    :param language: the language in which to localize data.
    :return: a dictionary representing the serialization of the context.
    """
    receivers = [
        r[0] for r in session.query(models.ReceiverContext.receiver_id).filter(
            models.ReceiverContext.context_id == context.id).order_by(
                models.ReceiverContext.order)
    ]

    picture = db_get_model_img(session, 'contexts', context.id)

    ret_dict = {
        'id': context.id,
        'status': context.status,
        'languages': context.languages,
        'tip_timetolive': context.tip_timetolive,
        'select_all_receivers': context.select_all_receivers,
        'maximum_selectable_receivers': context.maximum_selectable_receivers,
        'show_recipients_details': context.show_recipients_details,
        'allow_recipients_selection': context.allow_recipients_selection,
        'show_small_receiver_cards': context.show_small_receiver_cards,
        'enable_comments': context.enable_comments,
        'enable_messages': context.enable_messages,
        'enable_two_way_comments': context.enable_two_way_comments,
        'enable_two_way_messages': context.enable_two_way_messages,
        'enable_attachments': context.enable_attachments,
        'enable_rc_to_wb_files': context.enable_rc_to_wb_files,
        'score_threshold_medium': context.score_threshold_medium,
        'score_threshold_high': context.score_threshold_high,
        'score_receipt_text_custom': context.score_receipt_text_custom,
        'score_receipt_text_l': context.score_receipt_text_l,
        'score_receipt_text_m': context.score_receipt_text_m,
        'score_receipt_text_h': context.score_receipt_text_h,
        'score_threshold_receipt': context.score_threshold_receipt,
        'order': context.order,
        'show_receivers_in_alphabetical_order':
        context.show_receivers_in_alphabetical_order,
        'show_steps_navigation_interface':
        context.show_steps_navigation_interface,
        'questionnaire_id': context.questionnaire_id,
        'additional_questionnaire_id': context.additional_questionnaire_id,
        'receivers': receivers,
        'picture': picture
    }

    return get_localized_values(ret_dict, context, context.localized_keys,
                                language)
Example #20
0
def _db_serialize_archived_field_recursively(field, language):
    for key, _ in field.get('attrs', {}).items():
        if key not in field['attrs']:
            continue

        if 'type' not in field['attrs'][key]:
            continue

        if field['attrs'][key]['type'] == u'localized':
            if language in field['attrs'][key].get('value', []):
                field['attrs'][key]['value'] = field['attrs'][key]['value'][language]
            else:
                field['attrs'][key]['value'] = ""

    for o in field.get('options', []):
        get_localized_values(o, o, models.FieldOption.localized_keys, language)

    for c in field.get('children', []):
        _db_serialize_archived_field_recursively(c, language)

    return get_localized_values(field, field, models.Field.localized_keys, language)
Example #21
0
def _db_serialize_archived_field_recursively(field, language):
    for key, _ in field.get('attrs', {}).items():
        if key not in field['attrs']:
            continue

        if 'type' not in field['attrs'][key]:
            continue

        if field['attrs'][key]['type'] == 'localized':
            if language in field['attrs'][key].get('value', []):
                field['attrs'][key]['value'] = field['attrs'][key]['value'][language]
            else:
                field['attrs'][key]['value'] = ""

    for o in field.get('options', []):
        get_localized_values(o, o, models.FieldOption.localized_keys, language)

    for c in field.get('children', []):
        _db_serialize_archived_field_recursively(c, language)

    return get_localized_values(field, field, models.Field.localized_keys, language)
Example #22
0
def serialize_field_option(option, language):
    """
    Serialize a field option.
    """
    ret_dict = {
        'id': option.id,
        'presentation_order': option.presentation_order,
        'score_points': option.score_points,
        'score_type': option.score_type,
        'trigger_receiver': option.trigger_receiver
    }

    return get_localized_values(ret_dict, option, option.localized_keys, language)
Example #23
0
def serialize_field_option(option, language):
    """
    Serialize a field option.
    """
    ret_dict = {
        'id': option.id,
        'presentation_order': option.presentation_order,
        'score_points': option.score_points,
        'score_type': option.score_type,
        'trigger_receiver': option.trigger_receiver
    }

    return get_localized_values(ret_dict, option, option.localized_keys,
                                language)
def serialize_submission_substatus(substatus, language):
    """
    Transaction to serialize a submission substatus

    :param substatus: The status to be serialized
    :param language: The language to be used in the serialization
    :return: The serialized descriptor of the specified status
    """
    submission_substatus = {
        'id': substatus.id,
        'submissionstatus_id': substatus.submissionstatus_id,
        'order': substatus.order
    }

    return get_localized_values(submission_substatus, substatus, substatus.localized_keys, language)
Example #25
0
def serialize_questionnaire(session, tid, questionnaire, language, serialize_templates=True):
    """
    Serialize a questionnaire.
    """
    steps = session.query(models.Step).filter(models.Step.questionnaire_id == questionnaire.id,
                                              models.Questionnaire.id == questionnaire.id)

    ret_dict = {
        'id': questionnaire.id,
        'editable': questionnaire.editable and questionnaire.tid == tid,
        'name': questionnaire.name,
        'steps': sorted([serialize_step(session, tid, s, language, serialize_templates=serialize_templates) for s in steps],
                        key=lambda x: x['presentation_order'])
    }

    return get_localized_values(ret_dict, questionnaire, questionnaire.localized_keys, language)
Example #26
0
def serialize_field_option(option, language):
    """
    Serialize a field option, localizing its content depending on the language.

    :param option: the field option object to be serialized
    :param language: the language in which to localize data
    :return: a serialization of the object
    """
    ret_dict = {
        'id': option.id,
        'presentation_order': option.presentation_order,
        'score_points': option.score_points,
        'trigger_field': option.trigger_field if option.trigger_field else ''
    }

    return get_localized_values(ret_dict, option, option.localized_keys,
                                language)
Example #27
0
def admin_serialize_context(session, context, language):
    """
    Serialize the specified context

    :param session: the session on which perform queries.
    :param language: the language in which to localize data.
    :return: a dictionary representing the serialization of the context.
    """
    receivers = [r[0] for r in session.query(models.ReceiverContext.receiver_id)
                                      .filter(models.ReceiverContext.context_id == context.id)
                                      .order_by(models.ReceiverContext.presentation_order)]
    picture = db_get_model_img(session, 'contexts', context.id)

    ret_dict = {
        'id': context.id,
        'status': context.status,
        'tip_timetolive': context.tip_timetolive,
        'select_all_receivers': context.select_all_receivers,
        'maximum_selectable_receivers': context.maximum_selectable_receivers,
        'show_recipients_details': context.show_recipients_details,
        'allow_recipients_selection': context.allow_recipients_selection,
        'show_small_receiver_cards': context.show_small_receiver_cards,
        'enable_comments': context.enable_comments,
        'enable_messages': context.enable_messages,
        'enable_two_way_comments': context.enable_two_way_comments,
        'enable_two_way_messages': context.enable_two_way_messages,
        'enable_attachments': context.enable_attachments,
        'enable_rc_to_wb_files': context.enable_rc_to_wb_files,
        'enable_scoring_system': context.enable_scoring_system,
        'score_threshold_medium': context.score_threshold_medium,
        'score_threshold_high': context.score_threshold_high,
        'score_receipt_text_custom': context.score_receipt_text_custom,
        'score_receipt_text_l': context.score_receipt_text_l,
        'score_receipt_text_m': context.score_receipt_text_m,
        'score_receipt_text_h': context.score_receipt_text_h,
        'score_threshold_receipt': context.score_threshold_receipt,
        'presentation_order': context.presentation_order,
        'show_receivers_in_alphabetical_order': context.show_receivers_in_alphabetical_order,
        'show_steps_navigation_interface': context.show_steps_navigation_interface,
        'questionnaire_id': context.questionnaire_id,
        'additional_questionnaire_id': context.additional_questionnaire_id,
        'receivers': receivers,
        'picture': picture
    }

    return get_localized_values(ret_dict, context, context.localized_keys, language)
Example #28
0
def serialize_context(session, context, language, data=None):
    """
    Serialize a context.

    :param session: An ORM session
    :param context: The context to be serialized
    :param language: The language to be used during serialization
    :param data: The dictionary of prefetched resources
    """
    ret_dict = {
        'id': context.id,
        'status': context.status,
        'order': context.order,
        'languages': context.languages,
        'tip_timetolive': context.tip_timetolive,
        'select_all_receivers': context.select_all_receivers,
        'maximum_selectable_receivers': context.maximum_selectable_receivers,
        'show_recipients_details': context.show_recipients_details,
        'allow_recipients_selection': context.allow_recipients_selection,
        'show_small_receiver_cards': context.show_small_receiver_cards,
        'enable_comments': context.enable_comments,
        'enable_messages': context.enable_messages,
        'enable_two_way_comments': context.enable_two_way_comments,
        'enable_two_way_messages': context.enable_two_way_messages,
        'enable_attachments': context.enable_attachments,
        'enable_rc_to_wb_files': context.enable_rc_to_wb_files,
        'score_threshold_medium': context.score_threshold_medium,
        'score_threshold_high': context.score_threshold_high,
        'score_receipt_text_custom': context.score_receipt_text_custom,
        'score_receipt_text_l': context.score_receipt_text_l,
        'score_receipt_text_m': context.score_receipt_text_m,
        'score_receipt_text_h': context.score_receipt_text_h,
        'score_threshold_receipt': context.score_threshold_receipt,
        'show_receivers_in_alphabetical_order':
        context.show_receivers_in_alphabetical_order,
        'show_steps_navigation_interface':
        context.show_steps_navigation_interface,
        'questionnaire_id': context.questionnaire_id,
        'additional_questionnaire_id': context.additional_questionnaire_id,
        'receivers': data['receivers'].get(context.id, []),
        'picture': data['imgs'].get(context.id, '')
    }

    return get_localized_values(ret_dict, context, context.localized_keys,
                                language)
Example #29
0
def serialize_field_option(option, language):
    """
    Serialize a field option.

    :param option: The option to be serialized
    :param language: The language to be used during serialization
    :return: The serialized resource
    """
    ret = {
        'id': option.id,
        'order': option.order,
        'block_submission': option.block_submission,
        'score_points': option.score_points,
        'score_type': option.score_type,
        'trigger_receiver': option.trigger_receiver
    }

    return get_localized_values(ret, option, option.localized_keys, language)
Example #30
0
def admin_serialize_receiver(session, receiver, user, language):
    """
    Serialize the specified receiver

    :param language: the language in which to localize data
    :return: a dictionary representing the serialization of the receiver
    """
    ret_dict = user_serialize_user(session, user, language)

    ret_dict.update({
        'can_delete_submission': receiver.can_delete_submission,
        'can_postpone_expiration': receiver.can_postpone_expiration,
        'can_grant_permissions': receiver.can_grant_permissions,
        'mail_address': user.mail_address,
        'configuration': receiver.configuration
    })

    return get_localized_values(ret_dict, receiver, receiver.localized_keys, language)
Example #31
0
def user_serialize_user(session, user, language):
    """
    Serialize user description

    :param user:
    :param language:
    :param session: the session on which perform queries.
    :return: a serialization of the object
    """
    picture = db_get_model_img(session, 'users', user.id)

    ret_dict = {
        'id': user.id,
        'username': user.username,
        'password': '',
        'old_password': '',
        'salt': '',
        'role': user.role,
        'state': user.state,
        'last_login': datetime_to_ISO8601(user.last_login),
        'name': user.name,
        'description': user.description,
        'mail_address': user.mail_address,
        'change_email_address': user.change_email_address,
        'language': user.language,
        'password_change_needed': user.password_change_needed,
        'password_change_date': datetime_to_ISO8601(user.password_change_date),
        'pgp_key_fingerprint': user.pgp_key_fingerprint,
        'pgp_key_public': user.pgp_key_public,
        'pgp_key_expiration': datetime_to_ISO8601(user.pgp_key_expiration),
        'pgp_key_remove': False,
        'picture': picture,
        'can_edit_general_settings': user.can_edit_general_settings,
        'can_delete_submission': user.can_delete_submission,
        'can_postpone_expiration': user.can_postpone_expiration,
        'can_grant_permissions': user.can_grant_permissions,
        'recipient_configuration': user.recipient_configuration,
        'tid': user.tid,
        'notification': user.notification,
        'encryption': user.crypto_pub_key != b'',
        'two_factor_enable': user.two_factor_enable
    }

    return get_localized_values(ret_dict, user, user.localized_keys, language)
Example #32
0
def serialize_field(session, tid, field, language, data=None, serialize_templates=True):
    """
    Serialize a field.
    """
    if data is None:
        data = db_prepare_fields_serialization(session, [field])

    f_to_serialize = field
    if field.template_override_id is not None and serialize_templates is True:
        f_to_serialize = session.query(models.Field).filter(models.Field.id == field.template_override_id).one_or_none()
    elif field.template_id is not None and serialize_templates is True:
        f_to_serialize = session.query(models.Field).filter(models.Field.id == field.template_id).one_or_none()

    attrs = {}
    for attr in data['attrs'].get(field.id, {}):
        attrs[attr.name] = serialize_field_attr(attr, language)

    children = [serialize_field(session, tid, f, language) for f in data['fields'].get(f_to_serialize.id, [])]
    children.sort(key=lambda f:(f['y'], f['x']))

    ret_dict = {
        'id': field.id,
        'instance': field.instance,
        'editable': field.editable and field.tid == tid,
        'type': f_to_serialize.type,
        'template_id': field.template_id if field.template_id else '',
        'template_override_id': field.template_override_id if field.template_override_id else '',
        'step_id': field.step_id if field.step_id else '',
        'fieldgroup_id': field.fieldgroup_id if field.fieldgroup_id else '',
        'multi_entry': f_to_serialize.multi_entry,
        'required': field.required,
        'preview': field.preview,
        'encrypt': field.encrypt,
        'attrs': attrs,
        'x': field.x,
        'y': field.y,
        'width': field.width,
        'triggered_by_score': field.triggered_by_score,
        'triggered_by_options': db_get_triggers_by_type(session, 'field', field.id),
        'options': [serialize_field_option(o, language) for o in data['options'].get(f_to_serialize.id, [])],
        'children': children
    }

    return get_localized_values(ret_dict, field, field.localized_keys, language)
Example #33
0
def user_serialize_user(session, user, language):
    """
    Serialize user description

    :param session: the session on which perform queries.
    :param username: the username of the user to be serialized
    :return: a serialization of the object
    """
    picture = db_get_model_img(session, 'users', user.id)
    user_tenants = db_get_usertenant_associations(session, user)

    ret_dict = {
        'id': user.id,
        'username': user.username,
        'password': '',
        'old_password': u'',
        'salt': '',
        'role': user.role,
        'state': user.state,
        'last_login': datetime_to_ISO8601(user.last_login),
        'name': user.name,
        'description': user.description,
        'mail_address': user.mail_address,
        'change_email_address': user.change_email_address,
        'language': user.language,
        'password_change_needed': user.password_change_needed,
        'password_change_date': datetime_to_ISO8601(user.password_change_date),
        'pgp_key_fingerprint': user.pgp_key_fingerprint,
        'pgp_key_public': user.pgp_key_public,
        'pgp_key_expiration': datetime_to_ISO8601(user.pgp_key_expiration),
        'pgp_key_remove': False,
        'picture': picture,
        'can_edit_general_settings': user.can_edit_general_settings,
        'can_delete_submission': user.can_delete_submission,
        'can_postpone_expiration': user.can_postpone_expiration,
        'can_grant_permissions': user.can_grant_permissions,
        'recipient_configuration': user.recipient_configuration,
        'tid': user.tid,
        'notification': user.notification,
        'usertenant_assocations': user_tenants
    }

    return get_localized_values(ret_dict, user, user.localized_keys, language)
Example #34
0
def serialize_submission_status(session, status, language):
    """Serializes a submission status into dictionary form for the client"""
    submission_status = {
        'id': status.id,
        'presentation_order': status.presentation_order,
        'substatuses': []
    }

    # See if we have any substatuses we need to serialize
    substatuses = session.query(models.SubmissionSubStatus) \
                         .filter(models.SubmissionSubStatus.tid == status.tid,
                                 models.SubmissionSubStatus.submissionstatus_id == status.id) \
                        .order_by(models.SubmissionSubStatus.presentation_order)

    for substatus in substatuses:
        submission_status['substatuses'].append(
            serialize_submission_substatus(substatus, language))

    return get_localized_values(submission_status, status,
                                status.localized_keys, language)
Example #35
0
def serialize_receiver(session, user, language, data=None):
    """
    Serialize a receiver.
    """
    if data is None:
        data = db_prepare_receivers_serialization(session, [user])

    ret_dict = {
        'id': user.id,
        'username': user.username,
        'name': user.name,
        'state': user.state,
        'recipient_configuration': user.recipient_configuration,
        'can_delete_submission': user.can_delete_submission,
        'can_postpone_expiration': user.can_postpone_expiration,
        'can_grant_permissions': user.can_grant_permissions,
        'picture': data['imgs'].get(user.id, '')
    }

    return get_localized_values(ret_dict, user, user.localized_keys, language)
Example #36
0
def serialize_receiver(session, user, language, data=None):
    """
    Serialize a receiver.
    """
    if data is None:
        data = db_prepare_receivers_serialization(session, [user])

    ret_dict = {
        'id': user.id,
        'username': user.username,
        'name': user.name,
        'state': user.state,
        'recipient_configuration': user.recipient_configuration,
        'can_delete_submission': user.can_delete_submission,
        'can_postpone_expiration': user.can_postpone_expiration,
        'can_grant_permissions': user.can_grant_permissions,
        'picture': data['imgs'].get(user.id, '')
    }

    return get_localized_values(ret_dict, user, user.localized_keys, language)
Example #37
0
def serialize_questionnaire(session, tid, questionnaire, language, serialize_templates=True):
    """
    Serialize the specified questionnaire

    :param session: the session on which perform queries.
    :param language: the language in which to localize data.
    :return: a dictionary representing the serialization of the questionnaire.
    """
    steps = session.query(models.Step).filter(models.Step.questionnaire_id == questionnaire.id,
                                              models.Questionnaire.id == questionnaire.id)

    ret_dict = {
        'id': questionnaire.id,
        'editable': questionnaire.editable and questionnaire.tid == tid,
        'name': questionnaire.name,
        'steps': sorted([serialize_step(session, tid, s, language, serialize_templates=serialize_templates) for s in steps],
                        key=lambda x: x['presentation_order'])
    }

    return get_localized_values(ret_dict, questionnaire, questionnaire.localized_keys, language)
Example #38
0
def serialize_step(session, tid, step, language, serialize_templates=True):
    """
    Serialize a step.
    """
    children = session.query(models.Field).filter(models.Field.step_id == step.id)

    data = db_prepare_fields_serialization(session, children)

    children = [serialize_field(session, tid, f, language, data, serialize_templates=serialize_templates) for f in children]
    children.sort(key=lambda f:(f['y'], f['x']))

    ret_dict = {
        'id': step.id,
        'questionnaire_id': step.questionnaire_id,
        'presentation_order': step.presentation_order,
        'triggered_by_score': step.triggered_by_score,
        'triggered_by_options': db_get_triggers_by_type(session, 'step', step.id),
        'children': children
    }

    return get_localized_values(ret_dict, step, step.localized_keys, language)
Example #39
0
def serialize_submission_status(session, row, language):
    """Serializes a submission status into dictionary form for the client"""
    submission_status = {
        'id': row.id,
        'system_defined': row.system_defined,
        'system_usage': row.system_usage,
        'presentation_order': row.presentation_order,
        'substatuses': []
    }

    # See if we have any substatuses we need to serialize
    substatus_rows = session.query(models.SubmissionSubStatus) \
                            .filter(models.SubmissionSubStatus.submissionstatus_id == row.id) \
                            .order_by(models.SubmissionSubStatus.presentation_order)

    for substatus_row in substatus_rows:
        submission_status['substatuses'].append(
            serialize_submission_substatus(substatus_row, language)
        )

    return get_localized_values(submission_status, row, row.localized_keys, language)
Example #40
0
def user_serialize_user(session, user, language):
    """
    Serialize user description

    :param session: the session on which perform queries.
    :param username: the username of the user to be serialized
    :return: a serialization of the object
    """
    picture = db_get_model_img(session, 'users', user.id)
    user_tenants = db_get_usertenant_associations(session, user)

    ret_dict = {
        'id': user.id,
        'username': user.username,
        'password': '',
        'old_password': u'',
        'salt': '',
        'role': user.role,
        'state': user.state,
        'last_login': datetime_to_ISO8601(user.last_login),
        'name': user.name,
        'description': user.description,
        'mail_address': user.mail_address,
        'change_email_address': user.change_email_address,
        'language': user.language,
        'password_change_needed': user.password_change_needed,
        'password_change_date': datetime_to_ISO8601(user.password_change_date),
        'pgp_key_fingerprint': user.pgp_key_fingerprint,
        'pgp_key_public': user.pgp_key_public,
        'pgp_key_expiration': datetime_to_ISO8601(user.pgp_key_expiration),
        'pgp_key_remove': False,
        'picture': picture,
        'can_edit_general_settings': user.can_edit_general_settings,
        'tid': user.tid,
        'notification': user.notification,
        'usertenant_assocations': user_tenants
    }

    return get_localized_values(ret_dict, user, user.localized_keys, language)
Example #41
0
def serialize_questionnaire(session,
                            tid,
                            questionnaire,
                            language,
                            serialize_templates=True):
    """
    Serialize a questionnaire.

    :param session: An ORM session
    :param tid: A tenant ID
    :param questionnaire: A questionnaire model
    :param language: The language to be used during serialization
    :param serialize_templates: A boolean to require template serialization
    :return: The serialized resource
    """
    steps = session.query(models.Step).filter(models.Step.questionnaire_id == questionnaire.id,
                                              models.Questionnaire.id == questionnaire.id) \
                                      .order_by(models.Step.order)

    ret = {
        'id':
        questionnaire.id,
        'editable':
        questionnaire.id not in default_questionnaires
        and questionnaire.tid == tid,
        'name':
        questionnaire.name,
        'steps': [
            serialize_step(session,
                           tid,
                           s,
                           language,
                           serialize_templates=serialize_templates)
            for s in steps
        ]
    }

    return get_localized_values(ret, questionnaire,
                                questionnaire.localized_keys, language)
Example #42
0
def serialize_step(session, tid, step, language, serialize_templates=True):
    """
    Serialize a step.

    :param session: An ORM session
    :param tid: A tenant ID
    :param step: The option to be serialized
    :param language: The language to be used during serialization
    :param serialize_templates: A boolean to require template serialization
    :return: The serialized resource
    """
    children = session.query(
        models.Field).filter(models.Field.step_id == step.id)

    data = db_prepare_fields_serialization(session, children)

    children = [
        serialize_field(session,
                        tid,
                        f,
                        language,
                        data,
                        serialize_templates=serialize_templates)
        for f in children
    ]
    children.sort(key=lambda f: (f['y'], f['x']))

    ret_dict = {
        'id': step.id,
        'questionnaire_id': step.questionnaire_id,
        'order': step.order,
        'triggered_by_score': step.triggered_by_score,
        'triggered_by_options':
        db_get_triggers_by_type(session, 'step', step.id),
        'children': children
    }

    return get_localized_values(ret_dict, step, step.localized_keys, language)
Example #43
0
def serialize_context(session, context, language, data=None):
    """
    Serialize a context.
    """
    ret_dict = {
        'id': context.id,
        'status': context.status,
        'presentation_order': context.presentation_order,
        'tip_timetolive': context.tip_timetolive,
        'select_all_receivers': context.select_all_receivers,
        'maximum_selectable_receivers': context.maximum_selectable_receivers,
        'show_recipients_details': context.show_recipients_details,
        'allow_recipients_selection': context.allow_recipients_selection,
        'show_small_receiver_cards': context.show_small_receiver_cards,
        'enable_comments': context.enable_comments,
        'enable_messages': context.enable_messages,
        'enable_two_way_comments': context.enable_two_way_comments,
        'enable_two_way_messages': context.enable_two_way_messages,
        'enable_attachments': context.enable_attachments,
        'enable_rc_to_wb_files': context.enable_rc_to_wb_files,
        'enable_scoring_system': context.enable_scoring_system,
        'score_threshold_medium': context.score_threshold_medium,
        'score_threshold_high': context.score_threshold_high,
        'score_receipt_text_custom': context.score_receipt_text_custom,
        'score_receipt_text_l': context.score_receipt_text_l,
        'score_receipt_text_m': context.score_receipt_text_m,
        'score_receipt_text_h': context.score_receipt_text_h,
        'score_threshold_receipt': context.score_threshold_receipt,
        'show_receivers_in_alphabetical_order': context.show_receivers_in_alphabetical_order,
        'show_steps_navigation_interface': context.show_steps_navigation_interface,
        'questionnaire_id': context.questionnaire_id,
        'additional_questionnaire_id': context.additional_questionnaire_id,
        'receivers': data['receivers'].get(context.id, []),
        'picture': data['imgs'].get(context.id, '')
    }

    return get_localized_values(ret_dict, context, context.localized_keys, language)