- Partner model additions

- menu changes to allow for partners
- partner views and forms now in partner_forms.py and partner_views.py
- Introduction of services layer
- Allow all configuration to handle partner configurations, and adaptation of caching to allow for this
This commit is contained in:
Josako
2025-05-02 13:10:59 +02:00
parent 9652d0bff9
commit 6ef025363d
72 changed files with 1342 additions and 228 deletions

View File

@@ -8,9 +8,17 @@ import ast
from common.models.entitlements import License, LicenseTier, LicenseUsage, BusinessEventLog
from common.extensions import db, security, minio_client, simple_encryption
from common.services.entitlement_services import EntitlementServices
from common.services.partner_services import PartnerServices
from common.services.tenant_services import TenantServices
from common.services.user_services import UserServices
from common.utils.eveai_exceptions import EveAIException
from common.utils.security_utils import current_user_has_role
from .entitlements_forms import LicenseTierForm, LicenseForm
from common.utils.view_assistants import prepare_table_for_macro, form_validation_failed
from common.utils.nginx_utils import prefixed_url_for
from common.utils.document_utils import set_logging_information, update_logging_information
entitlements_bp = Blueprint('entitlements_bp', __name__, url_prefix='/entitlements')
@@ -25,6 +33,8 @@ def license_tier():
new_license_tier = LicenseTier()
form.populate_obj(new_license_tier)
set_logging_information(new_license_tier, dt.now(tz.utc))
try:
db.session.add(new_license_tier)
db.session.commit()
@@ -45,7 +55,7 @@ def license_tier():
@entitlements_bp.route('/view_license_tiers', methods=['GET', 'POST'])
@roles_accepted('Super User')
@roles_accepted('Super User', 'Partner Admin')
def view_license_tiers():
page = request.args.get('page', 1, type=int)
per_page = request.args.get('per_page', 10, type=int)
@@ -56,7 +66,18 @@ def view_license_tiers():
LicenseTier.end_date == None,
LicenseTier.end_date >= today
)
).order_by(LicenseTier.start_date.desc(), LicenseTier.id)
)
if current_user_has_role('Partner Admin'):
try:
license_tier_ids = PartnerServices.get_allowed_license_tier_ids()
except EveAIException as e:
flash(f"Cannot retrieve License Tiers: {str(e)}", 'danger')
current_app.logger.error(f'Cannot retrieve License Tiers for partner: {str(e)}')
return render_template("index.html")
if license_tier_ids and len(license_tier_ids) > 0:
query = query.filter(LicenseTier.id.in_(license_tier_ids))
query = query.order_by(LicenseTier.start_date.desc(), LicenseTier.id)
pagination = query.paginate(page=page, per_page=per_page, error_out=False)
license_tiers = pagination.items
@@ -64,11 +85,14 @@ def view_license_tiers():
rows = prepare_table_for_macro(license_tiers, [('id', ''), ('name', ''), ('version', ''), ('start_date', ''),
('end_date', '')])
return render_template('entitlements/view_license_tiers.html', rows=rows, pagination=pagination)
return render_template('entitlements/view_license_tiers.html',
rows=rows,
pagination=pagination,
can_assign_license=UserServices.can_user_assign_license())
@entitlements_bp.route('/handle_license_tier_selection', methods=['POST'])
@roles_accepted('Super User')
@roles_accepted('Super User', 'Partner Admin')
def handle_license_tier_selection():
action = request.form['action']
if action == 'create_license_tier':
@@ -76,7 +100,6 @@ def handle_license_tier_selection():
license_tier_identification = request.form['selected_row']
license_tier_id = ast.literal_eval(license_tier_identification).get('value')
the_license_tier = LicenseTier.query.get(license_tier_id)
match action:
case 'edit_license_tier':
@@ -85,6 +108,9 @@ def handle_license_tier_selection():
case 'create_license_for_tenant':
return redirect(prefixed_url_for('entitlements_bp.create_license',
license_tier_id=license_tier_id))
case 'associate_license_tier_to_partner':
EntitlementServices.associate_license_tier_with_partner(license_tier_id)
# Add more conditions for other actions
return redirect(prefixed_url_for('entitlements_bp.view_license_tiers'))
@@ -99,6 +125,8 @@ def edit_license_tier(license_tier_id):
# Populate the license_tier with form data
form.populate_obj(license_tier)
update_logging_information(license_tier, dt.now(tz.utc))
try:
db.session.add(license_tier)
db.session.commit()
@@ -118,12 +146,15 @@ def edit_license_tier(license_tier_id):
@entitlements_bp.route('/create_license/<int:license_tier_id>', methods=['GET', 'POST'])
@roles_accepted('Super User')
@roles_accepted('Super User', 'Partner Admin')
def create_license(license_tier_id):
form = LicenseForm()
tenant_id = session.get('tenant').get('id')
currency = session.get('tenant').get('currency')
if current_user_has_role("Partner Admin"): # The Partner Admin can only set the end date
readonly_fields = [field.name for field in form if (field.name != 'end_date' and field.name != 'start_date')]
if request.method == 'GET':
# Fetch the LicenseTier
license_tier = LicenseTier.query.get_or_404(license_tier_id)
@@ -167,6 +198,8 @@ def create_license(license_tier_id):
# Currency is added here again, as a form doesn't include disabled fields when passing it in the request
new_license.currency = currency
set_logging_information(new_license, dt.now(tz.utc))
try:
db.session.add(new_license)
db.session.commit()
@@ -178,23 +211,27 @@ def create_license(license_tier_id):
else:
form_validation_failed(request, form)
return render_template('entitlements/license.html', form=form, ext_disabled_fields=[])
return render_template('entitlements/license.html', form=form, ext_readonly_fields=readonly_fields)
@entitlements_bp.route('/license/<int:license_id>', methods=['GET', 'POST'])
@roles_accepted('Super User')
@roles_accepted('Super User', 'Partner Admin')
def edit_license(license_id):
license = License.query.get_or_404(license_id) # This will return a 404 if no license tier is found
form = LicenseForm(obj=license)
disabled_fields = []
readonly_fields = []
if len(license.usages) > 0: # There already are usage records linked to this license
# Define which fields should be disabled
disabled_fields = [field.name for field in form if field.name != 'end_date']
readonly_fields = [field.name for field in form if field.name != 'end_date']
if current_user_has_role("Partner Admin"): # The Partner Admin can only set the end date
readonly_fields = [field.name for field in form if field.name != 'end_date']
if form.validate_on_submit():
# Populate the license with form data
form.populate_obj(license)
update_logging_information(license, dt.now(tz.utc))
try:
db.session.add(license)
db.session.commit()
@@ -210,7 +247,7 @@ def edit_license(license_id):
else:
form_validation_failed(request, form)
return render_template('entitlements/license.html', form=form, ext_disabled_fields=disabled_fields)
return render_template('entitlements/license.html', form=form, ext_readonly_fields=readonly_fields)
@entitlements_bp.route('/view_usages')