Skip to content

Commit

Permalink
add achievements level and title
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalfox committed Sep 22, 2024
1 parent da55c7a commit 1495157
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
64 changes: 53 additions & 11 deletions people/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from datetime import date, timedelta
import json
from collections import namedtuple
from dataclasses import dataclass

from django.shortcuts import render, redirect
from django.http import Http404, HttpResponseRedirect, HttpResponse
Expand All @@ -16,6 +16,7 @@
from django.utils.translation import gettext as _
from django.db.models import Count, Sum
from django.db.models.functions import TruncMonth
from django.conf import settings

from people.models import Consultant
from crm.models import Company
Expand Down Expand Up @@ -194,44 +195,85 @@ def consultant_achievements(request, consultant_id):
except Consultant.DoesNotExist:
raise Http404

@dataclass
class Achievement:
key: str
name: str
icon: str
value: int
link: str
format: str = None
level_name: str = ""
level_color: str = "black"

def __post_init__(self):
LEVEL_COLORS = ("#8bbdd8", "#83a3bd", "#7c789c", "#876089", "#6d4672")
rank = -1
for level_settings in settings.ACHIEVEMENTS.get(self.key, []):
if self.value >= level_settings[1]:
self.level_name = level_settings[0]
rank += 1
try:
self.level_color = LEVEL_COLORS[rank]
except IndexError:
self.level_color = LEVEL_COLORS[-1]

def value_repr(self):
if self.format:
return self.format % self.value
else:
return self.value

achievements = []
Achievement = namedtuple("Achievement", ["name", "icon", "value", "link"])
achievements.append(Achievement(name=_("Mission done"),

achievements.append(Achievement(key="MISSION_COUNT",
name=_("Mission done"),
icon="list-ul",
value=Mission.objects.filter(nature="PROD", active=False, timesheet__consultant=consultant).distinct().count(),
link="#"))

achievements.append(Achievement(name=_("Active mission count"),
achievements.append(Achievement(key="ACTIVE_MISSION_COUNT",
name=_("Active mission count"),
icon="list-ul",
value=Mission.objects.filter(nature="PROD", active=True).filter(staffing__consultant=consultant).distinct().count(),
link="#"))

achievements.append(Achievement(name=_("Turnover"),
achievements.append(Achievement(key="TURNOVER",
name=_("Turnover"),
icon="calculator",
value="%i k€" % ((consultant.get_turnover() or 0)/1000),
value=(consultant.get_turnover() or 0)/1000,
format="%i k€",
link="#"))

achievements.append(Achievement(name=_("Turnover last 12 months"),
achievements.append(Achievement(key="LAST_YEAR_TURNOVER",
name=_("Turnover last 12 months"),
icon="calculator",
value="%i k€" % ((consultant.get_turnover(start_date=(date.today() - timedelta(365)).replace(day=1)) or 0) / 1000),
value=(consultant.get_turnover(start_date=(date.today() - timedelta(365)).replace(day=1)) or 0) / 1000,
format="%i k€",
link="#"))

longest_mission_qs = Timesheet.objects.filter(mission__nature="PROD", consultant=consultant).values("mission").annotate(Sum("charge")).order_by("charge__sum").last()
if longest_mission_qs:
achievements.append(Achievement(name=_("Longest mission: %s") % Mission.objects.get(id=longest_mission_qs["mission"]),
achievements.append(Achievement(key="LONGEST_MISSION",
name=_("Longest mission: %s") % Mission.objects.get(id=longest_mission_qs["mission"]),
icon="hourglass",
value=_("%i days") % longest_mission_qs.get("charge__sum", 0),
value= longest_mission_qs.get("charge__sum", 0),
format=_("%i days"),
link=reverse("staffing:mission_home", args=[longest_mission_qs["mission"], ])))

max_mission_per_month_qs = Mission.objects.filter(nature="PROD", timesheet__consultant=consultant)
max_mission_per_month_qs = max_mission_per_month_qs.annotate(month=TruncMonth("timesheet__working_date")).values("month")
max_mission_per_month_qs = max_mission_per_month_qs.annotate(Count("id", distinct=True)).order_by("id__count").last()
if max_mission_per_month_qs:
achievements.append(Achievement(name=_("Max mission in one month (%s)") % max_mission_per_month_qs["month"].strftime("%m/%Y"),
achievements.append(Achievement(key="MAX_MISSION_PER_MONTH",
name=_("Max mission in one month (%s)") % max_mission_per_month_qs["month"].strftime("%m/%Y"),
icon="list-nested",
value=max_mission_per_month_qs["id__count"],
link="#"))

#TODO: nb of distinct client so far and number of client last 12 month
#TODO number of missions last year

return render(request, "people/consultant_achievements.html",
{"consultant": consultant,
"achievements": achievements})
Expand Down
13 changes: 12 additions & 1 deletion pydici/settings/pydici.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Pydici specific parameters

import os
from django.utils.translation import gettext_lazy as _

# Root dir
PYDICI_ROOTDIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), os.path.pardir, os.path.pardir)
Expand Down Expand Up @@ -46,4 +47,14 @@
TELEGRAM_CHAT_MANAGER_LEVEL = 4 # Every people with level < to his will be notified individually if concerned

# Default company logo
COMPANY_LOGO = "/media/pydici/company_logo.png"
COMPANY_LOGO = "/media/pydici/company_logo.png"

# Achievements configuration
ACHIEVEMENTS = {
"MISSION_COUNT": [(_("rookie"), 0), (_("pro"), 50), (_("veteran"), 100), (_("emperor"), 200), (_("grandmaster"), 300)],
"ACTIVE_MISSION_COUNT": [(_("rookie"), 0), (_("busy"), 10), (_("expert"), 10), (_("master"), 15), (_("archivist killer"), 50)],
"TURNOVER": [(_("rookie"), 0), (_("pro"), 500), (_("good winner"), 1_000), (_("Cresus"), 2_000), (_("Picsou"), 5_000)],
"LAST_YEAR_TURNOVER": [(_("rookie"), 0), (_("good winner"), 100), (_("alchemist"), 150), (_("wolf of Wall Street"), 250), (_("grandmaster"), 350)],
"LONGEST_MISSION": [(_("rookie"), 0), (_("sprinter"), 50), (_("marathoner"), 100), (_("survivor"), 200), (_("grandmaster"), 500)],
"MAX_MISSION_PER_MONTH": [(_("rookie"), 0), (_("multithread"), 3), (_("ubiquitous"), 5), (_("ninja"), 10), (_("grandmaster"), 15)],
}
12 changes: 9 additions & 3 deletions templates/people/consultant_achievements.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ <h3>{% trans "This consultant does not work anymore for the company" %}</h3>

<div class="row my-2">
{% for achievement in achievements %}
<div class="col-lg-3 col-md-6 col-sm-6">
<div class="col-lg-3 col-md-6 col-sm-6 mt-3">
<div class="card">
<div class="card-header">
<div class="row">
<div class="col-3">
<i class="bi bi-{{ achievement.icon }}" style="font-size: 4rem"></i>
<i class="pydici-tooltip bi bi-{{ achievement.icon }}" style="font-size: 4rem; color:{{ achievement.level_color }}" title="{{ achievement.level_name }}"></i>
</div>
<div class="col-9 text-end">
<div class="huge">{{ achievement.value }}</div>
<div class="huge">{{ achievement.value_repr }}</div>
<div>{{ achievement.name }}</div>
</div>
</div>
Expand All @@ -34,3 +34,9 @@ <h3>{% trans "This consultant does not work anymore for the company" %}</h3>
</div>

</div> <!-- end of dashboard panels -->

<script type="text/JavaScript">
$(document).ready(function() {
$(".pydici-tooltip").tooltip();
});
</script>

0 comments on commit 1495157

Please sign in to comment.