Exemplo n.º 1
0
def update_submission(store, submission_id, request, finalize, language=GLSetting.memory_copy.default_language):

    context = store.find(Context, Context.id == unicode(request['context_id'])).one()
    if not context:
        log.err("Context requested: [%s] not found!" % request['context_id'])
        raise errors.ContextIdNotFound

    submission = store.find(InternalTip, InternalTip.id == unicode(submission_id)).one()
    if not submission:
        log.err("Invalid Submission requested %s in PUT" % submission_id)
        raise errors.SubmissionIdNotFound

    # this may happen if a submission try to update a context
    if submission.context_id != context.id:
        log.err("Can't be changed context in a submission update")
        raise errors.ContextIdNotFound("Context are immutable")

    if submission.mark != InternalTip._marker[0]:
        log.err("Submission %s do not permit update (status %s)" % (submission_id, submission.mark))
        raise errors.SubmissionConcluded

    try:
        import_files(store, submission, request['files'], finalize)
    except Exception as excep:
        log.err("Submission update: files import fail: %s" % excep)
        log.exception(excep)
        raise excep

    try:
        wb_fields = request['wb_fields']
        fo = Fields(context.localized_fields, context.unique_fields)
        fo.validate_fields(wb_fields, language, strict_validation=finalize)
        submission.wb_fields = wb_fields
    except Exception as excep:
        log.err("Submission update: fields validation fail: %s" % excep)
        log.exception(excep)
        raise excep

    try:
        import_receivers(store, submission, request['receivers'], required=finalize)
    except Exception as excep:
        log.err("Submission update: receiver import fail: %s" % excep)
        log.exception(excep)
        raise excep

    if finalize:
        submission.mark = InternalTip._marker[1] # Finalized

    submission_dict = wb_serialize_internaltip(submission)
    return submission_dict
Exemplo n.º 2
0
def create_submission(store, request, finalize, language=GLSetting.memory_copy.default_language):
    context = store.find(Context, Context.id == unicode(request['context_id'])).one()
    if not context:
        log.err("Context requested: [%s] not found!" % request['context_id'])
        raise errors.ContextIdNotFound

    submission = InternalTip()

    submission.escalation_threshold = context.escalation_threshold
    submission.access_limit = context.tip_max_access
    submission.download_limit = context.file_max_download
    submission.expiration_date = utc_future_date(seconds=context.tip_timetolive)
    submission.pertinence_counter = 0
    submission.context_id = context.id
    submission.creation_date = datetime_now()

    if finalize:
        submission.mark = InternalTip._marker[1] # Finalized
    else:
        submission.mark = InternalTip._marker[0] # Submission

    try:
        store.add(submission)
    except Exception as excep:
        log.err("Storm/SQL Error: %s (create_submission)" % excep)
        raise errors.InternalServerError("Unable to commit on DB")

    try:
        import_files(store, submission, request['files'], finalize)
    except Exception as excep:
        log.err("Submission create: files import fail: %s" % excep)
        raise excep

    try:
        wb_fields = request['wb_fields']
        fo = Fields(context.localized_fields, context.unique_fields)
        fo.validate_fields(wb_fields, language, strict_validation=finalize)
        submission.wb_fields = wb_fields
    except Exception as excep:
        log.err("Submission create: fields validation fail: %s" % excep)
        raise excep

    try:
        import_receivers(store, submission, request['receivers'], required=finalize)
    except Exception as excep:
        log.err("Submission create: receivers import fail: %s" % excep)
        raise excep

    submission_dict = wb_serialize_internaltip(submission)
    return submission_dict
Exemplo n.º 3
0
    def _transact_ro_add_context(self, store):
        c = self.localization_set(self.dummyContext, Context, 'en')
        context = Context(c)

        fo = Fields()
        fo.update_fields('en', self.dummyContext['fields'])
        fo.context_import(context)

        context.tags = self.dummyContext['tags']
        context.submission_timetolive = context.tip_timetolive = 1000
        context.description = context.name = \
            context.submission_disclaimer = \
            context.submission_introduction = { "en" : u'Localized723' }
        store.add(context)
        return context.id
Exemplo n.º 4
0
def do_appdata_init(store):

    try:
        appdata = store.find(models.ApplicationData).one()

        if not appdata:
            raise Exception

    except Exception as xxx:
        appdata = models.ApplicationData()
        source = opportunistic_appdata_init()
        appdata.version = source['version']
        appdata.fields = source['fields']
        store.add(appdata)

    fo = Fields()
    fo.noisy = True
    fo.default_fields(appdata.fields)
    (unique_fields, localized_fields) = fo.extensive_dump()

    return unique_fields, localized_fields
Exemplo n.º 5
0
def anon_serialize_context(context,
                           language=GLSetting.memory_copy.default_language):
    """
    @param context: a valid Storm object
    @return: a dict describing the contexts available for submission,
        (e.g. checks if almost one receiver is associated)
    """

    mo = Rosetta()
    mo.acquire_storm_object(context)
    fo = Fields(context.localized_fields, context.unique_fields)

    context_dict = {
        "id": context.id,
        "escalation_threshold": 0,
        "file_max_download": context.file_max_download,
        "file_required": context.file_required,
        "selectable_receiver": context.selectable_receiver,
        "tip_max_access": context.tip_max_access,
        "tip_timetolive": context.tip_timetolive,
        "submission_introduction":
        u'NYI',  # unicode(context.submission_introduction), # optlang
        "submission_disclaimer":
        u'NYI',  # unicode(context.submission_disclaimer), # optlang
        "select_all_receivers": context.select_all_receivers,
        "maximum_selectable_receivers": context.maximum_selectable_receivers,
        'require_pgp': context.require_pgp,
        "show_small_cards": context.show_small_cards,
        "presentation_order": context.presentation_order,
        # list is needed because .values returns a generator
        "receivers": list(context.receivers.values(models.Receiver.id)),
        'name': mo.dump_translated('name', language),
        "description": mo.dump_translated('description', language),
        "fields": fo.dump_fields(language)
    }

    if not len(context_dict['receivers']):
        return None

    return context_dict
Exemplo n.º 6
0
def default_context_fields():

    source = opportunistic_appdata_init()
    if not source.has_key('fields'):
        raise Exception("Invalid Application Data initialization")

    f = source['fields']
    fo = Fields()
    fo.noisy = True
    fo.default_fields(f)
    default_fields_unhappy = fo.dump_fields('en')

    ret_fields = []
    the_first_is_required = False

    for field in default_fields_unhappy:

        if not the_first_is_required:
            field['required'] = True
            the_first_is_required = True

        ret_fields.append(field)

    return ret_fields
Exemplo n.º 7
0
def get_receiver_tip_list(store,
                          receiver_id,
                          language=GLSetting.memory_copy.default_language):

    rtiplist = store.find(ReceiverTip, ReceiverTip.receiver_id == receiver_id)
    rtiplist.order_by(Desc(ReceiverTip.creation_date))

    node = store.find(Node).one()

    rtip_summary_list = []

    for rtip in rtiplist:

        postpone_superpower = (node.postpone_superpower
                               or rtip.internaltip.context.postpone_superpower
                               or rtip.receiver.postpone_superpower)

        can_delete_submission = (node.can_delete_submission or
                                 rtip.internaltip.context.can_delete_submission
                                 or rtip.receiver.can_delete_submission)

        rfiles_n = store.find(
            ReceiverFile, (ReceiverFile.internaltip_id == rtip.internaltip.id,
                           ReceiverFile.receiver_id == receiver_id)).count()

        your_messages = store.find(Message, Message.receivertip_id == rtip.id,
                                   Message.type == u'receiver').count()

        unread_messages = store.find(Message,
                                     Message.receivertip_id == rtip.id,
                                     Message.type == u'whistleblower',
                                     Message.visualized == False).count()

        read_messages = store.find(Message, Message.receivertip_id == rtip.id,
                                   Message.type == u'whistleblower',
                                   Message.visualized == True).count()

        single_tip_sum = dict({
            'id':
            rtip.id,
            'expressed_pertinence':
            rtip.expressed_pertinence,
            'creation_date':
            datetime_to_ISO8601(rtip.creation_date),
            'last_access':
            datetime_to_ISO8601(rtip.last_access),
            'expiration_date':
            datetime_to_ISO8601(rtip.internaltip.expiration_date),
            'access_counter':
            rtip.access_counter,
            'files_number':
            rfiles_n,
            'comments_number':
            rtip.internaltip.comments.count(),
            'unread_messages':
            unread_messages,
            'read_messages':
            read_messages,
            'your_messages':
            your_messages,
            'postpone_superpower':
            postpone_superpower,
            'can_delete_submission':
            can_delete_submission,
        })

        mo = Rosetta()
        mo.acquire_storm_object(rtip.internaltip.context)
        single_tip_sum["context_name"] = mo.dump_translated('name', language)

        preview_data = []

        fo = Fields(rtip.internaltip.context.localized_fields,
                    rtip.internaltip.context.unique_fields)
        for preview_key, preview_label in fo.get_preview_keys(
                language).iteritems():

            # preview in a format angular.js likes
            try:
                entry = dict({
                    'label': preview_label,
                    'text': rtip.internaltip.wb_fields[preview_key]
                })

            except KeyError as xxx:
                log.err("Legacy error: suppressed 'preview_keys' %s" %
                        xxx.message)
                continue

            preview_data.append(entry)

        single_tip_sum.update({'preview': preview_data})
        rtip_summary_list.append(single_tip_sum)

    return rtip_summary_list