コード例 #1
0
    def get(self, request, dataset_id=None, *args, **kwargs):

        user, profile = user_and_profile(request)

        dataset = get_object_or_404_extended(Dataset,
                                             user,
                                             include={'id': dataset_id})

        # Redirect to layer
        _resource = request.GET.get('resource')
        _layer = request.GET.get('layer')
        if _resource and _layer:
            return redirect(
                reverse('idgo_admin:layer_editor',
                        kwargs={
                            'dataset_id': dataset.id,
                            'resource_id': _resource,
                            'layer_id': _layer
                        }))

        resource = None
        id = request.GET.get('id')
        if id:
            include = {'id': id, 'dataset_id': dataset.id}
            resource = get_object_or_404_extended(Resource,
                                                  user,
                                                  include=include)

        form = Form(instance=resource, user=user)
        context = self.get_context(form, user, dataset, resource=resource)

        return render_with_info_profile(request, self.template, context)
コード例 #2
0
ファイル: resource.py プロジェクト: hcharp/DataSud-2017-2019
def handle_pust_request(request, dataset_name, resource_id=None):
    # title -> name
    # description -> description
    # language -> lang
    # format -> format_type.pk
    # type -> data_type raw|annexe|service
    # restricted_level -> public|registered|only_allowed_users|same_organization|any_organization
    # restricted_list -> list of: user.username|organisation.slug
    # up_file -> {File}
    user = request.user
    dataset = get_object_or_404_extended(
        Dataset, user, include={'slug': dataset_name})
    resource = None
    if resource_id:
        resource = get_object_or_404(Resource, ckan_id=resource_id)

    query_data = getattr(request, request.method)  # QueryDict

    # `title` est obligatoire
    title = query_data.pop('title', resource and [resource.title])
    if title:
        query_data.__setitem__('title', title[-1])

    # `lang` est obligatoire
    lang = query_data.pop('language', query_data.pop('lang', resource and [resource.lang]))
    if lang:
        query_data.__setitem__('lang', lang[-1])

    # `data_type` est obligatoire
    data_type = query_data.pop('data_type', query_data.pop('type', resource and [resource.data_type]))
    if data_type:
        query_data.__setitem__('data_type', data_type[-1])

    restricted_list = query_data.pop('restricted_list', [])
    profiles_allowed = None
    organisations_allowed = None

    restricted_level = query_data.pop('restricted_level', resource and [resource.restricted_level] or ['public'])
    if restricted_level[-1] == 'only_allowed_users':
        profiles_allowed = User.objects.filter(username__in=restricted_list)
        query_data.__setitem__('profiles_allowed', [instance.pk for instance in profiles_allowed])
    elif restricted_level[-1] in ('same_organization', 'any_organization'):
        organisations_allowed = Organisation.objects.filter(slug__in=restricted_list)
        query_data.__setitem__('organisations_allowed', [instance.pk for instance in organisations_allowed])
    query_data.__setitem__('restricted_level', restricted_level[-1])

    format_type_slug = query_data.pop('format_type', query_data.pop('format', None))
    if format_type_slug:
        try:
            resource_format = ResourceFormats.objects.get(slug=format_type_slug[-1])
        except ResourceFormats.DoesNotExist as e:
            raise GenericException(details=e.__str__())
        query_data.__setitem__('format_type', resource_format.pk)

    form = Form(query_data, request.FILES, instance=resource, dataset=dataset, user=user)
    if not form.is_valid():
        raise GenericException(details=form._errors)

    data = form.cleaned_data
    kvp = {
        'dataset': dataset,
        'title': data['title'],
        'description': data['description'],
        'lang': data['lang'],
        'data_type': data['data_type'],
        'format_type': data['format_type'],
        # 'last_update': data['last_update'],
        'restricted_level': data['restricted_level'],
        'up_file': data['up_file'],
        'dl_url': data['dl_url'],
        # 'synchronisation': data['synchronisation'],
        # 'sync_frequency': data['sync_frequency'],
        'referenced_url': data['referenced_url'],
        # 'ftp_file': data['ftp_file'],
        'crs': data['crs'],
        'encoding': data.get('encoding') or None,
        'extractable': data['extractable'],
        'ogc_services': data['ogc_services'],
        'geo_restriction': data['geo_restriction'],
        }

    profiles_allowed = None
    organisations_allowed = None
    if data['restricted_level'] == 'only_allowed_users':
        profiles_allowed = data['profiles_allowed']
    if data['restricted_level'] == 'same_organization':
        organisations_allowed = [form._dataset.organisation]
    if data['restricted_level'] == 'any_organization':
        organisations_allowed = data['organisations_allowed']

    memory_up_file = request.FILES.get('up_file')
    file_extras = memory_up_file and {
        'mimetype': memory_up_file.content_type,
        'resource_type': memory_up_file.name,
        'size': memory_up_file.size} or None

    try:
        with transaction.atomic():
            save_opts = {
                'current_user': user,
                'file_extras': file_extras,
                'synchronize': True}
            if resource:
                for k, v in kvp.items():
                    setattr(resource, k, v)
            else:
                resource = Resource.default.create(save_opts=save_opts, **kvp)
            if organisations_allowed:
                resource.organisations_allowed = organisations_allowed
            if profiles_allowed:
                resource.profiles_allowed = profiles_allowed
                save_opts['synchronize'] = True
                save_opts['file_extras'] = None  # IMPORTANT
            resource.save(**save_opts)
    except ValidationError as e:
        if e.code == 'crs':
            form.add_error(e.code, '')
            form.add_error('__all__', e.message)
        elif e.code == 'encoding':
            form.add_error(e.code, '')
            form.add_error('__all__', e.message)
        else:
            form.add_error(e.code, e.message)
    except CkanBaseError as e:
        form.add_error('__all__', e.__str__())
    else:
        if resource_id:
            send_resource_update_mail(user, resource)
        else:
            send_resource_creation_mail(user, resource)
        return resource
    raise GenericException(details=form._errors)
コード例 #3
0
    def post(self, request, dataset_id=None, *args, **kwargs):

        # Vider systèmatiquement les messages
        storage = messages.get_messages(request)
        storage.used = True

        user, profile = user_and_profile(request)

        dataset = get_object_or_404_extended(Dataset,
                                             user,
                                             include={'id': dataset_id})

        resource = None
        id = request.POST.get('id', request.GET.get('id'))
        if id:
            include = {'id': id, 'dataset': dataset}
            resource = get_object_or_404_extended(Resource,
                                                  user,
                                                  include=include)

        form = Form(request.POST,
                    request.FILES,
                    instance=resource,
                    dataset=dataset,
                    user=user)

        context = self.get_context(form, user, dataset, resource)

        ajax = 'ajax' in request.POST
        save_and_continue = 'continue' in request.POST

        if not form.is_valid():
            if ajax:
                error = dict([(k, [str(m) for m in v])
                              for k, v in form.errors.items()])
                msg = 'Veuillez corriger le formulaire.'
                if '__all__' in error:
                    error['__all__'].prepend(msg)
                else:
                    error['__all__'] = [msg]
                return JsonResponse(json.dumps({'error': error}), safe=False)
            return render_with_info_profile(request, self.template, context)

        data = form.cleaned_data

        kvp = {
            'dataset':
            dataset,
            'title':
            data['title'],
            'description':
            data['description'],
            'lang':
            data['lang'],
            'data_type':
            data['data_type'],
            'format_type':
            data['format_type'],
            'last_update':
            data['last_update'],
            'restricted_level':
            data['restricted_level'],
            'up_file':
            data['up_file'],
            'dl_url':
            data['dl_url'],
            'synchronisation':
            data['synchronisation'],
            'sync_frequency':
            data['sync_frequency'],
            'referenced_url':
            data['referenced_url'],
            'ftp_file':
            data['ftp_file']
            and os.path.join(FTP_DIR, user.username, data['ftp_file']) or None,
            'crs':
            data['crs'],
            'encoding':
            data.get('encoding') or None,
            'extractable':
            data['extractable'],
            'ogc_services':
            data['ogc_services'],
            'geo_restriction':
            data['geo_restriction'],
        }

        profiles_allowed = None
        organisations_allowed = None
        if data['restricted_level'] == 'only_allowed_users':
            profiles_allowed = data['profiles_allowed']
        elif data['restricted_level'] == 'same_organization':
            organisations_allowed = [form._dataset.organisation]
        elif data['restricted_level'] == 'any_organization':
            organisations_allowed = data['organisations_allowed']

        memory_up_file = request.FILES.get('up_file')
        file_extras = memory_up_file and {
            'mimetype': memory_up_file.content_type,
            'resource_type': memory_up_file.name,
            'size': memory_up_file.size
        } or None

        try:
            with transaction.atomic():
                save_opts = {
                    'current_user': user,
                    'file_extras': file_extras,
                    'synchronize': True
                }
                if not id:
                    resource = Resource.default.create(save_opts=save_opts,
                                                       **kvp)
                else:
                    resource = Resource.objects.get(pk=id)
                    for k, v in kvp.items():
                        setattr(resource, k, v)
                if organisations_allowed:
                    resource.organisations_allowed = organisations_allowed
                if profiles_allowed:
                    resource.profiles_allowed = profiles_allowed
                save_opts['synchronize'] = True
                save_opts['file_extras'] = None  # IMPORTANT
                resource.save(**save_opts)
        except ValidationError as e:
            if e.code == 'crs':
                form.add_error(e.code, '')
                form.add_error('__all__', e.message)
            elif e.code == 'encoding':
                form.add_error(e.code, '')
                form.add_error('__all__', e.message)
            else:
                form.add_error(e.code, e.message)
            messages.error(request, ' '.join(e))
            error = dict([(k, [str(m) for m in v])
                          for k, v in form.errors.items()])
        except CkanBaseError as e:
            error = {'__all__': [e.__str__()]}
            form.add_error('__all__', e.__str__())
            messages.error(request, e.__str__())
        else:
            if id:
                send_resource_update_mail(user, resource)
            else:
                send_resource_creation_mail(user, resource)

            dataset_href = reverse(self.namespace,
                                   kwargs={'dataset_id': dataset_id})
            messages.success(
                request,
                ('La ressource a été {0} avec succès. Souhaitez-vous '
                 '<a href="{1}">ajouter une nouvelle ressource</a> ? ou bien '
                 '<a href="{2}/dataset/{3}/resource/{4}" target="_blank">'
                 'voir la ressource dans CKAN</a> ?').format(
                     id and 'mise à jour' or 'créée', dataset_href, CKAN_URL,
                     dataset.slug, resource.ckan_id))

            if ajax:
                response = HttpResponse(status=201)  # Ugly hack
                if save_and_continue:
                    href = '{0}?id={1}'.format(dataset_href, resource.id)
                else:
                    href = '{0}?id={1}#resources/{2}'.format(
                        reverse('idgo_admin:dataset'), dataset_id, resource.id)
                response['Content-Location'] = href
                return response
            else:
                if save_and_continue:
                    url = '{0}?id={1}'.format(dataset_href, resource.id)
                    return HttpResponseRedirect(url)
                # else:
                url = '{0}?id={1}#resources/{2}'.format(
                    reverse('idgo_admin:dataset'), dataset_id, resource.id)
                return HttpResponseRedirect(url)

        if ajax:
            form._errors = None
            return JsonResponse(json.dumps({'error': error}), safe=False)
        return render_with_info_profile(request, self.template, context)
コード例 #4
0
def handle_pust_request(request, dataset_name, resource_id=None):
    # title -> name
    # description -> description
    # language -> lang
    # format -> format_type.pk
    # type -> data_type raw|annexe|service
    # restricted_level -> public|registered|only_allowed_users|same_organization|any_organization
    # restricted_list -> list of: user.username|organisation.slug
    # up_file -> {File}
    user = request.user
    dataset = get_object_or_404_extended(Dataset,
                                         user,
                                         include={'slug': dataset_name})
    resource = None
    if resource_id:
        resource = get_object_or_404(Resource, ckan_id=resource_id)

    # TODO: Vérifier les droits

    data = getattr(request, request.method).dict()

    restricted_list = data.get('restricted_list', [])
    profiles_allowed = None
    organisations_allowed = None

    restricted_level = data.get('restricted_level')
    if restricted_level == 'only_allowed_users':
        profiles_allowed = User.objects.filter(username__in=restricted_list)
    elif restricted_level in ('same_organization', 'any_organization'):
        organisations_allowed = Organisation.objects.filter(
            slug__in=restricted_list)

    data_form = {
        'title': data.get('title'),
        'description': data.get('description'),
        'lang': data.get('language', 'french'),
        'format_type': data.get('format'),
        'data_type': data.get('type'),
        'restricted_level': restricted_level,
        'profiles_allowed': profiles_allowed,
        'organisations_allowed': organisations_allowed,
        # 'up_file': '',
        # 'dl_url': '',
        # 'synchronisation': '',
        # 'sync_frequency': '',
        # 'referenced_url': '',
        # 'ftp_file': '',
        'crs': data.get('crs', None),
        'encoding': data.get('encoding', None),
        # 'extractable': data.get('extractable'),
        # 'ogc_services': data.get('ogc_services'),
        # 'geo_restriction': data.get('geo_restriction'),
        # 'last_update': data.get('last_update'),
    }

    form = Form(data_form,
                request.FILES,
                instance=resource,
                dataset=dataset,
                user=user)
    if not form.is_valid():
        raise GenericException(details=form._errors)

    data = form.cleaned_data
    kvp = {
        'dataset': dataset,
        'title': data['title'],
        'description': data['description'],
        'lang': data['lang'],
        'data_type': data['data_type'],
        'format_type': data['format_type'],
        'last_update': data['last_update'],
        'restricted_level': data['restricted_level'],
        'up_file': data['up_file'],
        # 'dl_url': data['dl_url'],
        # 'synchronisation': data['synchronisation'],
        # 'sync_frequency': data['sync_frequency'],
        # 'referenced_url': data['referenced_url'],
        # 'ftp_file': data['ftp_file'],
        'crs': data['crs'],
        'encoding': data.get('encoding') or None,
        'extractable': data['extractable'],
        'ogc_services': data['ogc_services'],
        'geo_restriction': data['geo_restriction'],
    }

    profiles_allowed = None
    organisations_allowed = None
    if data['restricted_level'] == 'only_allowed_users':
        profiles_allowed = data['profiles_allowed']
    if data['restricted_level'] == 'same_organization':
        organisations_allowed = [form._dataset.organisation]
    if data['restricted_level'] == 'any_organization':
        organisations_allowed = data['organisations_allowed']

    memory_up_file = request.FILES.get('up_file')
    file_extras = memory_up_file and {
        'mimetype': memory_up_file.content_type,
        'resource_type': memory_up_file.name,
        'size': memory_up_file.size
    } or None

    try:
        with transaction.atomic():
            save_opts = {
                'current_user': user,
                'file_extras': file_extras,
                'synchronize': True
            }
            if not id:
                resource = Resource.default.create(save_opts=save_opts, **kvp)
            else:
                resource = Resource.objects.get(pk=id)
                for k, v in kvp.items():
                    setattr(resource, k, v)
            if organisations_allowed:
                resource.organisations_allowed = organisations_allowed
            if profiles_allowed:
                resource.profiles_allowed = profiles_allowed
                save_opts['synchronize'] = True
                save_opts['file_extras'] = None  # IMPORTANT
                resource.save(**save_opts)
    except ValidationError as e:
        if e.code == 'crs':
            form.add_error(e.code, '')
            form.add_error('__all__', e.message)
        elif e.code == 'encoding':
            form.add_error(e.code, '')
            form.add_error('__all__', e.message)
        else:
            form.add_error(e.code, e.message)
    except CkanBaseError as e:
        form.add_error('__all__', e.__str__())
    else:
        if resource_id:
            send_resource_update_mail(user, resource)
        else:
            send_resource_creation_mail(user, resource)
        return resource
    raise GenericException(details=form._errors)