@admin_bp.route('/history', methods=['GET', 'POST'])
@login_required
@history_access_required
def history():
    if (request.method == 'POST'):
        if (current_user.role.name != 'Administrator'):
            return make_response(jsonify({'status': 'error', 'msg': 'You do not have permission to remove history.'}), 401)
        h = History()
        result = h.remove_all()
        if result:
            history = History(msg='Remove all histories', created_by=current_user.username)
            history.add()
            return make_response(jsonify({'status': 'ok', 'msg': 'Changed user role successfully.'}), 200)
        else:
            return make_response(jsonify({'status': 'error', 'msg': 'Can not remove histories.'}), 500)
    if (request.method == 'GET'):
        doms = accounts = users = ''
        if (current_user.role.name in ['Administrator', 'Operator']):
            all_domain_names = Domain.query.all()
            all_account_names = Account.query.all()
            all_user_names = User.query.all()
            for d in all_domain_names:
                doms += (d.name + ' ')
            for acc in all_account_names:
                accounts += (acc.name + ' ')
            for usr in all_user_names:
                users += (usr.username + ' ')
        else:
            all_domain_names = db.session.query(Domain).outerjoin(DomainUser, (Domain.id == DomainUser.domain_id)).outerjoin(Account, (Domain.account_id == Account.id)).outerjoin(AccountUser, (Account.id == AccountUser.account_id)).filter(db.or_((DomainUser.user_id == current_user.id), (AccountUser.user_id == current_user.id))).all()
            all_account_names = db.session.query(Account).outerjoin(Domain, (Domain.account_id == Account.id)).outerjoin(DomainUser, (Domain.id == DomainUser.domain_id)).outerjoin(AccountUser, (Account.id == AccountUser.account_id)).filter(db.or_((DomainUser.user_id == current_user.id), (AccountUser.user_id == current_user.id))).all()
            all_user_names = []
            for a in all_account_names:
                temp = db.session.query(User).join(AccountUser, (AccountUser.user_id == User.id)).outerjoin(Account, (Account.id == AccountUser.account_id)).filter(db.or_((Account.id == a.id), (AccountUser.account_id == a.id))).all()
                for u in temp:
                    if (u in all_user_names):
                        continue
                    all_user_names.append(u)
            for d in all_domain_names:
                doms += (d.name + ' ')
            for a in all_account_names:
                accounts += (a.name + ' ')
            for u in all_user_names:
                users += (u.username + ' ')
        return render_template('admin_history.html', all_domain_names=doms, all_account_names=accounts, all_usernames=users)
@admin_bp.route('/history', methods=['GET', 'POST'])
@login_required
@history_access_required
def history():
    if (request.method == 'POST'):
        if (current_user.role.name != 'Administrator'):
            return make_response(jsonify({'status': 'error', 'msg': 'You do not have permission to remove history.'}), 401)
        h = History()
        result = h.remove_all()
        if result:
            history = History(msg='Remove all histories', created_by=current_user.username)
            history.add()
            return make_response(jsonify({'status': 'ok', 'msg': 'Changed user role successfully.'}), 200)
        else:
            return make_response(jsonify({'status': 'error', 'msg': 'Can not remove histories.'}), 500)
    if (request.method == 'GET'):
        doms = accounts = users = 
        if (current_user.role.name in ['Administrator', 'Operator']):
            all_domain_names = Domain.query.all()
            all_account_names = Account.query.all()
            all_user_names = User.query.all()
            for d in all_domain_names:
                doms += (d.name + ' ')
            for acc in all_account_names:
                accounts += (acc.name + ' ')
            for usr in all_user_names:
                users += (usr.username + ' ')
        else:
            all_domain_names = db.session.query(Domain).outerjoin(DomainUser, (Domain.id == DomainUser.domain_id)).outerjoin(Account, (Domain.account_id == Account.id)).outerjoin(AccountUser, (Account.id == AccountUser.account_id)).filter(db.or_((DomainUser.user_id == current_user.id), (AccountUser.user_id == current_user.id))).all()
            all_account_names = db.session.query(Account).outerjoin(Domain, (Domain.account_id == Account.id)).outerjoin(DomainUser, (Domain.id == DomainUser.domain_id)).outerjoin(AccountUser, (Account.id == AccountUser.account_id)).filter(db.or_((DomainUser.user_id == current_user.id), (AccountUser.user_id == current_user.id))).all()
            all_user_names = []
            for a in all_account_names:
                temp = db.session.query(User).join(AccountUser, (AccountUser.user_id == User.id)).outerjoin(Account, (Account.id == AccountUser.account_id)).filter(db.or_((Account.id == a.id), (AccountUser.account_id == a.id))).all()
                for u in temp:
                    if (u in all_user_names):
                        continue
                    all_user_names.append(u)
            for d in all_domain_names:
                doms += (d.name + ' ')
            for a in all_account_names:
                accounts += (a.name + ' ')
            for u in all_user_names:
                users += (u.username + ' ')
        return render_template('admin_history.html', all_domain_names=doms, all_account_names=accounts, all_usernames=users)