Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/remove-legal-basis-api-connection #5857

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions config/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,10 +697,6 @@ def _add_hawk_credentials(id_env_name, key_env_name, scopes):
DNB_AUTOMATIC_UPDATE_LIMIT = env.int('DNB_AUTOMATIC_UPDATE_LIMIT', default=None)
DNB_MAX_COMPANIES_IN_TREE_COUNT = env.int('DNB_MAX_COMPANIES_IN_TREE_COUNT', default=1000)

# Legal Basis / Consent Service
CONSENT_SERVICE_BASE_URL = env('CONSENT_SERVICE_BASE_URL', default=None)
CONSENT_SERVICE_HAWK_ID = env('CONSENT_SERVICE_HAWK_ID', default=None)
CONSENT_SERVICE_HAWK_KEY = env('CONSENT_SERVICE_HAWK_KEY', default=None)

DATAHUB_SUPPORT_EMAIL_ADDRESS = env('DATAHUB_SUPPORT_EMAIL_ADDRESS', default=None)

Expand Down
11 changes: 2 additions & 9 deletions config/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@
'django.contrib.auth.hashers.MD5PasswordHasher',
]

CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'
}
}
CACHES = {'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}}

# Stop WhiteNoise emitting warnings when running tests without running collectstatic first
WHITENOISE_AUTOREFRESH = True
Expand Down Expand Up @@ -124,7 +120,7 @@
'aws_access_key_id': 'bar',
'aws_secret_access_key': 'baz',
'aws_region': 'eu-west-2',
}
},
}

DIT_EMAIL_INGEST_BLOCKLIST = [
Expand Down Expand Up @@ -161,9 +157,6 @@
ADMIN_OAUTH2_CLIENT_SECRET = 'client-secret'
ADMIN_OAUTH2_LOGOUT_PATH = 'http://sso-server/o/logout'

CONSENT_SERVICE_BASE_URL = 'http://consent.service/'
CONSENT_SERVICE_HAWK_ID = 'some-id'
CONSENT_SERVICE_HAWK_KEY = 'some-secret'

COMPANY_MATCHING_SERVICE_BASE_URL = 'http://content.matching/'
COMPANY_MATCHING_HAWK_ID = 'some-id'
Expand Down
198 changes: 0 additions & 198 deletions datahub/company/consent.py

This file was deleted.

95 changes: 1 addition & 94 deletions datahub/company/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@

from django.conf import settings
from django.db import models, transaction
from django.utils.timezone import now
from django.utils.translation import gettext_lazy
from rest_framework import serializers

from datahub.company import consent
from datahub.company.constants import (
BusinessTypeConstant,
OneListTierID,
Expand All @@ -30,13 +28,12 @@
OneListCoreTeamMember,
OneListTier,
)
from datahub.company.tasks.contact import schedule_update_contact_consent

from datahub.company.validators import (
has_no_invalid_company_number_characters,
has_uk_establishment_number_prefix,
validate_team_member_max_count,
)
from datahub.core.api_client import get_zipkin_headers
from datahub.core.constants import Country
from datahub.core.constants import HeadquarterType
from datahub.core.serializers import (
Expand Down Expand Up @@ -278,96 +275,6 @@ class Meta(ContactSerializer.Meta):
]


class ConsentMarketingField(serializers.BooleanField):
"""
ConsentMarketingField will lookup consent data.
The model that this fields is used must have an email field
BooleanField is subclassed here for validation.
"""

def to_internal_value(self, data):
"""Validate boolean on incoming data."""
return {
'accepts_dit_email_marketing': super().to_internal_value(data),
}

def to_representation(self, value):
"""Lookup from consent service api/"""
try:
representation = consent.get_one(value.email)
except consent.ConsentAPIError:
representation = False
return representation


class ContactDetailSerializer(ContactSerializer):
"""
This is the same as the ContactSerializer except it includes
accepts_dit_email_marketing in the fields. Only 3 endpoints will use this serialiser
"""

accepts_dit_email_marketing = ConsentMarketingField(source='*', required=False)

class Meta(ContactSerializer.Meta):
fields = ContactSerializer.Meta.fields + ('accepts_dit_email_marketing',)

def _notify_consent_service(self, validated_data):
"""
Trigger the update_contact_consent task with the current version
of `validated_data`. The actual enqueuing of the task happens in the
on_commit hook so it won't actually notify the consent service unless
the database transaction was successful.
"""
if 'accepts_dit_email_marketing' not in validated_data:
# If no consent value in request body
return
# Remove the accepts_dit_email_marketing from validated_data
accepts_dit_email_marketing = validated_data.pop('accepts_dit_email_marketing')
# If consent value in POST, notify
combiner = DataCombiner(self.instance, validated_data)
request = self.context.get('request', None)
transaction.on_commit(
lambda: schedule_update_contact_consent(
combiner.get_value('email'),
accepts_dit_email_marketing,
kwargs={
'modified_at': now().isoformat(),
'zipkin_headers': get_zipkin_headers(request),
},
),
)

@transaction.atomic
def create(self, validated_data):
"""
Create a new instance using this serializer

:return: created instance
"""
self._notify_consent_service(validated_data)
return super().create(validated_data)

@transaction.atomic
def update(self, instance, validated_data):
"""
Update the given instance with validated_data

:return: updated instance
"""
self._notify_consent_service(validated_data)
return super().update(instance, validated_data)


class ContactDetailV4Serializer(ContactV4Serializer, ContactDetailSerializer):
"""
This is the same as the ContactSerializer except it includes
accepts_dit_email_marketing in the fields.
"""

class Meta(ContactV4Serializer.Meta):
fields = ContactV4Serializer.Meta.fields + ('accepts_dit_email_marketing',)


class CompanyExportCountrySerializer(serializers.ModelSerializer):
"""
Export country serializer holding `Country` and its status.
Expand Down
2 changes: 0 additions & 2 deletions datahub/company/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

from datahub.company.tasks.contact import (
automatic_contact_archive,
update_contact_consent,
)

__all__ = (
'automatic_adviser_deactivate',
'automatic_contact_archive',
'update_contact_consent',
)
Loading
Loading