Skip to content

Commit

Permalink
Further fixes on model instances get
Browse files Browse the repository at this point in the history
  • Loading branch information
bewakes committed May 23, 2019
1 parent 43256a3 commit 0657626
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
44 changes: 21 additions & 23 deletions apps/ary/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
from geo.models import GeoArea
from project.mixins import ProjectEntityMixin

from .utils import FIELDS_KEYS_VALUE_EXTRACTORS
from .utils import (
FIELDS_KEYS_VALUE_EXTRACTORS,
get_title_or_none,
get_model_attrs_or_empty_dict,
)

from utils.common import identity, underscore_to_title

Expand Down Expand Up @@ -416,22 +420,17 @@ def get_methodology_json(self):
methodology_sch = self.create_schema_for_group(MethodologyGroup)
methodology_raw = self.methodology or {}

def _get_location_title(val):
geo = GeoArea.objects.filter(id=val).first()
return geo and geo.title

mapping = {
'attributes': lambda x: self.get_data_from_schema(
methodology_sch, x
),
'sectors': lambda x: Sector.objects.get(id=x).title,
'focuses': lambda x: Focus.objects.get(id=x).title,
'sectors': get_title_or_none(Sector),
'focuses': get_title_or_none(Focus),
'affected_groups': lambda x: {
'key': x,
'title': AffectedGroup.objects.get(id=x).title,
'order': AffectedGroup.objects.get(id=x).order,
**get_model_attrs_or_empty_dict(AffectedGroup, ['title', 'order'])(x)
},
'locations': _get_location_title,
'locations': get_title_or_none(GeoArea),
'objectives': identity,
'sampling': identity,
'limitations': identity,
Expand Down Expand Up @@ -518,28 +517,27 @@ def get_score_json(self):
final_pillars_score = {}
pillars = {}
for pid, pdata in pillars_raw.items():
pillar = ScorePillar.objects.get(id=pid)
pillar_title = get_title_or_none(ScorePillar)(pid)
data = {}
for qid, sid in pdata.items():
q = ScoreQuestion.objects.get(id=qid).title
scale = ScoreScale.objects.get(id=sid)
data[q] = {'title': scale.title, 'value': scale.value}
pillars[pillar.title] = data
final_pillars_score[pillar.title] = self.score.get('{}-score'.format(pid))
q = get_title_or_none(ScoreQuestion)(qid)
data[q] = get_model_attrs_or_empty_dict(ScoreScale, ['title', 'value'])(sid)
pillars[pillar_title] = data
final_pillars_score[pillar_title] = self.score.get('{}-score'.format(pid))

matrix_pillars = {}
for mpid, mpdata in matrix_pillars_raw.items():
mpillar = ScoreMatrixPillar.objects.get(id=mpid)
mpillar_title = get_title_or_none(ScoreMatrixPillar)(mpid)
data = {}
for sid, msid in mpdata.items():
sector = Sector.objects.get(id=sid)
scale = ScoreMatrixScale.objects.get(id=msid)
data[sector.title] = {
'value': scale.value,
sector_title = get_title_or_none(Sector)(sid)
scale = ScoreMatrixScale.objects.filter(id=msid).first()
data[sector_title] = {
'value': scale.value if scale else '',
'title': '{} / {}'.format(
scale.row.title, scale.column.title)
scale.row.title, scale.column.title) if scale else ''
}
matrix_pillars[mpillar.title] = data
matrix_pillars[mpillar_title] = data

return {
'final_score': self.score.get('final_score'),
Expand Down
23 changes: 23 additions & 0 deletions apps/ary/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@
from utils.common import parse_number


def get_title_or_none(Model):
def _get_title(val):
instance = Model.objects.filter(id=val).first()
return instance and instance.title
return _get_title


def get_model_attr_or_none(Model, attr):
def _get_attr(val):
instance = Model.objects.filter(id=val).first()
return instance and instance.__dict__.get(attr)
return _get_attr


def get_model_attrs_or_empty_dict(Model, attrs=[]):
def _get_attrs(val):
instance = Model.objects.filter(id=val).first()
if not instance:
return {attr: None for attr in attrs}
return {attr: instance.__dict__.get(attr) for attr in attrs}
return _get_attrs


def get_country_name(cid):
region = Region.objects.filter(id=parse_number(cid)).first()
return region and region.title
Expand Down

0 comments on commit 0657626

Please sign in to comment.