Disable account

This commit is contained in:
Marc 2014-10-07 13:50:59 +00:00
parent 9082770642
commit e124c830ac
8 changed files with 89 additions and 18 deletions

View File

@ -73,9 +73,9 @@ class ChangeViewActionsMixin(object):
new_urls += patterns('', new_urls += patterns('',
url('^(\d+)/%s/$' % action.url_name, url('^(\d+)/%s/$' % action.url_name,
admin_site.admin_view(action), admin_site.admin_view(action),
name='%s_%s_%s' % (opts.app_label, name='%s_%s_%s' % (opts.app_label, opts.model_name, action.url_name)
opts.model_name, )
action.url_name))) )
return new_urls + urls return new_urls + urls
def get_change_view_actions(self, obj=None): def get_change_view_actions(self, obj=None):

View File

@ -0,0 +1,22 @@
from django.contrib import messages
from django.db import transaction
from django.utils.translation import ungettext, ugettext_lazy as _
from orchestra.admin.decorators import action_with_confirmation
@transaction.atomic
@action_with_confirmation()
def disable(modeladmin, request, queryset):
num = 0
for account in queryset:
account.disable()
modeladmin.log_change(request, account, _("Disabled"))
num += 1
msg = ungettext(
_("Selected account and related services has been disabled."),
_("%s selected accounts and related services have been disabled.") % num,
num)
modeladmin.message_user(request, msg)
disable.url_name = 'disable'
disable.verbose_name = _("Disable")

View File

@ -13,6 +13,7 @@ from orchestra.admin.utils import wrap_admin_view, admin_link, set_url_query, ch
from orchestra.core import services, accounts from orchestra.core import services, accounts
from orchestra.forms import UserChangeForm from orchestra.forms import UserChangeForm
from .actions import disable
from .filters import HasMainUserListFilter from .filters import HasMainUserListFilter
from .forms import AccountCreationForm from .forms import AccountCreationForm
from .models import Account from .models import Account
@ -55,6 +56,8 @@ class AccountAdmin(ChangePasswordAdminMixin, auth.UserAdmin, ExtendedModelAdmin)
filter_horizontal = () filter_horizontal = ()
change_readonly_fields = ('username',) change_readonly_fields = ('username',)
change_form_template = 'admin/accounts/account/change_form.html' change_form_template = 'admin/accounts/account/change_form.html'
actions = [disable]
change_view_actions = actions
def formfield_for_dbfield(self, db_field, **kwargs): def formfield_for_dbfield(self, db_field, **kwargs):
""" Make value input widget bigger """ """ Make value input widget bigger """

View File

@ -65,6 +65,20 @@ class Account(auth.AbstractBaseUser):
if created and hasattr(self, 'systemusers'): if created and hasattr(self, 'systemusers'):
self.systemusers.create_user(self.username, account=self, password=self.password, is_main=True) self.systemusers.create_user(self.username, account=self, password=self.password, is_main=True)
def disable(self):
self.is_active = False
# self.save(update_fields=['is_active'])
for rel in self._meta.get_all_related_objects():
if not rel.model in services:
continue
try:
rel.model._meta.get_field_by_name('is_active')
except models.FieldDoesNotExist:
continue
else:
for obj in getattr(self, rel.get_accessor_name()).all():
obj.save(update_fields=[])
def send_email(self, template, context, contacts=[], attachments=[], html=None): def send_email(self, template, context, contacts=[], attachments=[], html=None):
contacts = self.contacts.filter(email_usages=contacts) contacts = self.contacts.filter(email_usages=contacts)
email_to = contacts.values_list('email', flat=True) email_to = contacts.values_list('email', flat=True)

View File

@ -14,7 +14,7 @@ from orchestra.utils.tests import (BaseLiveServerTestCase, random_ascii, save_re
snapshot_on_error) snapshot_on_error)
from ... import backends, settings from ... import backends, settings
from ...models import Database from ...models import Database, DatabaseUser
class DatabaseTestMixin(object): class DatabaseTestMixin(object):
@ -120,6 +120,16 @@ class AdminDatabaseMixin(DatabaseTestMixin):
name_field.submit() name_field.submit()
self.assertNotEqual(url, self.selenium.current_url) self.assertNotEqual(url, self.selenium.current_url)
@snapshot_on_error
def delete(self, dbname):
db = Database.objects.get(name=dbname)
self.admin_delete(db)
@snapshot_on_error
def delete_user(self, username):
user = DatabaseUser.objects.get(username=username)
self.admin_delete(user)
class RESTMysqlDatabaseTest(MySQLBackendMixin, RESTDatabaseMixin, BaseLiveServerTestCase): class RESTMysqlDatabaseTest(MySQLBackendMixin, RESTDatabaseMixin, BaseLiveServerTestCase):
pass pass

View File

@ -73,12 +73,15 @@ class OperationsMiddleware(object):
else: else:
update_fields = kwargs.get('update_fields', None) update_fields = kwargs.get('update_fields', None)
if update_fields: if update_fields:
append = False # "update_fileds=[]" is a convention for explicitly executing backend
for field in kwargs.get('update_fields', [None]): # i.e. account.disable()
if not update_fields == []:
execute = False
for field in update_fields:
if field not in backend.ignore_fields: if field not in backend.ignore_fields:
append = True execute = True
break break
if not append: if not execute:
continue continue
instance = copy.copy(instance) instance = copy.copy(instance)
pending_operations.add(Operation.create(backend, instance, action)) pending_operations.add(Operation.create(backend, instance, action))

View File

@ -27,7 +27,7 @@ def process_transactions(modeladmin, request, queryset):
procs = method.process(transactions) procs = method.process(transactions)
processes += procs processes += procs
for trans in transactions: for trans in transactions:
modeladmin.log_change(request, trans, 'Processed') modeladmin.log_change(request, trans, _("Processed"))
if not processes: if not processes:
return return
opts = modeladmin.model._meta opts = modeladmin.model._meta
@ -46,7 +46,7 @@ def process_transactions(modeladmin, request, queryset):
def mark_as_executed(modeladmin, request, queryset, extra_context={}): def mark_as_executed(modeladmin, request, queryset, extra_context={}):
for trans in queryset: for trans in queryset:
trans.mark_as_executed() trans.mark_as_executed()
modeladmin.log_change(request, trans, 'Executed') modeladmin.log_change(request, trans, _("Executed"))
msg = _("%s selected transactions have been marked as executed.") % queryset.count() msg = _("%s selected transactions have been marked as executed.") % queryset.count()
modeladmin.message_user(request, msg) modeladmin.message_user(request, msg)
mark_as_executed.url_name = 'execute' mark_as_executed.url_name = 'execute'
@ -58,7 +58,7 @@ mark_as_executed.verbose_name = _("Mark as executed")
def mark_as_secured(modeladmin, request, queryset): def mark_as_secured(modeladmin, request, queryset):
for trans in queryset: for trans in queryset:
trans.mark_as_secured() trans.mark_as_secured()
modeladmin.log_change(request, trans, 'Secured') modeladmin.log_change(request, trans, _("Secured"))
msg = _("%s selected transactions have been marked as secured.") % queryset.count() msg = _("%s selected transactions have been marked as secured.") % queryset.count()
modeladmin.message_user(request, msg) modeladmin.message_user(request, msg)
mark_as_secured.url_name = 'secure' mark_as_secured.url_name = 'secure'
@ -70,7 +70,7 @@ mark_as_secured.verbose_name = _("Mark as secured")
def mark_as_rejected(modeladmin, request, queryset): def mark_as_rejected(modeladmin, request, queryset):
for trans in queryset: for trans in queryset:
trans.mark_as_rejected() trans.mark_as_rejected()
modeladmin.log_change(request, trans, 'Rejected') modeladmin.log_change(request, trans, _("Rejected"))
msg = _("%s selected transactions have been marked as rejected.") % queryset.count() msg = _("%s selected transactions have been marked as rejected.") % queryset.count()
modeladmin.message_user(request, msg) modeladmin.message_user(request, msg)
mark_as_rejected.url_name = 'reject' mark_as_rejected.url_name = 'reject'
@ -105,7 +105,7 @@ _format_commit = partial(_format_display_objects, related=('all', 'secured'))
def mark_process_as_executed(modeladmin, request, queryset): def mark_process_as_executed(modeladmin, request, queryset):
for process in queryset: for process in queryset:
process.mark_as_executed() process.mark_as_executed()
modeladmin.log_change(request, process, 'Executed') modeladmin.log_change(request, process, _("Executed"))
msg = _("%s selected processes have been marked as executed.") % queryset.count() msg = _("%s selected processes have been marked as executed.") % queryset.count()
modeladmin.message_user(request, msg) modeladmin.message_user(request, msg)
mark_process_as_executed.url_name = 'executed' mark_process_as_executed.url_name = 'executed'
@ -117,7 +117,7 @@ mark_process_as_executed.verbose_name = _("Mark as executed")
def abort(modeladmin, request, queryset): def abort(modeladmin, request, queryset):
for process in queryset: for process in queryset:
process.abort() process.abort()
modeladmin.log_change(request, process, 'Aborted') modeladmin.log_change(request, process, _("Aborted"))
msg = _("%s selected processes have been aborted.") % queryset.count() msg = _("%s selected processes have been aborted.") % queryset.count()
modeladmin.message_user(request, msg) modeladmin.message_user(request, msg)
abort.url_name = 'abort' abort.url_name = 'abort'
@ -129,7 +129,7 @@ abort.verbose_name = _("Abort")
def commit(modeladmin, request, queryset): def commit(modeladmin, request, queryset):
for trans in queryset: for trans in queryset:
trans.mark_as_rejected() trans.mark_as_rejected()
modeladmin.log_change(request, trans, 'Rejected') modeladmin.log_change(request, trans, _("Rejected"))
msg = _("%s selected transactions have been marked as rejected.") % queryset.count() msg = _("%s selected transactions have been marked as rejected.") % queryset.count()
modeladmin.message_user(request, msg) modeladmin.message_user(request, msg)
commit.url_name = 'commit' commit.url_name = 'commit'

View File

@ -348,3 +348,22 @@ class AdminSystemUserTest(AdminSystemUserMixin, BaseLiveServerTestCase):
self.account = self.create_account(username=self.account.username, superuser=True) self.account = self.create_account(username=self.account.username, superuser=True)
self.selenium.delete_all_cookies() self.selenium.delete_all_cookies()
self.admin_login() self.admin_login()
@snapshot_on_error
def test_disable_account(self):
username = '%s_systemuser' % random_ascii(10)
password = '@!?%spppP001' % random_ascii(5)
self.add(username, password)
self.addCleanup(self.delete, username)
self.validate_ftp(username, password)
self.disable(username)
self.validate_user(username)
disable = reverse('admin:accounts_account_disable', args=(self.account.pk,))
url = self.live_server_url + disable
self.selenium.get(url)
confirmation = self.selenium.find_element_by_name('post')
confirmation.submit()
self.assertNotEqual(url, self.selenium.current_url)
self.assertRaises(ftplib.error_perm, self.validate_ftp, username, password)