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)
def add_entry(request, status, entry): db = request.site.db db( cmd, 'admin', #TODO: get actual app request.path, status, hasattr(request, 'user') and request.user._id or None, request.ip_address, request.remote_user, request.host, now(), int(float(request.get_elapsed()) * 1000), entry, )
def add_entry(request, status, entry): if request.site.logging: db = request.site.db db( cmd, hasattr(request, 'app') and request.app.name or None, request.path, status, hasattr(request, 'user') and request.user._id or None, request.ip_address, request.remote_user, request.host, now(), int(request.elapsed * 1000), entry, )
def post(put, sender, recipients, subject, body, attachments=None, style='plain'): """post an email message for delivery""" # pylint: disable=too-many-arguments dumps = json.dumps mail = SystemMail( sender=dumps(sender), recipients=dumps(recipients), subject=subject, body=body, attachments=dumps([a.as_tuple() for a in attachments or []]), style=style, status='waiting', created=now(), ) put(mail)
def audit(action, subject1, subject2, user=None): """audit an action""" db = context.site.db user_id = user and user._id or context.user._id app_name = context.request.app.name cmd = """ insert into audit_log ( app, user_id, activity, subject1, subject2, timestamp ) values (%s,%s,%s,%s,%s,%s) """ db( cmd, app_name, user_id, action, subject1, subject2, now(), )
def before_update(self, record): record['status'] = 'A' record['updated'] = now() record['updated_by'] = self.collection.user._id update_user_groups(record)