コード例 #1
0
ファイル: test_admin.py プロジェクト: victorlin/ez2pay
 def setUp(self):
     super(TestAdminView, self).setUp()
     from ez2pay.models.user import UserModel
     from ez2pay.models.group import GroupModel
     from ez2pay.models.permission import PermissionModel
     user_model = UserModel(self.testapp.session)
     group_model = GroupModel(self.testapp.session)
     permission_model = PermissionModel(self.testapp.session)
     with transaction.manager:
         user_model.create(
             user_name='tester', 
             display_name='tester', 
             password='testerpass', 
             email='[email protected]'
         )
         admin_id = user_model.create(
             user_name='admin', 
             display_name='admin', 
             password='adminpass', 
             email='[email protected]'
         )
         group_id = group_model.create(
             group_name='admin', 
             display_name='admin', 
         )
         permission_id = permission_model.create(
             permission_name='admin', 
             description='admin'
         )
         group_model.update_permissions(group_id, [permission_id])
         user_model.update_groups(admin_id, [group_id])
コード例 #2
0
ファイル: views.py プロジェクト: victorlin/ez2pay
def user_create(request):
    _ = get_localizer(request)
    
    user_model = UserModel(request.db_session)
    group_model = GroupModel(request.db_session)
    
    factory = FormFactory(_)
    UserCreateForm = factory.make_user_create_form()
    form = UserCreateForm(request.params)
    
    groups = group_model.get_list()
    form.groups.choices = [
        (str(g.group_id), '%s - %s' % (g.group_name, g.display_name)) 
        for g in groups
    ]
    
    if request.method == 'POST':
        check_csrf_token(request)
        
        validate_result = form.validate()
        user_name = request.params['user_name']
        display_name = request.params['display_name']
        password = request.params['password']
        email = request.params['email']
        groups = request.params.getall('groups')
        
        by_name = user_model.get_by_name(user_name)
        if by_name is not None:
            msg = _(u'Username %s already exists') % user_name
            form.user_name.errors.append(msg)
            validate_result = False
            
        by_email = user_model.get_by_email(email)
        if by_email is not None:
            msg = _(u'Email %s already exists') % email
            form.email.errors.append(msg)
            validate_result = False
        
        if validate_result:
            with transaction.manager:
                user_id = user_model.create(
                    user_name=user_name,
                    display_name=display_name,
                    password=password,
                    email=email,
                )
                user_model.update_groups(user_id, map(int, groups))
            
            msg = _(u"User ${user_name} has been created", 
                    mapping=dict(user_name=user_name))
            request.add_flash(msg, 'success')
            return HTTPFound(location=request.route_url('admin.user_list'))
    
    return dict(form=form)
コード例 #3
0
ファイル: views.py プロジェクト: victorlin/ez2pay
def group_create(request):
    _ = get_localizer(request)
    
    group_model = GroupModel(request.db_session)
    permission_model = PermissionModel(request.db_session)
    
    factory = FormFactory(_)
    GroupCreateForm = factory.make_group_create_form()
    form = GroupCreateForm(request.params)
    
    permissions = permission_model.get_list()
    form.permissions.choices = [
        (str(p.permission_id), p.permission_name) 
        for p in permissions
    ]
    
    if request.method == 'POST':
        check_csrf_token(request)
        
        validate_result = form.validate()
        group_name = request.params['group_name']
        display_name = request.params['display_name']
        permissions = request.params.getall('permissions')
        
        by_name = group_model.get_by_name(group_name)
        if by_name is not None:
            msg = _(u'Group name %s already exists') % group_name
            form.group_name.errors.append(msg)
            validate_result = False

        if validate_result:
            with transaction.manager:
                group_id = group_model.create(
                    group_name=group_name, 
                    display_name=display_name, 
                )
                group_model.update_permissions(
                    group_id=group_id, 
                    permission_ids=permissions,
                )
            
            msg = _(u"Group ${group_name} has been created", 
                    mapping=dict(group_name=group_name))
            request.add_flash(msg, 'success')
            return HTTPFound(location=request.route_url('admin.group_list'))
    
    return dict(form=form)
コード例 #4
0
ファイル: views.py プロジェクト: victorlin/ez2pay
def group_list(request):
    group_model = GroupModel(request.db_session)
    groups = group_model.get_list()
    return dict(groups=groups)
コード例 #5
0
ファイル: views.py プロジェクト: victorlin/ez2pay
def user_edit(request):
    _ = get_localizer(request)
    
    user_model = UserModel(request.db_session)
    group_model = GroupModel(request.db_session)
    
    user_name = request.matchdict['user_name']
    user = user_model.get_by_name(user_name)
    if user is None:
        msg = _(u'User %s does not exists') % user_name
        return HTTPNotFound(msg)
    user_groups = [str(g.group_id) for g in user.groups]
    
    factory = FormFactory(_)
    UserEditForm = factory.make_user_edit_form()
    form = UserEditForm(
        request.params, 
        display_name=user.display_name,
        email=user.email,
        groups=user_groups
    )
    
    groups = group_model.get_list()
    form.groups.choices = [
        (str(g.group_id), '%s - %s' % (g.group_name, g.display_name), ) 
        for g in groups
    ]
    
    if request.method == 'POST':
        check_csrf_token(request)
        
        validate_result = form.validate()
        display_name = request.params['display_name']
        password = request.params['password']
        email = request.params['email']
        groups = request.params.getall('groups')
        
        by_email = user_model.get_by_email(email)
        if by_email is not None and email != user.email:
            msg = _(u'Email %s already exists') % email
            form.email.errors.append(msg)
            validate_result = False
        
        if validate_result:
            with transaction.manager:
                user_model.update_user(
                    user_id=user.user_id,
                    display_name=display_name,
                    email=email,
                )
                if password:
                    user_model.update_password(user.user_id, password)
                user_model.update_groups(user.user_id, map(int, groups))
            
            msg = _(u"User ${user_name} has been updated", 
                    mapping=dict(user_name=user_name))
            request.add_flash(msg, 'success')
            url = request.route_url('admin.user_edit', 
                                    user_name=user.user_name)
            return HTTPFound(location=url)
    
    return dict(form=form, user=user)