Slim dict for faster loading
This commit is contained in:
parent
a361e496db
commit
b5e2e8ab2a
2 changed files with 30 additions and 1 deletions
|
|
@ -90,6 +90,20 @@ class Tune(db.Model):
|
||||||
"references": [r.to_dict() for r in self.references],
|
"references": [r.to_dict() for r in self.references],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def to_slim_dict(self):
|
||||||
|
return {
|
||||||
|
"id": self.id,
|
||||||
|
"name": self.name,
|
||||||
|
"key": self.key,
|
||||||
|
"modal": self.modal,
|
||||||
|
"source_id": self.source_id,
|
||||||
|
"created_at": self.created_at.isoformat() if self.created_at else None,
|
||||||
|
"source": self.source.to_dict() if self.source else None,
|
||||||
|
"instruments": [i.to_slim_dict() for i in self.instruments],
|
||||||
|
"reference_count": len(self.references),
|
||||||
|
"has_notes": bool(self.notes) or any(bool(e.notes) for e in self.instruments),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class TuneByInstrument(db.Model):
|
class TuneByInstrument(db.Model):
|
||||||
__tablename__ = "tune_by_instrument"
|
__tablename__ = "tune_by_instrument"
|
||||||
|
|
@ -126,6 +140,21 @@ class TuneByInstrument(db.Model):
|
||||||
"notes": [n.to_dict() for n in self.notes],
|
"notes": [n.to_dict() for n in self.notes],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def to_slim_dict(self):
|
||||||
|
return {
|
||||||
|
"id": self.id,
|
||||||
|
"tune_id": self.tune_id,
|
||||||
|
"instrument_id": self.instrument_id,
|
||||||
|
"instrument_name": self.instrument.name if self.instrument else None,
|
||||||
|
"learned_from_id": self.learned_from_id,
|
||||||
|
"learned_from": self.learned_from_musician.name if self.learned_from_musician else None,
|
||||||
|
"date_learned": self.date_learned.isoformat() if self.date_learned else None,
|
||||||
|
"tuning_id": self.tuning_id,
|
||||||
|
"tuning": self.tuning.name if self.tuning else None,
|
||||||
|
"status": self.status,
|
||||||
|
"difficulty": self.difficulty,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class TuneNote(db.Model):
|
class TuneNote(db.Model):
|
||||||
__tablename__ = "tune_notes"
|
__tablename__ = "tune_notes"
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ def list_tunes():
|
||||||
q = q.filter(TuneByInstrument.difficulty.ilike(difficulty))
|
q = q.filter(TuneByInstrument.difficulty.ilike(difficulty))
|
||||||
|
|
||||||
tunes = q.order_by(Tune.name).all()
|
tunes = q.order_by(Tune.name).all()
|
||||||
return jsonify([t.to_dict() for t in tunes])
|
return jsonify([t.to_slim_dict() for t in tunes])
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue