def send_notification_email_reciver(sender, user, profile, request, **kwargs): """send a notification email to admins/managers""" if not is_notification_enable(): return context = { 'user': user, 'profile': profile, 'site': get_site(request), } subject = render_to_string( settings.REGISTRATION_NOTIFICATION_EMAIL_SUBJECT_TEMPLATE_NAME, context) subject = "".join(subject.splitlines()) message = render_to_string( settings.REGISTRATION_NOTIFICATION_EMAIL_TEMPLATE_NAME, context) recipients = [] if settings.REGISTRATION_NOTIFICATION_ADMINS: for userinfo in settings.ADMINS: recipients.append(userinfo[1]) if settings.REGISTRATION_NOTIFICATION_MANAGERS: for userinfo in settings.MANAGERS: recipients.append(userinfo[1]) if settings.REGISTRATION_NOTIFICATION_RECIPIENTS: method_or_iterable = settings.REGISTRATION_NOTIFICATION_RECIPIENTS if callable(method_or_iterable): recipients.extend(method_or_iterable()) elif isinstance(method_or_iterable, (list, tuple)): recipients.extend(method_or_iterable) else: raise ImproperlyConfigured(( '``REGISTRATION_NOTIFICATION_RECIPIENTS`` must ' 'be a list of recipients or function which return ' 'a list of recipients (Currently the value was "%s")' ) % method_or_iterable) # remove duplications recipients = frozenset(recipients) mail_from = getattr(settings, 'REGISTRATION_FROM_EMAIL', '') or \ settings.DEFAULT_FROM_EMAIL send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipients)
def _send_email(self, site, action, extra_context=None): context = { 'user': self.user, 'site': site, } if action != 'activation': # the profile was deleted in 'activation' action context['profile'] = self if extra_context: context.update(extra_context) subject = render_to_string( 'registration/%s_email_subject.txt' % action, context) subject = ''.join(subject.splitlines()) message = render_to_string('registration/%s_email.txt' % action, context) send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [self.user.email])
def _send_email(self, site, action, extra_context=None): context = { 'user': self.user, 'site': site, } if action != 'activation': # the profile was deleted in 'activation' action context['profile'] = self if extra_context: context.update(extra_context) subject = render_to_string( 'registration/%s_email_subject.txt' % action, context) subject = ''.join(subject.splitlines()) message = render_to_string( 'registration/%s_email.txt' % action, context) send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [self.user.email])
def create_inactive_user(self, request, username, person_firstname, password, email, Identity, send_email=True, profile_callback=None, **kwargs): """ Create a new, inactive ``User``, generates a ``RegistrationProfile`` and email its activation key to the ``User``, returning the new ``User``. TODO: we will custom the USER """ #如果存在用户的话不必进行新建只需对权限表进行操作即可,否则新建用户 send_mail_flag = True if User.objects.filter(email=email).count() == 0: new_user = User.objects.create_user(username, email, password) #new_user.is_active = False #new_user.is_active = (not send_email) #special treat for expert_import new_user.is_active = True new_user.first_name = person_firstname new_user.save() registration_profile = self.create_profile(new_user) registration_profile.save() current_site = Site.objects.get_current() site_domain = current_site.domain if send_email: # from django.core.mail import send_mail subject = render_to_string( 'registration/activation_email_subject.txt', { 'site': get_current_site(request), 'username': username, 'password': password }) # Email subject *must not* contain newlines subject = ''.join(subject.splitlines()) message = render_to_string( 'registration/activation_email.txt', { 'activation_key': registration_profile.activation_key, 'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS, 'site': site_domain, 'username': username, 'password': password }) logger.info(message) #此处加监控标志 send_mail_flag = send_mail(subject, message, [new_user.email]) else: new_user = User.objects.get(email=email) #对用户权限写入数据库 try: new_authority = UserIdentity.objects.get(identity=Identity) new_authority.auth_groups.add(new_user) new_authority.save() except: pass #如果是学校注册 添加学校注册姓名 if kwargs.has_key('school_name'): schoolObj = SchoolDict.objects.get(id=kwargs["school_name"]) if SchoolProfile.objects.filter(school=schoolObj).count() == 0: schoolProfileObj = SchoolProfile(school=schoolObj, userid=new_user) schoolProfileObj.save() projectperlimits = ProjectPerLimits(school=schoolProfileObj, number=0, a_cate_number=0) projectperlimits.save() else: schoolProfileObj = SchoolProfile.objects.get(school=schoolObj) schoolProfileObj.userid = new_user schoolProfileObj.save() #如果是专家的话加上专家的所属学科 elif kwargs.has_key('expert_insitute'): insituteObj = InsituteCategory.objects.get( id=kwargs["expert_insitute"]) expertProfileObj = ExpertProfile(subject=insituteObj, userid=new_user) expertProfileObj.save() #学生注册的话直接填写校对应的管理员即可 else: school_staff_name = request.user.username school_staff = User.objects.get(username=school_staff_name) school_profile = SchoolProfile.objects.get(userid=school_staff) student_obj = StudentProfile(user=new_user, school=school_profile) student_obj.save() if profile_callback is not None: profile_callback(user=new_user) return new_user, send_mail_flag
def create_inactive_user(self,request, username,person_firstname,password,email, Identity,send_email=True, profile_callback=None, **kwargs): """ Create a new, inactive ``User``, generates a ``RegistrationProfile`` and email its activation key to the ``User``, returning the new ``User``. TODO: we will custom the USER """ #如果存在用户的话不必进行新建只需对权限表进行操作即可,否则新建用户 send_mail_flag = True if User.objects.filter(email=email).count() == 0: new_user = User.objects.create_user(username, email, password) #new_user.is_active = False #new_user.is_active = (not send_email) #special treat for expert_import new_user.is_active = True new_user.first_name = person_firstname new_user.save() registration_profile = self.create_profile(new_user) registration_profile.save() current_site = Site.objects.get_current() site_domain=current_site.domain if send_email: # from django.core.mail import send_mail subject = render_to_string('registration/activation_email_subject.txt', {'site':get_current_site(request), 'username':username, 'password':password}) # Email subject *must not* contain newlines subject = ''.join(subject.splitlines()) message = render_to_string('registration/activation_email.txt', {'activation_key':registration_profile.activation_key, 'expiration_days':settings.ACCOUNT_ACTIVATION_DAYS, 'site':site_domain, 'username':username, 'password':password} ) logger.info(message) #此处加监控标志 send_mail_flag = send_mail(subject, message, [new_user.email]) else: new_user = User.objects.get(email=email) #对用户权限写入数据库 try: new_authority = UserIdentity.objects.get(identity=Identity) new_authority.auth_groups.add(new_user) new_authority.save() except: pass #如果是学校注册 添加学校注册姓名 if kwargs.has_key('school_name'): schoolObj = SchoolDict.objects.get(id = kwargs["school_name"]) if SchoolProfile.objects.filter(school=schoolObj).count() == 0: schoolProfileObj = SchoolProfile(school=schoolObj, userid =new_user) schoolProfileObj.save() projectperlimits = ProjectPerLimits(school=schoolProfileObj, number=0, a_cate_number=0) projectperlimits.save() else: schoolProfileObj = SchoolProfile.objects.get(school=schoolObj) schoolProfileObj.userid = new_user schoolProfileObj.save() #如果是专家的话加上专家的所属学科 elif kwargs.has_key('expert_insitute'): insituteObj = InsituteCategory.objects.get(id=kwargs["expert_insitute"]) expertProfileObj = ExpertProfile(subject=insituteObj, userid =new_user) expertProfileObj.save() #学生注册的话直接填写校对应的管理员即可 else: school_staff_name = request.user.username school_staff = User.objects.get(username=school_staff_name) school_profile = SchoolProfile.objects.get(userid = school_staff) student_obj = StudentProfile(user = new_user,school = school_profile) student_obj.save() if profile_callback is not None: profile_callback(user=new_user) return new_user,send_mail_flag