Exemplo n.º 1
0
def get_pages_count(db: Session, category: CategoryENUM) -> int:
    category_record = db.query(Category).filter_by(
        enum_value=category.value).first()
    if category_record is None:
        return 1
    return db.query(PageIndex).filter(
        PageIndex.category_id == category_record.id).count()
Exemplo n.º 2
0
def get_page_index(db: Session, pageNumber: int,
                   category: CategoryENUM) -> PageIndex:
    category_record = db.query(Category).filter_by(
        enum_value=category.value).first()
    if category_record is None:
        return None
    return (db.query(PageIndex).filter(
        and_(PageIndex.page == pageNumber,
             PageIndex.category_id == category_record.id)).first())
Exemplo n.º 3
0
def get_guide_previews(
    db: Session,
    page_index: PageIndex,
    category: CategoryENUM,
    include_hidden: bool,
) -> List[GuidePreview]:
    category_record = db.query(Category).filter_by(
        enum_value=category.value).first()
    if category_record is None:
        return []
    if include_hidden:
        if page_index.page == 1:
            guide_previews = category_record.guides.filter(
                GuidePreview.created_at >= page_index.start_date).all()
            return guide_previews
        else:
            guide_previews = category_record.guides.filter(
                and_(GuidePreview.created_at >= page_index.start_date,
                     GuidePreview.created_at <= page_index.end_date)).all()
            return guide_previews
    else:
        if page_index.page == 1:
            guide_previews = category_record.guides.filter(
                and_(GuidePreview.created_at >= page_index.start_date,
                     GuidePreview.hidden.isnot(True))).all()
            return guide_previews
        else:
            guide_previews = category_record.guides.filter(
                and_(GuidePreview.created_at >= page_index.start_date,
                     GuidePreview.created_at <= page_index.end_date,
                     GuidePreview.hidden.isnot(True))).all()
            return guide_previews
Exemplo n.º 4
0
def upsert_guide(db: Session, guide_jsonified: GuideJsonified):
    categories = []
    for category in guide_jsonified.categories:
        category_record = db.query(Category).filter_by(
            enum_value=category.value).first()
        if category_record is None:
            category_record = Category(enum_value=category.value)
            db.add(category_record)
        categories.append(category_record)

    existing_record = db.query(Guide).filter_by(
        reference_id=guide_jsonified.referenceId).first()
    if existing_record is not None:
        existing_record.from_jsonified_dict(guide_jsonified)
        existing_record.categories.clear()
        existing_record.categories.extend(categories)
        existing_record.locations.clear()
        existing_record.locations = [
            GuideLocationInfo().from_jsonified_dict(location)
            for location in guide_jsonified.locations
        ]
        existing_icon_record = db.query(Icon).filter_by(
            id=existing_record.icon_id).first()
        existing_icon_record.from_jsonified_dict(guide_jsonified.icon)
    else:
        guide = Guide().from_jsonified_dict(guide_jsonified)
        guide.icon = Icon().from_jsonified_dict(guide_jsonified.icon)
        guide.locations = [
            GuideLocationInfo().from_jsonified_dict(location)
            for location in guide_jsonified.locations
        ]
        guide.categories.extend(categories)
        db.add(guide)
Exemplo n.º 5
0
def update_index(db: Session, categories: List[CategoryENUM]):
    for category in categories:
        all_materials = []
        category_record = db.query(Category).filter_by(
            enum_value=category.value).first()
        articles = category_record.articles.filter(Article.hidden.isnot(True))
        for a in articles:
            all_materials.append(a)

        ext_materials = category_record.ext_materials.filter(
            ExtMaterial.hidden.isnot(True))
        for e in ext_materials:
            all_materials.append(e)

        guides = category_record.guides.filter(Guide.hidden.isnot(True))
        for g in guides:
            all_materials.append(g)

        all_materials.sort(key=lambda m: m.created_at, reverse=True)

        page_size = config.get(PAGE_SIZE)

        page = 1
        if len(all_materials) <= page_size:
            start_date = all_materials[-1].created_at
            end_date = all_materials[0].created_at
            existing_record = db.query(PageIndex).filter(
                and_(PageIndex.page == page,
                     PageIndex.category_id == category_record.id)).first()
            if existing_record is not None:
                existing_record.start_date = start_date
                existing_record.end_date = end_date
            else:
                index = PageIndex(start_date=start_date,
                                  end_date=end_date,
                                  page=page,
                                  category=category_record)
                db.add(index)
        else:
            while len(all_materials) > 0:
                current_page_size = page_size if len(
                    all_materials) > page_size else len(all_materials)
                page_materials = all_materials[:current_page_size]
                del all_materials[:current_page_size]
                start_date = page_materials[-1].created_at
                end_date = page_materials[0].created_at
                existing_record = db.query(PageIndex).filter(
                    and_(PageIndex.page == page,
                         PageIndex.category_id == category_record.id)).first()
                if existing_record is not None:
                    existing_record.start_date = start_date
                    existing_record.end_date = end_date
                else:
                    index = PageIndex(start_date=start_date,
                                      end_date=end_date,
                                      page=page,
                                      category=category_record)
                    db.add(index)

                page += 1
Exemplo n.º 6
0
def upsert_ext_material(db: Session,
                        ext_material_jsonified: ExtMaterialJsonified):
    categories = []
    for category in ext_material_jsonified.categories:
        category_record = db.query(Category).filter_by(
            enum_value=category.value).first()
        if category_record is None:
            category_record = Category(enum_value=category.value)
            db.add(category_record)
        categories.append(category_record)

    existing_record = db.query(ExtMaterial).filter_by(
        reference_id=ext_material_jsonified.referenceId).first()
    if existing_record is not None:
        existing_record.from_jsonified_dict(ext_material_jsonified)
        existing_record.categories.clear()
        existing_record.categories.extend(categories)
        existing_icon_record = db.query(Icon).filter_by(
            id=existing_record.icon_id).first()
        existing_icon_record.from_jsonified_dict(ext_material_jsonified.icon)

    else:
        ext_material = ExtMaterial().from_jsonified_dict(
            ext_material_jsonified)
        ext_material.icon = Icon().from_jsonified_dict(
            ext_material_jsonified.icon)
        ext_material.categories.extend(categories)
        db.add(ext_material)
Exemplo n.º 7
0
def get_ext_material_previews(
    db: Session,
    page_index: PageIndex,
    category: CategoryENUM,
    include_hidden: bool,
) -> List[ExtMaterial]:
    category_record = db.query(Category).filter_by(
        enum_value=category.value).first()
    if category_record is None:
        return []
    if include_hidden:
        if page_index.page == 1:
            ext_material_previews = category_record.ext_materials.filter(
                ExtMaterial.created_at >= page_index.start_date).all()
            return ext_material_previews
        else:
            ext_material_previews = category_record.ext_materials.filter(
                and_(
                    ExtMaterial.created_at >= page_index.start_date,
                    ExtMaterial.created_at <= page_index.end_date,
                )).all()
            return ext_material_previews
    else:
        if page_index.page == 1:
            ext_material_previews = category_record.ext_materials.filter(
                and_(
                    ExtMaterial.created_at >= page_index.start_date,
                    ExtMaterial.hidden.isnot(True),
                )).all()
            return ext_material_previews
        else:
            ext_material_previews = category_record.ext_materials.filter(
                and_(
                    ExtMaterial.created_at >= page_index.start_date,
                    ExtMaterial.created_at <= page_index.end_date,
                    ExtMaterial.hidden.isnot(True),
                )).all()
            return ext_material_previews
Exemplo n.º 8
0
def upsert_article(db: Session, article_jsonified: ArticleJsonified):
    categories = []
    for category in article_jsonified.categories:
        category_record = db.query(Category).filter_by(
            enum_value=category.value).first()
        if category_record is None:
            category_record = Category(enum_value=category.value)
            db.add(category_record)
        categories.append(category_record)

    existing_record = db.query(Article).filter_by(
        reference_id=article_jsonified.referenceId).first()
    if existing_record is not None:
        existing_record.from_jsonified_dict(article_jsonified)
        existing_record.categories.clear()
        existing_record.categories.extend(categories)
        existing_icon_record = db.query(Icon).filter_by(
            id=existing_record.icon_id).first()
        existing_icon_record.from_jsonified_dict(article_jsonified.icon)
    else:
        article = Article().from_jsonified_dict(article_jsonified)
        article.icon = Icon().from_jsonified_dict(article_jsonified.icon)
        article.categories.extend(categories)
        db.add(article)
Exemplo n.º 9
0
def get_user_by_username(db: Session, username: str) -> User:
    return db.query(User).filter(User.username == username).first()
Exemplo n.º 10
0
def get_guide_by_id(db: Session, id: str) -> Guide:
    return db.query(Guide).filter_by(reference_id=id).first()
Exemplo n.º 11
0
def get_article_by_id(db: Session, id: str) -> Article:
    return db.query(Article).filter_by(reference_id=id).first()
Exemplo n.º 12
0
def get_ext_material_by_id(db: Session, id: str) -> ExtMaterial:
    return db.query(ExtMaterial).filter_by(reference_id=id).first()