def migrate_datas(): from autonomie_base.models.base import DBSESSION session = DBSESSION() from alembic.context import get_bind connection = get_bind() from autonomie.models.user.login import Login op.execute("update groups set editable=0;") op.execute("update groups set `primary`=0;") op.execute( "update groups set `primary`=1 where name IN ('admin', 'contractor', 'manager')" ) op.execute('update accounts set civilite="Monsieur"') for user in connection.execute(user_helper.select()): login = Login( user_id=user.id, login=user.login, ) login.pwd_hash = user.password, login.active = user.active == 'Y' session.add(login) session.flush() op.execute( 'UPDATE user_groups set login_id="%s" where user_id=%s' % ( login.id, user.id ) ) op.drop_column("accounts", "login") op.drop_column("accounts", "password") op.drop_column("accounts", "active") from autonomie.models.user.user import User for userdatas in connection.execute(userdatas_helper.select()): if userdatas.user_id is None: user = User( lastname=userdatas.coordonnees_lastname, firstname=userdatas.coordonnees_firstname, email=userdatas.coordonnees_email1, civilite=userdatas.coordonnees_civilite or 'Monsieur', ) session.add(user) session.flush() connection.execute( userdatas_helper.update().where( userdatas_helper.c.id == userdatas.id ).values(user_id=user.id) ) else: user = User.get(userdatas.user_id) user.civilite = userdatas.coordonnees_civilite or 'Monsieur' session.merge(user) session.flush() op.execute('update accounts set civilite="Monsieur" where civilite is NULL')
def other_user(dbsession): user = User( lastname=u"Lastname2", firstname=u"Firstname2", email="*****@*****.**", ) dbsession.add(user) dbsession.flush() return user
def user(dbsession): from autonomie.models.user.user import User user = User( lastname=u"Lastname", firstname=u"Firstname", email="*****@*****.**", ) dbsession.add(user) dbsession.flush() return user
def query_homonym(self, lastname, email): """ collect the accounts with same name or email :param str lastname: The lastname to check :param str email: the email to check :returns: The SQLAlchemy query object :rtype: obj """ query = User.query().filter( or_( User.lastname == lastname, User.email == email, )) return query
def query_homonym(self, lastname, email): """ collect the accounts with same name or email :param str lastname: The lastname to check :param str email: the email to check :returns: The SQLAlchemy query object :rtype: obj """ query = User.query().filter( or_( User.lastname == lastname, User.email == email, ) ) return query
def test_add_user_submit_success(config, get_csrf_request_with_db): from autonomie.views.user.user import UserAddView from autonomie.models.user.user import User config.add_route('/users/{id}', '/users/{id}') appstruct = { "lastname": u"Lastname 1", "firstname": u"Firstname 1", "email": u"*****@*****.**", "civilite": u"Monsieur", } view = UserAddView(get_csrf_request_with_db()) result = view.submit_success(appstruct) item = User.query().filter_by(lastname=u"Lastname 1").first() assert result.location == u"/users/%s" % item.id
def company_remove_employee_view(context, request): """ Enlève un employé de l'entreprise courante """ uid = request.params.get('uid') if not uid: request.session.flash('Missing uid parameter', 'error') user = User.get(uid) if not user: request.session.flash('User not found', 'error') if user in context.employees: context.employees = [ employee for employee in context.employees if employee != user ] request.session.flash( u"L'utilisateur {0} ne fait plus partie de l'entreprise {1}". format(format_account(user), context.name)) url = request.referer if url is None: url = request.route_path('company', id=context.id) return HTTPFound(url)
def company_remove_employee_view(context, request): """ Enlève un employé de l'entreprise courante """ uid = request.params.get('uid') if not uid: request.session.flash('Missing uid parameter', 'error') user = User.get(uid) if not user: request.session.flash('User not found', 'error') if user in context.employees: context.employees = [ employee for employee in context.employees if employee != user ] request.session.flash( u"L'utilisateur {0} ne fait plus partie de l'entreprise {1}".format( format_account(user), context.name) ) url = request.referer if url is None: url = request.route_path('company', id=context.id) return HTTPFound(url)
def submit_success(self, appstruct): """ Edit the database entry and return redirect """ come_from = appstruct.pop('come_from', None) user_id = appstruct.get('user_id') company = Company() company.activities = fetch_activities_objects(appstruct) company = merge_session_with_post(company, appstruct) if user_id is not None: user_account = User.get(user_id) if user_account is not None: company.employees.append(user_account) self.dbsession.add(company) self.dbsession.flush() message = u"L'entreprise '{0}' a bien été ajoutée".format(company.name) self.session.flash(message) if come_from is not None: return HTTPFound(come_from) else: return HTTPFound(self.request.route_path("company", id=company.id))
def title(self): user = User.get(self.request.matchdict['uid']) return u'Ajouter une feuille de notes de dépenses ({})'.format( user.label, )
def validate_trainer(node, value): user = User.get(value) if 'trainer' not in user.login.groups: raise colander.invalid(node, msg)
def test_find_user_complex_lastname(dbsession, other_user): other_user.lastname = "Part1 Part2" other_user = dbsession.merge(other_user) dbsession.flush() assert User.find_user("Part1 Part2 Firstname2") == other_user
def test_find_user(dbsession, user, other_user, login): assert User.find_user("login") == user assert User.find_user("nonexistinglogin") is None assert User.find_user("Lastname Firstname") == user assert User.find_user("Lastname2 Firstname2") == other_user assert User.find_user("LASTNAME2 FIRSTNAME2") == other_user
def test_edit_schema_login_context(dbsession, pyramid_request, login, user, groups): import colander from autonomie.forms.user.login import get_add_edit_schema from autonomie.models.user.login import Login from autonomie.models.user.user import User user2 = User(email='*****@*****.**', lastname='lastname2', firstname='firstname2') dbsession.add(user2) dbsession.flush() item = Login(user_id=user2.id, login="******") item.set_password('pwd2') dbsession.add(item) dbsession.flush() pyramid_request.context = item schema = get_add_edit_schema(edit=True) schema = schema.bind(request=pyramid_request) result = schema.deserialize({ 'login': '******', 'pwd_hash': '', 'primary_group': "manager", 'groups': ['trainer'], 'user_id': user2.id, }) assert 'pwd_hash' not in result result = schema.deserialize({ 'login': '******', 'pwd_hash': 'notpwd2', 'primary_group': "manager", 'groups': ['trainer'], 'user_id': user2.id, }) assert 'pwd_hash' in result # Login already used with pytest.raises(colander.Invalid): schema.deserialize({ 'login': '******', 'pwd_hash': '', 'primary_group': "manager", 'groups': ['trainer'], 'user_id': user2.id, }) # User already linked to Login class with pytest.raises(colander.Invalid): schema.deserialize({ 'login': '******', 'pwd_hash': 'ooo', 'primary_group': "manager", 'groups': ['trainer'], 'user_id': user.id }) # wrong primary group with pytest.raises(colander.Invalid): schema.deserialize({ 'login': '******', 'pwd_hash': 'ooo', "primary_group": "falseone", 'groups': ['trainer'], 'user_id': user2.id, }) # wrong group with pytest.raises(colander.Invalid): schema.deserialize({ 'login': '******', 'pwd_hash': 'ooo', "primary_group": "contractor", 'user_id': user2.id, "groups": ["falseone"], })