managers.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """
  2. This module provides class definition to manage Employee objects.
  3. Available classes:
  4. - EmployeeQuerySet: define a collection of Employee objects.
  5. - BaseEmployeeManager: interface through which database query operations are
  6. provided to model.
  7. - PassThroughEmployeeManager: allow to call methods defined in EmployeeQuerySet
  8. from Manager.
  9. """
  10. from django.db.models import Manager
  11. from django.contrib.auth.models import BaseUserManager
  12. from django.db.models.query import QuerySet
  13. from django.utils.timezone import now
  14. class UserManager(BaseUserManager):
  15. """
  16. Interface through which database query operations are provided to
  17. Employee model.
  18. """
  19. def _create_user(self, email, password, is_staff, is_superuser,
  20. **extra_fields):
  21. """
  22. Creates and saves an active User with the given email and password.
  23. """
  24. if not email:
  25. raise ValueError('Users must have an email address')
  26. email = self.normalize_email(email)
  27. user = self.model(email=email, age=0,
  28. is_staff=is_staff, is_active=True,
  29. is_superuser=is_superuser, **extra_fields)
  30. user.set_password(password)
  31. user.save(using=self._db)
  32. return user
  33. def create_user(self, email, password=None, **extra_fields):
  34. return self._create_user(email, password, False, False,
  35. **extra_fields)
  36. def create_superuser(self, email, password, **extra_fields):
  37. return self._create_user(email, password, True, True,
  38. **extra_fields)