This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Working insert,update,delete,select using magic model type behavior

  • Loading branch information...
gmr committed Feb 15, 2010
1 parent 54f538d commit 588fe6a30a4bc82325ba479ac23687908f6a72a2
Showing with 33 additions and 17 deletions.
  1. +0 −8 tinman/apps.py
  2. +33 −9 tinman/data.py
View
@@ -16,13 +16,5 @@
class Home(handler.RequestHandler):
def get(self):
-
- gmr = user.TinmanUser()
- #self.data.create_all()
- gmr.username = 'gmr'
- gmr.email = 'gavinmroy@gmail.com'
- gmr.password = 'foo'
- gmr.save()
-
self.render('templates/apps/home.html');
View
@@ -8,7 +8,7 @@
__date__ = "2010-02-07"
__version__ = 0.1
-from new import classobj
+from new import classobj
import logging
# A persistent dictionary for our database connection and elements
@@ -38,27 +38,40 @@ def __init__(self, configuration):
else:
logging.error('Unknown data driver type')
+ def commit(self):
+ global driver
+ driver['session'].commit()
+
def create_all(self):
global driver
driver['metadata'].create_all()
+
+ def flush(self):
+ global driver
+ driver['session'].flush()
+ def delete(self, obj):
+ global driver
+ driver['session'].delete(obj)
class Model(object):
# SQL Alchemy Model to be extended
- from sqlalchemy import Table, Column, MetaData, ForeignKey, \
+ from sqlalchemy import exceptions, Table, Column, MetaData, ForeignKey, \
Boolean, Date, DateTime, Float, Integer, String, \
Interval, LargeBinary, Numeric, SmallInteger, \
Text, Unicode, UnicodeText
+ count = 0
schema_name = None
- def __init__(self):
+ def __init__(self, *args, **kwargs):
from sqlalchemy.orm import mapper
# Get the driver
global driver
-
+
+ # Define our table name from our class name
table_name = self.__class__.__name__.lower()
# Look to see if the class already has our instance
@@ -82,7 +95,7 @@ def __init__(self):
if type(self.__class__.__dict__[attr]).__name__ == 'Column':
self.table.append_column(self.__class__.__dict__[attr])
self.columns[attr] = None
-
+
# Dynamically create or data object
DataObject = type('DataObject',(object,),self.columns)
@@ -92,20 +105,31 @@ def __init__(self):
# Create our internal object to use for data manipulation
self.obj = DataObject()
+ # Query based upon a kwargs
+ if len(kwargs):
+
+ # A query object for our model object
+ query = driver['session'].query(DataObject)
+ self.results = query.filter_by(**kwargs).all()
+ self.count = len(self.results)
+
+ # If it's only one row, map it to the our model object
+ if len(self.results) == 1:
+ for column in self.columns:
+ self.__dict__[column] = self.results[0].__dict__[column]
+
# Pop this handle in the global driver stack
driver['models'][table_name] = self.table
-
-
def create(self):
self.table.create()
def save(self):
global driver
-
+
for column in self.columns:
if self.__dict__.has_key(column):
self.obj.__dict__[column] = self.__dict__[column]
-
+
driver['session'].add(self.obj)
driver['session'].commit()

0 comments on commit 588fe6a

Please sign in to comment.