def log(self): "Create a log entry for the change." entry = dict(_id=utils.get_iuid(), entity=self.doc['_id'], changed=self.changed, modified=self.doc['modified']) if self.rqh: # xheaders argument to HTTPServer takes care of X-Real-Ip # and X-Forwarded-For entry['remote_ip'] = self.rqh.request.remote_ip try: entry['user_agent'] = self.rqh.request.headers['User-Agent'] except KeyError: pass entry[constants.DOCTYPE] = constants.LOG try: entry['user'] = self.current_user['username'] except (TypeError, KeyError): pass self.db.save(entry)
def __init__(self, doc=None, rqh=None, db=None): assert self.doctype if rqh is not None: self.rqh = rqh self.db = rqh.db self.current_user = rqh.current_user elif db is not None: self.rqh = None self.db = db self.current_user = dict() else: raise AttributeError('neither db nor rqh given') self.doc = doc or dict() self.changed = dict() if '_id' in self.doc: assert self.doctype == self.doc[constants.DOCTYPE] else: self.doc[constants.DOCTYPE] = self.doctype self.doc['_id'] = utils.get_iuid() self.initialize() self.setup()
def create_user(db): "Get user information from command line." print('Provide information for the new user...') username = raw_input('username > ') if not username: raise ValueError('username is required') if not constants.NAME_RX.match(username): raise ValueError('invalid username') view = db.view('user/username') if len(view[username]) > 0: raise ValueError('username already in use') email = raw_input('email > ') if not email: raise ValueError('email is required') if not constants.EMAIL_RX.match(email): raise ValueError('invalid email') role = raw_input('role [admin] > ') if not role: role = 'admin' role = utils.normalize(role) if role not in constants.ROLES: raise ValueError('invalid role') password = getpass.getpass('password > ') if not password: raise ValueError('password is required') if len(password) < constants.MIN_PASSWORD_LENGTH: raise ValueError("too short password; must be at least {} characters". format(constants.MIN_PASSWORD_LENGTH)) doc = {'_id': utils.get_iuid(), constants.DOCTYPE: constants.USER, 'username': username, 'email': email, 'role': role, 'password': utils.hashed_password(password), 'owner': username, 'created': utils.timestamp(), 'modified': utils.timestamp()} db.save(doc)