diff --git a/app/crud/jemengage.py b/app/crud/jemengage.py index 93f15e3..ae55af7 100644 --- a/app/crud/jemengage.py +++ b/app/crud/jemengage.py @@ -97,11 +97,12 @@ def get_survey( return None if zone.type == 'department': - return db.query(JecouteDataSurvey, JecouteSurvey) \ - .filter(JecouteDataSurvey.postal_code is not None) \ + return db.query(JecouteDataSurvey) \ + .options(joinedload(JecouteDataSurvey.author)) \ + .options(joinedload(JecouteDataSurvey.survey)) \ + .filter(JecouteDataSurvey.postal_code != '') \ .join(GeoCity, func.instr(GeoCity.postal_code, JecouteDataSurvey.postal_code)) \ .join(GeoDepartment) \ - .join(JecouteSurvey) \ .filter(GeoDepartment.code == zone.code) \ .filter(JecouteDataSurvey.latitude != '') \ .filter(JecouteDataSurvey.longitude != '') \ @@ -109,7 +110,8 @@ def get_survey( if zone.type == 'region': return db.query(JecouteDataSurvey) \ - .options(joinedload(JecouteDataSurvey.jecoute_survey)) \ + .options(joinedload(JecouteDataSurvey.author)) \ + .options(joinedload(JecouteDataSurvey.survey)) \ .filter(JecouteDataSurvey.postal_code != '') \ .join(GeoCity, GeoCity.postal_code.like('%' + JecouteDataSurvey.postal_code + '%')) \ .join(GeoDepartment) \ diff --git a/app/models/models_enmarche.py b/app/models/models_enmarche.py index 9099942..1ec064c 100644 --- a/app/models/models_enmarche.py +++ b/app/models/models_enmarche.py @@ -15,6 +15,8 @@ class Adherents(Base): __tablename__ = 'adherents' id = Column(Integer, primary_key=True, index=True) + first_name = Column(String, nullable=False) + last_name = Column(String, nullable=False) uuid = Column(String(36), unique=True, nullable=False, index=True) candidate_managed_area_id = Column(Integer, ForeignKey('candidate_managed_area.id')) candidate_managed_area = relationship('CandidateManagedArea') @@ -93,7 +95,8 @@ class JecouteDataSurvey(Base): __tablename__ = 'jecoute_data_survey' id = Column(Integer, primary_key=True, index=True) - author_id = Column(Integer, nullable=True) + author_id = Column(Integer, ForeignKey('adherents.id'), nullable=True) + author = relationship('Adherents', lazy='joined') posted_at = Column(DateTime, nullable=False) postal_code = Column(String, nullable=True) age_range = Column(String, nullable=True) @@ -101,7 +104,7 @@ class JecouteDataSurvey(Base): latitude = Column(Float, nullable=True) longitude = Column(Float, nullable=True) survey_id = Column(Integer, ForeignKey('jecoute_survey.id')) - jecoute_survey = relationship('JecouteSurvey', lazy='joined') + survey = relationship('JecouteSurvey', lazy='joined') class JecouteSurvey(Base): @@ -109,10 +112,11 @@ class JecouteSurvey(Base): __tablename__ = 'jecoute_survey' id = Column(Integer, primary_key=True, index=True) - author_id = Column(Integer, nullable=True) + author_id = Column(Integer, ForeignKey('adherents.id'), nullable=True) + author = relationship('Adherents', lazy='joined') name = Column(String, nullable=False) created_at = Column(DateTime, nullable=False) updated_at = Column(DateTime, nullable=False) type = Column(String, nullable=False) zone_id = Column(Integer, ForeignKey('geo_zone.id')) - geo_zone = relationship('GeoZone') + geo_zone_relation = relationship('GeoZone') \ No newline at end of file diff --git a/app/schemas/schemas.py b/app/schemas/schemas.py index e738ad2..7779a70 100644 --- a/app/schemas/schemas.py +++ b/app/schemas/schemas.py @@ -2,7 +2,7 @@ Schemas """ from enum import Enum -from typing import List, Set, Optional +from typing import List, Set, Union, Optional from pydantic import BaseModel, Field from datetime import datetime @@ -76,10 +76,18 @@ class Config: """ +class AdherentName(BaseModel): + adherent_id: int = Field(alias="id") + first_name: str + last_name: str + + class Config: + orm_mode = True + class JecouteSurvey(BaseModel): - survey_id: int = Field(alias="id") - survey_author_id: Optional[str] + id: int + survey_author: Optional[AdherentName] name: str created_at: datetime updated_at: datetime @@ -91,14 +99,14 @@ class Config: class JecouteDataSurvey(BaseModel): id: int - author_id: Optional[str] + author: Optional[AdherentName] posted_at: datetime postal_code: Optional[str] age_range: Optional[str] gender: Optional[str] latitude: Optional[float] longitude: Optional[float] - survey: JecouteSurvey = Field(alias="jecoute_survey") + survey: JecouteSurvey class Config: json_encoders = {datetime: lambda v: v.date().strftime("%d/%m/%y")}