# Path: labshare/models.py
# class Device(models.Model):
#     name = models.CharField(max_length=255)
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
# 
#     class Meta:
#         permissions = (
#             ('use_device', 'User/Group is allowed to use that device'),
#         )
# 
#     def __str__(self):
#         return self.name
# 
#     def can_be_used_by(self, user):
#         content_type = ContentType.objects.get_for_model(self)
#         permission_name = "{app}.use_{model}".format(app=content_type.app_label, model=content_type.model)
#         return user.has_perm(permission_name, self) or user.has_perm(permission_name)
# 
#     def serialize(self):
#         return {
#             'name': self.name,
#         }
# 
# class EmailAddress(models.Model):
#     user = models.ForeignKey(User, related_name="email_addresses", on_delete=models.CASCADE)
#     email = models.EmailField(max_length=255)
# 
#     def __str__(self):
#         return "{}: {}".format(self.user, self.email)


from django.contrib import admin
from django.contrib.admin.sites import NotRegistered
from django.contrib.auth import admin as upstream
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from guardian.admin import GuardedModelAdmin
from .models import Device, EmailAddress


class LabshareUserCreationForm(UserCreationForm):

    def __init__(self, *args, **kwargs):
        super(LabshareUserCreationForm, self).__init__(*args, **kwargs)

        self.fields['email'].required = True


class LabshareUserAdmin(upstream.UserAdmin):
    fieldsets = (
        (None, {'fields': ('username', 'password', 'email')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name')}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'password1', 'password2', 'email')}
        ),
    )
    form = UserChangeForm
    add_form = LabshareUserCreationForm

try:
    admin.site.unregister(User)
except NotRegistered:

# Path: labshare/models.py
# class Device(models.Model):
#     name = models.CharField(max_length=255)
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
# 
#     class Meta:
#         permissions = (
#             ('use_device', 'User/Group is allowed to use that device'),
#         )
# 
#     def __str__(self):
#         return self.name
# 
#     def can_be_used_by(self, user):
#         content_type = ContentType.objects.get_for_model(self)
#         permission_name = "{app}.use_{model}".format(app=content_type.app_label, model=content_type.model)
#         return user.has_perm(permission_name, self) or user.has_perm(permission_name)
# 
#     def serialize(self):
#         return {
#             'name': self.name,
#         }
# 
# class EmailAddress(models.Model):
#     user = models.ForeignKey(User, related_name="email_addresses", on_delete=models.CASCADE)
#     email = models.EmailField(max_length=255)
# 
#     def __str__(self):
#         return "{}: {}".format(self.user, self.email)


# Path: labshare/admin.py
from django.contrib import admin
from django.contrib.admin.sites import NotRegistered
from django.contrib.auth import admin as upstream
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from guardian.admin import GuardedModelAdmin
from .models import Device, EmailAddress



class LabshareUserCreationForm(UserCreationForm):

    def __init__(self, *args, **kwargs):
        super(LabshareUserCreationForm, self).__init__(*args, **kwargs)

        self.fields['email'].required = True


class LabshareUserAdmin(upstream.UserAdmin):
    fieldsets = (
        (None, {'fields': ('username', 'password', 'email')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name')}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'password1', 'password2', 'email')}
        ),
    )
    form = UserChangeForm
    add_form = LabshareUserCreationForm

try:
    admin.site.unregister(User)
except NotRegistered:

    pass