Created
May 27, 2011 22:19
-
-
Save rochacbruno/996312 to your computer and use it in GitHub Desktop.
Trying to create an app ORM inside modules folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
import os | |
import sys | |
import unittest | |
sys.path.append(os.path.realpath('../../../')) | |
from gluon import * | |
class Database(DAL): | |
"""extends DAL including app objects """ | |
def client(self): | |
client = self.define_table('client', | |
Field('name'), | |
Field('copany_id'), | |
) | |
return client | |
def company(self): | |
company = self.define_table('company', | |
Field('name'), | |
) | |
return company | |
class ORM(object): | |
"""Creates an app ORM """ | |
def __init__(self): | |
self.db = Database('sqlite://mydatabase.sqlite', folder=os.path.realpath('../databases')) | |
commit = self.db.commit | |
rollback = self.db.rollback | |
self.errors = [] | |
def new_company(self, name): | |
company = self.db.company() | |
new_company = False | |
try: | |
new_company = company.insert(name=name) | |
commit() | |
except Exception, e: | |
self.errors.append(('InsertError','company',e)) | |
rollback() | |
return new_company | |
def new_client(self, name, company): | |
client = self.db.client() | |
company = self.db.company() | |
if isinstance(company, str): | |
CID = company(name=company) | |
elif isinstance(company, int): | |
CID = company | |
else: | |
raise TypeError('company type not suported') | |
new_client = False | |
try: | |
new_client = client.insert(name=name,company_id=CID) | |
commit() | |
except Exception, e: | |
self.errors.append(('Insert Error','client',e)) | |
rollback() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> from myapi import ORM | |
>>> orm = ORM() #database is created well | |
>>> orm.new_company(name='Apple Inc.') | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
File "myapi.py", line 39, in new_company | |
company = self.db.company() | |
File "myapi.py", line 24, in company | |
Field('name'), | |
File "/home/rochacbruno/projects/web2pytrunk/gluon/dal.py", line 4135, in define_table | |
raise SyntaxError, 'invalid table name: %s' % tablename | |
SyntaxError: invalid table name: company | |
>>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment