2008-04-04 20:36:46 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2007-07-02 11:14:47 +02:00
|
|
|
#
|
2009-07-19 01:03:23 +02:00
|
|
|
# Copyright (C) 2007, 2008, 2009 by Jan Dittberner.
|
2007-07-02 11:14:47 +02:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
# USA.
|
|
|
|
#
|
|
|
|
# Version: $Id$
|
2008-06-07 23:25:35 +02:00
|
|
|
"""This module defines the BackendEntityHandler class."""
|
2007-07-02 11:14:47 +02:00
|
|
|
|
2008-06-06 21:20:18 +02:00
|
|
|
import logging
|
2009-07-19 01:03:23 +02:00
|
|
|
from sqlalchemy.orm import create_session, mapper, relation
|
|
|
|
from gnuviechadmin.backend.tables import dbsetup
|
2007-07-02 11:14:47 +02:00
|
|
|
|
2008-06-06 21:20:18 +02:00
|
|
|
|
2007-07-02 11:14:47 +02:00
|
|
|
class BackendEntityHandler(object):
|
2008-06-07 23:25:35 +02:00
|
|
|
"""This class is a handler for BackendEntity instances."""
|
2008-06-06 21:20:18 +02:00
|
|
|
|
2009-07-19 01:03:23 +02:00
|
|
|
def __init__(self, entityclass, toclass, config, verbose = False):
|
2008-06-07 23:25:35 +02:00
|
|
|
"""Initialize the handler with a specific entity class,
|
|
|
|
transfer object class and verbosity flag."""
|
2007-07-09 08:46:36 +02:00
|
|
|
self.logger = logging.getLogger("%s.%s" % (
|
|
|
|
self.__class__.__module__, self.__class__.__name__))
|
2009-07-19 01:03:23 +02:00
|
|
|
dbsetup(config)
|
|
|
|
|
2007-07-02 11:14:47 +02:00
|
|
|
self.entityclass = entityclass
|
2007-07-09 08:46:36 +02:00
|
|
|
self.toclass = toclass
|
2009-07-19 01:03:23 +02:00
|
|
|
self.config = config
|
2007-07-02 11:14:47 +02:00
|
|
|
self.verbose = verbose
|
|
|
|
|
|
|
|
def create(self, **kwargs):
|
2007-07-09 08:46:36 +02:00
|
|
|
"""Create a new entity of the managed type with the fields set
|
|
|
|
to the values in kwargs."""
|
|
|
|
self.logger.debug("create with params %s", str(kwargs))
|
2009-07-19 01:03:23 +02:00
|
|
|
delegate = self.toclass(self.config, **kwargs)
|
|
|
|
entity = self.entityclass(self.config, delegate, self.verbose)
|
2008-04-04 20:36:46 +02:00
|
|
|
sess = create_session()
|
2007-07-09 08:46:36 +02:00
|
|
|
try:
|
2009-07-19 01:03:23 +02:00
|
|
|
sess.begin()
|
|
|
|
sess.add(delegate)
|
2007-07-09 08:46:36 +02:00
|
|
|
sess.flush()
|
|
|
|
sess.refresh(delegate)
|
|
|
|
entity.create_hook(sess)
|
2009-07-19 01:03:23 +02:00
|
|
|
sess.commit()
|
2007-07-09 08:46:36 +02:00
|
|
|
except:
|
2009-07-19 01:03:23 +02:00
|
|
|
sess.rollback()
|
2007-07-09 08:46:36 +02:00
|
|
|
self.logger.exception("Exception in create.")
|
|
|
|
raise
|
2007-07-02 11:14:47 +02:00
|
|
|
|
2007-07-09 08:46:36 +02:00
|
|
|
def fetchall(self, **kwargs):
|
2007-07-02 11:14:47 +02:00
|
|
|
"""Fetches all entities of the managed entity type."""
|
2007-07-09 08:46:36 +02:00
|
|
|
self.logger.debug("fetchall with params %s", str(kwargs))
|
2008-04-04 20:36:46 +02:00
|
|
|
session = create_session()
|
2007-07-09 08:46:36 +02:00
|
|
|
query = session.query(self.toclass)
|
|
|
|
if kwargs:
|
2008-04-04 20:36:46 +02:00
|
|
|
allentities = query.filter_by(**kwargs).all()
|
2007-07-09 08:46:36 +02:00
|
|
|
else:
|
2008-04-04 20:36:46 +02:00
|
|
|
allentities = query.all()
|
2009-07-19 01:03:23 +02:00
|
|
|
return [self.entityclass(self.config, entity, self.verbose) \
|
2007-07-09 08:46:36 +02:00
|
|
|
for entity in allentities]
|
2007-07-02 11:14:47 +02:00
|
|
|
|
|
|
|
def delete(self, pkvalue):
|
|
|
|
"""Deletes the entity of the managed entity type that has the
|
|
|
|
specified primary key value."""
|
2007-07-09 08:46:36 +02:00
|
|
|
self.logger.debug("delete with primary key %s", str(pkvalue))
|
2008-04-04 20:36:46 +02:00
|
|
|
sess = create_session()
|
2007-07-02 11:14:47 +02:00
|
|
|
try:
|
2009-07-19 01:03:23 +02:00
|
|
|
sess.begin()
|
2008-06-07 23:25:35 +02:00
|
|
|
tobj = sess.query(self.toclass).get(pkvalue)
|
|
|
|
if tobj:
|
2009-07-19 01:03:23 +02:00
|
|
|
entity = self.entityclass(self.config, tobj, self.verbose)
|
2007-07-09 08:46:36 +02:00
|
|
|
self.logger.info("delete %s", str(entity))
|
2007-07-02 11:14:47 +02:00
|
|
|
if self.verbose:
|
|
|
|
print "delete %s" % (str(entity))
|
2007-07-09 08:46:36 +02:00
|
|
|
entity.delete_hook(sess)
|
2008-06-07 23:25:35 +02:00
|
|
|
sess.delete(tobj)
|
2009-07-19 01:03:23 +02:00
|
|
|
sess.commit()
|
2008-06-07 23:25:35 +02:00
|
|
|
except Exception:
|
2009-07-19 01:03:23 +02:00
|
|
|
sess.rollback()
|
2007-07-09 08:46:36 +02:00
|
|
|
self.logger.exception("Exception in delete.")
|
|
|
|
raise
|