<commit_before>import datetime
import json
import logging
from typing import List, Literal

from flask import request, Response
from sqlalchemy import and_, not_

from anyway.app_and_db import db
from anyway.backend_constants import BE_CONST
from anyway.models import NewsFlash
from anyway.infographics_utils import is_news_flash_resolution_supported

from pydantic import BaseModel, ValidationError, validator

DEFAULT_OFFSET_REQ_PARAMETER = 0
DEFAULT_LIMIT_REQ_PARAMETER = 100

class QueryParams(BaseModel):
    road_number: int
    offset: int
    limit: int
    interurban_only: bool
    road_segment_only: bool
    source: Literal['ynet', 'walla', 'twitter']
    start_date: datetime.datetime
    end_date: datetime.datetime

    @validator('end_date')
    def check_missing_date(cls, end_date, start_date):
        if end_date ^ start_date:
            raise ValidationError('Missing start or end date')
        return end_date

def news_flash():
    news_flash_id = request.values.get("id")

    if news_flash_id is not None:
        query = db.session.query(NewsFlash)
        news_flash_obj = query.filter(NewsFlash.id == news_flash_id).first()
        if news_flash_obj is not None:
            if is_news_flash_resolution_supported(news_flash_obj):
                return Response(
                    json.dumps(news_flash_obj.serialize(), default=str), mimetype="application/json"
                )
            else:
                return Response("News flash location not supported", 406)
        return Response(status=404)

    query = gen_news_flash_query(
        db.session,
        source=request.values.get("source"),
        start_date=request.values.get("start_date"),
        end_date=request.values.get("end_date"),
        interurban_only=request.values.get("interurban_only"),<commit_msg>fix the query validator class<commit_after>import datetime
import json
import logging
from typing import List, Literal

from flask import request, Response
from sqlalchemy import and_, not_

from anyway.app_and_db import db
from anyway.backend_constants import BE_CONST
from anyway.models import NewsFlash
from anyway.infographics_utils import is_news_flash_resolution_supported

from pydantic import BaseModel, ValidationError, validator

DEFAULT_OFFSET_REQ_PARAMETER = 0
DEFAULT_LIMIT_REQ_PARAMETER = 100

class NewsFlashQuery(BaseModel):

    road_number: Optional[int]
    offset: Optional[int]
    limit: Optional[int]
    interurban_only: Optional[bool]
    road_segment_only: Optional[bool]
    source: Optional[str]
    # Must set default value in order to be accessed from "check_missing_date" validator when no value provided
    # from the request
    start_date: Optional[datetime.datetime] = None
    end_date: Optional[datetime.datetime] = None

    @validator('end_date', always=True)
    def check_missing_date(cls, v, values):
        if bool(v) != bool(values['start_date']):
            raise ValueError('Missing start or end date')
        return v
    
    @validator('source')
    def check_source_exist(cls, v):
        valid_sources =  [
        str(source_name[0]) for source_name in db.session.query(NewsFlash.source).distinct().all()
        ]
        if not v in valid_sources:
            raise ValueError('Source must be one of: {}'.format(valid_sources))
        return v


def news_flash():
    news_flash_id = request.values.get("id")

    if news_flash_id is not None:
        query = db.session.query(NewsFlash)
        news_flash_obj = query.filter(NewsFlash.id == news_flash_id).first()
        if news_flash_obj is not None:
            if is_news_flash_resolution_supported(news_flash_obj):
                return Response(
                    json.dumps(news_flash_obj.serialize(), default=str), mimetype="application/json"
                )
            else:
                return Response("News flash location not supported", 406)
        return Response(status=404)

    query = gen_news_flash_query(
        db.session,
        source=request.values.get("source"),
        start_date=request.values.get("start_date"),
        end_date=request.values.get("end_date"),
        interurban_only=request.values.get("interurban_only"),
{'fraction_non_alphanumeric': 0.08281622911694511, 'fraction_numerical': 0.005011933174224344, 'mean_word_length': 3.3565488565488564, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1659672', 'n_tokens_mistral': 1230, 'n_tokens_neox': 1183, 'n_words': 299}