def get_device(self, device_id: str) -> dict: """Gets the device ready to be sent to another database.""" # It is just so easy to load stuff (through permissions, etc) through Python-Eve's API embedded = json.dumps({'components': 1}) projection = json.dumps({'events': 0}) token = AccountDomain.hash_token(AccountDomain.actual_token) url = '{}/devices/{}?embedded={}&projection={}'.format(self.database, device_id, embedded, projection) device = execute_get(url, token) self.clean_device(device) for component in device.get('components', []): self.clean_device(component) return device
def get_device(self, device_id: str) -> dict: """Gets the device ready to be sent to another database.""" # It is just so easy to load stuff (through permissions, etc) through Python-Eve's API embedded = json.dumps({'components': 1}) projection = json.dumps({'events': 0}) token = AccountDomain.hash_token(AccountDomain.actual_token) url = '{}/devices/{}?embedded={}&projection={}'.format( self.database, device_id, embedded, projection) device = execute_get(url, token) self.clean_device(device) for component in device.get('components', []): self.clean_device(component) return device
def export(db, resource): try: file_type = FILE_TYPE_MIME_TABLE[request.accept_mimetypes.best] except KeyError: raise NotAcceptable() ids = request.args.getlist('ids') token = AccountDomain.hash_token(AccountDomain.actual_token) group_by = request.args.get('groupBy') default_group = request.args.get('defaultGroup', 'Others') embedded = {'byUser': 1, 'events': 1, 'components': 1, 'place': 1, 'owners': 1} translator = SpreadsheetTranslator(current_app.config, group_by=group_by, default_group=default_group) exporter = Exporter(translator, embedded, token) spreadsheets, _ = exporter.export(ids, db, resource)[0] return excel.make_response_from_book_dict(spreadsheets, file_type, file_name=resource)
def create_account(email: str, password: str, databases: list, role: str = None, name: str = None, organization: str = None, blocked: bool = False, default_database: str = None, mongo_host: str = None, mongo_port: int = None, db_name: str = 'dh__accounts'): """ Creates an account. You can call the file directly:: create_account.py [email protected] 123 [\'db1\'] superuser :param email: :param password: :param databases: The databases the user has access to :param mongo_host: Leave empty to use default. :param mongo_port: Leave empty to use default. :param db_name: The name of the database to create the account to. Leave empty to use default. :param default_database: if none we use the first database of *databases*. :param role: the role of the user, if none, we use the default set in Account type. :param name: :param organization: :param blocked: False if it is not set :throws UserAlreadyExists: :return: The account with the *base256* token and *hashed* password. """ connection = MongoClient(mongo_host, mongo_port) db = connection[db_name] if db.accounts.find_one({'email': email}): raise UserAlreadyExists() account = { 'email': email, 'password': password, 'databases': databases, 'defaultDatabase': default_database or databases[0], '@type': 'Account', 'blocked': eval(blocked) if type(blocked) is str else blocked, 'active': True } if role is not None: account['role'] = role if name is not None: account['name'] = name if organization is not None: account['organization'] = organization token = generate_token() while db.accounts.find_one({'token': token}) is not None: token = generate_token() account["token"] = token hash_password([account]) db.accounts.insert(account) returned_account = db.accounts.find_one({'email': email}) return returned_account, AccountDomain.hash_token(returned_account['token'])
def login(): """ Performs a login. We make this out of eve, being totally open. :return: """ try: account = AccountDomain.get_one({'email': request.json['email']}) if not AccountDomain.verify_password(request.json['password'], account['password']): raise WrongCredentials() account['token'] = AccountDomain.hash_token(account['token']) account['_id'] = str(account['_id']) return jsonify(account) except (KeyError, TypeError, UserNotFound): raise WrongCredentials()
def export(db, resource): try: file_type = FILE_TYPE_MIME_TABLE[request.accept_mimetypes.best] except KeyError: raise NotAcceptable() ids = request.args.getlist('ids') token = AccountDomain.hash_token(AccountDomain.actual_token) group_by = request.args.get('groupBy') default_group = request.args.get('defaultGroup', 'Others') embedded = { 'byUser': 1, 'events': 1, 'components': 1, 'place': 1, 'owners': 1 } translator = SpreadsheetTranslator(current_app.config, group_by=group_by, default_group=default_group) exporter = Exporter(translator, embedded, token) spreadsheets, _ = exporter.export(ids, db, resource)[0] return excel.make_response_from_book_dict(spreadsheets, file_type, file_name=resource)
def create_account( email: str, password: str, databases: list, role: str = None, name: str = None, organization: str = None, blocked: bool = False, default_database: str = None, mongo_host: str = None, mongo_port: int = None, db_name: str = "dh__accounts", ): """ Creates an account. You can call the file directly:: create_account.py [email protected] 123 [\'db1\'] superuser :param email: :param password: :param databases: The databases the user has access to :param mongo_host: Leave empty to use default. :param mongo_port: Leave empty to use default. :param db_name: The name of the database to create the account to. Leave empty to use default. :param default_database: if none we use the first database of *databases*. :param role: the role of the user, if none, we use the default set in Account type. :param name: :param organization: :param blocked: False if it is not set :throws UserAlreadyExists: :return: The account with the *base256* token and *hashed* password. """ connection = MongoClient(mongo_host, mongo_port) db = connection[db_name] if db.accounts.find_one({"email": email}): raise UserAlreadyExists() databases = eval(databases) if type(databases) is not list: raise TypeError("databases has to be a list") account = { "email": email, "password": password, "databases": databases, "defaultDatabase": default_database or databases[0], "@type": "Account", "blocked": eval(blocked) if type(blocked) is str else blocked, "active": True, } if role is not None: account["role"] = role if name is not None: account["name"] = name if organization is not None: account["organization"] = organization token = generate_token() while db.accounts.find_one({"token": token}) is not None: token = generate_token() account["token"] = token hash_password([account]) db.accounts.insert(account) returned_account = db.accounts.find_one({"email": email}) return returned_account, AccountDomain.hash_token(returned_account["token"])