Beispiel #1
0
def edit_coordinator(request):
    coordinator_id = request.GET.get('coordinator', request.GET.get('uuid'))
    doc = None
    workflow_uuid = None

    if coordinator_id:
        cid = {}
        if coordinator_id.isdigit():
            cid['id'] = coordinator_id
        else:
            cid['uuid'] = coordinator_id
        doc = Document2.objects.get(**cid)
        coordinator = Coordinator(document=doc)
    else:
        coordinator = Coordinator()
        coordinator.set_workspace(request.user)

    if request.GET.get('workflow'):
        workflow_uuid = request.GET.get('workflow')

    if workflow_uuid:
        coordinator.data['properties']['workflow'] = workflow_uuid

    api = get_oozie(request.user)
    credentials = Credentials()

    try:
        credentials.fetch(api)
    except Exception as e:
        LOG.error(smart_str(e))

    if USE_NEW_EDITOR.get():
        scheduled_uuid = coordinator.data['properties'][
            'workflow'] or coordinator.data['properties']['document']
        if scheduled_uuid:
            try:
                document = Document2.objects.get(uuid=scheduled_uuid)
            except Document2.DoesNotExist as e:
                document = None
                coordinator.data['properties']['workflow'] = ''
                LOG.warning("Workflow with uuid %s doesn't exist: %s" %
                            (scheduled_uuid, e))

            if document and document.is_trashed:
                raise PopupException(
                    _('Your workflow %s has been trashed!') %
                    (document.name if document.name else ''))

            if document and not document.can_read(request.user):
                raise PopupException(
                    _('You don\'t have access to the workflow or document of this coordinator.'
                      ))
    else:
        workflows = [
            dict([('uuid', d.content_object.uuid),
                  ('name', d.content_object.name)])
            for d in Document.objects.available_docs(
                Document2, request.user).filter(extra='workflow2')
        ]

        if coordinator_id and not [
                a for a in workflows
                if a['uuid'] == coordinator.data['properties']['workflow']
        ]:
            raise PopupException(
                _('You don\'t have access to the workflow of this coordinator.'
                  ))

    if USE_NEW_EDITOR.get():  # In Hue 4, merge with above
        workflows = [
            dict([('uuid', d.uuid), ('name', d.name)])
            for d in Document2.objects.documents(
                request.user, include_managed=False).search_documents(
                    types=['oozie-workflow2'])
        ]

    can_edit = doc is None or (doc.can_write(request.user)
                               if USE_NEW_EDITOR.get() else
                               doc.doc.get().is_editable(request.user))
    if request.GET.get('format') == 'json':  # For Editor
        return JsonResponse({
            'coordinator':
            coordinator.get_data_for_json(),
            'credentials':
            list(credentials.credentials.keys()),
            'workflows':
            workflows,
            'doc_uuid':
            doc.uuid if doc else '',
            'is_embeddable':
            request.GET.get('is_embeddable', False),
            'can_edit':
            can_edit,
            'layout':
            django_mako.render_to_string(
                'editor2/common_scheduler.mako',
                {'coordinator_json': coordinator.to_json_for_html()})
        })
    else:
        return render(
            'editor2/coordinator_editor.mako', request, {
                'coordinator_json':
                coordinator.to_json_for_html(),
                'credentials_json':
                json.dumps(list(credentials.credentials.keys()),
                           cls=JSONEncoderForHTML),
                'workflows_json':
                json.dumps(workflows, cls=JSONEncoderForHTML),
                'doc_uuid':
                doc.uuid if doc else '',
                'is_embeddable':
                request.GET.get('is_embeddable', False),
                'can_edit_json':
                json.dumps(can_edit)
            })