Created
October 24, 2014 20:18
-
-
Save deontologician/d5c6f50e3c902f4589e4 to your computer and use it in GitHub Desktop.
Baby ORM sketch
This file contains hidden or 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
| class KamilionORM(object): | |
| required_fields = [] | |
| db = None | |
| table = None | |
| @classmethod | |
| def create(cls, message, **properties): | |
| """ | |
| Create a new KamilionORM object. | |
| @return: A special ORM object instantiated from the supplied data or None. | |
| """ | |
| properties.update({'updated_at': r.now()}) | |
| obj = { | |
| 'meta': properties, | |
| 'message': message | |
| } | |
| try: # To make the database entry with the user_id | |
| db = rdb[cdb].split(':') | |
| inserted = r.db(db[0]).table(db[1]).insert(obj).run(g.rdb_conn) | |
| except RqlRuntimeError: | |
| return None | |
| return cls(inserted['generated_keys'][0]) | |
| def check_required(self, results): | |
| # you could modify this a bit to allow required_fields to have | |
| # keys like "meta.name" etc, then split on periods. | |
| for field in self.required_fields: | |
| if field not in results['meta']: | |
| results['meta'][field] = "No" + field.capitalize() | |
| def __init__(self, uuid): | |
| db = rdb[cdb].split(':') | |
| try: | |
| results = r.db(self.db).table(self.table).get(uuid).run(g.rbd_conn) | |
| except RqlRuntimeError: | |
| print("TICKETSMODEL: InitTicket: Critical Failure: Saving Throw Failed! while looking up UUID: {}".format(uuid)) | |
| self.check_required(results) | |
| for field, value in results['meta'].iteritems(): | |
| setattr(self, field, value) | |
| class Ticket(KamilionORM): | |
| require_fields = ['email', 'phone', 'name'] | |
| table = 'tickets' | |
| db = 'products' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment