Source code for shuup.gdpr.utils

# -*- coding: utf-8 -*-
# This file is part of Shuup.
#
# Copyright (c) 2012-2021, Shuup Commerce Inc. All rights reserved.
#
# This source code is licensed under the OSL-3.0 license found in the
# LICENSE file in the root directory of this source tree.
import json
from datetime import timedelta
from django.conf import settings
from django.template import loader
from django.utils.timezone import now
from django.utils.translation import activate, get_language, ugettext_lazy as _
from reversion import create_revision

from shuup.simple_cms.models import Page
from shuup.utils.django_compat import force_text
from shuup.utils.i18n import format_datetime








[docs]def get_all_contact_data(contact): from shuup.core.models import CompanyContact from shuup.gdpr.serializers import GDPRCompanyContactSerializer, GDPRPersonContactSerializer if isinstance(contact, CompanyContact): return GDPRCompanyContactSerializer(contact).data return GDPRPersonContactSerializer(contact).data
[docs]def ensure_gdpr_privacy_policy(shop, force_update=False): from shuup.gdpr.models import GDPRSettings from shuup.simple_cms.models import Page gdpr_document = get_privacy_policy_page(shop) current_language = get_language() if force_update or not gdpr_document: now_date = now() company_name = shop.public_name or shop.name address = shop.contact_address full_address = "" if address: full_address = ", ".join( [ item for item in [ company_name, address.street, address.city, address.region_code, address.postal_code, address.country.code, ] if item ] ) context = { "last_updated": format_datetime(now(), "LLLL dd, YYYY").capitalize(), "company_name": company_name, "full_address": full_address, "store_email": address.email if address else "", } content = loader.render_to_string(template_name="shuup/admin/gdpr/privacy_policy_page.jinja", context=context) created = False if not gdpr_document: with create_revision(): gdpr_document = Page.objects.create( shop=shop, content=content, available_from=now_date, title=force_text(_("Privacy Policy")), url=settings.GDPR_PRIVACY_POLICY_PAGE_URLS.get(current_language, "privacy-policy"), ) created = True gdpr_settings = GDPRSettings.get_for_shop(shop) gdpr_settings.privacy_policy_page = gdpr_document gdpr_settings.save() # update only if it was created if created or force_update: for code, language in settings.LANGUAGES: if code == current_language: continue url = settings.GDPR_PRIVACY_POLICY_PAGE_URLS.get(code) if not url: continue activate(code) content = loader.render_to_string( template_name="shuup/admin/gdpr/privacy_policy_page.jinja", context=context ) gdpr_document.set_current_language(code) gdpr_document.title = force_text(_("Privacy Policy")) gdpr_document.url = url gdpr_document.content = content gdpr_document.save() # return to old language activate(current_language) gdpr_document.set_current_language(current_language) return gdpr_document
[docs]def should_reconsent_privacy_policy(shop, user): from shuup.gdpr.models import GDPRUserConsent consent = GDPRUserConsent.objects.filter(shop=shop, user=user).first() if not consent: return False privacy_policy = get_privacy_policy_page(shop) if not privacy_policy: return False return consent.should_reconsent_to_page(privacy_policy)
[docs]def get_privacy_policy_page(shop): from shuup.gdpr.models import GDPRSettings gdpr_settings = GDPRSettings.get_for_shop(shop) if not gdpr_settings.enabled: return None return gdpr_settings.privacy_policy_page