Пример #1
0
class UserStore(borg.Borg):
    try:
        _store = Store(Database().get_database())
    except DatabaseException:
        _store = None

    def get_store(self):
        return self._store
Пример #2
0
    def perform(self, args):
        opts=self.parse_args(args)
        if userquery('ATENTION: This command will delete any project tables at'\
                   ' database server.\nWould you like yo continue?')=='no':
            print '\nQuitting.'
            sys.exit(1)
        print '\n'+bold('Creating tables...')
        gen=Generator(opts['verbose'])
        gen.generate_database()
        tables=gen.get_database()
        db=Database()
        db.connect()

        for table in tables:
            if opts['verbose']:
                print bold('Creating table {0}...'.format(table['name']))
            db.create(table['script'])

        print bold('Database created successfully.')
Пример #3
0
    def perform(self, args):
        opts = self.parse_args(args)
        if userquery('ATENTION: This command will delete any project tables at'\
                   ' database server.\nWould you like yo continue?')=='no':
            print '\nQuitting.'
            sys.exit(1)
        print '\n' + bold('Creating tables...')
        gen = Generator(opts['verbose'])
        gen.generate_database()
        tables = gen.get_database()
        db = Database()
        db.connect()

        for table in tables:
            if opts['verbose']:
                print bold('Creating table {0}...'.format(table['name']))
            db.create(table['script'])

        print bold('Database created successfully.')
Пример #4
0
 def __init__(self):
     super(Model, self).__init__()
     self._schema=Database().get_schema()
Пример #5
0
class Model(Borg):
    """Convenience class for Models."""

    def __init__(self):
        super(Model, self).__init__()
        self._schema=Database().get_schema()

    def get_model_info(self, model):
        """Returns a dict containing the model scheme information."""
        return (self._schema.get_model_schema(model.__storm_table__),
                self._schema.get_model_view(model.__storm_table__))

    def view(self, model):
        """Perform read CRUD action."""

        def cb_find(results):
            return results.all().addCallback(cb_order)

        def cb_order(results):
            ret=[]
            for row in results:
                ret.append(self._parse_result_with_schema(row,
                    self.get_model_info(model)[0]))
            return {'success' : True, 'data' : ret}

        if _cfg.get_config('Goliat')['Project']['tos']:
            return model.store.find(model).addCallback(cb_find)
        else:
            result=cb_order(model.store.find(model))
            return defer.succeed({'success' : True, 'data' : result})

    def get(self, id, model):
        """Perform read CRUD action."""

        def cb_order(result):
            if not result:
                return {
                    'success' : False,
                    'message' : 'ID {0} doesn\'t exists on {1} table'.format(
                    id, model.__storm_table__)
                }
            data=self._parse_result_with_schema(result,
                self.get_model_info(model)[0])
            return {'success' : True, 'data' : data}

        if id=='url':
            return {
                'success' : True,
                'data' : {
                    'url' : model.get_register_url()
                }
            }

        if _cfg.get_config('Goliat')['Project']['tos']:
            return model.store.get(model, id).addCallback(cb_order)
        else:
            result=cb_order(model.store.get(model, id))
            return defer.succeed(result)

    def create(self, obj, model, data):
        """Perform create CRUD action."""

        def cb_sendback(result):
            data['id']=obj.id
            return {
                'success' : True,
                'message' : 'Created record',
                'data' : data
            }

        if _cfg.get_config('Goliat')['Project']['tos']:
            return model.store.add(obj).addCallback(
                lambda ign: model.store.commit()).addCallback(cb_sendback)
        else:
            result=model.store.add(obj)
            model.store.commit()
            return defer.succeed({'success' : True})

    def update(self, model, data):
        """Perform update CRUD action."""

        def cb_sendback(result):
            return {'success' : True}

        def cb_update(result):
            for k, v in data.iteritems():
                if k!='id':
                    result.__setattr__(k, v)
            return model.store.commit().addCallback(cb_sendback)

        if _cfg.get_config('Goliat')['Project']['tos']:
            return model.store.get(model, data['id']).addCallback(cb_update)
        else:
            newobj=model.store.get(model, data['id'])
            for k, v in data.iteritems():
                if k is not 'id':
                    newobj.setattr(k, v)
            model.store.commit()
            return defer.succeed({'success' : True})

    def destroy(self, id, model):
        """Perform destroy CRUD action"""

        def cb_sendback(ign):
            model.store.commit()
            return {
                'success' : True
            }

        def cb_remove(row):
            if not row:
                return {
                    'success' : False,
                    'message' : 'ID {0} doesn\'t exists on {1} table'.format(
                        id, model.__storm_table__)
                }
            return model.store.remove(row).addCallback(cb_sendback)

        if _cfg.get_config('Goliat')['Project']['tos']:
            return model.store.get(model, id).addCallback(cb_remove)
        else:
            row=model.store.get(model, id)
            if not row:
                return {
                    'success' : False,
                    'message' : 'ID {0} doesn\'t exists on {1} table'.format(
                        id, model.__storm_table__)
                }
            return cb_sendback(model.store.remove(row))

    def search(self, model, *args, **kwargs):
        """Perform a database search using store find."""

        def cb_order(results):
            if not results:
                return {
                    'success' : False,
                    'message' : 'There is no row that match with the criteria.'
                }

            ret=[]
            for row in results:
                ret.append(self._parse_result_with_schema(row,
                    self.get_model_info(model)[0]))

            return {'success' : True, 'data' : ret}

        def cb_find(results):
            return results.all().addCallback(cb_order)

        if _cfg.get_config('Goliat')['Project']['tos']:
            return model.store.find(model, *args, **kwargs).addCallback(cb_find)
        else:
            try:
                result=cb_order(model.store.find(model, *args, **kwargs))
                return defer.succeed(result)
            except ProgrammingError, e:
                model.store.commit()
                return {
                    'success' : False,
                    'message' : '{0}'.format(e[0])
                }
Пример #6
0
 def __init__(self):
     super(Model, self).__init__()
     self._schema = Database().get_schema()
Пример #7
0
class Model(Borg):
    """Convenience class for Models."""
    def __init__(self):
        super(Model, self).__init__()
        self._schema = Database().get_schema()

    def get_model_info(self, model):
        """Returns a dict containing the model scheme information."""
        return (self._schema.get_model_schema(model.__storm_table__),
                self._schema.get_model_view(model.__storm_table__))

    def view(self, model):
        """Perform read CRUD action."""
        def cb_find(results):
            return results.all().addCallback(cb_order)

        def cb_order(results):
            ret = []
            for row in results:
                ret.append(
                    self._parse_result_with_schema(
                        row,
                        self.get_model_info(model)[0]))
            return {'success': True, 'data': ret}

        if _cfg.get_config('Goliat')['Project']['tos']:
            return model.store.find(model).addCallback(cb_find)
        else:
            result = cb_order(model.store.find(model))
            return defer.succeed({'success': True, 'data': result})

    def get(self, id, model):
        """Perform read CRUD action."""
        def cb_order(result):
            if not result:
                return {
                    'success':
                    False,
                    'message':
                    'ID {0} doesn\'t exists on {1} table'.format(
                        id, model.__storm_table__)
                }
            data = self._parse_result_with_schema(
                result,
                self.get_model_info(model)[0])
            return {'success': True, 'data': data}

        if id == 'url':
            return {'success': True, 'data': {'url': model.get_register_url()}}

        if _cfg.get_config('Goliat')['Project']['tos']:
            return model.store.get(model, id).addCallback(cb_order)
        else:
            result = cb_order(model.store.get(model, id))
            return defer.succeed(result)

    def create(self, obj, model, data):
        """Perform create CRUD action."""
        def cb_sendback(result):
            data['id'] = obj.id
            return {'success': True, 'message': 'Created record', 'data': data}

        if _cfg.get_config('Goliat')['Project']['tos']:
            return model.store.add(obj).addCallback(
                lambda ign: model.store.commit()).addCallback(cb_sendback)
        else:
            result = model.store.add(obj)
            model.store.commit()
            return defer.succeed({'success': True})

    def update(self, model, data):
        """Perform update CRUD action."""
        def cb_sendback(result):
            return {'success': True}

        def cb_update(result):
            for k, v in data.iteritems():
                if k != 'id':
                    result.__setattr__(k, v)
            return model.store.commit().addCallback(cb_sendback)

        if _cfg.get_config('Goliat')['Project']['tos']:
            return model.store.get(model, data['id']).addCallback(cb_update)
        else:
            newobj = model.store.get(model, data['id'])
            for k, v in data.iteritems():
                if k is not 'id':
                    newobj.setattr(k, v)
            model.store.commit()
            return defer.succeed({'success': True})

    def destroy(self, id, model):
        """Perform destroy CRUD action"""
        def cb_sendback(ign):
            model.store.commit()
            return {'success': True}

        def cb_remove(row):
            if not row:
                return {
                    'success':
                    False,
                    'message':
                    'ID {0} doesn\'t exists on {1} table'.format(
                        id, model.__storm_table__)
                }
            return model.store.remove(row).addCallback(cb_sendback)

        if _cfg.get_config('Goliat')['Project']['tos']:
            return model.store.get(model, id).addCallback(cb_remove)
        else:
            row = model.store.get(model, id)
            if not row:
                return {
                    'success':
                    False,
                    'message':
                    'ID {0} doesn\'t exists on {1} table'.format(
                        id, model.__storm_table__)
                }
            return cb_sendback(model.store.remove(row))

    def search(self, model, *args, **kwargs):
        """Perform a database search using store find."""
        def cb_order(results):
            if not results:
                return {
                    'success': False,
                    'message': 'There is no row that match with the criteria.'
                }

            ret = []
            for row in results:
                ret.append(
                    self._parse_result_with_schema(
                        row,
                        self.get_model_info(model)[0]))

            return {'success': True, 'data': ret}

        def cb_find(results):
            return results.all().addCallback(cb_order)

        if _cfg.get_config('Goliat')['Project']['tos']:
            return model.store.find(model, *args,
                                    **kwargs).addCallback(cb_find)
        else:
            try:
                result = cb_order(model.store.find(model, *args, **kwargs))
                return defer.succeed(result)
            except ProgrammingError, e:
                model.store.commit()
                return {'success': False, 'message': '{0}'.format(e[0])}