def delete_user(id=None, username=None, email=None): user = get_user(id=id, username=username, email=email) if not user: return None DBSession.delete(user) DBSession.commit() return user
def update_user(dictionary, id=None, username=None, email=None): user = get_user(id=id, username=username, email=email) if not user: return None user.from_dict(dictionary) DBSession.merge(user) DBSession.commit() return user
def create_user(dictionary=None): user = User() user.from_dict(dictionary) # created_by = dictionary.get('created_by', CREATED_BY_SIGNUP) # if user.status == USER_STATUS_DISABLED: # user.activation = Activation(created_by) DBSession.add(user) DBSession.commit() return user
def get_user_by_activation_code(username, code): """Get the user for this code""" activation = DBSession.query(Activation) \ .filter(Activation.code == code) \ .filter(User.username == username) \ .first() if activation is not None: return activation.user else: return None
def non_activated_account(delete=False): """Get a list of user accounts which are not verified since 30 days of signup""" test_date = datetime.utcnow() - NON_ACTIVATION_AGE subquery = DBSession.query(Activation.id). \ filter(Activation.valid_until < test_date). \ subquery(name="query") user_query = DBSession.query(User). \ filter(User.status == USER_STATUS_DISABLED). \ filter(User.id.in_(subquery)) # Delete the non activated accounts only if it is asked to. if delete: for user in user_query.all(): DBSession.delete(user) # If the non activated accounts are not asked to be deleted, # return their details. else: return user_query.all()
def main(global_config, **settings): """ This function returns a WSGI application. """ engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) Base.metadata.bind = engine Base.metadata.create_all() # Configure WSGI server (app is a WSGI callable) app = falcon.API( # media type to use as the value for the Content-Type header on responses media_type='application/json; charset=utf-8', # Middleware components provide a way to execute logic before the framework routes each request, # after each request is routed but before the target responder is called, or just before the # response is returned for each request. Middleware component's methods (e.g. process_request) # are executed hierarchically, as a stack, following the ordering of the list. If one of the # process_request middleware methods raises an error, it will be processed according # to the error type. If the type matches a registered error handler, that handler will be invoked # and then the framework will begin to unwind the stack, skipping any lower layers. middleware=[ DBSessionLifeCycle(), ReqRequireJSONType(), ProcessCommonReqParams(), ParseJSONReqBody(), SetCORSRespHeaders(), ], # ``Request``-like class to use instead of Falcon's default class. Among other things, # this feature affords inheriting from ``falcon.request.Request`` in order # to override the ``context_type`` class variable. # (default ``falcon.request.Request``) request_type=Request, response_type=Response, ) app.set_error_serializer(json_error_serializer) add_routes(app) return app
def get_user(id=None, username=None, email=None): q = DBSession.query(User) if id is not None: return q.filter(User.id == id).first() if username is not None: return q.filter(User.username == username).first() if email is not None: return q.filter(User.email == email).first() return None
def activate_user(username, code, new_pass): """Given this code get the user with this code make sure they exist""" activation = DBSession.query(Activation) \ .filter(Activation.code == code) \ .filter(User.username == username) \ .first() if acceptable_password(new_pass) and activation is not None: user = activation.user user.status = USER_STATUS_ENABLED user.password = new_pass activation.activate() return user else: return None
def process_response(self, req, resp, resource): log_debug(req, 'DBSessionLifeCycle: after processing response') DBSession.close()
def activation_count(self): """Count how many activations are in the system.""" return DBSession.query(Activation).count()
def activate(activation): """Remove this activation""" DBSession.delete(activation)
def user_count(self): """Number of users in the system.""" return DBSession.query(User).count()