def edit(request, object_id): object_id = long(object_id) model = get_object_or_404(User, pk=object_id) can_delete = model.lastlogin == None if request.method == method.GET: form = UserForm(instance=model) elif request.method == method.POST: form = UserForm(request.POST, instance=model) if form.is_valid(): try: model = form.save(True) flash_success(request, _('The user \'%s\' was changed successfully.') % model.username) except IntegrityError: flash_error(request, _('The user \'%s\' already exists.') % form.cleaned_data['username'].strip().lower()) else: flash_form_error(request, form) redir = _redirect_if_needed(request, InView.EDIT, object_id) if redir != None: return redir context_instance = RequestContext(request) template_name = 'users/edit.html' extra_context = { 'menu': 'users', 'form': form, 'model': model, 'can_delete': can_delete, } return render_to_response(template_name, extra_context, context_instance)
def add(request): object_id = None if request.method == method.GET: form = UserForm() elif request.method == method.POST: form = UserForm(request.POST) if form.is_valid(): try: model = form.save(False) model.status = 'FLN' model.save() object_id = model.id flash_success(request, _('The user \'%s\' was created successfully.') % model.username) form = UserForm() except IntegrityError: flash_error(request, _('The user \'%s\' already exists.') % form.cleaned_data['username'].strip().lower()) else: flash_form_error(request, form) redir = _redirect_if_needed(request, InView.ADD, object_id) if redir != None: return redir context_instance = RequestContext(request) template_name = 'users/add.html' extra_context = { 'menu': 'users', 'form': form } return render_to_response(template_name, extra_context, context_instance)
def update(request): user = get_object_or_404(User, pk=request.user.id) profile = get_object_or_404(Profile, user=request.user.id) if request.method == method.GET: form_user = UserUpdateForm(instance=user) form_profile = ProfileUpdateForm(instance=profile) if request.method == method.POST: form_user = UserUpdateForm(request.POST.copy(), instance=user) form_profile = ProfileUpdateForm(request.POST.copy(), instance=profile) if form_user.is_valid() and form_profile.is_valid(): user = form_user.save(True) form_user.data['old_password'] = '' form_user.data['new_password1'] = '' form_user.data['new_password2'] = '' profile = form_profile.save(True) flash_success(request, _('Your profile was updated successfully.')) return set_language(request) else: form_wrong = form_user if not form_user.is_valid() else form_profile flash_form_error(request, form_wrong) #print 'language = ' + repr(form.base_fields['language'].initial) context_instance = RequestContext(request) template_name = 'profiles/update.html' extra_context = { 'menu': '', 'form_user': form_user, 'form_profile': form_profile, 'show_debug': 'debug' in request.GET } return render_to_response(template_name, extra_context, context_instance)
def edit(request, object_id): object_id = long(object_id) model = get_object_or_404(Badword, pk=object_id) if request.method == method.GET: form = BadwordForm(instance=model) elif request.method == method.POST: object_id = None form = BadwordForm(request.POST, instance=model) if form.is_valid(): try: model = form.save(True) object_id = model.id flash_success(request, _('The badword \'%s\' was changed successfully.') % model.badword) except IntegrityError: flash_error(request, _('The badword \'%s\' already exists.') % form.cleaned_data['badword'].strip().lower()) else: flash_form_error(request, form) redir = _redirect_if_needed(request, InView.EDIT, object_id) if redir != None: return redir context_instance = RequestContext(request) template_name = 'badwords/edit.html' extra_context = { 'menu': 'badwords', 'form': form, 'model': model, } return render_to_response(template_name, extra_context, context_instance)
def update(request): if request.method == method.GET: form = SettingsUpdateForm() elif request.method == method.POST: form = SettingsUpdateForm(request.POST) if form.is_valid(): form = form.save() flash_success(request, _('The settings were changed successfully.')) else: flash_form_error(request, form) context_instance = RequestContext(request) template_name = 'settings/update.html' extra_context = { 'menu': 'settings', 'form': form } return render_to_response(template_name, extra_context, context_instance)
def add(request): object_id = None if request.method == method.GET: form = AclForm() elif request.method == method.POST: form = AclForm(request.POST) if form.is_valid(): model = form.save(True) object_id = model.id flash_success(request, _('The acl %d was created successfully.') % object_id) form = AclForm() else: flash_form_error(request, form) redir = _redirect_if_needed(request, InView.ADD, object_id) if redir != None: return redir #print form.errors['__all__'][0] context_instance = RequestContext(request) template_name = 'acls/add.html' extra_context = { 'menu': 'acls', 'form': form } return render_to_response(template_name, extra_context, context_instance)
def add(request): object_id = None if request.method == method.GET: form = GroupForm() elif request.method == method.POST: form = GroupForm(request.POST) if form.is_valid(): users_added = map(long, request.POST.getlist('users')) rules_added = map(long, request.POST.getlist('rules')) try: model = form.save(True) object_id = model.id if users_added: User.objects.filter(pk__in=users_added).update(group=object_id) if rules_added: for rule_id in rules_added: GroupRule(group_id=object_id, rule_id=rule_id).save() flash_success(request, _('The group \'%s\' was created successfully.') % model.groupname) form = GroupForm() except IntegrityError: flash_error(request, _('The group \'%s\' already exists.') % form.cleaned_data['groupname'].strip()) else: flash_form_error(request, form) redir = _redirect_if_needed(request, InView.ADD, object_id) if redir != None: return redir available_users = User.objects.all().order_by('group', 'username') available_rules = Rule.objects.all().order_by('id') context_instance = RequestContext(request) template_name = 'groups/add.html' extra_context = { 'menu': 'groups', 'form': form, 'available_users': available_users, 'available_rules': available_rules } return render_to_response(template_name, extra_context, context_instance)
def edit(request, object_id): object_id = long(object_id) model = get_object_or_404(Acl, pk=object_id) if request.method == method.GET: form = AclForm(instance=model) elif request.method == method.POST: form = AclForm(request.POST, instance=model) if form.is_valid(): model = form.save(True) flash_success(request, _('The acl %d was changed successfully.') % object_id) else: flash_form_error(request, form) redir = _redirect_if_needed(request, InView.EDIT, object_id) if redir != None: return redir context_instance = RequestContext(request) template_name = 'acls/edit.html' extra_context = { 'menu': 'acls', 'form': form, 'model': model, } return render_to_response(template_name, extra_context, context_instance)
def edit(request, object_id): object_id = long(object_id) model = get_object_or_404(UserGroup, pk=object_id) can_delete = not model.isbuiltin if request.method == method.GET: form = GroupForm(instance=model) elif request.method == method.POST: form = GroupForm(request.POST, instance=model) if form.is_valid(): class ChangedUsers: # Map submitted users to long submitted = map(long, request.POST.getlist('users')) # Select users that are currently part of the group current = list(User.objects \ .filter(group=object_id) \ .values_list('id', flat=True)) added = list(set(submitted).difference(current)) removed = list(set(current).difference(submitted)) #print 'ChangedUsers.added = %s' % added #print 'ChangedUsers.removed = %s' % removed class ChangedRules: # Map submitted rules to long submitted = map(long, request.POST.getlist('rules')) # Select rules that are currently assigned to the group current = list(GroupRule.objects \ .filter(group=object_id) \ .values_list('rule_id', flat=True)) added = list(set(submitted).difference(current)) removed = list(set(current).difference(submitted)) #print 'ChangedRules.added = %s' % added #print 'ChangedRules.removed = %s' % removed users = ChangedUsers() rules = ChangedRules() try: model = form.save(True) if users.added: User.objects.filter(pk__in=users.added).update(group=object_id) if users.removed: User.objects.filter(pk__in=users.removed).update(group=1L) if rules.added: for rule_id in rules.added: GroupRule(group_id=object_id, rule_id=rule_id).save() if rules.removed: GroupRule.objects.filter(rule__in=rules.removed).delete() flash_success(request, _('The group \'%s\' was changed successfully.') % model.groupname) if users.removed and object_id == 1L: flash_warning(request, _('However, the users weren\'t removed' ' because they must belong to one group at least.')) except IntegrityError: flash_error(request, _('The group \'%s\' already exists.') % form.cleaned_data['groupname'].strip()) else: flash_form_error(request, form) redir = _redirect_if_needed(request, InView.EDIT, object_id) if redir != None: return redir available_users = User.objects.exclude(group__id=object_id) \ .order_by('group', 'username') # With sub-query: # SELECT r.* # FROM rules as r # WHERE r.id NOT IN ( # SELECT gr.rule_id # FROM grouprules as gr # WHERE gr.group_id = 1 # ) # Without sub-query: # SELECT r.* # FROM rules as r # LEFT OUTER JOIN grouprules as gr ON gr.rule_id = r.id AND gr.group_id = 1 # WHERE gr.group_id IS NULL available_rules = Rule.objects.extra(where=[''' rules.id NOT IN ( SELECT rule_id FROM grouprules WHERE group_id = %d ) ''' % object_id]).order_by('id') context_instance = RequestContext(request) template_name = 'groups/edit.html' extra_context = { 'menu': 'groups', 'form': form, 'model': model, 'can_delete': can_delete, 'available_users': available_users, 'available_rules': available_rules } return render_to_response(template_name, extra_context, context_instance)