def api_list_users(): """ Retrieve the users cached in the local database :rtype: Response :return """ if "POST" == request.method: per_page = utils.get_safe_int(request.form.get("per_page")) page_num = utils.get_safe_int(request.form.get("page_num")) else: per_page = utils.get_safe_int(request.args.get("per_page")) page_num = utils.get_safe_int(request.args.get("page_num")) pagination = UserEntity.query.order_by(db.desc(UserEntity.id)).paginate(page_num, per_page, False) items = [i.serialize() for i in pagination.items] return utils.jsonify_success({"total_pages": pagination.pages, "list_of_users": items})
def api_send_verification_email(): """ Send Verification Email to the `user_id` specified in the request :rtype: Response :return the success or failed in json format """ passed, errors = check_email_config() if not passed: app.logger.warn(" ".join(errors)) return utils.jsonify_error( {"message": "Unable to send email due to configuration errors."}) user_id = utils.get_safe_int(request.form.get('user_id')) user = UserEntity.get_by_id(user_id) try: emails.send_verification_email(user) return utils.jsonify_success( {"message": "Verification email was sent."}) except Exception as exc: details = "Connection config: {}/{}:{}".format( app.config['MAIL_USERNAME'], app.config['MAIL_SERVER'], app.config['MAIL_PORT']) app.logger.debug(details) return utils.jsonify_error( {"message": "Unable to send email due: {} {}".format(exc, details)})
def api_list_logs(): """ Render the specified page of event logs @TODO: show user-specific logs for non-admins? :rtype: string :return the json list of logs """ if "POST" == request.method: per_page = utils.get_safe_int(request.form.get("per_page")) page_num = utils.get_safe_int(request.form.get("page_num")) else: per_page = utils.get_safe_int(request.args.get("per_page")) page_num = utils.get_safe_int(request.args.get("page_num")) logs, total_pages = LogEntity.get_logs(per_page, page_num) return utils.jsonify_success(dict(list_of_events=logs, total_pages=total_pages))
def api_list_users(): """ Retrieve the users cached in the local database :rtype: Response :return """ if 'POST' == request.method: per_page = utils.get_safe_int(request.form.get('per_page')) page_num = utils.get_safe_int(request.form.get('page_num')) else: per_page = utils.get_safe_int(request.args.get('per_page')) page_num = utils.get_safe_int(request.args.get('page_num')) pagination = UserEntity.query.order_by( db.desc(UserEntity.id)).paginate(page_num, per_page, False) items = [i.serialize() for i in pagination.items] return utils.jsonify_success( {"total_pages": pagination.pages, "list_of_users": items})
def api_list_logs(): """ Render the specified page of event logs @TODO: show user-specific logs for non-admins? :rtype: string :return the json list of logs """ if 'POST' == request.method: per_page = utils.get_safe_int(request.form.get('per_page')) page_num = utils.get_safe_int(request.form.get('page_num')) else: per_page = utils.get_safe_int(request.args.get('per_page')) page_num = utils.get_safe_int(request.args.get('page_num')) logs, total_pages = LogEntity.get_logs(per_page, page_num) return utils.jsonify_success( dict(list_of_events=logs, total_pages=total_pages))
def api_deactivate_account(): """ De-activate an user. @TODO: should change expiration date too? :rtype: Response :return the success or failed in json format """ user_id = utils.get_safe_int(request.form.get("user_id")) user = UserEntity.get_by_id(user_id) user = UserEntity.update(user, active=False) LogEntity.account_modified(session["uuid"], "User deactivated: {}".format(user)) return utils.jsonify_success({"message": "User deactivated."})
def api_deactivate_account(): """ De-activate an user. @TODO: should change expiration date too? :rtype: Response :return the success or failed in json format """ user_id = utils.get_safe_int(request.form.get('user_id')) user = UserEntity.get_by_id(user_id) user = UserEntity.update(user, active=False) LogEntity.account_modified(session['uuid'], "User deactivated: {}".format(user)) return utils.jsonify_success({"message": "User deactivated."})
def api_expire_account(): """ Change the `User.usrAccessExpiresAt` to today's date and 00:00:00 time effectively blocking the user access. :rtype: Response :return the success or failed in json format """ user_id = utils.get_safe_int(request.form.get("user_id")) user = UserEntity.get_by_id(user_id) today = datetime.today() today_start = datetime(today.year, today.month, today.day) user = UserEntity.update(user, access_expires_at=today_start) # @TODO: add dedicated log type LogEntity.account_modified(session["uuid"], "User access was expired. {}".format(user.email)) return utils.jsonify_success({"message": "User access was expired."})
def api_expire_account(): """ Change the `User.usrAccessExpiresAt` to today's date and 00:00:00 time effectively blocking the user access. :rtype: Response :return the success or failed in json format """ user_id = utils.get_safe_int(request.form.get('user_id')) user = UserEntity.get_by_id(user_id) today = datetime.today() today_start = datetime(today.year, today.month, today.day) user = UserEntity.update(user, access_expires_at=today_start) # @TODO: add dedicated log type LogEntity.account_modified(session['uuid'], "User access was expired. {}".format(user.email)) return utils.jsonify_success({"message": "User access was expired."})
def api_send_verification_email(): """ @TODO: Send Verification Email to user_id :rtype: Response :return the success or failed in json format """ user_id = utils.get_safe_int(request.form.get("user_id")) user = UserEntity.get_by_id(user_id) try: emails.send_verification_email(user) return utils.jsonify_success({"message": "Verification email was sent."}) except Exception as exc: details = "Connection config: {}/{}:{}".format( app.config["MAIL_USERNAME"], app.config["MAIL_SERVER"], app.config["MAIL_PORT"] ) app.logger.debug(details) return utils.jsonify_error({"message": "Unable to send email due: {} {}".format(exc, details)})