mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-13 22:01:06 +00:00
Clean org register code
This commit is contained in:
143
base/accounts.py
143
base/accounts.py
@@ -320,146 +320,3 @@ class RegistrationForm(forms.Form):
|
|||||||
raise forms.ValidationError("两次输入的密码不一致")
|
raise forms.ValidationError("两次输入的密码不一致")
|
||||||
return self.cleaned_data
|
return self.cleaned_data
|
||||||
|
|
||||||
class OrgRegistrationForm(RegistrationForm):
|
|
||||||
"""
|
|
||||||
Form for registering a business user account.
|
|
||||||
|
|
||||||
Validates that the requested email is not already in use, and
|
|
||||||
requires the password to be entered twice to catch typos.
|
|
||||||
"""
|
|
||||||
|
|
||||||
org_name = forms.CharField(max_length=256,
|
|
||||||
widget=forms.TextInput(),
|
|
||||||
label=_("Organization Name"))
|
|
||||||
url_prefix = forms.RegexField(label=_("Url Prefix"), max_length=20,
|
|
||||||
regex=r'^[a-z0-9]+$',
|
|
||||||
error_message="域名前缀只能包含字母或数字")
|
|
||||||
|
|
||||||
def clean_url_prefix(self):
|
|
||||||
url_prefix = self.cleaned_data['url_prefix']
|
|
||||||
org = ccnet_threaded_rpc.get_org_by_url_prefix(url_prefix)
|
|
||||||
if not org:
|
|
||||||
return url_prefix
|
|
||||||
else:
|
|
||||||
raise forms.ValidationError("该域名前缀已被注册")
|
|
||||||
|
|
||||||
class OrgRegistrationBackend(object):
|
|
||||||
def register(self, request, **kwargs):
|
|
||||||
"""
|
|
||||||
Given a username, email address and password, register a new
|
|
||||||
user account, which will initially be inactive.
|
|
||||||
|
|
||||||
Along with the new ``User`` object, a new
|
|
||||||
``registration.models.RegistrationProfile`` will be created,
|
|
||||||
tied to that ``User``, containing the activation key which
|
|
||||||
will be used for this account.
|
|
||||||
|
|
||||||
An email will be sent to the supplied email address; this
|
|
||||||
email should contain an activation link. The email will be
|
|
||||||
rendered using two templates. See the documentation for
|
|
||||||
``RegistrationProfile.send_activation_email()`` for
|
|
||||||
information about these templates and the contexts provided to
|
|
||||||
them.
|
|
||||||
|
|
||||||
After the ``User`` and ``RegistrationProfile`` are created and
|
|
||||||
the activation email is sent, the signal
|
|
||||||
``registration.signals.user_registered`` will be sent, with
|
|
||||||
the new ``User`` as the keyword argument ``user`` and the
|
|
||||||
class of this backend as the sender.
|
|
||||||
|
|
||||||
"""
|
|
||||||
email, password = kwargs['email'], kwargs['password1']
|
|
||||||
username = email
|
|
||||||
org_name, url_prefix = kwargs['org_name'], kwargs['url_prefix']
|
|
||||||
|
|
||||||
if Site._meta.installed:
|
|
||||||
site = Site.objects.get_current()
|
|
||||||
else:
|
|
||||||
site = RequestSite(request)
|
|
||||||
|
|
||||||
|
|
||||||
if settings.ACTIVATE_AFTER_REGISTRATION:
|
|
||||||
# since user will be activated after registration,
|
|
||||||
# so we will not use email sending, just create acitvated user
|
|
||||||
new_user = RegistrationProfile.objects.create_active_user(username, email,
|
|
||||||
password, site,
|
|
||||||
send_email=False)
|
|
||||||
# create orgnization account
|
|
||||||
try:
|
|
||||||
ccnet_threaded_rpc.create_org(org_name, url_prefix, username)
|
|
||||||
except SearpcError, e:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
# login the user
|
|
||||||
new_user.backend='auth.backends.ModelBackend'
|
|
||||||
login(request, new_user)
|
|
||||||
else:
|
|
||||||
# create inactive user, user can be activated by admin, or through activated email
|
|
||||||
new_user = RegistrationProfile.objects.create_inactive_user(username, email,
|
|
||||||
password, site,
|
|
||||||
send_email=settings.REGISTRATION_SEND_MAIL)
|
|
||||||
|
|
||||||
signals.user_registered.send(sender=self.__class__,
|
|
||||||
user=new_user,
|
|
||||||
request=request)
|
|
||||||
return new_user
|
|
||||||
|
|
||||||
def activate(self, request, activation_key):
|
|
||||||
"""
|
|
||||||
Given an an activation key, look up and activate the user
|
|
||||||
account corresponding to that key (if possible).
|
|
||||||
|
|
||||||
After successful activation, the signal
|
|
||||||
``registration.signals.user_activated`` will be sent, with the
|
|
||||||
newly activated ``User`` as the keyword argument ``user`` and
|
|
||||||
the class of this backend as the sender.
|
|
||||||
|
|
||||||
"""
|
|
||||||
activated = RegistrationProfile.objects.activate_user(activation_key)
|
|
||||||
if activated:
|
|
||||||
signals.user_activated.send(sender=self.__class__,
|
|
||||||
user=activated,
|
|
||||||
request=request)
|
|
||||||
# login the user
|
|
||||||
activated.backend='auth.backends.ModelBackend'
|
|
||||||
login(request, activated)
|
|
||||||
|
|
||||||
return activated
|
|
||||||
|
|
||||||
def registration_allowed(self, request):
|
|
||||||
"""
|
|
||||||
Indicate whether account registration is currently permitted,
|
|
||||||
based on the value of the setting ``REGISTRATION_OPEN``. This
|
|
||||||
is determined as follows:
|
|
||||||
|
|
||||||
* If ``REGISTRATION_OPEN`` is not specified in settings, or is
|
|
||||||
set to ``True``, registration is permitted.
|
|
||||||
|
|
||||||
* If ``REGISTRATION_OPEN`` is both specified and set to
|
|
||||||
``False``, registration is not permitted.
|
|
||||||
|
|
||||||
"""
|
|
||||||
return getattr(settings, 'REGISTRATION_OPEN', True)
|
|
||||||
|
|
||||||
def get_form_class(self, request):
|
|
||||||
"""
|
|
||||||
Return the default form class used for user registration.
|
|
||||||
|
|
||||||
"""
|
|
||||||
return RegistrationForm
|
|
||||||
|
|
||||||
def post_registration_redirect(self, request, user):
|
|
||||||
"""
|
|
||||||
Return the name of the URL to redirect to after successful
|
|
||||||
user registration.
|
|
||||||
|
|
||||||
"""
|
|
||||||
return ('registration_complete', (), {})
|
|
||||||
|
|
||||||
def post_activation_redirect(self, request, user):
|
|
||||||
"""
|
|
||||||
Return the name of the URL to redirect to after successful
|
|
||||||
account activation.
|
|
||||||
|
|
||||||
"""
|
|
||||||
return ('myhome', (), {})
|
|
||||||
|
@@ -6,20 +6,13 @@ from registration.views import activate
|
|||||||
from registration.views import register
|
from registration.views import register
|
||||||
|
|
||||||
from seahub.base.accounts import RegistrationForm
|
from seahub.base.accounts import RegistrationForm
|
||||||
from seahub.base.accounts import OrgRegistrationForm
|
|
||||||
|
|
||||||
reg_dict = { 'backend': 'seahub.base.accounts.RegistrationBackend',
|
reg_dict = { 'backend': 'seahub.base.accounts.RegistrationBackend',
|
||||||
'form_class': RegistrationForm,
|
'form_class': RegistrationForm,
|
||||||
}
|
}
|
||||||
|
|
||||||
org_reg_dict = { 'backend': 'seahub.base.accounts.OrgRegistrationBackend',
|
|
||||||
'form_class': OrgRegistrationForm,
|
|
||||||
'template_name': 'registration/org_registration_form.html',
|
|
||||||
}
|
|
||||||
|
|
||||||
if settings.ACTIVATE_AFTER_REGISTRATION == True:
|
if settings.ACTIVATE_AFTER_REGISTRATION == True:
|
||||||
reg_dict['success_url'] = settings.SITE_ROOT
|
reg_dict['success_url'] = settings.SITE_ROOT
|
||||||
org_reg_dict['success_url'] = settings.SITE_ROOT
|
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
url(r'^activate/complete/$',
|
url(r'^activate/complete/$',
|
||||||
@@ -47,11 +40,5 @@ urlpatterns = patterns('',
|
|||||||
direct_to_template,
|
direct_to_template,
|
||||||
{ 'template': 'registration/registration_closed.html' },
|
{ 'template': 'registration/registration_closed.html' },
|
||||||
name='registration_disallowed'),
|
name='registration_disallowed'),
|
||||||
|
|
||||||
# url(r'^business/register/$',
|
|
||||||
# register,
|
|
||||||
# org_reg_dict,
|
|
||||||
# name='registration_register'),
|
|
||||||
|
|
||||||
(r'', include('registration.auth_urls')),
|
(r'', include('registration.auth_urls')),
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user