<commit_before>from datetime import datetime
from django.db.models.loading import get_model
from django.core.exceptions import ValidationError
from calendartools import defaults
from calendartools.signals import collect_validators
from calendartools.validators.base import (
    BaseOccurrenceValidator, BaseUserAttendanceValidator
)


class FinishGTStartValidator(BaseOccurrenceValidator):
    def validate(self):
        if self.occurrence.start >= self.occurrence.finish:
            raise ValidationError(
                'Finish date/time must be greater than start date/time.'
            )


class FutureOccurrencesOnlyValidator(BaseOccurrenceValidator):
    def validate(self):
        if not self.occurrence.id and self.occurrence.start < datetime.now():
            raise ValidationError(
                'Event occurrences cannot be created in the past.'
            )


class MaxOccurrenceLengthValidator(BaseOccurrenceValidator):
    def validate(self):
        max_length = defaults.MAX_OCCURRENCE_DURATION
        if max_length:
            length = self.occurrence.finish - self.occurrence.start
            if not length <= max_length:
                raise ValidationError(
                    'Events must be less than %s long.' % max_length
                )


class MinOccurrenceLengthValidator(BaseOccurrenceValidator):
    def validate(self):
        min_length = defaults.MIN_OCCURRENCE_DURATION
        if min_length:
            length = self.occurrence.finish - self.occurrence.start
            if not length >= min_length:
                raise ValidationError(
                    'Events must be greater than %s long.' % min_length
                )


class CannotBookFinishedEventsValidator(BaseUserAttendanceValidator):
    def validate(self):
        if (not self.attendance.id and
            self.attendance.occurrence.finish < datetime.now()):
            raise ValidationError(
                'Cannot book/attend events which have occurred in the past.'
            )


class CannotAttendFutureEventsValidator(BaseUserAttendanceValidator):
    def validate(self):
        if (self.attendance.status == self.attendance.ATTENDED and
            self.attendance.occurrence.start > datetime.now()):
            raise ValidationError(
                'Cannot attend events which have not yet occurred.'
            )


class CannotCancelAttendedEventsValidator(BaseUserAttendanceValidator):
    def validate(self):
        if (self.attendance.status == self.attendance.CANCELLED
            and self.attendance.pk):
            Attendance = get_model(defaults.CALENDAR_APP_LABEL, 'Attendance')
            try:
                previous_status = Attendance.objects.values_list(
                    'status', flat=True).get(pk=self.attendance.pk)
                if previous_status == self.attendance.ATTENDED:
                    raise ValidationError(
                        'Cannot cancel attendance for events which have '
                        'already been attended.'
                    )
            except Attendance.DoesNotExist:
                pass


class OnlyOneActiveAttendanceForOccurrenceValidator(BaseUserAttendanceValidator):
    priority = 50

    def validate(self):
        Attendance = get_model(defaults.CALENDAR_APP_LABEL, 'Attendance')
        already_attending = Attendance.objects.filter(
            user=self.attendance.user,
            occurrence=self.attendance.occurrence,
            status__in=[Attendance.BOOKED, Attendance.ATTENDED]
        )
        if self.attendance.id:
            already_attending = already_attending.exclude(id=self.attendance.id)

        if already_attending.exists():
            raise ValidationError(
                'User already has an active attendance record for this event.'
            )

def activate_default_occurrence_validators():
    Occurrence = get_model(defaults.CALENDAR_APP_LABEL, 'Occurrence')
    collect_validators.connect(
        FinishGTStartValidator,
        sender=Occurrence
    )
    collect_validators.connect(
        FutureOccurrencesOnlyValidator,
        sender=Occurrence<commit_msg>Refactoring: objects ==> _default_manager for default validators<commit_after>from datetime import datetime
from django.db.models.loading import get_model
from django.core.exceptions import ValidationError
from calendartools import defaults
from calendartools.signals import collect_validators
from calendartools.validators.base import (
    BaseOccurrenceValidator, BaseUserAttendanceValidator
)


class FinishGTStartValidator(BaseOccurrenceValidator):
    def validate(self):
        if self.occurrence.start >= self.occurrence.finish:
            raise ValidationError(
                'Finish date/time must be greater than start date/time.'
            )


class FutureOccurrencesOnlyValidator(BaseOccurrenceValidator):
    def validate(self):
        if not self.occurrence.id and self.occurrence.start < datetime.now():
            raise ValidationError(
                'Event occurrences cannot be created in the past.'
            )


class MaxOccurrenceLengthValidator(BaseOccurrenceValidator):
    def validate(self):
        max_length = defaults.MAX_OCCURRENCE_DURATION
        if max_length:
            length = self.occurrence.finish - self.occurrence.start
            if not length <= max_length:
                raise ValidationError(
                    'Events must be less than %s long.' % max_length
                )


class MinOccurrenceLengthValidator(BaseOccurrenceValidator):
    def validate(self):
        min_length = defaults.MIN_OCCURRENCE_DURATION
        if min_length:
            length = self.occurrence.finish - self.occurrence.start
            if not length >= min_length:
                raise ValidationError(
                    'Events must be greater than %s long.' % min_length
                )


class CannotBookFinishedEventsValidator(BaseUserAttendanceValidator):
    def validate(self):
        if (not self.attendance.id and
            self.attendance.occurrence.finish < datetime.now()):
            raise ValidationError(
                'Cannot book/attend events which have occurred in the past.'
            )


class CannotAttendFutureEventsValidator(BaseUserAttendanceValidator):
    def validate(self):
        if (self.attendance.status == self.attendance.ATTENDED and
            self.attendance.occurrence.start > datetime.now()):
            raise ValidationError(
                'Cannot attend events which have not yet occurred.'
            )


class CannotCancelAttendedEventsValidator(BaseUserAttendanceValidator):
    def validate(self):
        if (self.attendance.status == self.attendance.CANCELLED
            and self.attendance.pk):
            Attendance = get_model(defaults.CALENDAR_APP_LABEL, 'Attendance')
            try:
                previous_status = Attendance._default_manager.values_list(
                    'status', flat=True).get(pk=self.attendance.pk)
                if previous_status == self.attendance.ATTENDED:
                    raise ValidationError(
                        'Cannot cancel attendance for events which have '
                        'already been attended.'
                    )
            except Attendance.DoesNotExist:
                pass


class OnlyOneActiveAttendanceForOccurrenceValidator(BaseUserAttendanceValidator):
    priority = 50

    def validate(self):
        Attendance = get_model(defaults.CALENDAR_APP_LABEL, 'Attendance')
        already_attending = Attendance._default_manager.filter(
            user=self.attendance.user,
            occurrence=self.attendance.occurrence,
            status__in=[Attendance.BOOKED, Attendance.ATTENDED]
        )
        if self.attendance.id:
            already_attending = already_attending.exclude(id=self.attendance.id)

        if already_attending.exists():
            raise ValidationError(
                'User already has an active attendance record for this event.'
            )

def activate_default_occurrence_validators():
    Occurrence = get_model(defaults.CALENDAR_APP_LABEL, 'Occurrence')
    collect_validators.connect(
        FinishGTStartValidator,
        sender=Occurrence
    )
    collect_validators.connect(
        FutureOccurrencesOnlyValidator,
        sender=Occurrence
{'fraction_non_alphanumeric': 0.0679108970499699, 'fraction_numerical': 0.00048163756773028296, 'mean_word_length': 2.4450435503940273, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '6421410', 'n_tokens_mistral': 2174, 'n_tokens_neox': 2122, 'n_words': 476}