add customer field to osusers.models.User

- allow association of os users to Django users (customers)
- adapt admin forms
- add migration
This commit is contained in:
Jan Dittberner 2014-12-27 19:26:16 +01:00
parent 39fd326ac2
commit 18e47d73b4
4 changed files with 31 additions and 4 deletions

View file

@ -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

View file

@ -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):

View file

@ -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,
),
]

View file

@ -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()