# Path: radioco/apps/users/models.py
# class UserProfile(models.Model):
#     user = models.OneToOneField(User, unique=True)
#     bio = RichTextUploadingField(blank=True, verbose_name=_("biography"))
#     avatar = models.ImageField(
#         upload_to='avatars/', default='defaults/default-userprofile-avatar.jpg', verbose_name=_("avatar")
#     )
#     display_personal_page = models.BooleanField(default=False, verbose_name=_("display personal page"))
#     slug = models.SlugField(max_length=30)
# 
#     def get_absolute_url(self):
#         return reverse('users:detail', args=[self.slug])
# 
#     def save(self, *args, **kwargs):
#         if not self.pk:
#             try:
#                 p = UserProfile.objects.get(user=self.user)
#                 self.pk = p.pk
#             except UserProfile.DoesNotExist:
#                 pass
#         self.slug = slugify(self.user.username)
#         super(UserProfile, self).save(*args, **kwargs)
# 
#     class Meta:
#         default_permissions = ('change',)
#         verbose_name = _('user profile')
#         verbose_name_plural = _('user profile')
# 
#     def __str__(self):
#         return "%s's profile" % self.user


import re
from django import forms
from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django.core import validators
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _
from radioco.apps.users.models import UserProfile
    from django.utils.encoding import force_unicode
    from django.utils.encoding import force_text as force_unicode

# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserProfileAdmin)


# PROFILE

class NonStaffUserProfileForm(forms.ModelForm):
    username = forms.CharField(
        label=_('username'), max_length=30,
        help_text=_('Required. 30 characters or fewer. Letters, numbers and '
                    '@/./+/-/_ characters'),
        validators=[
            validators.RegexValidator(re.compile('^[\w.@+-]+$'), _('Enter a valid username.'), 'invalid')
        ]
    )
    first_name = forms.CharField(label=_('first name'), max_length=30, required=False)
    last_name = forms.CharField(label=_('last name'), max_length=30, required=False)
    email = forms.EmailField(label=_('email address'), required=False)

    def __init__(self, *args, **kwargs):
        super(NonStaffUserProfileForm, self).__init__(*args, **kwargs)
        if 'instance' in kwargs:
            self.instance = kwargs['instance']
            self.fields['username'].initial = self.instance.user.username
            self.fields['first_name'].initial = self.instance.user.first_name
            self.fields['last_name'].initial = self.instance.user.last_name
            self.fields['email'].initial = self.instance.user.email


# Path: radioco/apps/users/models.py
# class UserProfile(models.Model):
#     user = models.OneToOneField(User, unique=True)
#     bio = RichTextUploadingField(blank=True, verbose_name=_("biography"))
#     avatar = models.ImageField(
#         upload_to='avatars/', default='defaults/default-userprofile-avatar.jpg', verbose_name=_("avatar")
#     )
#     display_personal_page = models.BooleanField(default=False, verbose_name=_("display personal page"))
#     slug = models.SlugField(max_length=30)
# 
#     def get_absolute_url(self):
#         return reverse('users:detail', args=[self.slug])
# 
#     def save(self, *args, **kwargs):
#         if not self.pk:
#             try:
#                 p = UserProfile.objects.get(user=self.user)
#                 self.pk = p.pk
#             except UserProfile.DoesNotExist:
#                 pass
#         self.slug = slugify(self.user.username)
#         super(UserProfile, self).save(*args, **kwargs)
# 
#     class Meta:
#         default_permissions = ('change',)
#         verbose_name = _('user profile')
#         verbose_name_plural = _('user profile')
# 
#     def __str__(self):
#         return "%s's profile" % self.user


# Path: radioco/apps/users/admin.py
import re
from django import forms
from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django.core import validators
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _
from radioco.apps.users.models import UserProfile
    from django.utils.encoding import force_unicode
    from django.utils.encoding import force_text as force_unicode


# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserProfileAdmin)


# PROFILE

class NonStaffUserProfileForm(forms.ModelForm):
    username = forms.CharField(
        label=_('username'), max_length=30,
        help_text=_('Required. 30 characters or fewer. Letters, numbers and '
                    '@/./+/-/_ characters'),
        validators=[
            validators.RegexValidator(re.compile('^[\w.@+-]+$'), _('Enter a valid username.'), 'invalid')
        ]
    )
    first_name = forms.CharField(label=_('first name'), max_length=30, required=False)
    last_name = forms.CharField(label=_('last name'), max_length=30, required=False)
    email = forms.EmailField(label=_('email address'), required=False)

    def __init__(self, *args, **kwargs):
        super(NonStaffUserProfileForm, self).__init__(*args, **kwargs)
        if 'instance' in kwargs:
            self.instance = kwargs['instance']
            self.fields['username'].initial = self.instance.user.username
            self.fields['first_name'].initial = self.instance.user.first_name
            self.fields['last_name'].initial = self.instance.user.last_name
            self.fields['email'].initial = self.instance.user.email


    def save(self, force_insert=False, force_update=False, commit=True):