def user(request, user_id): user = get_object_or_404(USER_MODEL, id=user_id) if request.user.id != user.id: if get_role_key_for_user(user) not in USER_EDIT_PERMISSIONS_MAP[get_role_key_for_user(request.user)]: return HttpResponse('Unauthorized', status=401) role_choices, config_entity_choices = get_config_entity_and_role_choices_for_user(request.user) form = UserForm( request.POST or None, instance=user, role_choices=role_choices, initial_role=ROLE_ID_MAP[get_role_key_for_user(user)] ) group_formset_class = get_group_formset_class(config_entity_choices) initial_group_values = [{'config_entity': g.id} for g in user.groups.all()] group_formset = group_formset_class(request.POST or None, initial=initial_group_values) if form.is_valid() and group_formset.is_valid(): update_or_create_user( username=user.username, password=form.cleaned_data.get('password') or form.cleaned_data.get('new_password'), email=form.cleaned_data.get('email'), first_name=form.cleaned_data.get('first_name'), last_name=form.cleaned_data.get('last_name'), api_key=None, groups=get_group_names_from_formset(group_formset) ) messages.add_message(request, messages.SUCCESS, 'User successfully updated.') return HttpResponseRedirect('/footprint/users/') if form.errors: for error in form.errors: messages.add_message(request, messages.ERROR, error) if group_formset.non_form_errors(): for error in group_formset.non_form_errors(): messages.add_message(request, messages.ERROR, error) return render( request, 'footprint/user.html', { 'form': form, 'group_formset': group_formset, 'user_id': user.id, 'requesting_user_role': get_role_key_for_user(request.user), 'config_entity_choices': json.dumps(config_entity_choices), 'admin_user': request.user } )
def user(request, user_id): user = get_object_or_404(USER_MODEL, id=user_id) if request.user.id != user.id: if get_role_key_for_user(user) not in USER_EDIT_PERMISSIONS_MAP[ get_role_key_for_user(request.user)]: return HttpResponse('Unauthorized', status=401) role_choices, config_entity_choices = get_config_entity_and_role_choices_for_user( request.user) form = UserForm(request.POST or None, instance=user, role_choices=role_choices, initial_role=ROLE_ID_MAP[get_role_key_for_user(user)]) group_formset_class = get_group_formset_class(config_entity_choices) initial_group_values = [{'config_entity': g.id} for g in user.groups.all()] group_formset = group_formset_class(request.POST or None, initial=initial_group_values) if form.is_valid() and group_formset.is_valid(): update_or_create_user( username=user.username, password=form.cleaned_data.get('password') or form.cleaned_data.get('new_password'), email=form.cleaned_data.get('email'), first_name=form.cleaned_data.get('first_name'), last_name=form.cleaned_data.get('last_name'), api_key=None, is_active=str(form.cleaned_data.get('is_active')), groups=get_group_names_from_formset(group_formset)) messages.add_message(request, messages.SUCCESS, 'User successfully updated.') return HttpResponseRedirect('/footprint/users/') if form.errors: for error in form.errors: messages.add_message(request, messages.ERROR, error) if group_formset.non_form_errors(): for error in group_formset.non_form_errors(): messages.add_message(request, messages.ERROR, error) return render( request, 'footprint/user.html', { 'form': form, 'group_formset': group_formset, 'user_id': user.id, 'requesting_user_role': get_role_key_for_user(request.user), 'config_entity_choices': json.dumps(config_entity_choices), 'admin_user': request.user })
def add_user(request): if get_role_key_for_user(request.user) not in [ UserGroupKey.SUPERADMIN, UserGroupKey.ADMIN, UserGroupKey.MANAGER ]: return HttpResponse('Unauthorized', status=401) role_choices, config_entity_choices = get_config_entity_and_role_choices_for_user( request.user) form = UserForm(request.POST or None, initial={'is_active': True}, role_choices=role_choices) group_formset_class = get_group_formset_class(config_entity_choices) group_formset = group_formset_class(request.POST or None) if form.is_valid() and group_formset.is_valid(): groups = str( get_group_names_from_formset(group_formset)[0]).split('__') seq_id = len( Group.objects.get(name=get_group_names_from_formset(group_formset) [0]).user_set.all()) + 1 new_user = '******'.format(groups[-2], groups[-1], seq_id) update_or_create_user( username=new_user, password=form.cleaned_data.get('password'), email=form.cleaned_data.get('email'), first_name=form.cleaned_data.get('first_name'), last_name=form.cleaned_data.get('last_name'), is_active=str(form.cleaned_data.get('is_active')), api_key=None, groups=get_group_names_from_formset(group_formset)) messages.add_message(request, messages.SUCCESS, 'User successfully added.') return HttpResponseRedirect('/footprint/users/') if form.errors: for error in form.errors: messages.add_message(request, messages.ERROR, error) if group_formset.non_form_errors(): for error in group_formset.non_form_errors(): messages.add_message(request, messages.ERROR, error) return render( request, 'footprint/user.html', { 'form': form, 'group_formset': group_formset, 'requesting_user_role': get_role_key_for_user(request.user), 'config_entity_choices': json.dumps(config_entity_choices), 'admin_user': request.user })
def add_user(request): if get_role_key_for_user(request.user) not in [UserGroupKey.SUPERADMIN, UserGroupKey.ADMIN, UserGroupKey.MANAGER]: return HttpResponse('Unauthorized', status=401) role_choices, config_entity_choices = get_config_entity_and_role_choices_for_user(request.user) form = UserForm( request.POST or None, initial={'is_active': True}, role_choices=role_choices ) group_formset_class = get_group_formset_class(config_entity_choices) group_formset = group_formset_class(request.POST or None) if form.is_valid() and group_formset.is_valid(): update_or_create_user( # TODO: # Unfortunately, it appears that changing the length of the username in Django get complicated # quickly so we're leaving this to 30 characters for now. # See http://stackoverflow.com/questions/2610088/can-djangos-auth-user-username-be-varchar75-how-could-that-be-done username=form.cleaned_data.get('email')[:30], password=form.cleaned_data.get('password'), email=form.cleaned_data.get('email'), first_name=form.cleaned_data.get('first_name'), last_name=form.cleaned_data.get('last_name'), api_key=None, groups=get_group_names_from_formset(group_formset) ) messages.add_message(request, messages.SUCCESS, 'User successfully added.') return HttpResponseRedirect('/footprint/users/') if form.errors: for error in form.errors: messages.add_message(request, messages.ERROR, error) if group_formset.non_form_errors(): for error in group_formset.non_form_errors(): messages.add_message(request, messages.ERROR, error) return render( request, 'footprint/user.html', { 'form': form, 'group_formset': group_formset, 'requesting_user_role': get_role_key_for_user(request.user), 'config_entity_choices': json.dumps(config_entity_choices), 'admin_user': request.user } )
def add_user(request): if get_role_key_for_user(request.user) not in [ UserGroupKey.SUPERADMIN, UserGroupKey.ADMIN, UserGroupKey.MANAGER ]: return HttpResponse('Unauthorized', status=401) role_choices, config_entity_choices = get_config_entity_and_role_choices_for_user( request.user) form = UserForm(request.POST or None, initial={'is_active': True}, role_choices=role_choices) group_formset_class = get_group_formset_class(config_entity_choices) group_formset = group_formset_class(request.POST or None) if form.is_valid() and group_formset.is_valid(): update_or_create_user( # TODO: # Unfortunately, it appears that changing the length of the username in Django get complicated # quickly so we're leaving this to 30 characters for now. # See http://stackoverflow.com/questions/2610088/can-djangos-auth-user-username-be-varchar75-how-could-that-be-done username=form.cleaned_data.get('email')[:30], password=form.cleaned_data.get('password'), email=form.cleaned_data.get('email'), first_name=form.cleaned_data.get('first_name'), last_name=form.cleaned_data.get('last_name'), api_key=None, groups=get_group_names_from_formset(group_formset)) messages.add_message(request, messages.SUCCESS, 'User successfully added.') return HttpResponseRedirect('/footprint/users/') if form.errors: for error in form.errors: messages.add_message(request, messages.ERROR, error) if group_formset.non_form_errors(): for error in group_formset.non_form_errors(): messages.add_message(request, messages.ERROR, error) return render( request, 'footprint/user.html', { 'form': form, 'group_formset': group_formset, 'requesting_user_role': get_role_key_for_user(request.user), 'config_entity_choices': json.dumps(config_entity_choices), 'admin_user': request.user })