コード例 #1
0
ファイル: serializers.py プロジェクト: dmacmillan/Kive
    def create(self, validated_data):
        """
        Create a CodeResourceRevision from the validated deserialized data.

        Note that no cleaning occurs here.  That will fall to the calling method.
        """
        crr_data = validated_data
        users_allowed = crr_data.pop(
            "users_allowed") if "users_allowed" in crr_data else []
        groups_allowed = crr_data.pop(
            "groups_allowed") if "groups_allowed" in crr_data else []

        with transaction.atomic():
            crr = CodeResourceRevision(**crr_data)
            crr.clean()  # this sets the MD5
            crr.save()
            crr.users_allowed.add(*users_allowed)
            crr.groups_allowed.add(*groups_allowed)

        return crr
コード例 #2
0
def _make_crv(file_in_memory,
              creating_user,
              crv_form,
              parent_revision=None,
              code_resource=None):
    """
    Helper that creates a CodeResourceRevision (and a CodeResource as well if appropriate).
    """
    assert isinstance(crv_form,
                      (CodeResourcePrototypeForm, CodeResourceRevisionForm))
    # If parent_revision is specified, we are only making a CodeResourceRevision and not its parent CodeResource.
    assert not (parent_revision is None
                and isinstance(crv_form, CodeResourceRevision))

    cr_filename = "" if file_in_memory is None else file_in_memory.name

    if code_resource is None and parent_revision is not None:
        code_resource = parent_revision.coderesource
    if code_resource is None:
        # crv_form is a CodeResourcePrototypeForm.
        code_resource = CodeResource(
            name=crv_form.cleaned_data['resource_name'],
            description=crv_form.cleaned_data['resource_desc'],
            filename=cr_filename,
            user=creating_user)
        try:
            code_resource.full_clean()
            # Skip the clean until later; after all, we're protected by a transaction here.
            code_resource.save()
        except ValidationError as e:
            crv_form.add_error('content_file',
                               e.error_dict.get('filename', []))
            crv_form.add_error('resource_name', e.error_dict.get('name', []))
            crv_form.add_error('resource_desc',
                               e.error_dict.get('description', []))
            raise e

        code_resource.grant_from_json(crv_form.cleaned_data["permissions"])

        rev_name = "Prototype"
        rev_desc = crv_form.cleaned_data["resource_desc"]
    else:
        rev_name = crv_form.cleaned_data["revision_name"]
        rev_desc = crv_form.cleaned_data["revision_desc"]

    # Modify actual filename prior to saving revision object.
    if file_in_memory is not None:
        file_in_memory.name += '_' + datetime.now().strftime('%Y%m%d%H%M%S')

    revision = CodeResourceRevision(revision_parent=parent_revision,
                                    revision_name=rev_name,
                                    revision_desc=rev_desc,
                                    coderesource=code_resource,
                                    content_file=file_in_memory,
                                    user=creating_user)
    # This sets the MD5.

    try:
        revision.clean()
    except ValidationError as e:
        crv_form.add_error(None, e)
        raise e

    revision.save()
    revision.grant_from_json(crv_form.cleaned_data["permissions"])

    try:
        code_resource.full_clean()
        revision.full_clean()
    except ValidationError as e:
        crv_form.add_error(None, e)
        raise e

    return revision
コード例 #3
0
ファイル: views.py プロジェクト: cfe-lab/Kive
def _make_crv(file_in_memory,
              creating_user,
              crv_form,
              parent_revision=None,
              code_resource=None):
    """
    Helper that creates a CodeResourceRevision (and a CodeResource as well if appropriate).
    """
    assert isinstance(crv_form, (CodeResourcePrototypeForm, CodeResourceRevisionForm))
    # If parent_revision is specified, we are only making a CodeResourceRevision and not its parent CodeResource.
    assert not (parent_revision is None and isinstance(crv_form, CodeResourceRevision))

    cr_filename = "" if file_in_memory is None else file_in_memory.name

    if code_resource is None and parent_revision is not None:
        code_resource = parent_revision.coderesource
    if code_resource is None:
        # crv_form is a CodeResourcePrototypeForm.
        code_resource = CodeResource(
            name=crv_form.cleaned_data['resource_name'],
            description=crv_form.cleaned_data['resource_desc'],
            filename=cr_filename,
            user=creating_user
        )
        try:
            code_resource.full_clean()
            # Skip the clean until later; after all, we're protected by a transaction here.
            code_resource.save()
        except ValidationError as e:
            crv_form.add_error('content_file', e.error_dict.get('filename', []))
            crv_form.add_error('resource_name', e.error_dict.get('name', []))
            crv_form.add_error('resource_desc', e.error_dict.get('description', []))
            raise e

        code_resource.grant_from_json(crv_form.cleaned_data["permissions"])

        rev_name = "Prototype"
        rev_desc = crv_form.cleaned_data["resource_desc"]
    else:
        rev_name = crv_form.cleaned_data["revision_name"]
        rev_desc = crv_form.cleaned_data["revision_desc"]

    # Modify actual filename prior to saving revision object.
    if file_in_memory is not None:
        file_in_memory.name += '_' + datetime.now().strftime('%Y%m%d%H%M%S')

    revision = CodeResourceRevision(
        revision_parent=parent_revision,
        revision_name=rev_name,
        revision_desc=rev_desc,
        coderesource=code_resource,
        content_file=file_in_memory,
        user=creating_user
    )
    # This sets the MD5.

    try:
        revision.clean()
    except ValidationError as e:
        crv_form.add_error(None, e)
        raise e

    revision.save()
    revision.grant_from_json(crv_form.cleaned_data["permissions"])

    try:
        code_resource.full_clean()
        revision.full_clean()
    except ValidationError as e:
        crv_form.add_error(None, e)
        raise e

    return revision