Skip to content

Commit

Permalink
Convert WaypointService to use local database sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
viiru- committed Feb 13, 2024
1 parent d3f83ab commit d5df903
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 51 deletions.
2 changes: 1 addition & 1 deletion pytrainer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def __init__(self):
self.selectInitialDate()

logging.debug('Loading waypoint service...')
self.waypoint = WaypointService(self.environment.data_path, self)
self.waypoint = WaypointService(self.environment.data_path)
logging.debug('Loading extension service...')
self.extension = Extension(self.environment.data_path, self)
logging.debug('Loading plugins service...')
Expand Down
8 changes: 1 addition & 7 deletions pytrainer/test/test_waypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

from datetime import date
from unittest.mock import Mock

from pytrainer.lib.ddbb import DDBB
from pytrainer.waypoint import WaypointService
from pytrainer.test import DDBBTestCase

Expand All @@ -27,9 +23,7 @@ class WaypointTest(DDBBTestCase):

def setUp(self):
super().setUp()
main = Mock()
main.ddbb = self.ddbb
self.waypoint = WaypointService(parent=main)
self.waypoint = WaypointService()

def tearDown(self):
self.waypoint = None
Expand Down
91 changes: 48 additions & 43 deletions pytrainer/waypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import logging
from pytrainer.lib.date import unixtime2date
from sqlalchemy import Column, Unicode, Float, Integer, Date, select
from pytrainer.lib.ddbb import DeclarativeBase
from pytrainer.lib.ddbb import DeclarativeBase, DDBB


class Waypoint(DeclarativeBase):
Expand All @@ -33,67 +33,72 @@ class Waypoint(DeclarativeBase):
sym = Column(Unicode(length=200))
time = Column(Date)

class WaypointService(object):
def __init__(self, data_path = None, parent = None):

class WaypointService:

def __init__(self, data_path = None):
logging.debug(">>")
self.parent = parent
self.pytrainer_main = parent
self.ddbb = DDBB()
self.data_path = data_path
logging.debug("<<")

def removeWaypoint(self,id_waypoint):
logging.debug(">>")
logging.debug("Deleting id_waypoint=%s" %id_waypoint)
waypoint = self.pytrainer_main.ddbb.session.query(Waypoint).filter(Waypoint.id == id_waypoint).one()
self.pytrainer_main.ddbb.session.delete(waypoint)
self.pytrainer_main.ddbb.session.commit()
logging.debug("Deleting id_waypoint=%s", id_waypoint)
with self.ddbb.sessionmaker.begin() as session:
waypoint = session.query(Waypoint).filter(Waypoint.id == id_waypoint).one()
session.delete(waypoint)
logging.debug("<<")

def updateWaypoint(self,id_waypoint,lat,lon,name,desc,sym):
logging.debug(">>")
logging.debug("Updating waypoint id: %d with lat %s,lon %s,comment %s,name %s,sym %s" %(id_waypoint,lat,lon,desc,name,sym) )
waypoint = self.pytrainer_main.ddbb.session.query(Waypoint).filter(Waypoint.id == id_waypoint).one()
waypoint.lat = lat
waypoint.lon = lon
waypoint.name = name
waypoint.comment = desc
waypoint.sym = sym
self.pytrainer_main.ddbb.session.commit()
logging.debug("Updating waypoint id: %d with lat %s,lon %s,comment %s,name %s,sym %s", id_waypoint, lat, lon, desc, name, sym)
with self.ddbb.sessionmaker.begin() as session:
waypoint = session.query(Waypoint).filter(Waypoint.id == id_waypoint).one()
waypoint.lat = lat
waypoint.lon = lon
waypoint.name = name
waypoint.comment = desc
waypoint.sym = sym
logging.debug("<<")

def addWaypoint(self,lon=None,lat=None,name=None,comment=None,sym=None):
logging.debug(">>")
waypoint = Waypoint(lon=lon, lat=lat, name=name, comment=comment, sym=sym)
logging.debug("Adding waypoint with details lat %s,lon %s,comment %s,name %s,sym %s" % (lat,lon,comment,name,sym) )
self.pytrainer_main.ddbb.session.add(waypoint)
self.pytrainer_main.ddbb.session.commit()
with self.ddbb.sessionmaker.begin() as session:
waypoint = Waypoint(lon=lon, lat=lat, name=name, comment=comment, sym=sym)
logging.debug("Adding waypoint with details lat %s,lon %s,comment %s,name %s,sym %s", lat, lon, comment, name, sym)
session.add(waypoint)
session.flush()
waypoint_id = waypoint.id
logging.debug("<<")
return waypoint.id
return waypoint_id

def getwaypointInfo(self, id_waypoint):
return self.pytrainer_main.ddbb.session.execute(
select(
Waypoint.lat,
Waypoint.lon,
Waypoint.ele,
Waypoint.comment,
Waypoint.time,
Waypoint.name,
Waypoint.sym,
).where(Waypoint.id == id_waypoint)).all()
with self.ddbb.sessionmaker.begin() as session:
return session.execute(
select(
Waypoint.lat,
Waypoint.lon,
Waypoint.ele,
Waypoint.comment,
Waypoint.time,
Waypoint.name,
Waypoint.sym,
).where(Waypoint.id == id_waypoint)).all()

def getAllWaypoints(self):
return self.pytrainer_main.ddbb.session.execute(
select(
Waypoint.id,
Waypoint.lat,
Waypoint.lon,
Waypoint.ele,
Waypoint.comment,
Waypoint.time,
Waypoint.name,
Waypoint.sym,
).order_by(Waypoint.name)).all()
with self.ddbb.sessionmaker.begin() as session:
return session.execute(
select(
Waypoint.id,
Waypoint.lat,
Waypoint.lon,
Waypoint.ele,
Waypoint.comment,
Waypoint.time,
Waypoint.name,
Waypoint.sym,
).order_by(Waypoint.name)).all()

def actualize_fromgpx(self,gpxfile):
logging.debug(">>")
Expand Down

0 comments on commit d5df903

Please sign in to comment.