<commit_before>from enum import Enum

from django.conf import settings
from django.db import models, transaction
from django.utils import timezone

from foodsaving.applications.stats import application_status_update
from foodsaving.applications.tasks import notify_about_accepted_application, notify_about_declined_application
from foodsaving.base.base_models import BaseModel
from foodsaving.history.models import History, HistoryTypus
from foodsaving.utils import markdown


class GroupApplicationStatus(Enum):
    PENDING = 'pending'
    ACCEPTED = 'accepted'
    DECLINED = 'declined'
    WITHDRAWN = 'withdrawn'


class GroupApplication(BaseModel):
    group = models.ForeignKey('groups.Group', on_delete=models.CASCADE)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    decided_at = models.DateTimeField(null=True)
    decided_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.SET_NULL,
        null=True,
        related_name='groupapplication_decided',
    )
    questions = models.TextField()
    answers = models.TextField()
    status = models.CharField(
        default=GroupApplicationStatus.PENDING.value,
        choices=[(status.value, status.value) for status in GroupApplicationStatus],
        max_length=100,
    )

    def questions_rendered(self, **kwargs):
        return markdown.render(self.questions, **kwargs)

    def answers_rendered(self, **kwargs):
        return markdown.render(self.answers, **kwargs)

    def save(self, **kwargs):
        old = type(self).objects.get(pk=self.pk) if self.pk else None
        super().save(**kwargs)
        if old is None or old.status != self.status:
            application_status_update(self)

    @transaction.atomic
    def accept(self, accepted_by):
        self.status = GroupApplicationStatus.ACCEPTED.value
        self.decided_by = accepted_by
        self.decided_at = timezone.now()
        self.save()
        self.group.add_member(
            self.user,
            added_by=accepted_by,
            history_payload={
                'accepted_by': accepted_by.id,
                'application_date': self.created_at.isoformat(),
            }
        )
        notify_about_accepted_application(self)

    @transaction.atomic
    def decline(self, declined_by):
        self.status = GroupApplicationStatus.DECLINED.value
        self.decided_by = declined_by
        self.decided_at = timezone.now()
        self.save()
        History.objects.create(
            typus=HistoryTypus.GROUP_APPLICATION_DECLINED,
            group=self.group,
            users=[declined_by],
            payload={
                'applicant': self.user.id,
                'applicant_name': self.user.display_name,
                'application_date': self.created_at.isoformat()
            }
        )

        notify_about_declined_application(self)

    @transaction.atomic
    def withdraw(self):
        self.status = GroupApplicationStatus.WITHDRAWN.value
        self.save()<commit_msg>GroupApplications: set decided_* fields when withdrawing<commit_after>from enum import Enum

from django.conf import settings
from django.db import models, transaction
from django.utils import timezone

from foodsaving.applications.stats import application_status_update
from foodsaving.applications.tasks import notify_about_accepted_application, notify_about_declined_application
from foodsaving.base.base_models import BaseModel
from foodsaving.history.models import History, HistoryTypus
from foodsaving.utils import markdown


class GroupApplicationStatus(Enum):
    PENDING = 'pending'
    ACCEPTED = 'accepted'
    DECLINED = 'declined'
    WITHDRAWN = 'withdrawn'


class GroupApplication(BaseModel):
    group = models.ForeignKey('groups.Group', on_delete=models.CASCADE)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    decided_at = models.DateTimeField(null=True)
    decided_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.SET_NULL,
        null=True,
        related_name='groupapplication_decided',
    )
    questions = models.TextField()
    answers = models.TextField()
    status = models.CharField(
        default=GroupApplicationStatus.PENDING.value,
        choices=[(status.value, status.value) for status in GroupApplicationStatus],
        max_length=100,
    )

    def questions_rendered(self, **kwargs):
        return markdown.render(self.questions, **kwargs)

    def answers_rendered(self, **kwargs):
        return markdown.render(self.answers, **kwargs)

    def save(self, **kwargs):
        old = type(self).objects.get(pk=self.pk) if self.pk else None
        super().save(**kwargs)
        if old is None or old.status != self.status:
            application_status_update(self)

    @transaction.atomic
    def accept(self, accepted_by):
        self.status = GroupApplicationStatus.ACCEPTED.value
        self.decided_by = accepted_by
        self.decided_at = timezone.now()
        self.save()
        self.group.add_member(
            self.user,
            added_by=accepted_by,
            history_payload={
                'accepted_by': accepted_by.id,
                'application_date': self.created_at.isoformat(),
            }
        )
        notify_about_accepted_application(self)

    @transaction.atomic
    def decline(self, declined_by):
        self.status = GroupApplicationStatus.DECLINED.value
        self.decided_by = declined_by
        self.decided_at = timezone.now()
        self.save()
        History.objects.create(
            typus=HistoryTypus.GROUP_APPLICATION_DECLINED,
            group=self.group,
            users=[declined_by],
            payload={
                'applicant': self.user.id,
                'applicant_name': self.user.display_name,
                'application_date': self.created_at.isoformat()
            }
        )

        notify_about_declined_application(self)

    @transaction.atomic
    def withdraw(self):
        self.status = GroupApplicationStatus.WITHDRAWN.value
        self.decided_by = self.user
        self.decided_at = timezone.now()
        self.save()
{'fraction_non_alphanumeric': 0.0900259067357513, 'fraction_numerical': 0.0009715025906735751, 'mean_word_length': 2.855805243445693, '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': '6592238', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1710, 'n_words': 324}