Exemplo n.º 1
0
 def save(self):
     """ Find the user, create a ticket and send it by email """
     try:
         user = User.objects.get(email__iexact = self.clean_data['email'])
     except User.DoesNotExist:
         user = None
     # Send the email if there is any user
     if user is not None:
         # Create the ticket
         ticket = PasswordTicket.objects.create_ticket(user)
         # Select the templates corresponding to the number of tickets
         email_subject = 'accounts/register/reset_password/email_subject.txt'
         email_message = 'accounts/register/reset_password/email_message.txt'
         # The site
         current_site = Site.objects.get_current()
         # The subject
         subject = render_to_string(email_subject, {'site': current_site})
         # Email subject *must not* contain newlines
         subject = ''.join(subject.splitlines())
         # The message
         message = render_to_string(email_message,
                 {
                     'site': current_site,
                     'user': user,
                     'ticket': ticket,
                     'expiration_days': settings.PASSWORD_TICKET_MAX_AGE,
                     'expiration_date': datetime.now() + timedelta(days = settings.PASSWORD_TICKET_MAX_AGE),
                 }
             )
         # Send the email
         send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [self.clean_data['email'],])
Exemplo n.º 2
0
 def save(self):
     """ Create the tickets """
     from translator.apps.accounts.models import RegistrationTicket
     # Create the tickets
     ticket_list = RegistrationTicket.objects.create_tickets(count = self.clean_data['count'])
     # Send the email, if any address has been specified
     if self.clean_data['email'] != '':
         # Select the templates corresponding to the number of tickets
         if len(ticket_list) == 1:
             email_subject = 'accounts/tickets/one/email_subject.txt'
             email_message = 'accounts/tickets/one/email_message.txt'
         else:
             email_subject = 'accounts/tickets/many/email_subject.txt'
             email_message = 'accounts/tickets/many/email_message.txt'
         # The site
         current_site = Site.objects.get_current()
         # The subject
         subject = render_to_string(email_subject, {'site': current_site})
         # Email subject *must not* contain newlines
         subject = ''.join(subject.splitlines())
         # The message
         message = render_to_string(email_message,
                 {
                     'site': current_site,
                     'ticket_list': ticket_list,
                     'ticket': ticket_list[0].ticket,
                     'expiration_days': settings.TICKET_MAX_AGE,
                     'expiration_date': datetime.now() + timedelta(days = settings.TICKET_MAX_AGE),
                 }
             )
         # Send the email
         send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [self.clean_data['email'],])
Exemplo n.º 3
0
    def confirm_user(self, confirmation_key, send_email = True):
        """ Validate a confirmation key and confirm the corresponding 'User' if valid

        If the key is valid and has not expired, returns the 'User' after confirmation.
        If the key is not valid or has expired, returns 'False'.
        If the key is valid but the 'User' is already confimed, returns the 'User'.
        """
        # Make sure the key we're trying conforms to the pattern of a
        # SHA1 hash; if it doesn't, no point trying to look it up in
        # the database.
        if SHA1_RE.search(confirmation_key):
            try:
                profile = self.get(confirmation_key = confirmation_key, confirmed = False)
            except self.model.DoesNotExist:
                return False
            if not profile.confirmation_key_expired():
                # Mark the user as confirmed
                profile.confirmed = True
                profile.save()
                # Activate the user
                user = profile.user
                user.is_active = True
                user.save()
                # Send the email
                if send_email:
                    # The site
                    current_site = Site.objects.get_current()
                    # The subject
                    subject = render_to_string('accounts/register/confirm_email_subject.txt', { 'site': current_site })
                    # Email subject *must not* contain newlines
                    subject = ''.join(subject.splitlines())
                    # The message
                    message = render_to_string('accounts/register/confirm_email.txt',
                            {
                                'user': profile.user,
                                'site': current_site,
                                }
                            )
                    # Send the email
                    send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [profile.user.email])
                # Mail the managers
                mail_managers('New account confirmation (%s)' % profile.user.username,
                        'Username: %s\nFirst name: %s\nLast name: %s\nEmail: %s\n' % (
                            profile.user.username,
                            profile.user.first_name,
                            profile.user.last_name,
                            profile.user.email))
                return profile.user
        return False
Exemplo n.º 4
0
    def create_inactive_user(self, username, firstname, lastname, password, email, send_email = True, profile_callback = None):
        """ Create a new, inactive 'User', generate a 'RegistrationProfile' and email its confirmation key to him

        Return the new 'User'. To disable the email, call with 'send_email = False'.
        To enable creation of a custom user profile along with the 'User'
        (e.g., the model specified in the 'AUTH_PROFILE_MODULE' setting), define
        a function which knows how to create and save an instance of that model with
        appropriate default values, and pass it as the keyword argument 'profile_callback'.

        This function should accept one keyword argument:

        'user'
            The 'User' to relate the profile to
        """

        # Create the inactive user and save it
        new_user = User.objects.create_user(username, email, password)
        new_user.first_name = firstname
        new_user.last_name = lastname
        new_user.is_active = False
        new_user.save()
        # Create the registration profile
        registration_profile = self.create_profile(new_user)
        # Call the profile feedback
        if profile_callback is not None:
            profile_callback(user = new_user)
        # Send the email
        if send_email:
            # The site
            current_site = Site.objects.get_current()
            # The subject
            subject = render_to_string('accounts/register/register_email_subject.txt', { 'site': current_site })
            # Email subject *must not* contain newlines
            subject = ''.join(subject.splitlines())
            # The message
            message = render_to_string('accounts/register/register_email.txt',
                    {
                        'confirmation_key': registration_profile.confirmation_key,
                        'expiration_date': datetime.now() + timedelta(days = settings.ACCOUNT_CONFIRMATION_DAYS),
                        'expiration_days': settings.ACCOUNT_CONFIRMATION_DAYS,
                        'user': new_user,
                        'site': current_site,
                        }
                    )
            # Send the email
            send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [new_user.email])
        return new_user
Exemplo n.º 5
0
 def send_xmlrpc_key(self):
     """ Send the XML-RPC shared secret by email """
     # The site
     current_site = Site.objects.get_current()
     # The subject
     subject = render_to_string('accounts/xmlrpc/email_subject.txt', {'site': current_site})
     # Email subject *must not* contain newlines
     subject = ''.join(subject.splitlines())
     # The message
     message = render_to_string('accounts/xmlrpc/email_message.txt',
             {
                 'key': self.xmlrpc_key,
                 'user': self.user,
                 'site': current_site,
             }
     )
     # Send the email
     send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [self.user.email,])
Exemplo n.º 6
0
 def save(self):
     """ Create the email and send it """
     # Send the email if there is any user
     if self.user is not None:
         # The subject
         subject = ' '.join(self.clean_data['subject'].splitlines())
         # The message
         message = self.clean_data['message']
         # Send the email
         try:
             # Create the sener
             sender = '%s <%s>' % (self.user.get_full_name(), self.user.email)
             # Create the recipients list
             recipients = ['%s <%s>' % (item[0], item[1]) for item in settings.CONTACT]
             # Add the sender, if not already
             if not self.user.email in [item[1] for item in settings.CONTACT]:
                 recipients.append(sender)
             # Send the email
             send_mail(subject, message, sender, recipients)
         except:
             return False
         else:
             return True