def open_doors(self, account: Account, doors): can_open_door = authz_management.can_haz_door_access(account, doors) if not can_open_door: f = "{} does not have an active membership, or lacks door circle membership" raise P2k16UserException(f.format(account.display_name())) if not event_management.has_opened_door(account): system = Account.find_account_by_username("system") logger.info("First door opening for {}".format(account)) badge_management.create_badge(account, system, "first-door-opening") for door in doors: lf = "Opening door. username={}, door={}, open_time={}" logger.info(lf.format(account.username, door.key, door.open_time)) event_management.save_event(OpenDoorEvent(door.key)) # Make sure everything has been written to the database before actually opening the door. db.session.flush() # TODO: move this to a handler that runs after the transaction is done # TODO: we can look at the responses and see if they where successfully sent/received. for door in doors: if isinstance(door, DlockDoor): self.dlock.open(door) elif isinstance(door, MqttDoor): self.mqtt.open(door) else: P2k16TechnicalException("Unknown kind of door")
def open_doors(self, account: Account, doors: List[Door]): can_open_door = authz_management.can_haz_door_access(account) if not can_open_door: raise P2k16UserException( '{} does not have an active membership, or lack door circle membership' .format(account.display_name())) publishes = [] if not event_management.has_opened_door(account): system = Account.find_account_by_username("system") logger.info("First door opening for {}".format(account)) badge_management.create_badge(account, system, "first-door-opening") for door in doors: logger.info( 'Opening door. username={}, door={}, open_time={}'.format( account.username, door.key, door.open_time)) event_management.save_event(OpenDoorEvent(door.key)) publishes.append((self.prefix + door.topic, str(door.open_time))) # Make sure everything has been written to the database before actually opening the door. db.session.flush() # TODO: move this to a handler that runs after the transaction is done # TODO: we can look at the responses and see if they where successfully sent/received. for topic, open_time in publishes: logger.info("Sending message: {}: {}".format(topic, open_time)) self._client.publish(topic, open_time)
def open_doors(self, account: Account, doors: List[Door]): door_circle = Circle.get_by_name('door') can_open_door = False if account_management.is_account_in_circle( account, door_circle) and membership_management.active_member(account): can_open_door = True if len(Company.find_active_companies_with_account(account.id)) > 0: can_open_door = True if not can_open_door: # Only non-office users may fail here if not membership_management.active_member(account): raise P2k16UserException( '{} does not have an active membership'.format( account.display_name())) if not account_management.is_account_in_circle( account, door_circle): raise P2k16UserException('{} is not in the door circle'.format( account.display_name())) publishes = [] if not event_management.has_opened_door(account): system = Account.find_account_by_username("system") logger.info("First door opening for {}".format(account)) badge_management.create_badge(account, system, "first-door-opening") for door in doors: logger.info( 'Opening door. username={}, door={}, open_time={}'.format( account.username, door.key, door.open_time)) event_management.save_event(OpenDoorEvent(door.key)) publishes.append((self.prefix + door.topic, str(door.open_time))) # Make sure everything has been written to the database before actually opening the door. db.session.flush() # TODO: move this to a handler that runs after the transaction is done # TODO: we can look at the responses and see if they where successfully sent/received. for topic, open_time in publishes: logger.info("Sending message: {}: {}".format(topic, open_time)) self._client.publish(topic, open_time)
def create(): account = flask_login.current_user.account # type: Account title = request.json["title"] recipient_username = request.json.get("recipient", None) if recipient_username: recipient = Account.find_account_by_username(recipient_username) if not recipient: raise P2k16UserException( "No such username: {}".format(recipient_username)) else: recipient = account badge_management.create_badge(recipient, account, title) circles = account_management.get_circles_for_account(account.id) badges = badge_management.badges_for_account(account.id) db.session.commit() return jsonify(account_to_json(account, circles, badges))