def __init__(self):
        current_subject = Subject().get_current()
        if current_subject is None:
            logging.error("Cannot find current subject!!!")

        now = datetime.datetime.now()
        if now <= current_subject.active_to:
            return

        # invalidate currect subject
        current_subject.is_current = False

        # new subject to pick
        new_subject = (
            Subject.all().filter("active_from =", datetime.datetime(now.year, now.month, now.day, 0, 0, 0)).get()
        )
        if new_subject is None:
            logging.error("Cannot find next subject!!!")
        new_subject.is_current = True

        s = SaveState().get_by_k("recalc_subject_id")
        if s is not None and s.v != "0":
            logging.error(
                "Cannot start recalculating new subject (%s), because old one (%s) is still active!!!"
                % (new_subject.key().id(), current_subject.key().id())
            )
            return

        # ready to go!
        current_subject.put()
        new_subject.put()

        s = SaveState().get_by_k("recalc_subject_id")
        if s is None:
            s = SaveState()
            s.k = "recalc_subject_id"
            s.v = str(current_subject.key().id())
            s.put()
        else:
            s.v = str(current_subject.key().id())
            s.put()

        s = SaveState().get_by_k("recalc_action")
        if s is None:
            s = SaveState()
            s.k = "recalc_action"
            s.v = "points"
            s.put()
        else:
            s.v = "points"
            s.put()
    def __init__(self):
        subject = SaveState().get_by_k('recalc_subject_id')
        if subject is None or subject.v == '0':
            return

        task = SaveState().get_by_k('recalc_action')
        if task is None:
            task = SaveState()
            task.k = 'recalc_action'
            task.v = 'points'
            task.put()
        if task.v in ('points', 'final_score', 'subject', 'leaderboard'):
            taskqueue.add(url='/api/task/', params={'type': task.v, 'subject_id': int(subject.v)})
        else:
            logging.error('Not valid recalc with combination: %s|%s' % (subject.v, task.v))
            subject.delete()
            task.delete()