Ejemplo n.º 1
0
    def get(self, request, publisher_id):
        users = OrganisationGroup.objects.get(
            publisher_id=publisher_id).organisationuser_set.all()

        serializer = OrganisationUserSerializer(
            users,
            many=True,
            context=self.get_serializer_context(),
            fields=('username', 'email'))

        return Response(serializer.data)
Ejemplo n.º 2
0
    def get(self, request, publisher_id):
        users = OrganisationAdminGroup.objects.get(
            publisher_id=publisher_id).organisationuser_set.all()

        # print(users)

        serializer = OrganisationUserSerializer(
            users,
            many=True,
            context={
                'request': request,
            },
            fields=('username', 'email'),
        )

        return Response(serializer.data)
Ejemplo n.º 3
0
    def post(self, request):

        # TODO: If verifying for the first time, OrganisationGroup and
        # OrganisationAdminGroup don't exist yet. - 2016-10-25

        # publisher = Publisher.objects.get(pk=publisher_id)
        # group = OrganisationAdminGroup.objects.get(publisher_id=publisher_id)

        user = request.user.organisationuser
        api_key = request.data.get('apiKey')
        user_id = request.data.get('userId')

        if not api_key or not user_id:
            raise exceptions.ParseError(
                detail="apiKey or userId not specified")

        client = RemoteCKAN(settings.CKAN_URL, apikey=api_key)

        try:
            result = client.call_action('user_show', {
                "id": user_id,
                "include_datasets": True,
            })
        except BaseException:
            raise exceptions.APIException(
                detail="user with id {} not found".format(user_id))

        # print('got user')
        # print(result)

        try:
            orgList = client.call_action('organization_list_for_user', {})
        except BaseException:
            raise exceptions.APIException(
                detail="Can't get organisation list for user".format(user_id))


#         print('got orgList')
#         print(orgList)

        if not len(orgList):
            raise exceptions.APIException(
                detail="This user has no organisations yet".format(user_id))

        primary_org_id = orgList[0]['id']

        try:
            primary_org = client.call_action('organization_show',
                                             {"id": primary_org_id})
        except BaseException:
            raise exceptions.APIException(
                detail="Can't call organization_show for organization with id {}"
                .format(primary_org_id))
            return Response(status=401)

        # print('got primary_org')
        # print(primary_org)

        if not primary_org:
            raise exceptions.APIException(
                detail="Can't call organization_show for organization with id {}"
                .format(primary_org_id))

        primary_org_iati_id = primary_org.get('publisher_iati_id')
        publisher_org = get_or_none(
            Organisation, organisation_identifier=primary_org_iati_id)

        if not publisher_org:
            raise exceptions.APIException(
                detail=
                "publisher_iati_id of {} not found in Organisation standard, correct this in the IATI registry"
                .format(primary_org_iati_id))

        # TODO: add organisation foreign key - 2016-10-25
        publisher = Publisher.objects.update_or_create(
            iati_id=primary_org_id,
            publisher_iati_id=primary_org_iati_id,
            defaults={
                "name": primary_org.get('name'),
                "display_name": primary_org.get('display_name'),
                "organisation": publisher_org,
            })

        organisation_group = OrganisationGroup.objects.get_or_create(
            publisher=publisher[0],
            defaults={
                "name": "{} Organisation Group".format(primary_org.get('name'))
            })
        organisation_group[0].organisationuser_set.add(user)

        if publisher[1]:  # has been created
            organisation_admin_group = OrganisationAdminGroup.objects.get_or_create(
                publisher=publisher[0],
                defaults={
                    "owner":
                    user,
                    "name":
                    "{} Organisation Admin Group".format(
                        primary_org.get('name')),
                })
        else:  # already exists
            organisation_admin_group = OrganisationAdminGroup.objects.get_or_create(
                publisher=publisher[0],
                defaults={
                    "name":
                    "{} Organisation Admin Group".format(
                        primary_org.get('name')),
                })
        organisation_admin_group[0].organisationuser_set.add(user)

        user.iati_api_key = api_key
        user.iati_user_id = user_id
        user.save()

        serializer = OrganisationUserSerializer(user,
                                                context={
                                                    'request': request,
                                                })

        return Response(serializer.data)