from datetime import date
from plone import api
from plone.dexterity.utils import datify


def autoset_news_publication_date(obj, event):
    """If a newly created news has no effective date, set it to today"""
    if obj.effective_date is not None:
        return
    unpublished_states = api.portal.get_registry_record(
        "ploneintranet.news.unpublished_states", default=["private", "pending"]
    )
    if api.content.get_state(obj) in unpublished_states:
        return
    obj.effective_date = datify(date.today())
