From 18e47d73b4f507a634b54331408628820e6036b3 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Sat, 27 Dec 2014 19:26:16 +0100 Subject: [PATCH] add customer field to osusers.models.User - allow association of os users to Django users (customers) - adapt admin forms - add migration --- docs/changelog.rst | 1 + gnuviechadmin/osusers/admin.py | 5 +++-- .../osusers/migrations/0003_user_customer.py | 22 +++++++++++++++++++ gnuviechadmin/osusers/models.py | 7 ++++-- 4 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 gnuviechadmin/osusers/migrations/0003_user_customer.py diff --git a/docs/changelog.rst b/docs/changelog.rst index 149e45c..acc838a 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,7 @@ Changelog ========= +* :feature:`-` add a `customer` field to :py:class:`osusers.models.User` * :feature:`-` allow empty password input in :py:class:`osusers.admin.UserCreationForm` to allow generated passwords for new users diff --git a/gnuviechadmin/osusers/admin.py b/gnuviechadmin/osusers/admin.py index d2835e2..1025038 100644 --- a/gnuviechadmin/osusers/admin.py +++ b/gnuviechadmin/osusers/admin.py @@ -38,7 +38,7 @@ class UserCreationForm(forms.ModelForm): class Meta: model = User - fields = [] + fields = ['customer'] def clean_password2(self): """ @@ -57,6 +57,7 @@ class UserCreationForm(forms.ModelForm): """ user = User.objects.create_user( + customer=self.cleaned_data['customer'], password=self.cleaned_data['password1'], commit=commit) return user @@ -75,7 +76,7 @@ class UserAdmin(admin.ModelAdmin): add_fieldsets = ( (None, { 'classes': ('wide',), - 'fields': ('password1', 'password2')}), + 'fields': ('customer', 'password1', 'password2')}), ) def get_form(self, request, obj=None, **kwargs): diff --git a/gnuviechadmin/osusers/migrations/0003_user_customer.py b/gnuviechadmin/osusers/migrations/0003_user_customer.py new file mode 100644 index 0000000..b8043ef --- /dev/null +++ b/gnuviechadmin/osusers/migrations/0003_user_customer.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations +from django.conf import settings + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('osusers', '0002_auto_20141226_1456'), + ] + + operations = [ + migrations.AddField( + model_name='user', + name='customer', + field=models.ForeignKey(default=1, to=settings.AUTH_USER_MODEL), + preserve_default=False, + ), + ] diff --git a/gnuviechadmin/osusers/models.py b/gnuviechadmin/osusers/models.py index 93a4c3d..1b0d567 100644 --- a/gnuviechadmin/osusers/models.py +++ b/gnuviechadmin/osusers/models.py @@ -104,7 +104,9 @@ class UserManager(models.Manager): return nextuser @transaction.atomic - def create_user(self, username=None, password=None, commit=False): + def create_user( + self, customer, username=None, password=None, commit=False + ): uid = self.get_next_uid() gid = Group.objects.get_next_gid() if username is None: @@ -114,7 +116,7 @@ class UserManager(models.Manager): homedir = os.path.join(settings.OSUSER_HOME_BASEPATH, username) group = Group.objects.create(groupname=username, gid=gid) user = self.create(username=username, group=group, uid=uid, - homedir=homedir, + homedir=homedir, customer=customer, shell=settings.OSUSER_DEFAULT_SHELL) user.set_password(password) if commit: @@ -132,6 +134,7 @@ class User(TimeStampedModel, models.Model): gecos = models.CharField(_('Gecos field'), max_length=128, blank=True) homedir = models.CharField(_('Home directory'), max_length=256) shell = models.CharField(_('Login shell'), max_length=64) + customer = models.ForeignKey(settings.AUTH_USER_MODEL) objects = UserManager()