Ejemplo n.º 1
0
def edit(request, dataset_revision_id=None):
    account_id = request.auth_manager.account_id
    auth_manager = request.auth_manager
    language = request.auth_manager.language
    user_id = request.auth_manager.id
    extensions_list = SOURCE_EXTENSION_LIST

    # TODO: Put line in a common place
    collect_types = {0: 'file', 1: 'url', 2: 'webservice'}

    # TODO: Review. Category was not loading options from form init.
    category_choices = [[
        category['category__id'], category['name']
    ] for category in CategoryI18n.objects.filter(
        language=language, category__account=account_id).values(
            'category__id', 'name')]

    # Get data set and the right template depending on the collected type
    dataset = DatasetDBDAO().get(user=request.user,
                                 dataset_revision_id=dataset_revision_id)

    initial_values = dict(
        # Dataset Form
        dataset_id=dataset.get('id'),
        title=dataset.get('title'),
        description=dataset.get('description'),
        category=dataset.get('category_id'),
        status=dataset.get('status'),
        notes=dataset.get('notes'),
        file_name=dataset.get('filename'),
        end_point=dataset.get('end_point'),
        impl_type=dataset.get('impl_type'),
        license_url=dataset.get('license_url'),
        spatial=dataset.get('spatial'),
        frequency=dataset.get('frequency'),
        mbox=dataset.get('mbox'),
        sources=dataset.get('sources'),
        tags=dataset.get('tags'),
        doc=dataset.get('doc'))

    if request.method == 'GET':
        status_options = auth_manager.get_allowed_actions()
        url = 'editDataset/{0}.html'.format(
            collect_types[dataset['collect_type']])

        # Import the form that we really need
        if collect_types[dataset['collect_type']] is not 'url':
            className = [
                collect_types[dataset['collect_type']].capitalize(), "Form"
            ]
        else:
            className = ['Dataset', "Form"]

        className = ''.join(str(elem) for elem in className)
        mod = __import__('workspace.manageDatasets.forms',
                         fromlist=[className])

        form = getattr(mod, className)(status_options=status_options)
        form.label_suffix = ''
        form.fields['category'].choices = category_choices
        form.initial = initial_values

        return render_to_response(url, locals())
    elif request.method == 'POST':
        """ Update dataset """
        form = DatasetFormFactory(request.POST.get('collect_type')).create(
            request,
            account_id=account_id,
            language=language,
            status_choices=auth_manager.get_allowed_actions())

        # Agrego los valores iniciales para que el changed_data de correctamente
        form.initial = initial_values

        if form.is_valid():
            lifecycle = DatasetLifeCycleManager(
                user=request.user, dataset_revision_id=dataset_revision_id)

            dataset_revision = lifecycle.edit(
                collect_type=request.POST.get('collect_type'),
                changed_fields=form.changed_data,
                language=language,
                **form.cleaned_data)

            # Signal
            dataset_changed.send_robust(sender='edit_view',
                                        id=lifecycle.dataset.id,
                                        rev_id=lifecycle.dataset_revision.id)

            data = dict(
                status='ok',
                messages=[ugettext('APP-DATASET-CREATEDSUCCESSFULLY-TEXT')],
                dataset_revision_id=dataset_revision.id)
            return HttpResponse(json.dumps(data), content_type='text/plain')
        else:
            raise DatasetSaveException(form)