use get_user_model and AUTH_USER_MODEL

This commit is contained in:
Thomas Waldmann 2013-11-24 09:37:47 +01:00
parent a1210b21eb
commit d83df3ead1
7 changed files with 135 additions and 28 deletions

View File

@ -31,12 +31,13 @@ def db_init(db): # note: db is a predefined fixture and required here to have t
"""
Init the database contents for testing, so we have a service domain, ...
"""
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from nsupdate.main.models import Host, Domain
user_model = get_user_model()
# create a fresh test user
u = User.objects.create_user(USERNAME, settings.DEFAULT_FROM_EMAIL, PASSWORD)
u = user_model.objects.create_user(USERNAME, settings.DEFAULT_FROM_EMAIL, PASSWORD)
u.save()
u2 = User.objects.create_user(USERNAME2, 'test@example.org', PASSWORD)
u2 = user_model.objects.create_user(USERNAME2, 'test@example.org', PASSWORD)
u2.save()
# this is for updating:
Domain.objects.create(

View File

@ -1,12 +1,12 @@
# -*- coding: utf-8 -*-
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
class UserProfileForm(forms.ModelForm):
class Meta(object):
model = User
model = get_user_model()
fields = ['first_name', 'last_name', 'email']
widgets = {
'email': forms.widgets.TextInput(attrs=dict(autofocus=None)),

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from django.views.generic import UpdateView
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
from .forms import UserProfileForm
@ -8,7 +8,7 @@ from .forms import UserProfileForm
class UserProfileView(UpdateView):
template_name = "accounts/user_profile.html"
model = User
model = get_user_model()
fields = ['first_name', 'last_name', 'email']
form_class = UserProfileForm

View File

@ -0,0 +1,102 @@
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Changing field 'BlacklistedDomain.created_by'
db.alter_column(u'main_blacklisteddomain', 'created_by_id', self.gf('django.db.models.fields.related.ForeignKey')(default=None, to=orm['auth.User']))
# Changing field 'Domain.created_by'
db.alter_column(u'main_domain', 'created_by_id', self.gf('django.db.models.fields.related.ForeignKey')(default=None, to=orm['auth.User']))
def backwards(self, orm):
# Changing field 'BlacklistedDomain.created_by'
db.alter_column(u'main_blacklisteddomain', 'created_by_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'], null=True))
# Changing field 'Domain.created_by'
db.alter_column(u'main_domain', 'created_by_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'], null=True))
models = {
u'auth.group': {
'Meta': {'object_name': 'Group'},
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
},
u'auth.permission': {
'Meta': {'ordering': "(u'content_type__app_label', u'content_type__model', u'codename')", 'unique_together': "((u'content_type', u'codename'),)", 'object_name': 'Permission'},
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
u'auth.user': {
'Meta': {'object_name': 'User'},
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
},
u'contenttypes.contenttype': {
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
},
u'main.blacklisteddomain': {
'Meta': {'object_name': 'BlacklistedDomain'},
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'created_by': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'blacklisted_domains'", 'to': u"orm['auth.User']"}),
'domain': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'last_update': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'})
},
u'main.domain': {
'Meta': {'object_name': 'Domain'},
'available': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'comment': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '255', 'null': 'True', 'blank': 'True'}),
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'created_by': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'domains'", 'to': u"orm['auth.User']"}),
'domain': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'last_update': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'nameserver_ip': ('django.db.models.fields.GenericIPAddressField', [], {'max_length': '39'}),
'nameserver_update_algorithm': ('django.db.models.fields.CharField', [], {'default': "'HMAC_SHA512'", 'max_length': '16'}),
'nameserver_update_secret': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '88'}),
'public': ('django.db.models.fields.BooleanField', [], {'default': 'False'})
},
u'main.host': {
'Meta': {'unique_together': "(('subdomain', 'domain'),)", 'object_name': 'Host'},
'comment': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '255', 'null': 'True', 'blank': 'True'}),
'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'created_by': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'hosts'", 'to': u"orm['auth.User']"}),
'domain': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['main.Domain']"}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'last_update': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
'last_update_ipv4': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
'last_update_ipv6': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
'ssl_update_ipv4': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'ssl_update_ipv6': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'subdomain': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
'update_secret': ('django.db.models.fields.CharField', [], {'max_length': '64'})
}
}
complete_apps = ['main']

View File

@ -4,7 +4,7 @@ import base64
import dns.resolver
from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.conf import settings
@ -23,7 +23,7 @@ class BlacklistedDomain(models.Model):
last_update = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User, blank=True, null=True)
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='blacklisted_domains')
def __unicode__(self):
return u"%s" % (self.domain, )
@ -86,7 +86,7 @@ class Domain(models.Model):
last_update = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User, blank=True, null=True, related_name='domains')
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='domains')
def __unicode__(self):
return u"%s" % (self.domain, )
@ -94,7 +94,8 @@ class Domain(models.Model):
def generate_ns_secret(self):
algorithm = self.nameserver_update_algorithm
bitlength = UPDATE_ALGORITHMS[algorithm].bitlength
secret = User.objects.make_random_password(length=bitlength / 8)
user_model = get_user_model()
secret = user_model.objects.make_random_password(length=bitlength / 8)
self.nameserver_update_secret = secret_base64 = base64.b64encode(secret)
self.save()
return secret_base64
@ -132,7 +133,7 @@ class Host(models.Model):
last_update = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User, blank=True, null=True, related_name='hosts')
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='hosts')
def __unicode__(self):
return u"%s.%s" % (
@ -182,7 +183,8 @@ class Host(models.Model):
# many update clients might use http without ssl, so it is not too
# secure anyway.
if secret is None:
secret = User.objects.make_random_password()
user_model = get_user_model()
secret = user_model.objects.make_random_password()
self.update_secret = make_password(
secret,
hasher='sha1'

View File

@ -7,7 +7,7 @@ from django.views.generic import View, TemplateView, CreateView
from django.views.generic.edit import UpdateView, DeleteView
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.contrib import messages
from django.utils.decorators import method_decorator
from django.core.urlresolvers import reverse
@ -121,16 +121,17 @@ class StatusView(TemplateView):
context['hosts_ipv6_2y'] = Host.objects.filter(last_update_ipv6__gt=before_2y).count()
context['hosts_ipv4_ssl_2y'] = Host.objects.filter(last_update_ipv4__gt=before_2y, ssl_update_ipv4=True).count()
context['hosts_ipv6_ssl_2y'] = Host.objects.filter(last_update_ipv6__gt=before_2y, ssl_update_ipv6=True).count()
context['users_total'] = User.objects.count()
context['users_active'] = User.objects.filter(is_active=True).count()
context['users_created_2d'] = User.objects.filter(date_joined__gt=before_2d).count()
context['users_loggedin_2d'] = User.objects.filter(last_login__gt=before_2d).count()
context['users_created_2w'] = User.objects.filter(date_joined__gt=before_2w).count()
context['users_loggedin_2w'] = User.objects.filter(last_login__gt=before_2w).count()
context['users_created_2m'] = User.objects.filter(date_joined__gt=before_2m).count()
context['users_loggedin_2m'] = User.objects.filter(last_login__gt=before_2m).count()
context['users_created_2y'] = User.objects.filter(date_joined__gt=before_2y).count()
context['users_loggedin_2y'] = User.objects.filter(last_login__gt=before_2y).count()
user_model = get_user_model()
context['users_total'] = user_model.objects.count()
context['users_active'] = user_model.objects.filter(is_active=True).count()
context['users_created_2d'] = user_model.objects.filter(date_joined__gt=before_2d).count()
context['users_loggedin_2d'] = user_model.objects.filter(last_login__gt=before_2d).count()
context['users_created_2w'] = user_model.objects.filter(date_joined__gt=before_2w).count()
context['users_loggedin_2w'] = user_model.objects.filter(last_login__gt=before_2w).count()
context['users_created_2m'] = user_model.objects.filter(date_joined__gt=before_2m).count()
context['users_loggedin_2m'] = user_model.objects.filter(last_login__gt=before_2m).count()
context['users_created_2y'] = user_model.objects.filter(date_joined__gt=before_2y).count()
context['users_loggedin_2y'] = user_model.objects.filter(last_login__gt=before_2y).count()
return context

View File

@ -3,7 +3,7 @@ reinitialize the test user account (and clean up)
"""
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.core.management.base import NoArgsCommand
@ -11,14 +11,15 @@ class Command(NoArgsCommand):
help = 'reinitialize the test user'
def handle_noargs(self, **options):
user_model = get_user_model()
try:
u = User.objects.get(username='test')
u = user_model.objects.get(username='test')
# delete test user and (via CASCADE behaviour) everything that
# points to it (has user as ForeignKey), e.g. via created_by.
u.delete()
except User.DoesNotExist:
except user_model.DoesNotExist:
pass
# create a fresh test user
u = User.objects.create_user('test', settings.DEFAULT_FROM_EMAIL, 'test')
u = user_model.objects.create_user('test', settings.DEFAULT_FROM_EMAIL, 'test')
u.save()
self.stdout.write('Successfully reinitialized the test user')