ctdo-trac/TracRendezVous/tracrendezvous/location/model.py

194 lines
6.4 KiB
Python
Raw Normal View History

2012-03-31 15:45:24 +00:00
# -*- coding: utf-8 -*-
from trac.core import *
from trac.env import IEnvironmentSetupParticipant
from trac.db import Table, Column, Index
from trac.search.api import search_to_sql
from ctdotools.utils import validate_id
__all__ = ['LocationModelProvider', 'ItemLocation']
class ItemLocation(object):
def __init__(self, env, location_id=0, name=None, lat_side=None, lat_deg=None, lat_min=None, lat_sec=None, lon_side=None, lon_deg=None, lon_min=None, lon_sec=None, lat=None, lon=None):
self.env = env
self.location_id = location_id
self.name = name
self.lat_side = lat_side
self.lat_deg = lat_deg
self.lat_min = lat_min
self.lat_sec = lat_sec
self.lon_side = lon_side
self.lon_deg = lon_deg
self.lon_min = lon_min
self.lon_sec = lon_sec
self.lat = lat
self.lon = lon
@staticmethod
def fetch_all(env):
db = env.get_db_cnx()
cursor = db.cursor()
cursor.execute("SELECT * FROM item_location")
rows = cursor.fetchall()
if not rows:
return []
res = []
for row in rows:
res.append(ItemLocation(env, *row))
return res
@staticmethod
def fetch_one(env, location_id):
db = env.get_db_cnx()
cursor = db.cursor()
cursor.execute("SELECT * FROM item_location WHERE location_id=%s",
(location_id,))
row = cursor.fetchone()
if not row:
return None
return ItemLocation(env, *row)
@staticmethod
def fetch_by_name(env, name):
db = env.get_db_cnx()
cursor = db.cursor()
cursor.execute("SELECT * FROM item_location WHERE name=%s",
(name,))
row = cursor.fetchone()
if not row:
return None
return ItemLocation(env, *row)
@staticmethod
def search_one(env, name):
db = env.get_db_cnx()
cursor = db.cursor()
sql, params = search_to_sql(db, ["name",], [name,])
cursor.execute("SELECT * FROM item_location WHERE " + sql, params)
row = cursor.fetchone()
if not row:
return None
return ItemLocation(env, *row)
@staticmethod
def exists(env, name):
db = env.get_db_cnx()
cursor = db.cursor()
cursor.execute("""SELECT *
FROM 'item_location'
WHERE name=%s""", (name,))
row = cursor.fetchone()
return row != None
def commit(self):
db = self.env.get_db_cnx()
cursor = db.cursor()
cursor.execute("""INSERT INTO item_location
(name,lat_side,lat_deg,lat_min,lat_sec,lon_side,lon_deg,lon_min,lon_sec,lat,lon)
VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""", (self.name, self.lat_side, self.lat_deg, self.lat_min, self.lat_sec, self.lon_side, self.lon_deg, self.lon_min, self.lon_sec, self.lat, self.lon))
db.commit()
self.location_id = db.get_last_id(cursor, 'item_location')
def update(self):
db = self.env.get_db_cnx()
cursor = db.cursor()
cursor.execute("""UPDATE item_location
SET name =%s,
lat_side=%s,
lat_deg=%s,
lat_min=%s,
lat_sec=%s,
lon_side=%s,
lon_deg=%s,
lon_min=%s,
lon_sec=%s,
lat=%s,
lon=%s
WHERE location_id=%s""", (self.name, self.lat_side, self.lat_deg, self.lat_min, self.lat_sec, self.lon_side, self.lon_deg, self.lon_min, self.lon_sec, self.lat, self.lon, self.location_id))
db.commit()
@staticmethod
def delete(env, location_id):
db = env.get_db_cnx()
cursor = db.cursor()
cursor.execute("DELETE FROM item_location WHERE location_id=%s", (location_id,))
db.commit()
def coordinate_str(self):
if self.lat == None:
return u""
return u"%s%d°%d'%.5f\" %s%d°%d'%.5f\""%(self.lat_side, self.lat_deg, self.lat_min, self.lat_sec, self.lon_side, self.lon_deg, self.lon_min, self.lon_sec)
def __str__(self):
return "<ItemLocation: %d %s>" % (self.location_id, self.name)
class LocationModelProvider(Component):
implements(IEnvironmentSetupParticipant)
def environment_created(self):
self._create_models(self.env.get_db_cnx())
def environment_needs_upgrade(self, db):
"""First version - nothing to migrate, but possibly to create.
"""
cursor = db.cursor()
try:
cursor.execute("select count(*) from item_location;")
cursor.fetchone()
return False
except:
db.rollback()
return True
def upgrade_environment(self, db):
""" nothing to do here for now
"""
self._create_models(db)
def _create_models(self, db):
"""Called when a new Trac environment is created."""
db_backend = None
try:
from trac.db import DatabaseManager
db_backend, _ = DatabaseManager(self.env)._get_connector()
except ImportError:
db_backend = self.env.get_db_cnx()
cursor = db.cursor()
t = Table('item_location', key='location_id')[
Column('location_id', auto_increment=True),
Column('name'),
Column('lat_side'),
Column('lat_deg', type='int'),
Column('lat_min', type='int'),
Column('lat_sec', type='real'),
Column('lon_side'),
Column('lon_deg', type='int'),
Column('lon_min', type='int'),
Column('lon_sec', type='real'),
Column('lat', type='real'),
Column('lon', type='real'),
Index(['name'])]
for stmt in db_backend.to_sql(t):
self.env.log.debug(stmt)
try:
cursor.execute(stmt)
db.commit()
except Exception, e:
self.env.log.warning(str(e))
db.rollback()
#LOCATION_DATA = (
#(u"CTDO, Langer August", "N", 51, 31, 39.4, "E", 7, 27, 53.8, 51.527611, 7.464922),
#(u"WILA, Langer August", "N", 51, 31, 39.4, "E", 7, 27, 53.8, 51.527611, 7.464922))
#cursor.executemany("""INSERT INTO 'item_location'
#(name,lat_side,lat_deg,lat_min,lat_sec,lon_side,lon_deg,lon_min,lon_sec, lat, lon)
#VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""", LOCATION_DATA)
#db.commit()