Пример #1
0
 def __init__(self, door_bell_controls):
     self.door_bell_controls = door_bell_controls
     self.on_bell_rung_notification = None
     self.on_opened_door_notification = None
     self.open_always = False
     self.token_manager = TokenManager()
     self.timer = None
     pass
Пример #2
0
    def writ_inactive(cls, writ):
        if writ.settings.turn_type == writ.settings.choices.turn_type.FREE_FOR_ALL: 
            delay_interval = timedelta(seconds=writ.settings.inactive_time / 4)
            if datetime.now() - writ.last_email_sent_at > delay_interval:
                bwuser = writ.get_random_bwuser()
                while bwuser.pk == writ.last_email_sent_to.pk:
                    bwuser = writ.get_random_bwuser()
                
                if not bwuser.email_reminders_enabled or not cls.is_sending_enabled:
                    return
                rec = None
                if writ.is_testing:
                    rec = [TEST_EMAIL]
                else:
                    rec = [bwuser.authuser.email]
                if cls.is_realtime_mode:
                    context = {
                        'penname':bwuser.get_penname(),
                        'first_line':writ.get_first_line(),
                        'direct_login_link': TokenManager.make_autologin_url(authuser, writ.get_absolute_url()),
                    }

                    m = make_email_message(rec, 'turn_reminder', context)
                    m.send()
                    writ.last_email_sent_to = bwuser
                    writ.last_email_sent_at = datetime.now()
                    writ.save()
            
        elif writ.settings.turn_type == writ.settings.choices.turn_type.ROUND_ROBIN: 
            raise Exception("Invalid turn type.") 
Пример #3
0
    def bwuser_begins_turn(cls, bwuser, writ):
        if not bwuser.email_reminders_enabled or not cls.is_sending_enabled:
            return
        if cls.is_realtime_mode:

            context = {
                'penname':bwuser.get_penname(),
                'first_line':writ.get_first_line(),
                'direct_login_link': TokenManager.make_autologin_url(bwuser.authuser, writ.get_absolute_url()),
            }

            rec = None
            if writ.is_testing:
                rec = [TEST_EMAIL]
            else:
                rec = [bwuser.authuser.email]
            
            m = make_email_message(rec, 'turn_reminder', context)
            if m: m.send()
            
            writ.last_email_sent_to = bwuser
            writ.last_email_sent_at = datetime.now()
            writ.save()
Пример #4
0
class Engine:
    def __init__(self, door_bell_controls):
        self.door_bell_controls = door_bell_controls
        self.on_bell_rung_notification = None
        self.on_opened_door_notification = None
        self.open_always = False
        self.token_manager = TokenManager()
        self.timer = None
        pass

    def start(self):
        self.door_bell_controls.watch_door_bell_ring(self.on_ring)
        pass

    def insert_token(self, token=None):
        expire_time = DEFAULT_EXPIRE_TIME_TOKENS * 1000
        if token:
            self.token_manager.insert_token(token, expire_time)
            return token
        else:
            return self.token_manager.insert_random_token(expire_time)

    def activate_token(self, token):
        if self.token_manager.token_is_valid(token):
            self.token_manager.delete_token(token)
            self.open_always = True
            self.reset_timer_for_token()
        else:
            print("Access denied: Token was not valid or expired")

    def disable_always_open(self):
        self.open_always = False

    def reset_timer_for_token(self):
        # cancel timer if exist
        if self.timer:
            self.timer.cancel()

        # start timer and after that
        self.timer = Timer(DEFAULT_TIME_FOR_RING_AND_OPEN,
                           self.disable_always_open)
        self.timer.start()
        pass

    def on_ring(self):
        if self.on_bell_rung_notification:
            self.on_bell_rung_notification()

        if self.open_always:
            self.open_door()
            if self.on_opened_door_notification:
                self.on_opened_door_notification()
        pass

    def open_door(self):
        self.door_bell_controls.open_door()
        if self.light_is_needed():
            self.door_bell_controls.turn_lights_on()
        pass

    def set_on_bell_rung_notification(self, callback):
        self.on_bell_rung_notification = callback
        pass

    def set_on_opened_door_notification(self, callback):
        self.on_opened_door_notification = callback
        pass

    @staticmethod
    def light_is_needed():
        return False