Guest User

Untitled

a guest
Jul 19th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. cat /usr/lib/python2.6/site-packages/registration/backends/default/__init__.py
  2. from django.conf import settings
  3. from django.contrib.sites.models import RequestSite
  4. from django.contrib.sites.models import Site
  5.  
  6. from registration import signals
  7. from registration.forms import RegistrationForm
  8. from registration.models import RegistrationProfile
  9.  
  10.  
  11. class DefaultBackend(object):
  12. """
  13. A registration backend which follows a simple workflow:
  14.  
  15. 1. User signs up, inactive account is created.
  16.  
  17. 2. Email is sent to user with activation link.
  18.  
  19. 3. User clicks activation link, account is now active.
  20.  
  21. Using this backend requires that
  22.  
  23. * ``registration`` be listed in the ``INSTALLED_APPS`` setting
  24. (since this backend makes use of models defined in this
  25. application).
  26.  
  27. * The setting ``ACCOUNT_ACTIVATION_DAYS`` be supplied, specifying
  28. (as an integer) the number of days from registration during
  29. which a user may activate their account (after that period
  30. expires, activation will be disallowed).
  31.  
  32. * The creation of the templates
  33. ``registration/activation_email_subject.txt`` and
  34. ``registration/activation_email.txt``, which will be used for
  35. the activation email. See the notes for this backends
  36. ``register`` method for details regarding these templates.
  37.  
  38. Additionally, registration can be temporarily closed by adding the
  39. setting ``REGISTRATION_OPEN`` and setting it to
  40. ``False``. Omitting this setting, or setting it to ``True``, will
  41. be interpreted as meaning that registration is currently open and
  42. permitted.
  43.  
  44. Internally, this is accomplished via storing an activation key in
  45. an instance of ``registration.models.RegistrationProfile``. See
  46. that model and its custom manager for full documentation of its
  47. fields and supported operations.
  48.  
  49. """
  50. def register(self, request, **kwargs):
  51. """
  52. Given a username, email address and password, register a new
  53. user account, which will initially be inactive.
  54.  
  55. Along with the new ``User`` object, a new
  56. ``registration.models.RegistrationProfile`` will be created,
  57. tied to that ``User``, containing the activation key which
  58. will be used for this account.
  59.  
  60. An email will be sent to the supplied email address; this
  61. email should contain an activation link. The email will be
  62. rendered using two templates. See the documentation for
  63. ``RegistrationProfile.send_activation_email()`` for
  64. information about these templates and the contexts provided to
  65. them.
  66.  
  67. After the ``User`` and ``RegistrationProfile`` are created and
  68. the activation email is sent, the signal
  69. ``registration.signals.user_registered`` will be sent, with
  70. the new ``User`` as the keyword argument ``user`` and the
  71. class of this backend as the sender.
  72.  
  73. """
  74. username, email, password = kwargs['username'], kwargs['email'], kwargs['password1']
  75. if Site._meta.installed:
  76. site = Site.objects.get_current()
  77. else:
  78. site = RequestSite(request)
  79. new_user = RegistrationProfile.objects.create_inactive_user(username, email,
  80. password, site)
  81. signals.user_registered.send(sender=self.__class__,
  82. user=new_user,
  83. request=request)
  84. return new_user
  85.  
  86. def activate(self, request, activation_key):
  87. """
  88. Given an an activation key, look up and activate the user
  89. account corresponding to that key (if possible).
  90.  
  91. After successful activation, the signal
  92. ``registration.signals.user_activated`` will be sent, with the
  93. newly activated ``User`` as the keyword argument ``user`` and
  94. the class of this backend as the sender.
  95.  
  96. """
  97. activated = RegistrationProfile.objects.activate_user(activation_key)
  98. if activated:
  99. signals.user_activated.send(sender=self.__class__,
  100. user=activated,
  101. request=request)
  102. return activated
  103.  
  104. def registration_allowed(self, request):
  105. """
  106. Indicate whether account registration is currently permitted,
  107. based on the value of the setting ``REGISTRATION_OPEN``. This
  108. is determined as follows:
  109.  
  110. * If ``REGISTRATION_OPEN`` is not specified in settings, or is
  111. set to ``True``, registration is permitted.
  112.  
  113. * If ``REGISTRATION_OPEN`` is both specified and set to
  114. ``False``, registration is not permitted.
  115.  
  116. """
  117. return getattr(settings, 'REGISTRATION_OPEN', True)
  118.  
  119. def get_form_class(self, request):
  120. """
  121. Return the default form class used for user registration.
  122.  
  123. """
  124. return RegistrationForm
  125.  
  126. def post_registration_redirect(self, request, user):
  127. """
  128. Return the name of the URL to redirect to after successful
  129. user registration.
  130.  
  131. """
  132. return ('registration_complete', (), {})
  133.  
  134. def post_activation_redirect(self, request, user):
  135. """
  136. Return the name of the URL to redirect to after successful
  137. account activation.
  138.  
  139. """
  140. return ('registration_activation_complete', (), {})
Add Comment
Please, Sign In to add comment