Dinamyc assignement of states

I am trying to develop a system in DJANGO that allows me to monitor the objectives assigned to my team. Each objective is measured through one or more performance indicators, and the idea is to have a traffic light associated with each performance indicator and each objective. The system should dynamically attribute the colors for each performance indicator and objective: black, white, red, green and yellow, according the following conditions (for now let´s address only the problem of the performance indicators):

  • Black – if the state of the performance indicator (pindicator_status) is different from “Active”;
  • White – if the state of the performance indicator (pindicator_status) is “Active”, today´s date is greater than the performance indicator assessment date (piassessment_date) and the performance indicator result (piassessment_result) for that date remains blank (‘ ‘);
  • Green - if the state of the performance indicator is “Active”, the performance indicator result for that date has a value on it and the performance indicator score (pi_result / pi_target) is greater than the upper threshold (pindicator_upper_treshold);
  • Red - if the state of the performance indicator is “Active”, the performance indicator result for that date has a value on it and the performance indicator score (pi_result / pi_target) is lower than the lower threshold (pindicator_lower_treshold);
  • Yellow - if the state of the performance indicator is “Active”, the performance indicator result for that date has a value on it and the performance indicator score (pi_result / pi_target) is between the lower threshold and the upper threshold;

Each performance indicator has two kinds of states: a state (draft, under approval, approved, active, closed) set manually and an action state (black, white, green, red, yellow) set dynamically. Periodically (monthly, quarterly, etc.) (pindcator_frequency) someone (performance indicators owners) goes into the system and updates the result of their performance indicators (piassessment_indicator) which are then compared with the targets (piassessment_target) set for that dates.

To develop the system I defined the following models:

Questions: Do I need to have a field in the database to store the colors (action_state) of the performance indicators? How can I address this problem? I would much appreciate if someone could give a clue.

Gave your question a quick read so my apologies if my answer is incomplete or not what you are looking for. Seems to me like you want to render a different html color code based on various fields stored within the model.

What I would do: create a method on the model, get_status() for example, which contains all of the conditional logic you just listed. For example with black it would be something like:

Class Model(models.Model):
  #on mobile- just a minimal example 
  pindicator_status = ....
  #other fields 
  def get_status(self):
      if self.pindicator_status.lower() != “active”:
          return “#000000”

Let’s assume you have passed a model instance with pindicator_status = “Visible” to the view in a context variable named “my_obj”. Now if you use inline css you could do something like…

<a href=“#” style=“background-color: {{ my_obj.get_status }};”>Status</>

Since the CSS is inline and you used a template tag to call the get_status method, it will set the background-color attribute to the appropriate hex value. You will obviously need to build out the functionality of get_status() to include logic for all indicator lights.

Another option is simply returning “black” as the result of get_status if the pindicator_status != “Visible” then check in the template with some kind of logic like so:

<p>My beautiful web page </p>
{% if my_obj.get_status == “black” %}
  <a class=“black”>Status</a>
{% endif %}

Basically, instead of using inline css directly from calling the object’s get_status method you can use a conditional to identify what predefined CSS class to apply to the element. This example assumes you have passed a model instance “my_obj” and have predefined css classes for styling.

I did not test this code and don’t frequent this forum. If you need a more complete example email me at: namwebdevri@gmail.com

Or… Vitor could help you I’m sure.