# Path: backend/flindt/user/models.py
# class User(PermissionsMixin, AbstractBaseUser):
#     USERNAME_FIELD = 'email'
# 
#     email = models.EmailField(
#         unique=True
#     )
#     first_name = models.CharField(
#         max_length=255,
#         blank=True,
#     )
#     last_name = models.CharField(
#         max_length=255,
#         blank=True
#     )
#     prefix = models.CharField(
#         max_length=255,
#         blank=True,
#     )
#     # TODO: FEED-26: Use ExtraUserInfo for the glassfrog_id. That way we can
#     # cleanly extend the import to work for Nestr et al.
#     glassfrog_id = models.IntegerField(
#         blank=True,
#         null=True,
#     )
#     slack_user_name = models.CharField(
#         blank=True,
#         max_length=255,
#     )
#     extra_info = models.ManyToManyField(
#         'ExtraUserInfo',
#         blank=True,
#     )
#     is_active = models.BooleanField(default=True)
#     is_admin = models.BooleanField(default=False)
# 
#     objects = UserManager()
# 
#     def get_full_name(self):
#         return self.first_name
# 
#     def get_short_name(self):
#         return '{} {}'.format(self.first_name, self.last_name)
# 
#     def __str__(self):
#         return self.email
# 
#     def has_perm(self, perm, obj=None):
#         """
#         Does the user have a specific permission?
#         """
#         # TODO: FEED-27: Implement the correct permission system.
#         # Simplest possible answer for now: Yes, always
#         return True
# 
#     def has_module_perms(self, app_label):
#         """
#         Does the user have permissions to view the app `app_label`?
#         """
#         # Simplest possible answer: Yes, always
#         return True
# 
#     @property
#     def is_staff(self):
#         """
#         Is the user a member of staff?
#         """
#         # Simplest possible answer: All admins are staff
#         return self.is_admin


from django import forms
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import Group
from .models import User
                                      'glassfrog_id', 'slack_user_name')}),
        ('Permissions', {'fields': ('is_admin',)}),
    )
    # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
    # overrides get_fieldsets to use this attribute when creating a user.
    add_fieldsets = ((None, {
        'classes': ('wide',),
        'fields': ('email', 'password1', 'password2')
    }),
    )
    search_fields = ('email',)
    ordering = ('email',)
    filter_horizontal = ()

    def save_model(self, request, obj, form, change):
        """
        Override saving User model in Django Admin to use password encryption.
        """
        if change:
            # Updating existing user.
            orig_obj = User.objects.get(pk=obj.pk)
            if obj.password != orig_obj.password:
                obj.set_password(obj.password)
        else:
            # Creating new user.
            obj.set_password(obj.password)

        obj.save()

# Now register the new UserAdmin...

# Path: backend/flindt/user/models.py
# class User(PermissionsMixin, AbstractBaseUser):
#     USERNAME_FIELD = 'email'
# 
#     email = models.EmailField(
#         unique=True
#     )
#     first_name = models.CharField(
#         max_length=255,
#         blank=True,
#     )
#     last_name = models.CharField(
#         max_length=255,
#         blank=True
#     )
#     prefix = models.CharField(
#         max_length=255,
#         blank=True,
#     )
#     # TODO: FEED-26: Use ExtraUserInfo for the glassfrog_id. That way we can
#     # cleanly extend the import to work for Nestr et al.
#     glassfrog_id = models.IntegerField(
#         blank=True,
#         null=True,
#     )
#     slack_user_name = models.CharField(
#         blank=True,
#         max_length=255,
#     )
#     extra_info = models.ManyToManyField(
#         'ExtraUserInfo',
#         blank=True,
#     )
#     is_active = models.BooleanField(default=True)
#     is_admin = models.BooleanField(default=False)
# 
#     objects = UserManager()
# 
#     def get_full_name(self):
#         return self.first_name
# 
#     def get_short_name(self):
#         return '{} {}'.format(self.first_name, self.last_name)
# 
#     def __str__(self):
#         return self.email
# 
#     def has_perm(self, perm, obj=None):
#         """
#         Does the user have a specific permission?
#         """
#         # TODO: FEED-27: Implement the correct permission system.
#         # Simplest possible answer for now: Yes, always
#         return True
# 
#     def has_module_perms(self, app_label):
#         """
#         Does the user have permissions to view the app `app_label`?
#         """
#         # Simplest possible answer: Yes, always
#         return True
# 
#     @property
#     def is_staff(self):
#         """
#         Is the user a member of staff?
#         """
#         # Simplest possible answer: All admins are staff
#         return self.is_admin


# Path: backend/flindt/user/admin.py
from django import forms
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import Group
from .models import User

                                      'glassfrog_id', 'slack_user_name')}),
        ('Permissions', {'fields': ('is_admin',)}),
    )
    # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
    # overrides get_fieldsets to use this attribute when creating a user.
    add_fieldsets = ((None, {
        'classes': ('wide',),
        'fields': ('email', 'password1', 'password2')
    }),
    )
    search_fields = ('email',)
    ordering = ('email',)
    filter_horizontal = ()

    def save_model(self, request, obj, form, change):
        """
        Override saving User model in Django Admin to use password encryption.
        """
        if change:
            # Updating existing user.
            orig_obj = User.objects.get(pk=obj.pk)
            if obj.password != orig_obj.password:
                obj.set_password(obj.password)
        else:
            # Creating new user.
            obj.set_password(obj.password)

        obj.save()

# Now register the new UserAdmin...

admin.site.register(User, UserAdmin)