[POS-commit] r231 - Stoq2/lib/domain

Evandro Vale Miquelito evandro at async.com.br
Tue Mar 29 20:00:35 BRT 2005


Author: evandro
Date: 2005-03-29 20:00:35 -0300 (Tue, 29 Mar 2005)
New Revision: 231

Removed:
   Stoq2/lib/domain/registry.py
Modified:
   Stoq2/lib/domain/__init__.py
   Stoq2/lib/domain/person.py
   Stoq2/lib/domain/stock.py
Log:
Fix for bug 1850: we need to implement a global method to create domain
objects.

Now we just have a "new" method in Model class that should provide everything
we need. when it's necessary, child classes can now implement a new version
of after_insert_model to allow some tasks after the creation process of domain
objects.
I also improved the way we were working with sequences and, with the new
atributte "sequence_name" set properly in the child classes, the ids will be
generated automatically via "new" method.

r=johan


Modified: Stoq2/lib/domain/__init__.py
===================================================================
--- Stoq2/lib/domain/__init__.py	2005-03-29 19:24:14 UTC (rev 230)
+++ Stoq2/lib/domain/__init__.py	2005-03-29 23:00:35 UTC (rev 231)
@@ -30,6 +30,7 @@
 from IndexedCatalog import Persistent, IndexedObject, attr, classref
 
 class Model(IndexedObject):
+    sequence_name = None
     _required_attrs = []
     _validated_attrs = {}
     _ic_index = 0
@@ -40,6 +41,24 @@
         self.ic_created = now()
         self.ic_modified = self.ic_created
     
+    def new(klass, conn, *args):
+        """ A method to return an IndexedCatalog instance for our model
+        object. """
+        if klass.sequence_name:
+            id = conn.sequence_increase(klass.sequence_name)
+            # By default, all the Stoq domain classes have the id as the
+            # first argument of its constructor.
+            args = (id,) + args
+
+        catalog = conn.get_catalog(klass)
+        obj = catalog.new(*args)
+
+        # Calling a hook for doing something needed after the model insertion in
+        # the catalog 
+        obj.after_insert_model(conn)
+        return obj
+    new = classmethod(new)
+                            
     def _remove_required(self, attrs):
         for attr in attrs:
             if attr in self._required_attrs:
@@ -68,22 +87,7 @@
         return self._p_oid is None
 
     def after_insert_model(self, conn):
-        """ A hook which is called by ensure_insert_model. Redefine it to
-        execute some instructions *after* the object insertion on a catalog.
+        """ A hook which is called by ensure_insert_model and new_method. 
+        Redefine it to execute some instructions *after* the object 
+        insertion on a catalog.
         """
-        pass
-
-#
-# O segundo parametro para o create() e' o parâmetro lonely
-#
-
-def create_object(conn, class_name, *args, **kwargs):
-    from domain.registry import get_creation_function
-    create = get_creation_function(class_name)
-    return create(conn, 0, *args, **kwargs)
-
-def create_lonely_object(conn, class_name, *args, **kwargs):
-    from domain.registry import get_creation_function
-    create = get_creation_function(class_name)
-    return create(conn, 1, *args, **kwargs)
-

Modified: Stoq2/lib/domain/person.py
===================================================================
--- Stoq2/lib/domain/person.py	2005-03-29 19:24:14 UTC (rev 230)
+++ Stoq2/lib/domain/person.py	2005-03-29 23:00:35 UTC (rev 231)
@@ -85,6 +85,8 @@
     Base class to register persons in the system. This class should never 
     be instantiated directly. 
     """
+
+    sequence_name = 'Person'
     
     # Person roles
     ROLE_CLIENT        = 0
@@ -108,7 +110,6 @@
         Liaison.__init__(self)
         self.id = id 
 
-   
 class Individual(Person):
     """
     Class inherited from person to store the individual's informations
@@ -233,6 +234,10 @@
                         "person must to be a Company instance!"
         PersonRole.__init__(self, person)
 
+    def after_insert_model(self, conn):
+        # to avoid circular reference
+        from domain.product import update_stocks
+        update_stocks(conn, self)
 
 class Salesman(PersonRole):
     _ic_options = [attr("comission", float, default=0.0, lazy_numbers=True),
@@ -242,44 +247,3 @@
         #XXX: Here we have to ensure the person is a Employee
         PersonRole.__init__(self, person)
 
-
-#
-# Creating objects
-#
-
-def _create_person(conn, lonely, klass, role_args):
-    id = conn.sequence_increase('Person')
-    if lonely:
-        object = klass(id)
-    else:
-        catalog = conn.get_catalog(klass)
-        object = catalog.new(id)
-    return object
-
-def create_individual(conn, lonely, role_args=()):
-    return _create_person(conn, lonely, Individual, role_args)
-
-def create_company(conn, lonely, role_args=()):
-    return _create_person(conn, lonely, Company, role_args)
-
-def create_user(conn, lonely, person):
-    if lonely:
-        object = User(person)
-    else:
-        catalog = conn.get_catalog(User)
-        object = catalog.new(person)
-    return object
-
-def create_branch_company(conn, lonely, person, **kwargs):
-    if lonely:
-        object = BranchCompany(person, **kwargs)
-    else:
-        catalog = conn.get_catalog(BranchCompany)
-        object = catalog.new(person, **kwargs)
-
-    # to avoid circular reference
-    from domain.product import update_stocks
-    update_stocks(conn, object)
-
-    return object
-

Deleted: Stoq2/lib/domain/registry.py
===================================================================
--- Stoq2/lib/domain/registry.py	2005-03-29 19:24:14 UTC (rev 230)
+++ Stoq2/lib/domain/registry.py	2005-03-29 23:00:35 UTC (rev 231)
@@ -1,46 +0,0 @@
-# -*- Mode: Python; coding: iso-8859-1 -*-
-# vi:si:et:sw=4:sts=4:ts=4
-
-##
-## Copyright (C) 2004 Async Open Source <http://www.async.com.br>
-## All rights reserved
-##
-## 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
-## USA.
-##
-"""
-domain/registry.py:
-
-    List of classes and methods to instantiate them.
-"""
-
-registry = {}
-
-def add(class_name, function_name, path):
-    if registry.has_key(class_name):
-        raise TypeError, "The domain class %s does already exist" % class_name
-    registry[class_name] = (function_name, path)
-
-def get_creation_function(klass):
-    class_name = klass.__name__
-    if not registry.has_key(class_name):
-        raise AttributeError, "no creation func for: %s" % class_name
-    function_name = registry[class_name][0]
-    path = registry[class_name][1]
-    module = __import__(path, locals(), globals(), function_name)
-    return getattr(module, function_name)
-
-# nome da classe, nome da função de criação, nome do módulo
-XXX: To implement here

Modified: Stoq2/lib/domain/stock.py
===================================================================
--- Stoq2/lib/domain/stock.py	2005-03-29 19:24:14 UTC (rev 230)
+++ Stoq2/lib/domain/stock.py	2005-03-29 23:00:35 UTC (rev 231)
@@ -49,15 +49,4 @@
     def __init__(self):
         Model.__init__(self)
 
-#
-# Creation functions
-#
 
-def create_stock_item(conn, lonely, **kwargs):
-    if lonely:
-        object = StockItem(**kwargs)
-    else:
-        catalog = conn.get_catalog(StockItem)
-        object = catalog.new(**kwargs)
-    return object
-



More information about the POS-commit mailing list