Commit 65bcecc7 authored by Imane Misrar's avatar Imane Misrar
Browse files

refacto(university.py/FilterService) : refacto denormalized_infos

Relates to #148
parent 797fc4e6
...@@ -78,21 +78,13 @@ def update_denormalized_univ_field(): ...@@ -78,21 +78,13 @@ def update_denormalized_univ_field():
for university in University.objects.all().prefetch_related( for university in University.objects.all().prefetch_related(
"offers", "exchanges", "corresponding_utc_partners" "offers", "exchanges", "corresponding_utc_partners"
): ):
denormalized_infos = dict() semesters_majors_minors = dict()
# handling of corresponding_utc_partners
denormalized_infos["isDestinationOpen"] = (
university.corresponding_utc_partners.filter(
is_destination_open=True
).count()
> 0
)
# handling of offers # handling of offers
for offer in university.offers.all(): for offer in university.offers.all():
semester = "{}{}".format(offer.semester, offer.year) semester = "{}{}".format(offer.semester, offer.year)
if semester not in denormalized_infos.keys(): if semester not in semesters_majors_minors.keys():
denormalized_infos[semester] = dict() semesters_majors_minors[semester] = dict()
majors = offer.majors majors = offer.majors
if majors is not None: if majors is not None:
possibilities = set( possibilities = set(
...@@ -100,25 +92,36 @@ def update_denormalized_univ_field(): ...@@ -100,25 +92,36 @@ def update_denormalized_univ_field():
) )
for major in possibilities: for major in possibilities:
# No minors in offers # No minors in offers
denormalized_infos[semester][major] = list() semesters_majors_minors[semester][major] = list()
# handling of exchanges # handling of exchanges
for exchange in university.exchanges.all(): for exchange in university.exchanges.all():
semester = "{}{}".format(exchange.semester, exchange.year) semester = "{}{}".format(exchange.semester, exchange.year)
if semester not in denormalized_infos.keys(): if semester not in semesters_majors_minors.keys():
denormalized_infos[semester] = dict() semesters_majors_minors[semester] = dict()
major = exchange.student_major major = exchange.student_major
minor = exchange.student_minor minor = exchange.student_minor
if major is not None: if major is not None:
if major not in denormalized_infos[semester].keys(): if major not in semesters_majors_minors[semester].keys():
denormalized_infos[semester][major] = list() semesters_majors_minors[semester][major] = list()
if ( if (
minor is not None minor is not None
and minor not in denormalized_infos[semester][major] and minor not in semesters_majors_minors[semester][major]
): ):
denormalized_infos[semester][major].append(minor) semesters_majors_minors[semester][major].append(minor)
university.denormalized_infos = denormalized_infos # handling of corresponding_utc_partners
id_destination_open = (
university.corresponding_utc_partners.filter(
is_destination_open=True
).count()
> 0
)
university.denormalized_infos = dict(
id_destination_open=id_destination_open,
semesters_majors_minors=semesters_majors_minors,
)
university.save() university.save()
logger.info("Done the denormalized offers/semester/major in university") logger.info("Done the denormalized offers/semester/major in university")
...@@ -77,10 +77,10 @@ class FilterService { ...@@ -77,10 +77,10 @@ class FilterService {
getMajorInUniv(univObj) { getMajorInUniv(univObj) {
return [ return [
...new Set( ...new Set(
Object.values(univObj.denormalized_infos).flatMap(forSemester => Object.values(
Object.keys(forSemester) univObj.denormalized_infos.semesters_majors_minors
).flatMap(forSemester => Object.keys(forSemester))
) )
)
]; ];
} }
...@@ -90,7 +90,9 @@ class FilterService { ...@@ -90,7 +90,9 @@ class FilterService {
* @return {array.<string>} * @return {array.<string>}
*/ */
getMajorMinorsInUniv(univObj, allowedSemesters = null) { getMajorMinorsInUniv(univObj, allowedSemesters = null) {
const realMinors = Object.entries(univObj.denormalized_infos) const realMinors = Object.entries(
univObj.denormalized_infos.semesters_majors_minors
)
.filter(([sem]) => .filter(([sem]) =>
allowedSemesters === null ? true : allowedSemesters.includes(sem) allowedSemesters === null ? true : allowedSemesters.includes(sem)
) )
...@@ -105,7 +107,7 @@ class FilterService { ...@@ -105,7 +107,7 @@ class FilterService {
} }
getSemestersInUniv(univObj) { getSemestersInUniv(univObj) {
return Object.keys(univObj.denormalized_infos); return Object.keys(univObj.denormalized_infos.semesters_majors_minors);
} }
/** /**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment