<commit_before>import rules

from agir.events.models import Event
from .actions.required_documents import get_is_blocking_project
from ..gestion.models import Projet
from ..groups.models import Membership
from ..lib.rules import is_authenticated_person


@rules.predicate
def is_hidden_event(role, event=None):
    return event is not None and event.visibility == Event.VISIBILITY_ADMIN


@rules.predicate
def is_public_event(role, event=None):
    return event is not None and event.visibility == Event.VISIBILITY_PUBLIC


@rules.predicate
def is_organizer_of_event(role, event=None):
    if event is None:
        return False

    # The person who created the event:
    if role.person.organized_events.filter(pk=event.pk).exists():
        return True

    # All the managers of the groups organizing the event:
    if event.organizers_groups.filter(
        memberships__person=role.person,
        memberships__membership_type__gte=Membership.MEMBERSHIP_TYPE_REFERENT,
    ).exists():
        return True

    return False


@rules.predicate
# From organizers and group organizers of event : get is at least manager
def is_at_least_manager_of_event(role, event=None):
    if event is None:
        return False

    # The person who created the event:
    if role.person.organized_events.filter(pk=event.pk).exists():
        return True

    # All the managers of the groups organizing the event:
    if event.organizers_groups.filter(
        memberships__person=role.person,
        memberships__membership_type__gte=Membership.MEMBERSHIP_TYPE_MANAGER,
    ).exists():
        return True

    return False


@rules.predicate
def is_own_rsvp(role, rsvp=None):
    return rsvp is not None and role.person == rsvp.person


@rules.predicate
def has_rsvp_for_event(role, event=None):
    return event is not None and role.person.rsvps.filter(event=event).exists()


@rules.predicate
def has_right_subscription(role, event=None):
    return event is not None and event.can_rsvp(role.person)


@rules.predicate
def is_free_event(role, event=None):
    return event is not None and event.is_free


@rules.predicate
def is_editable_event(role, event=None):
    return event is not None and event.subtype.is_editable


@rules.predicate
def has_event_with_missing_documents(role):
    organized_event_projects = Projet.objects.filter(
        event__in=role.person.organized_events.select_related("subtype")
        .exclude(subtype__related_project_type="")
        .exclude(visibility=Event.VISIBILITY_ADMIN)
    )
    return any(get_is_blocking_project(project) for project in organized_event_projects)


@rules.predicate
def can_respond_to_coorganization_invitation(role, invitation):
    if invitation and invitation.person_recipient == role.person:
        return True
    if invitation and invitation.group and role.person in invitation.group.referents:
        return True
    return False


rules.add_perm(
    "events.add_event", is_authenticated_person & ~has_event_with_missing_documents
)
rules.add_perm(
    "events.view_event",
    is_public_event
    | (~is_hidden_event & is_authenticated_person & is_at_least_manager_of_event),
)
rules.add_perm(
    "events.view_event_settings",
    ~is_hidden_event & is_authenticated_person & is_at_least_manager_of_event,
)
rules.add_perm(
    "events.upload_event_documents",
    ~is_hidden_event & is_authenticated_person & is_at_least_manager_of_event,
)
rules.add_perm(
    "events.change_event",
    ~is_hidden_event
    & is_editable_event
    & is_authenticated_person
    & is_at_least_manager_of_event,
)
rules.add_perm(<commit_msg>fix(events): ne plus bloquer la création d'événements<commit_after>import rules

from agir.events.models import Event
from .actions.required_documents import get_is_blocking_project
from ..gestion.models import Projet
from ..groups.models import Membership
from ..lib.rules import is_authenticated_person


@rules.predicate
def is_hidden_event(role, event=None):
    return event is not None and event.visibility == Event.VISIBILITY_ADMIN


@rules.predicate
def is_public_event(role, event=None):
    return event is not None and event.visibility == Event.VISIBILITY_PUBLIC


@rules.predicate
def is_organizer_of_event(role, event=None):
    if event is None:
        return False

    # The person who created the event:
    if role.person.organized_events.filter(pk=event.pk).exists():
        return True

    # All the managers of the groups organizing the event:
    if event.organizers_groups.filter(
        memberships__person=role.person,
        memberships__membership_type__gte=Membership.MEMBERSHIP_TYPE_REFERENT,
    ).exists():
        return True

    return False


@rules.predicate
# From organizers and group organizers of event : get is at least manager
def is_at_least_manager_of_event(role, event=None):
    if event is None:
        return False

    # The person who created the event:
    if role.person.organized_events.filter(pk=event.pk).exists():
        return True

    # All the managers of the groups organizing the event:
    if event.organizers_groups.filter(
        memberships__person=role.person,
        memberships__membership_type__gte=Membership.MEMBERSHIP_TYPE_MANAGER,
    ).exists():
        return True

    return False


@rules.predicate
def is_own_rsvp(role, rsvp=None):
    return rsvp is not None and role.person == rsvp.person


@rules.predicate
def has_rsvp_for_event(role, event=None):
    return event is not None and role.person.rsvps.filter(event=event).exists()


@rules.predicate
def has_right_subscription(role, event=None):
    return event is not None and event.can_rsvp(role.person)


@rules.predicate
def is_free_event(role, event=None):
    return event is not None and event.is_free


@rules.predicate
def is_editable_event(role, event=None):
    return event is not None and event.subtype.is_editable


@rules.predicate
def has_event_with_missing_documents(role):
    organized_event_projects = Projet.objects.filter(
        event__in=role.person.organized_events.exclude(
            subtype__related_project_type=""
        ).exclude(visibility=Event.VISIBILITY_ADMIN)
    )
    return any(get_is_blocking_project(project) for project in organized_event_projects)


@rules.predicate
def can_respond_to_coorganization_invitation(role, invitation):
    if invitation and invitation.person_recipient == role.person:
        return True
    if invitation and invitation.group and role.person in invitation.group.referents:
        return True
    return False


rules.add_perm("events.add_event", is_authenticated_person)
rules.add_perm(
    "events.view_event",
    is_public_event
    | (~is_hidden_event & is_authenticated_person & is_at_least_manager_of_event),
)
rules.add_perm(
    "events.view_event_settings",
    ~is_hidden_event & is_authenticated_person & is_at_least_manager_of_event,
)
rules.add_perm(
    "events.upload_event_documents",
    ~is_hidden_event & is_authenticated_person & is_at_least_manager_of_event,
)
rules.add_perm(
    "events.change_event",
    ~is_hidden_event
    & is_editable_event
    & is_authenticated_person
    & is_at_least_manager_of_event,
)
rules.add_perm(
{'fraction_non_alphanumeric': 0.07689087356963438, 'fraction_numerical': 0.0, 'mean_word_length': 4.293205317577548, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2443704', 'n_tokens_mistral': 2303, 'n_tokens_neox': 2205, 'n_words': 511}