<commit_before># -*- coding: UTF-8 -*-
# Copyright 2017-2019 Rumma & Ko Ltd
# License: BSD (see file COPYING for details)

from __future__ import unicode_literals

from builtins import str
from decimal import Decimal

from django.utils.translation import ugettext_lazy as _
from django.utils.translation import pgettext_lazy as pgettext

from etgen.html import E, join_elems

from lino.core.gfks import gfk2lookup
from lino_xl.lib.courses.models import *
from lino.modlib.users.mixins import UserAuthored
from lino_xl.lib.courses.roles import CoursesUser
from lino_xl.lib.excerpts.mixins import Certifiable
from lino_xl.lib.cal.choicelists import EntryStates, GuestStates
from lino_xl.lib.ledger.utils import ZERO, myround
# from lino.modlib.checkdata.choicelists import Checker
# from lino.modlib.summaries.mixins import SimpleSummary
from lino_avanti.lib.avanti.roles import ClientsUser

from .choicelists import ReminderStates, ReminderDegrees

# contacts = dd.resolve_app('contacts')


# class CourseProvider(contacts.Company):

#     """
#     A CourseProvider is a Company that offers Courses. 
#     """
#     class Meta:
#         app_label = 'courses'
#         verbose_name = _("Course provider")
#         verbose_name_plural = _("Course providers")

#     def disable_delete(self, ar=None):
#         # skip the is_imported_partner test
#         return super(contacts.Partner, self).disable_delete(ar)


# class CourseProviderDetail(contacts.CompanyDetail):
#     """Same as CompanyDetail, except that we add a tab
#     :guilabel:`Courses`.

#     """
#     box5 = "remarks"
#     main = "general courses.LinesByProvider"


# class CourseProviders(contacts.Companies):
#     """Table of all course providers

#     """
#     required_roles = dd.login_required(CoursesUser)
#     model = 'courses.CourseProvider'
#     detail_layout = CourseProviderDetail()


# class UpdateMissingRates(dd.Action):
#     label = _("Update missing rates")
#     button_text = ' ☉ '  # like gueststate 'missing'
#     # icon_name = 'lightning'
#     readonly = False

#     def run_from_ui(self, ar, **kw):
#         for obj in ar.selected_rows:
#             obj.run_update_missing_rates()
#         ar.success(refresh=True)


class Course(Course):
    
    class Meta(Course.Meta):
        # app_label = 'courses'
        abstract = dd.is_abstract_model(__name__, 'Course')
        # verbose_name = _("Course")
        # verbose_name_plural = _('Courses')

    @dd.virtualfield(models.IntegerField(_("Bus")))
    def bus_needed(self, ar):
        return self.get_places_sum(
            state=EnrolmentStates.requested, needs_bus=True)

    @dd.virtualfield(models.IntegerField(_("Childcare")))
    def childcare_needed(self, ar):
        return self.get_places_sum(
            state=EnrolmentStates.requested, needs_childcare=True)

    @dd.virtualfield(models.IntegerField(_("Evening")))
    def evening_needed(self, ar):
        return self.get_places_sum(
            state=EnrolmentStates.requested, needs_evening=True)

    @dd.virtualfield(models.IntegerField(_("School")))
    def school_needed(self, ar):
        return self.get_places_sum(
            state=EnrolmentStates.requested, needs_school=True)

    # def update_reminders(self, ar):
    #     super(Course, self).update_reminders(ar)
    #     self.run_update_missing_rates()
        
    @dd.action(label = _("Update missing rates"),
               button_text=' ☉ ')  # like gueststate 'missing'
    def update_missing_rates(self, ar):
        # Event = rt.models.cal.Event
        # expected_states = (EntryStates.took_place, EntryStates.draft)
        # done = Event.objects.filter(
        #     state__in=expected_states,
        #     **gfk2lookup(Event.owner, self)).count()
        
        for obj in self.enrolment_set.all():
            obj.update_missing_rate(ar)
            # flt = {'event__'+k: v
            #        for k, v in gfk2lookup(Event.owner, self).items()}

# class Line(Line):
    
#     class Meta(Line.Meta):
#         # app_label = 'courses'
#         abstract = dd.is_abstract_model(__name__, 'Course')
#         verbose_name = pgettext("singular form", "Course line")
#         verbose_name_plural = pgettext("plural form", 'Course lines')

#     provider = dd.ForeignKey(
#         'courses.CourseProvider', blank=True, null=True)
    

class Enrolment(Enrolment):
   
    class Meta(Enrolment.Meta):<commit_msg>update_missing_rates didn't work<commit_after># -*- coding: UTF-8 -*-
# Copyright 2017-2019 Rumma & Ko Ltd
# License: BSD (see file COPYING for details)

from __future__ import unicode_literals

from builtins import str
from decimal import Decimal

from django.utils.translation import ugettext_lazy as _
from django.utils.translation import pgettext_lazy as pgettext

from etgen.html import E, join_elems

from lino.core.gfks import gfk2lookup
from lino_xl.lib.courses.models import *
from lino.modlib.users.mixins import UserAuthored
from lino_xl.lib.courses.roles import CoursesUser
from lino_xl.lib.excerpts.mixins import Certifiable
from lino_xl.lib.cal.choicelists import EntryStates, GuestStates
from lino_xl.lib.ledger.utils import ZERO, myround
# from lino.modlib.checkdata.choicelists import Checker
# from lino.modlib.summaries.mixins import SimpleSummary
from lino_avanti.lib.avanti.roles import ClientsUser

from .choicelists import ReminderStates, ReminderDegrees

# contacts = dd.resolve_app('contacts')


# class CourseProvider(contacts.Company):

#     """
#     A CourseProvider is a Company that offers Courses. 
#     """
#     class Meta:
#         app_label = 'courses'
#         verbose_name = _("Course provider")
#         verbose_name_plural = _("Course providers")

#     def disable_delete(self, ar=None):
#         # skip the is_imported_partner test
#         return super(contacts.Partner, self).disable_delete(ar)


# class CourseProviderDetail(contacts.CompanyDetail):
#     """Same as CompanyDetail, except that we add a tab
#     :guilabel:`Courses`.

#     """
#     box5 = "remarks"
#     main = "general courses.LinesByProvider"


# class CourseProviders(contacts.Companies):
#     """Table of all course providers

#     """
#     required_roles = dd.login_required(CoursesUser)
#     model = 'courses.CourseProvider'
#     detail_layout = CourseProviderDetail()


# class UpdateMissingRates(dd.Action):
#     label = _("Update missing rates")
#     button_text = ' ☉ '  # like gueststate 'missing'
#     # icon_name = 'lightning'
#     readonly = False

#     def run_from_ui(self, ar, **kw):
#         for obj in ar.selected_rows:
#             obj.run_update_missing_rates()
#         ar.success(refresh=True)


class Course(Course):
    
    class Meta(Course.Meta):
        # app_label = 'courses'
        abstract = dd.is_abstract_model(__name__, 'Course')
        # verbose_name = _("Course")
        # verbose_name_plural = _('Courses')

    @dd.virtualfield(models.IntegerField(_("Bus")))
    def bus_needed(self, ar):
        return self.get_places_sum(
            state=EnrolmentStates.requested, needs_bus=True)

    @dd.virtualfield(models.IntegerField(_("Childcare")))
    def childcare_needed(self, ar):
        return self.get_places_sum(
            state=EnrolmentStates.requested, needs_childcare=True)

    @dd.virtualfield(models.IntegerField(_("Evening")))
    def evening_needed(self, ar):
        return self.get_places_sum(
            state=EnrolmentStates.requested, needs_evening=True)

    @dd.virtualfield(models.IntegerField(_("School")))
    def school_needed(self, ar):
        return self.get_places_sum(
            state=EnrolmentStates.requested, needs_school=True)

    # def update_reminders(self, ar):
    #     super(Course, self).update_reminders(ar)
    #     self.run_update_missing_rates()
        
    @dd.action(label = _("Update missing rates"),
               button_text=' ☉ ')  # like gueststate 'missing'
    def update_missing_rates(self, ar):
        # Event = rt.models.cal.Event
        # expected_states = (EntryStates.took_place, EntryStates.draft)
        # done = Event.objects.filter(
        #     state__in=expected_states,
        #     **gfk2lookup(Event.owner, self)).count()
        
        for obj in self.enrolment_set.all():
            obj.update_missing_rate.run_from_session(ar)
            # obj.update_missing_rate(ar)
            # flt = {'event__'+k: v
            #        for k, v in gfk2lookup(Event.owner, self).items()}

# class Line(Line):
    
#     class Meta(Line.Meta):
#         # app_label = 'courses'
#         abstract = dd.is_abstract_model(__name__, 'Course')
#         verbose_name = pgettext("singular form", "Course line")
#         verbose_name_plural = pgettext("plural form", 'Course lines')

#     provider = dd.ForeignKey(
#         'courses.CourseProvider', blank=True, null=True)
    

class Enrolment(Enrolment):
   
    class Meta(Enrolment.Meta):
{'fraction_non_alphanumeric': 0.11068702290076336, 'fraction_numerical': 0.0029187247418051188, 'mean_word_length': 3.212293144208038, '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': '106528', 'n_tokens_mistral': 2923, 'n_tokens_neox': 2806, 'n_words': 584}