def create_button(self, *args, **data): """Create a record""" collection = self.collection user = collection.user if collection.fields.validate(data): record = collection.model( collection.fields, ) record.pop('key', None) try: key = record.key except AttributeError: key = None if key and locate(collection, record.key) is not None: error('That {} already exists'.format(collection.item_name)) else: try: # convert property to data attribute # so it gets stored as data record.key = record.key except AttributeError: # can happen when key depends on database # auto-increment value. pass record.update(dict( created=now(), updated=now(), owner_id=user._id, created_by=user._id, updated_by=user._id, )) self.before_insert(record) collection.store.put(record) collection.search_engine(collection).add( record._id, collection.fields.as_searchable(), ) self.after_insert(record) msg = '%s added %s %s' % ( user.link, collection.link, record.link ) logger = logging.getLogger(__name__) logger.info(msg) log_activity(msg) return redirect_to(collection.url)
def save_button(self, key, *a, **data): """Save a record""" collection = self.collection user = collection.user user.authorize('update', collection) if collection.fields.validate(data): record = locate(collection, key) if record: user.authorize('update', record) record.update(collection.fields) record.pop('key', None) if record.key != key and locate(collection, record.key): # record key should always be a str, even if the actual # record.id is being used as the key. error('That {} already exists'.format(collection.item_name)) else: record.updated = now() record.updated_by = user._id # convert property to data attribute # so it gets stored as data record.key = record.key self.before_update(record) collection.store.put(record) collection.search_engine(collection).update( record._id, collection.fields.as_searchable(), ) self.after_update(record) msg = '%s updated %s %s' % ( user.link, collection.link, record.link ) logger = logging.getLogger(__name__) logger.info(msg) log_activity(msg) if record.key != key: log_activity( '%s changed %s %s to %s' % ( user.link, collection.link, key, record.key ) ) return redirect_to(record.url)