Skip to content

Commit

Permalink
Author name
Browse files Browse the repository at this point in the history
  • Loading branch information
arniverd committed May 12, 2021
1 parent 903b63c commit 5474d67
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
10 changes: 6 additions & 4 deletions app/crud/jemengage.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,21 @@ 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 != '') \
.all()

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) \
Expand Down
12 changes: 8 additions & 4 deletions app/models/models_enmarche.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -93,26 +95,28 @@ 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)
gender = Column(String, nullable=True)
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):
""" Table jecoute_data_survey """
__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')
18 changes: 13 additions & 5 deletions app/schemas/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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")}
Expand Down

0 comments on commit 5474d67

Please sign in to comment.