Esempio n. 1
0
def create(request, collect_type='index'):

    auth_manager = request.auth_manager
    account_id = auth_manager.account_id
    language = auth_manager.language
    extensions_list = SOURCE_EXTENSION_LIST
    
    collect_type_id = next(x[1] for x in COLLECT_TYPE_SLUGS if x[0] == collect_type)

    if request.method == 'GET':
        form = DatasetFormFactory(collect_type_id).create(
            account_id=account_id,
            language=language,
            status_choices=auth_manager.get_allowed_actions()
        )
        form.label_suffix = ''
        url = 'createDataset/{0}.html'.format(collect_type)
        return render_to_response(url, locals())

    elif request.method == 'POST':
        """update dataset """
        form = DatasetFormFactory(collect_type_id).create(request, account_id=account_id, language=language,
                                                          status_choices=auth_manager.get_allowed_actions())

        if form.is_valid():
            lifecycle = DatasetLifeCycleManager(user=request.user)
            dataset_revision = lifecycle.create(collect_type=request.POST.get('collect_type'), language=language,
                                                account_id=account_id, **form.cleaned_data)

            # TODO: Create a CreateDatasetResponse object
            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)
Esempio n. 2
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)