from flask import abort

from .models import Service
from .validation import get_validation_errors
from .service_utils import filter_services


def validate_brief_data(brief, enforce_required=True, required_fields=None):
    errs = get_validation_errors(
        'briefs-{}-{}'.format(brief.framework.slug, brief.lot.slug),
        brief.data,
        enforce_required=enforce_required,
        required_fields=required_fields
    )

    if errs:
        abort(400, errs)


def is_supplier_eligible_for_brief(supplier, brief):
    services = filter_services(
        framework_slugs=[brief.framework.slug],
        statuses=["published"],
        lot_slug=brief.lot.slug,
        location=brief.data["location"],
        role=brief.data["specialistRole"] if brief.lot.slug == "digital-specialists" else None
    )

    services = services.filter(Service.supplier_id == supplier.supplier_id)

    return services.count() > 0

from flask import abort

from .models import Service
from .validation import get_validation_errors
from .service_utils import filter_services


def validate_brief_data(brief, enforce_required=True, required_fields=None):
    errs = get_validation_errors(
        'briefs-{}-{}'.format(brief.framework.slug, brief.lot.slug),
        brief.data,
        enforce_required=enforce_required,
        required_fields=required_fields
    )

    criteria_weighting_keys = ['technicalWeighting', 'culturalWeighting', 'priceWeighting']
    # Only check total if all weightings are set
    if all(key in brief.data for key in criteria_weighting_keys):
        criteria_weightings = sum(brief.data[key] for key in criteria_weighting_keys)
        if criteria_weightings != 100:
            for key in set(criteria_weighting_keys) - set(errs):
                errs[key] = 'total_should_be_100'

    if errs:
        abort(400, errs)


def is_supplier_eligible_for_brief(supplier, brief):
    services = filter_services(
        framework_slugs=[brief.framework.slug],
        statuses=["published"],
        lot_slug=brief.lot.slug,
        location=brief.data["location"],
        role=brief.data["specialistRole"] if brief.lot.slug == "digital-specialists" else None
    )

    services = services.filter(Service.supplier_id == supplier.supplier_id)

    return services.count() > 0

Add criteria weighting 100% total validation
Add criteria weighting 100% total validation

Checks the criteria weighting sum if all criteria fields are set.
This relies on all three fields being required.

If the fields don't add up to a 100 an error is added for each field
that doesn't have any other validation errors.

  from flask import abort
  
  from .models import Service
  from .validation import get_validation_errors
  from .service_utils import filter_services
  
  
  def validate_brief_data(brief, enforce_required=True, required_fields=None):
      errs = get_validation_errors(
          'briefs-{}-{}'.format(brief.framework.slug, brief.lot.slug),
          brief.data,
          enforce_required=enforce_required,
          required_fields=required_fields
      )
+ 
+     criteria_weighting_keys = ['technicalWeighting', 'culturalWeighting', 'priceWeighting']
+     # Only check total if all weightings are set
+     if all(key in brief.data for key in criteria_weighting_keys):
+         criteria_weightings = sum(brief.data[key] for key in criteria_weighting_keys)
+         if criteria_weightings != 100:
+             for key in set(criteria_weighting_keys) - set(errs):
+                 errs[key] = 'total_should_be_100'
  
      if errs:
          abort(400, errs)
  
  
  def is_supplier_eligible_for_brief(supplier, brief):
      services = filter_services(
          framework_slugs=[brief.framework.slug],
          statuses=["published"],
          lot_slug=brief.lot.slug,
          location=brief.data["location"],
          role=brief.data["specialistRole"] if brief.lot.slug == "digital-specialists" else None
      )
  
      services = services.filter(Service.supplier_id == supplier.supplier_id)
  
      return services.count() > 0
  
Add criteria weighting 100% total validation
## Code Before:
from flask import abort

from .models import Service
from .validation import get_validation_errors
from .service_utils import filter_services


def validate_brief_data(brief, enforce_required=True, required_fields=None):
    errs = get_validation_errors(
        'briefs-{}-{}'.format(brief.framework.slug, brief.lot.slug),
        brief.data,
        enforce_required=enforce_required,
        required_fields=required_fields
    )

    if errs:
        abort(400, errs)


def is_supplier_eligible_for_brief(supplier, brief):
    services = filter_services(
        framework_slugs=[brief.framework.slug],
        statuses=["published"],
        lot_slug=brief.lot.slug,
        location=brief.data["location"],
        role=brief.data["specialistRole"] if brief.lot.slug == "digital-specialists" else None
    )

    services = services.filter(Service.supplier_id == supplier.supplier_id)

    return services.count() > 0

## Instruction:
Add criteria weighting 100% total validation
## Code After:
from flask import abort

from .models import Service
from .validation import get_validation_errors
from .service_utils import filter_services


def validate_brief_data(brief, enforce_required=True, required_fields=None):
    errs = get_validation_errors(
        'briefs-{}-{}'.format(brief.framework.slug, brief.lot.slug),
        brief.data,
        enforce_required=enforce_required,
        required_fields=required_fields
    )

    criteria_weighting_keys = ['technicalWeighting', 'culturalWeighting', 'priceWeighting']
    # Only check total if all weightings are set
    if all(key in brief.data for key in criteria_weighting_keys):
        criteria_weightings = sum(brief.data[key] for key in criteria_weighting_keys)
        if criteria_weightings != 100:
            for key in set(criteria_weighting_keys) - set(errs):
                errs[key] = 'total_should_be_100'

    if errs:
        abort(400, errs)


def is_supplier_eligible_for_brief(supplier, brief):
    services = filter_services(
        framework_slugs=[brief.framework.slug],
        statuses=["published"],
        lot_slug=brief.lot.slug,
        location=brief.data["location"],
        role=brief.data["specialistRole"] if brief.lot.slug == "digital-specialists" else None
    )

    services = services.filter(Service.supplier_id == supplier.supplier_id)

    return services.count() > 0
