In progress: Jeeves communication is now based on SQLAlchemy

This commit is contained in:
Marcus Lindvall 2019-04-05 16:50:38 +02:00
parent 0fdc029153
commit 28726fee01
21 changed files with 637 additions and 78 deletions

View file

@ -0,0 +1,2 @@
from .location import Location
from .article import Article

View file

@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
from pyjeeves.models.raw import Articles
from sqlalchemy.sql.expression import and_
from pyjeeves import logging
logger = logging.getLogger("PyJeeves." + __name__)
# Relocate Jeeves modules to separate folder and let a "master" module handle imports, and setup.
class Article():
"""Handles dispatch locations in Jeeves"""
def __init__(self):
super(Article, self).__init__()
@staticmethod
def get(art_no):
""" Query an article by number """
return Articles.query.filter_by(
ArtNr=art_no
).one()
@staticmethod
def get_all(filter_=and_(Articles.ItemStatusCode == 0, Articles.ArtKod != 2)):
# .filter_by(ItemStatusCode=0, ArtKod=2)
return Articles.query.filter(filter_).all()
if __name__ == '__main__':
# print([column.key for column in Companies.__table__.columns])
logger.info("Starting TEST")
# session = RawSession()
logger.info("Testing gettings an article")
# c1 = session.query(Companies).filter_by(FtgNr="179580").first()
# print(Articles)
c1 = Articles.query.filter_by(ArtNr="2103").first()
print(c1)
logger.info(c1.json)
print (
len(Article.get_all())
)

View file

@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-
from pyjeeves.models.raw import Companies, DelivLoc
from pyjeeves import logging
logger = logging.getLogger("PyJeeves." + __name__)
# Relocate Jeeves modules to separate folder and let a "master" module handle imports, and setup.
class Location():
"""Handles dispatch locations in Jeeves"""
def __init__(self):
super(Location, self).__init__()
self.associated_company = '' # Company with new/existing locations
self._deliv_locs = [] # List of locations to be connected
def _connect_deliv_loc(self, ftgnr, description, code):
if self.associated_company == '':
raise
if len(description) > 36:
logger.warn("Truncated description %s", (description))
description = description[:36]
_deliv_loc = DelivLoc(
FtgNr=self.associated_company, OrdLevPlats1=ftgnr,
OrdLevPlBeskr=description, ForetagKod=1)
self._deliv_locs.append(_deliv_loc)
# self.session.merge(_deliv_loc)
return _deliv_loc
def create_lev_location(self, ftgnr='', name='', address='',
postal_code='', city='', gln='', invoice_ref='', phone=''):
_loc = Companies(
FtgNr=str(ftgnr), FtgNamn=name, FtgPostadr5=address,
FtgLevPostNr=postal_code, FtgPostLevAdr3=city,
EAN_Loc_Code=gln, FtgPostAdr1=invoice_ref, ComNr=phone,
ForetagKod=1)
# logger.debug("Adding company to location session")
# with self.session.no_autoflush:
# # self.session.merge(_loc) # "merge" updates if existing location exists.
_deliv_loc = self._connect_deliv_loc(ftgnr, name, gln)
return _loc, _deliv_loc
def save_locations(self):
logger.debug("Committing all location changes")
# self.session.commit() # Location company needs to be created in order to connect them.
for deliv_loc in self._deliv_locs:
deliv_loc.merge()
# self.session.merge(deliv_loc) # Create "connnections" between Customer and Location.
Companies.commit()
# self.session.commit()
if __name__ == '__main__':
# print([column.key for column in Companies.__table__.columns])
logger.info("Starting TEST")
# session = RawSession()
logger.info("Testing gettings a company")
# c1 = session.query(Companies).filter_by(FtgNr="179580").first()
print(Companies)
c1 = Companies.query.filter_by(FtgNr="179580").first()
logger.info(c1.json)
# RawSession.remove()
# from sqlalchemy.inspection import inspect
# print (inspect(Companies).columns.items())