Exemplo n.º 1
0
async def get_courier(courier_id: int):
    db = Session()
    query = db.query(CourierSchema).get(courier_id)
    # TODO дополнительные поля и подсчет рейтинга!
    if not query:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
                            detail="Courier not found")
    return JSONResponse(query.to_dict())
Exemplo n.º 2
0
async def post_orders(order_list: Orders):
    db = Session()
    order_ids = []
    order_fail_ids = []
    for order in order_list.data:
        db_order = OrderSchema(order_id=order.order_id,
                               weight=order.weight,
                               region=order.region,
                               delivery_hours=order.delivery_hours)
        db.add(db_order)
        try:
            db.commit()
        except IntegrityError:
            order_fail_ids.append(order.order_id)
        else:
            db.refresh(db_order)
            order_ids.append(order.order_id)

    if len(order_fail_ids) == 0:
        return JSONResponse(
            content={'couriers': [{
                'id': c_id
            } for c_id in order_ids]},
            status_code=status.HTTP_201_CREATED)
    else:
        return JSONResponse(content={
            "validation_error": {
                'couriers': [{
                    'id': c_id
                } for c_id in order_fail_ids]
            }
        },
                            status_code=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 3
0
async def post_couriers(courier_list: Couriers):
    db = Session()
    courier_ids = []
    courier_fail_ids = []
    for courier in courier_list.data:
        db_courier = CourierSchema(courier_id=courier.courier_id,
                                   courier_type=courier.courier_type,
                                   regions=courier.regions,
                                   working_hours=courier.working_hours)

        db.add(db_courier)
        try:
            db.commit()
        except IntegrityError:
            courier_fail_ids.append(courier.courier_id)
        else:
            db.refresh(db_courier)
            courier_ids.append(courier.courier_id)

    if len(courier_fail_ids) == 0:
        return JSONResponse(
            content={'couriers': [{
                'id': c_id
            } for c_id in courier_ids]},
            status_code=status.HTTP_201_CREATED)
    else:
        return JSONResponse(content={
            "validation_error": {
                'couriers': [{
                    'id': c_id
                } for c_id in courier_fail_ids]
            }
        },
                            status_code=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 4
0
async def patch_courier(courier_id: int, data: PatchCourier):
    db = Session()
    query = db.query(CourierSchema).get(courier_id)
    if not query:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
                            detail="Courier not found")
    else:

        if data.courier_type and data.regions and data.working_hours:
            query.regions = data.regions
            db.commit()
            return JSONResponse(query.to_dict(),
                                status_code=status.HTTP_200_OK)
        else:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                                detail="Wrong fields")
Exemplo n.º 5
0
def get_test_data(min_appt=5):
    file_path = os.path.join(PRIVATE_DIR, 'vectors',
                             f'test_data_{min_appt}.json')

    if os.path.exists(file_path):
        with open(file_path, 'r') as f:
            return json.load(f)

    session = Session()
    users = get_users(session, min_appt)

    user_data = {}
    for user in progressbar(users):
        last_doctor, town_doctors, all_appts = get_last_doctor_with_all_in_town_with_all_appts(
            session, user)
        if last_doctor.id not in town_doctors:
            continue
        user_data[user] = {
            'selected_doctor': last_doctor.id,
            'suggested_doctors': town_doctors,
            'all_appts': [appt.doctor_id for appt in all_appts],
        }

    with open(file_path, 'w') as f:
        json.dump(user_data, f)
    print('len ->', len(user_data))
    return user_data
Exemplo n.º 6
0
def get_biggest_data():
    file_path = os.path.join(PRIVATE_DIR, 'vectors', f'test_full_data.json')

    if os.path.exists(file_path):
        with open(file_path, 'r') as f:
            return json.load(f)

    session = Session()
    appts = get_all_last_appointment(session)

    user_data = {}
    for appt in progressbar(appts, max_value=appts.count()):
        user_id, appt_id, spec_id, town_id, last_doctor_id = appt
        town_doctors = get_town_doctor_list(session, town_id, spec_id)

        if last_doctor_id not in town_doctors:
            continue
        user_data[user_id] = {
            'selected_doctor': last_doctor_id,
            'suggested_doctors': town_doctors,
            'all_appts': [],
        }

    with open(file_path, 'w') as f:
        json.dump(user_data, f)
    print('len ->', len(user_data))
    return user_data
Exemplo n.º 7
0
    def create(self):
        session = Session()
        data = pd.DataFrame.from_records(session.query(Doctor).order_by(
            Doctor.id).all(),
                                         columns=Doctor._fields)

        doctor_stats = data.iloc[:, [0, 1, 2]]
        matrix = MinMaxScaler().fit_transform(data.iloc[:, 3:])

        doctors = {}
        progress = progressbar.ProgressBar(max_value=data.shape[0])
        for stat, values in zip(doctor_stats.iterrows(), matrix):
            doctors[str(int(stat[1][0]))] = values.tolist()
            progress.next()

        self._vectors = doctors
Exemplo n.º 8
0
def create_top_one_train_data(
        min_appts: int) -> Tuple[pd.DataFrame, pd.DataFrame]:
    session = Session()
    all_users = get_all_appointment_users(session, min_appts)

    doctor_vectors = DoctorVector()
    doctor_vectors.load()

    test = []
    train = []

    for user in progressbar(all_users):
        # add test data
        last_appt, test_appt = get_two_last_appointment(session, user)

        test_doctor_model = session.query(Doctor).filter(
            Doctor.id == test_appt.doctor_id).first()
        test_doctors = get_town_doctor_list(session, test_doctor_model.town_id,
                                            test_appt.spec_id)
        test_appt_id = test_appt.id
        for doctor in test_doctors[:100]:
            if doctor == test_doctor_model.id:
                continue
            test.append([0, test_appt_id, *doctor_vectors[doctor]])
        test.append([1, test_appt_id, *doctor_vectors[test_doctor_model.id]])

        # add train data
        all_user_appts = get_all_appointments(
            session, user, [last_appt.doctor_id, test_appt.doctor_id])
        for appt in all_user_appts:
            appt_id = appt.id
            train.append([1, appt_id, *doctor_vectors[appt.doctor_id]])

            train_doctor_model = session.query(Doctor).filter(
                Doctor.id == appt.doctor_id).first()
            train_town_doctors = get_town_doctor_list(
                session, train_doctor_model.town_id, appt.spec_id)
            for doctor in train_town_doctors[:100]:
                if doctor == train_doctor_model.id:
                    continue
                train.append([0, appt_id, *doctor_vectors[doctor]])

    test_df = pd.DataFrame(test)
    train_df = pd.DataFrame(train)

    return test_df, train_df
Exemplo n.º 9
0
 def session_scope(self):
     session = Session()
     try:
         yield session
         session.commit()
     except:
         session.rollback()
         raise
     finally:
         session.close()
Exemplo n.º 10
0
    def create(self):
        doctor_vectors = DoctorVector()
        doctor_vectors.load()

        session = Session()

        appts = {}
        for appt in session.query(Appointment.user_id,
                                  Appointment.doctor_id).all():
            appts.setdefault(str(appt.user_id), []).append(int(appt.doctor_id))

        users_vectors = {}
        progress = progressbar.ProgressBar(max_value=len(appts))
        for user_id, doctor_ids in appts.items():
            vectors = [doctor_vectors[doctor_id] for doctor_id in doctor_ids]
            users_vectors[user_id] = np.sum(vectors, axis=0).tolist()
            progress.update(progress.value + 1)

        self._vectors = users_vectors
Exemplo n.º 11
0
async def assign_post(courier_id: CourierID):
    response_ids = []
    default_response = JSONResponse(status_code=status.HTTP_400_BAD_REQUEST)
    db = Session()
    courier_from_db = db.query(CourierSchema).get(courier_id.courier_id)
    if not courier_from_db:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                            detail="Courier not found")

    query = db.query(OrderSchema).filter(
        and_(OrderSchema.region.in_(courier_from_db.regions)),
        (OrderSchema.weight <= courier_from_db.courier_type.weight())).all()

    if not query:
        return default_response

    for order in query:
        if check_courier_time_for_order(courier_from_db.working_hours,
                                        order.delivery_hours):
            order.courier_id_assigned = courier_from_db.courier_id
            response_ids.append(order.order_id)
    if response_ids:
        db.commit()
        return JSONResponse(status_code=status.HTTP_200_OK,
                            content={
                                'orders': [{
                                    'id': id_
                                } for id_ in response_ids],
                                'assign_time':
                                datetime.now().isoformat()[:-4] + 'Z'
                            })
    else:
        return default_response
Exemplo n.º 12
0
    def __init__(self):
        """
        Create the Analyzer object
        """
        self.db = Session()

        # TODO there must be a better way of reading the config file path!
        analyzers_dir = os.path.dirname(__file__)
        self.config = configparser.ConfigParser()
        self.config.read(
            os.path.abspath(
                os.path.join(analyzers_dir, '..', 'metrics',
                             'thresholds.conf')))
Exemplo n.º 13
0
async def complete_post(order: OrderDone):
    db = Session()
    courier_from_db = db.query(CourierSchema).get(order.courier_id)
    order_from_db = db.query(OrderSchema).get(order.order_id)

    if not courier_from_db or not order_from_db or courier_from_db.courier_id != order.courier_id:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                            detail="Wrong courier or order id")

    # TODO count rating

    db.delete(order_from_db)
    db.commit()
    return JSONResponse(status_code=status.HTTP_200_OK,
                        content={'order_id': order.order_id})
Exemplo n.º 14
0
Arquivo: group.py Projeto: s16536/MAS
    def __init__(self, name: str, players: List, session=Session()):
        if type(players) is not list or len(players) < 1:
            raise MissingRequiredParameterError("Players", self.__class__.name)

        if name is None:
            raise MissingRequiredParameterError("Name", self.__class__.name)

        max_players_no = self.get_max_players_no(session)
        if len(players) > max_players_no:
            raise ValueError(
                f"Number of players exceeds the limit of {max_players_no}!")

        self.players = players
        self.name = name
Exemplo n.º 15
0
    def get_basic_metrics(self):
        """
        Get a set of basic metrics related to the Spark application.
        :return: dict (metric_name: value)
        """
        db = Session()
        basic_metrics = {}
        runtime = self.duration / 1000.0  # seconds
        cpu_time = cast_or_none(
            db.query(func.sum(StageEntity.executor_cpu_time) /
                     1.0e9).filter(StageEntity.app_id == self.app_id).scalar(),
            float)  # seconds
        total_tasks_time = cast_or_none(
            db.query(func.sum(TaskEntity.duration) / 1000.0).filter(
                and_(TaskEntity.stage_key == StageEntity.stage_key,
                     StageEntity.app_id == self.app_id)).scalar(), float)

        total_gc_time = cast_or_none(
            db.query(func.sum(ExecutorEntity.total_gc_time) / 1.0e3).filter(
                ExecutorEntity.app_id == self.app_id).scalar(),
            float)  # seconds

        basic_metrics["Duration"] = fmt_time(runtime)  # seconds
        basic_metrics["CPU Time"] = fmt_time(cpu_time)  # seconds
        basic_metrics["Total Tasks Time"] = fmt_time(total_tasks_time)
        basic_metrics["Spark Mode"] = self.mode
        basic_metrics["Jobs Number"] = db.query(func.count(
            JobEntity.job_id)).filter(
                JobEntity.app_id == self.app_id).scalar()
        basic_metrics["Stages Number"] = db.query(
            func.count(StageEntity.stage_id)).filter(
                StageEntity.app_id == self.app_id).scalar()
        basic_metrics["Tasks Number"] = cast_or_none(
            db.query(func.sum(StageEntity.num_tasks)).filter(
                StageEntity.app_id == self.app_id).scalar(), int)
        basic_metrics["Total GC Time"] = fmt_time(total_gc_time)  # seconds

        return basic_metrics
Exemplo n.º 16
0
 def __init__(self):
     Base.metadata.create_all(engine)
     self.session = Session()
Exemplo n.º 17
0
class Context:
    def __init__(self):
        Base.metadata.create_all(engine)
        self.session = Session()

    def getAdById(self, adId):
        return self.session.query(Ad).get(adId)

    def getAdByTitle(self, title):
        return self.session.query(Ad).filter_by(title=title).one_or_none()

    def getAdsByTags(self, tags):
        return self.session.query(Ad).filter(Ad.hiddenTags.any(Tag.value.in_(tags))).all()

    def addAd(self, ad):
        self._adTags(ad)
        try:
            self.session.commit()
            self.session.add(ad)
            self.session.commit()
            return ad
        except exc.SQLAlchemyError:
            print("Context: cannot create ad: ", ad)
            return None

    def getAds(self, limit, offset=0):
        return self.session.query(Ad).limit(limit).offset(offset).all()

    def updateAd(self, adId, updated):
        fromDb = self.getAdById(adId)
        if fromDb is None:
            return None
        self._removeTags(fromDb)
        self._adTags(updated)
        try:
            fromDb.update(updated)
            self.session.commit()
            return fromDb
        except exc.SQLAlchemyError:
            print("Context: cannot remove ad: ", fromDb)
            return None

    def _adTags(self, ad):
        try:
            for tag in ad.tags:
                self.session.add(tag)
        except exc.SQLAlchemyError:
            print("Context: cannot add hidden tags: ", ad.tags)
            return None
        try:
            for tag in ad.hiddenTags:
                self.session.add(tag)
        except exc.SQLAlchemyError:
            print("Context: cannot add hidden tags: ", ad.hiddenTags)

    def _removeTags(self, ad):
        try:
            for tag in ad.tags:
                self.session.query(ad_tags_association).filter_by(tag_id=tag.id).delete()
                self.session.delete(tag)
                self.session.commit()
        except exc.SQLAlchemyError:
            print("Context: cannot delete tags: ", ad.tags)
            return None
        try:
            for tag in ad.hiddenTags:
                self.session.query(hidden_ad_tags_association).filter_by(tag_id=tag.id).delete()
                self.session.delete(tag)
                self.session.commit()
        except exc.SQLAlchemyError:
            print("Context: cannot delete hidden tags: ", ad.hiddenTags)

    def removeAd(self, adId):
        fromDb = self.getAdById(adId)
        if fromDb is None:
            return False
        self._removeTags(fromDb)
        try:
            self.session.commit()
            self.session.delete(fromDb)
            self.session.commit()
            return True
        except exc.SQLAlchemyError:
            print("Context: cannot delete ad: ", fromDb)
            return False

    def getTagById(self, tagId):
        return self.session.query(Tag).get(tagId)

    def getTagByValue(self, value):
        return self.session.query(Tag).filter_by(value=value).first()

    def addTag(self, tag):
        self.session.add(tag)
        self.session.commit()

    def __del__(self):
        try:
            self.session.close()
        except AttributeError as err:
            print(err)
Exemplo n.º 18
0
from db.entities.stage import StageEntity
from db.entities.task import TaskEntity
from sparkscope_web.config import Config
from sparkscope_web.graphs import GraphCreator
from sparkscope_web.forms import SearchForm, CompareForm, HistoryForm

import requests

requests.packages.urllib3.disable_warnings(
    requests.packages.urllib3.exceptions.InsecureRequestWarning)
app = Flask(__name__)
app.config.from_object(Config)

# Initialize the database
Base.metadata.create_all(engine)
session = Session()


@app.route('/')
def home():
    """
    Home page
    """
    counts = {
        'applications':
        session.query(func.count(ApplicationEntity.app_id)).scalar(),
        'executors':
        session.query(func.count(ExecutorEntity.app_id)).scalar(),
        'jobs':
        session.query(func.count(JobEntity.app_id)).scalar(),
        'stages':
Exemplo n.º 19
0
def init_db():
    Base.metadata.create_all(engine)
    global session
    session = Session()
Exemplo n.º 20
0
from db.base import Session, engine, Base
from history_fetcher.data_fetcher import DataFetcher
import time
import logging.config
import argparse
import os
from logger.logger import SparkscopeLogger

# Set up logger
logging.config.fileConfig(
    os.path.join(os.path.dirname(__file__), 'logger.conf'))
logger = logging.getLogger(__name__)

# Initialize the database
Base.metadata.create_all(engine)
session = Session()

# read arguments
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
    "--test-mode",
    action="store_true",
    help="fetch some defined number of apps from the history "
    "server, regardless of what has been processed before")
arg_parser.add_argument("--truncate",
                        action="store_true",
                        help="truncate the database before fetching new data")
args = arg_parser.parse_args()

if args.truncate:
    session.execute('''TRUNCATE TABLE application CASCADE''')
Exemplo n.º 21
0
def excel_read(ctx):
    """
    read from excel file and fill the database
    """

    session = Session()
    session.query(DenysOrders).delete()
    session.query(DenysClients).delete()

    loc = '../task/ПримерМСКонтрагентыSQLAzure2.xlsx'

    wb = xlrd.open_workbook(loc)
    orders_sheet = wb.sheet_by_index(0)
    counterparty_sheet = wb.sheet_by_index(1)

    clients_list = []

    for i in range(1, counterparty_sheet.nrows):
        values = counterparty_sheet.row_values(i)
        clients_list.append(DenysClients(
            id=values[0],
            name=values[1],
        ))
    session.add_all(clients_list)
    session.commit()

    orders_list = []
    for i in range(1, orders_sheet.nrows):
        values = orders_sheet.row_values(i)
        orders_list.append(
            DenysOrders(
                id=values[0],
                name=values[1],
                description=values[2],
                moment=values[3],
                sum=values[4],
                counterparty_id=values[5],
            ))
    session.add_all(orders_list)
    session.commit()
Exemplo n.º 22
0
def prepare_database():
    Base.metadata.create_all(engine)
    session = Session()
    commit_and_close(session)
Exemplo n.º 23
0
class PrometeyService():
    def __init__(self):
        Base.metadata.create_all(engine)
        self.session = Session()
        self.__downloader = PrometeyDownloader()
        self.__amazon = PrometeyAmazon(config['aws'])

    @Transactional
    def register_user(self, telegram_user_id):
        user = User(telegram_user_id)
        self.session.add(user)
        return user

    @Transactional
    def get_user(self, telegram_user_id):
        user = self.session.query(User).filter(User.telegram_user_id == telegram_user_id).one_or_none()
        return user

    @Transactional
    def create_content(self, user_id, name):
        content = Content(name, 'DRAFT')
        content.user_id = user_id
        self.session.add(content)
        self.session.flush()
        user = self.session.query(User).filter(User.id == user_id).one()
        user.current_content = content
        return content

    @Transactional
    def change_current(self, user_id, new_content_id):
        user = self.session.query(User).filter(User.id == user_id).one()
        user.current_content_id = new_content_id

    @Transactional
    def add_url(self, user_id, url):
        user = self.session.query(User).filter(User.id == user_id).one()
        t = urlparse(url)
        if user.current_content and user.current_content.status == 'DRAFT' and t.hostname == 'vm.tiktok.com':
            video = Video(url)
            user.current_content.videos.append(video)
            return video
        else:
            pass  # todo ошибка

    @Transactional
    def finish_video(self, user_id):
        user = self.session.query(User).filter(User.id == user_id).one()
        content = user.current_content
        if content:
            content.status = 'FINISHED'
            user.current_content_id = None
            return content

    # stay
    def get_download_list(self):
        videos = self.session.query(Video).join(Content). \
            filter(Video.status == 'NEW').limit(3)
        return videos

    @Transactional
    def set_video_status_downloaded(self, video):
        video.status = 'DOWNLOADED'

    @Transactional
    def get_content_to_encode(self):
        content = self.session.query(Content).join(Video). \
            filter(Content.status == "FINISHED"). \
            filter(Content.all_downloaded).first()
        return content

    @Transactional
    def get_content_to_send(self):
        content = self.session.query(Content).filter(Content.status == "READY").first()
        return content

    @Transactional
    def set_content_status(self, content, status):
        content.status = status
        return content

    def download_videos(self):
        videos = self.get_download_list()
        for video in videos:
            self.__download_video(video)
        return videos

    @Transactional
    def __download_video(self, video):
        data = self.__downloader.download_video(video.url)
        print(f'{video.url} - скачано из сети.')
        key = f'{video.content_id}/{video.id}.mp4'
        print(f'Загружаю на s3 {key}')
        print(self.__amazon.upload(key, data))
        video.status = 'DOWNLOADED'
Exemplo n.º 24
0
 def __init__(self):
     Base.metadata.create_all(engine)
     self.session = Session()
     self.__downloader = PrometeyDownloader()
     self.__amazon = PrometeyAmazon(config['aws'])
Exemplo n.º 25
0
    def __init__(self, user_id, quote_id, reaction, **kwargs):
        self.user = user_id
        self.quote = quote_id
        if reaction not in REACTIONS_OPTIONS:
            raise ModelError(f'reaction must be one of {REACTIONS_OPTIONS}')
        self.reaction = reaction
        self.seen_date = kwargs.get('creation_date', datetime.now().date())

    def __str__(self):
        return f'Reaction[id={self.id}, seen_date={self.seen_date}, reaction={self.reaction}, user={self.user}, quote_id={self.quote}]'


if __name__ == '__main__':
    Base.metadata.create_all(engine)
    session = Session()
    user = User('3649028588457703', 'DAILY')
    session.add(user)
    quote = Quote(
        "If you want to achieve greatness stop asking for permission.",
        "Anonymous")
    session.add(quote)
    session.commit()
    user_quote = Reaction('3649028588457703', quote.id, 'LIKE')
    session.add(user_quote)
    session.commit()
    session.close()
    print("=== Now querying data ===")
    session = Session()
    users = session.query(User).all()
    for user in users:
Exemplo n.º 26
0
from db.base import Base, Session, engine
from db.context import Context
from services.adsService import AdsService
from services.wordService import WordService

Base.metadata.create_all(engine)
session = Session()

adsService = AdsService(Context(), WordService())

# american
burgerTags = [('fast-food', 'en'), ('fast', 'en'), ('burger', 'en'),
              ('american', 'en'), ('amerykańska', 'pl'), ('meat', 'en'),
              ('mięso', 'pl'), ('beef', 'en'), ('wołowina', 'pl'),
              ('loaf', 'en'), ('bułka', 'pl'), ('dinner', 'en'),
              ('obiad', 'pl'), ('tłuste', 'pl'), ('fat', 'en'), ('hot', 'en'),
              ('ciepło', 'pl')]
adsService.create('Burger', 'Burger amerykański, fast-food',
                  'http://127.0.0.1:5000/api/image?path=burger.jpg',
                  [{
                      'value': tag[0],
                      'lang': tag[1]
                  } for tag in burgerTags])
burritoTags = [('fast-food', 'en'), ('fast', 'en'), ('burrito', 'en'),
               ('american', 'en'), ('amerykańska', 'pl'), ('tortilla', 'en'),
               ('naleśnik', 'pl'), ('mięso', 'pl'), ('meat', 'en'),
               ('bean', 'en'), ('fasola', 'pl'), ('dinner', 'en'),
               ('obiad', 'pl'), ('ostry', 'pl'), ('spicy', 'en'),
               ('hot', 'en'), ('ciepło', 'pl')]
adsService.create('Buritto', 'Buritto meksykańskie - fast-food',
                  'http://127.0.0.1:5000/api/image?path=buritto.jpg',
Exemplo n.º 27
0
import csv

from db.base import Session
from db.models import Quote

if __name__ == '__main__':
    session = Session()
    with open('quotes.csv', newline='') as csvfile:
        data = list(csv.reader(csvfile))
    for row in data:
        print(row)
        quote = Quote(row[0], row[1])
        session.add(quote)
    session.commit()
Exemplo n.º 28
0
from Models.ad_tags import ad_tags_association, hidden_ad_tags_association
from Models.ad import Ad
from Models.tag import Tag
from db.base import Base, Session, engine

Base.metadata.create_all(engine)

session = Session()

session.query(ad_tags_association).delete()
session.query(hidden_ad_tags_association).delete()
session.query(Tag).delete()
session.query(Ad).delete()

session.commit()
session.close()
Exemplo n.º 29
0
def today_orders_import(ctx):
    """
    read orders from url and write new today orders into database
    """

    url = 'https://online.moysklad.ru/api/remap/1.1/entity/customerorder'
    login = '******'
    password = '******'

    resp = get(url, auth=HTTPBasicAuth(login, password))
    get_orders = resp.json()

    session_orders = Session()
    session_clients = Session()

    if settings.debug:
        today = date(2019, 2, 15)
    else:
        today = datetime.today().date()

    for i in get_orders['rows']:
        if datetime.strptime(i['moment'], "%Y-%m-%d %H:%M:%S").date() == today:
            if not session_orders.query(DenysOrders).filter(
                    DenysOrders.id == i['id']).all():
                client_data = get(i['agent']['meta']['href'],
                                  auth=HTTPBasicAuth(login, password)).json()

                if not session_clients.query(DenysClients).filter(
                        DenysClients.id == client_data['id']).all():
                    session_clients.add(
                        DenysClients(
                            id=client_data['id'],
                            name=client_data['name'],
                        ))

                session_orders.add(
                    DenysOrders(
                        id=i['id'],
                        name=i['name'],
                        # description =
                        moment=i['moment'],
                        sum=i['sum'],
                        counterparty_id=client_data['id'],
                    ))
    session_clients.commit()
    session_orders.commit()
Exemplo n.º 30
0
def main():
    Base.metadata.drop_all(engine)
    Base.metadata.create_all(engine)

    models.Group.set_max_players_no(5)

    session = Session()

    address_1 = models.Address("city", "postcode", "street", 2)
    address2 = models.Address("city", "postcode", "street", 1)

    person = models.Person(first_name="Jan", last_name="Kowalski")

    owner_1 = models.EscapeRoomOwnerPerson(username="******",
                                           password="******",
                                           address=address2,
                                           er_owner_person=person)
    owner_2 = models.EscapeRoomOwnerCompany(username="******",
                                            password="******",
                                            name="Escapers Inc.",
                                            establishment_date=date(
                                                2020, 1, 1),
                                            address=address_1)
    owner_3 = models.EscapeRoomOwnerCompany(username="******",
                                            password="******",
                                            name="Crazytown",
                                            establishment_date=date(
                                                2020, 1, 1),
                                            address=address_1)

    escape_room1 = models.FixedPriceEscapeRoom(
        name="W krainie zła",
        opening_date=date(2020, 5, 1),
        category=models.EscapeRoomCategory.THRILLER,
        min_players_no=1,
        max_players_no=5,
        price=80,
        owner=owner_1)
    escape_room2 = models.VariablePriceEscapeRoom(
        name="Dziki zachód",
        opening_date=date(2020, 5, 1),
        category=models.EscapeRoomCategory.HORROR,
        min_players_no=1,
        max_players_no=5,
        max_price=15,
        price=80,
        owner=owner_3)
    escape_room3 = models.WeekendPriceEscapeRoom(
        name="Wszyscy ludzie prezydenta",
        opening_date=date(2020, 5, 1),
        category=models.EscapeRoomCategory.CRIME,
        min_players_no=1,
        max_players_no=5,
        weekend_price=90,
        price=80,
        owner=owner_2)
    escape_room4 = models.FixedPriceEscapeRoom(
        name="Uprowadzeni przez wampira",
        opening_date=date(2019, 1, 1),
        closing_date=date(2020, 5, 3),
        category=models.EscapeRoomCategory.THRILLER,
        min_players_no=1,
        max_players_no=5,
        price=80,
        owner=owner_2)
    escape_room5 = models.FixedPriceEscapeRoom(
        name="Rycerze",
        opening_date=date(2020, 5, 1),
        category=models.EscapeRoomCategory.HISTORY,
        min_players_no=1,
        max_players_no=5,
        price=80,
        owner=owner_2)
    escape_room6 = models.FixedPriceEscapeRoom(
        name="Subnautica",
        opening_date=date(2020, 5, 1),
        category=models.EscapeRoomCategory.ADVENTURE,
        min_players_no=1,
        max_players_no=5,
        price=80,
        owner=owner_1)

    group1 = create_group()
    player1 = models.Player(username="******",
                            password="******",
                            person=models.Person(first_name="Sandra",
                                                 last_name="Rawicz"))

    player2 = models.Player(username="******",
                            password="******",
                            person=models.Person(first_name="Anna",
                                                 last_name="Barańska"))

    player3 = models.Player(username="******",
                            password="******",
                            person=models.Person(first_name="Jan",
                                                 last_name="Nowacki"))

    group2 = models.Group(name="Ja i mój chłopak", players=[player1])
    group3 = models.Group(name="Znajomi z pracy", players=[player1, player2])

    visit1 = models.Visit(group=group2,
                          escape_room=escape_room2,
                          visit_date=date(2020, 6, 19),
                          duration=61,
                          rating=3)
    visit2 = models.Visit(group=group3,
                          escape_room=escape_room6,
                          visit_date=date(2020, 6, 16),
                          duration=50,
                          rating=5)

    recommendation1 = models.Recommendation(player=player1,
                                            escape_room=escape_room1,
                                            expected_rating=4)
    recommendation2 = models.Recommendation(player=player2,
                                            escape_room=escape_room2,
                                            expected_rating=5)
    recommendation3 = models.Recommendation(player=player1,
                                            escape_room=escape_room3,
                                            expected_rating=3)

    objects = [
        escape_room1, escape_room2, escape_room3, escape_room4, escape_room5,
        escape_room6, player3, group1, group2, group3, visit1, visit2,
        recommendation1, recommendation2, recommendation3
    ]

    session.add(player1)
    try:
        session.commit()

    except Exception as ex:
        print(ex)

    for obj in objects:
        session.add(obj)

    try:
        session.commit()

    except Exception as ex:
        print(ex)

    print(person.player)
    print(person.er_owner)
    print(escape_room5.get_rating())