def new_user(request): """Add a new user to the system manually.""" rdict = request.params u = User() u.username = unicode(rdict.get('username')) u.email = unicode(rdict.get('email')) passwd = get_random_word(8) u.password = passwd u.activated = True u.is_admin = False u.api_key = User.gen_api_key() try: DBSession.add(u) DBSession.flush() # We need to return the password since the admin added the user # manually. This is only time we should have/give the original # password. ret = dict(u) ret['random_pass'] = passwd return _api_response(request, ret) except IntegrityError, exc: # We might try to add a user that already exists. LOG.error(exc) request.response.status_int = 400 return _api_response(request, { 'error': 'Bad Request: User exists.', })
def setUp(self): self.config = testing.setUp() from sqlalchemy import create_engine engine = create_engine('sqlite://') from {{ cookiecutter.project_name }}.models import ( Base, MyModel, ) DBSession.configure(bind=engine)
def setUp(self): self.config = testing.setUp() from sqlalchemy import create_engine engine = create_engine('sqlite://') from {{ cookiecutter.project_name }}.models import ( Base, MyModel, ) DBSession.configure(bind=engine) Base.metadata.create_all(engine) with transaction.manager: model = MyModel(name='one', value=55) DBSession.add(model)
def invite(self, email): """Invite a user""" if not self.has_invites(): return False if not email: raise ValueError('You must supply an email address to invite') else: # get this invite party started, create a new useracct new_user = UserMgr.signup_user(email, self.username) # decrement the invite counter self.invite_ct = self.invite_ct - 1 DBSession.add(new_user) return new_user
def signup_user(email, signup_method): # Get this invite party started, create a new user acct. new_user = User() new_user.email = email new_user.username = email new_user.invited_by = signup_method new_user.api_key = User.gen_api_key() # they need to be deactivated new_user.reactivate('invite') # decrement the invite counter DBSession.add(new_user) return new_user
def __init__(self): try: self.session = DBSession.db_session() # 实例化 self.status = True # session异常的判断标记 except Exception as e: print e.message self.status = False
def __init__(self): try: self.session = DBSession.db_session() self.status = True except Exception as e: print e.message self.status = False
def __init__(self): """ self.session 数据库连接会话 self.status 判断数据库是否连接无异常 """ self.session, self.status = DBSession.get_session() pass
def __init__(self): try: self.session = DBSession.db_session() except Exception as e: print "=====================message========================" print e.message print "=====================message========================"
def by_id(cls, id: str) -> Any: """ Get the first entry in the database with the given id. """ with DBSession() as db: return db.query(cls).filter(cls.id == id).first()
def __init__(self): try: self.session = DBSession.db_session() except Exception as e: # raise e from WeiDian.common.loggers import generic_log generic_log(e) print(e.message)
def __init__(self): try: self.session = DBSession.db_session() # 初始化 self.status = True # session异常的判断标记 self.slogs = SLogs() except Exception as e: print e.message self.status = False
def read_dummies(skip: int = 0, limit: int = 100) -> Any: """ Retrieve dummies. """ with DBSession() as db: dummies = db.query(models.Dummy).offset(skip).limit(limit).all() return dummies
def delete(self): """ Hard delete the current object. """ with DBSession() as db: db.delete(self) db.commit() return
def signup_user(email, signup_method): # Get this invite party started, create a new user acct. exists = UserMgr.get(email=email) if exists: return exists else: new_user = User() new_user.email = email.lower() new_user.username = email.lower() new_user.invited_by = signup_method new_user.api_key = User.gen_api_key() # they need to be deactivated new_user.reactivate('signup') # decrement the invite counter DBSession.add(new_user) return new_user
def update(self, schema, **kwargs): """ Updates the current object. """ content = jsonable_encoder(schema) for key, value in content.items(): setattr(self, key, value) with DBSession() as db: db.commit() return self
def create(cls, schema): """ Creates an entry for the current table and returns it once in the database. """ content = jsonable_encoder(schema) obj = cls(**content) with DBSession() as db: db.add(obj) db.commit() db.refresh(obj) return obj
def get(self): return DBSession.query(Task).get(int(self.request.matchdict['id'])).to_json()
def activate(self): """Remove this activation""" DBSession.delete(self)
def validate_post(self, request): name = request.json['name'] num_existing = DBSession.query(Task).filter(Task.name==name).count() if num_existing > 0: request.errors.add(request.url, 'Non-unique task name.', 'There is already a task with this name.')
def list_users(request): try: users = DBSession.query(User) except DBAPIError: return Response(conn_err_msg, content_type='text/plain', status_int=500) return {'users': users}
def __init__(self): try: self.session = DBSession.db_session() except Exception as e: print(e.message)
def collection_post(self): # Adds a new task. task = self.request.json DBSession.add(Task.from_json(task))
def collection_get(self): return {'tasks': [task.name for task in DBSession.query(Task)]}
def tearDown(self): DBSession.remove() testing.tearDown()
def store(**kwargs): """Store a new log record to the db""" stored = AppLog(**kwargs) DBSession.add(stored)