Пример #1
0
    def _try_edit_user_password(self, data: dict, user: User):
        new_password = data['new_password']
        old_password = data['old_password']

        old_password_confirmed = False
        if old_password:
            old_password = self._get_validated_password(old_password)
            old_password_confirmed = check_password_hash(
                user.password, old_password)
            if not old_password_confirmed:
                msg = 'Das alte Passwort ist nicht korrekt.'
                logger.debug(msg)
                raise ResourceException(msg)

        if old_password and not new_password:
            msg = 'Anfrage enthält nicht das neue Passwort.'
            logger.debug(msg)
            raise ResourceException(msg)
        if new_password and not old_password:
            msg = 'Anfrage enthält nicht das alte Passwort.'
            logger.debug(msg)
            raise ResourceException(msg)

        new_password_differs = False
        if new_password:
            new_password = self._get_validated_password(new_password)
            new_password_differs = not check_password_hash(
                user.password, new_password)

        if old_password_confirmed and new_password_differs:
            logger.debug('Trying to change password of user {}'.format(
                user.public_id))
            hashed_password = generate_password_hash(new_password,
                                                     method='sha256')
            user.password = hashed_password
Пример #2
0
 def _get_validated_email(email: str) -> str:
     if not email:
         raise ResourceException('Email-Adresse fehlt.')
     try:
         validated_email = validate_email(email)
     except EmailNotValidError as err:
         raise ResourceException(
             'Email-Adresse {0} hat kein valides Format: {1}'.format(
                 email, str(err)))
     except EmailSyntaxError as err:
         raise ResourceException(
             'Email-Adresse {0} hat keine valide Syntax: {1}'.format(
                 email, str(err)))
     return validated_email['email']
Пример #3
0
 def _check_response(self, response):
     status_code = response.status_code
     if 200 <= status_code <= 302:
         return
     json_obj = response.json()
     raise ResourceException(status_code=status_code,
                             message=json_obj['message'])
Пример #4
0
 def open(self):
     try:
         self._api.use_session(self)
     except DeviceBlockedException:
         raise ResourceException(status_code=405,
                                 message='Gerät ist aktuell belegt.')
     self._gpio_session.open()
Пример #5
0
 def turn_off(self, token: str) -> DeviceStatus:
     status = CM_API.status
     if status.is_off():
         raise ResourceException(status_code=405,
                                 message='Kaffeemaschine ist nicht bereit.')
     CM_API.toggle_power()
     return CM_API.status
Пример #6
0
 def _get_user(self, **kwargs):
     public_id = kwargs['public_id']
     if public_id is None:
         raise ResourceException('Public ID fehlt.')
     user = User.query.filter_by(public_id=public_id).first()
     if user is None:
         raise ResourceNotFound(
             'Nutzer mit ID {} nicht gefunden.'.format(public_id))
     return user
Пример #7
0
 def make_coffee(self, doses: int):
     if doses == 1:
         pin = GPIO_PINS['ONE_DOSE']
     elif doses == 2:
         pin = GPIO_PINS['TWO_DOSES']
     else:
         msg = 'Kaffeeauftrag mit {doses} Dosen nicht möglich.'.format(
             doses=doses)
         logger.error(msg)
         raise ResourceException(status_code=405, message=msg)
     self.press_button(pin=pin)
Пример #8
0
    def set_status(self, token: str, status: EditDeviceStatus) -> DeviceStatus:
        session = RemoteSession(cm_hw_api=CM_API)
        session.open()
        try:
            current_status = CM_API.status

            state_id = status.coffee_machine_runtime_state
            if not (state_id is None):
                state = status.device_runtime_state
                logger.info('Changing runtime state.')
                if state == DeviceRuntimeState.ON:
                    current_status = self.turn_on(token, current_status)
                elif state == DeviceRuntimeState.OFF:
                    current_status = self.turn_off(token, current_status)
                # No other actions are allowed if runtime state changed
                new_status = CM_API.status
                new_status.coffee_machine_runtime_state = state.state_id
                return new_status

            eco_mode = status.device_eco_mode
            if not (eco_mode is None):
                logger.info('Changing eco mode.')
                if eco_mode:
                    current_status = self.turn_eco_mode_on(current_status)
                elif not eco_mode:
                    current_status = self.turn_eco_mode_off(current_status)

            maintenance = status.device_maintenance
            if not (maintenance is None):
                logger.info('Changing maintenance.')
                if maintenance:
                    current_status = self.turn_maintenance_on(current_status)
                elif not maintenance:
                    current_status = self.turn_maintenance_off(current_status)

            steam = status.device_steam
            if not (steam is None):
                logger.info('Changing steam.')
                if steam:
                    current_status = self.turn_steam_on(current_status)
                elif not steam:
                    current_status = self.turn_steam_off(current_status)

            new_status = CM_API.status
        except ValueError as err:
            raise ResourceException(status_code=404, message=str(err))
        finally:
            session.close()

        return new_status
Пример #9
0
 def set_coffee_strength_in_percent(self, coffee_strength_in_percent: int):
     address = I2C_ADDRESS_MAPPINGS['COFFEE_STRENGTH']
     sensor_name = 'Coffee Strength'
     bus_number = I2C_BUS_NUMBER
     try:
         set_dac_value(bus_number=bus_number,
                       address=address,
                       percent_value=coffee_strength_in_percent,
                       i2c_delay=I2C_DELAY,
                       sensor_name=sensor_name)
     except ValueError as err:
         raise ResourceException(
             status_code=400,
             message='Percent value of sensor {0} is invalid: {1}'.format(
                 sensor_name, coffee_strength_in_percent))
Пример #10
0
 def set_water_in_percent(self, water_in_percent: int):
     address = I2C_ADDRESS_MAPPINGS['WATER']
     sensor_name = 'Water'
     bus_number = I2C_BUS_NUMBER
     try:
         set_dac_value(bus_number=bus_number,
                       address=address,
                       percent_value=water_in_percent,
                       i2c_delay=I2C_DELAY,
                       sensor_name=sensor_name)
     except ValueError as err:
         raise ResourceException(
             status_code=400,
             message='Percent value of sensor {0} is invalid: {1}'.format(
                 sensor_name, water_in_percent))
Пример #11
0
    def create_job(self, token: str, create_job: CreateDeviceJob) -> DeviceJob:
        status = CM_API.status
        if not status.device_ready:
            raise ResourceException(status_code=405,
                                    message='Kaffeemaschine ist nicht bereit.')
        settings = CM_API.settings
        create_job_body = {**(create_job.__dict__), **(settings.__dict__)}
        water_in_percent = create_job.water_in_percent
        water_in_percent = get_percent_value(value=water_in_percent,
                                             accuracy=0)
        coffee_strength_in_percent = create_job.coffee_strength_in_percent
        coffee_strength_in_percent = get_percent_value(
            value=coffee_strength_in_percent, accuracy=0)
        doses = create_job.doses
        logger.debug('Doses: {0}, Water: {1}, Coffee: {2}'.format(
            doses, water_in_percent, coffee_strength_in_percent))
        CM_API.set_water_in_percent(water_in_percent=water_in_percent)
        CM_API.set_coffee_strength_in_percent(
            coffee_strength_in_percent=coffee_strength_in_percent)
        CM_API.make_coffee(doses=doses)
        response = WEB_API.create_job(token, create_job_body)
        response_json = response.json()

        return DeviceJob(**response_json)
Пример #12
0
 def _get_validated_user_name(name: str) -> str:
     if not name:
         raise ResourceException('Nutzername fehlt.')
     if len(name) < 6:
         raise ResourceException('Nutzername ist zu kurz (min. 6 Zeichen).')
     return name
Пример #13
0
 def _get_validated_password(password: str) -> str:
     if not password:
         raise ResourceException('Passwort fehlt.')
     if len(password) < 6:
         raise ResourceException('Password ist zu kurz (min. 6 Zeichen).')
     return password
Пример #14
0
def get_validated_request_body_as_json(template: dict):
    _data = request.get_json()
    if not is_dict_structure_equal(template, _data):
        raise ResourceException('Anfrage enthält unbekannte Wertkombinationen.')
    return _data