Пример #1
0
def import_email(email, import_path, format, **kwargs):

    from caliopen.core.user import User
    from caliopen.core.contact import Contact, ContactLookup
    from caliopen.core.mail import MailMessage
    from caliopen.smtp.agent import DeliveryAgent

    AVATAR_DIR = '../../../caliopen.ng/src/assets/images/avatars'

    if format == 'maildir':
        emails = Maildir(import_path, factory=message_from_file)
        mode = 'maildir'
    else:
        if os.path.isdir(import_path):
            mode = 'mbox_directory'
            emails = {}
            files = [
                f for f in listdir(import_path)
                if os.path.isfile(os.path.join(import_path, f))
            ]
            for f in files:
                with open('%s/%s' % (import_path, f)) as fh:
                    emails[f] = message_from_file(fh)
        else:
            mode = 'mbox'
            emails = mbox(import_path)

    print email
    user = User.get(email)

    agent = DeliveryAgent()
    mailfrom = ''
    rcpts = [email]

    log.info("Processing mode %s" % mode)
    msgs = []
    for key, mail in emails.iteritems():
        # Create contact for user
        log.info('Processing mail %s' % key)
        msgs.append(MailMessage(mail))

    msgs = sorted(msgs, key=lambda msg: msg.date)

    for msg in msgs:
        for type, addresses in msg.recipients.iteritems():
            if not addresses:
                continue
            for alias, _address in addresses:
                lookup = ContactLookup.get(user, alias)
                if not lookup:
                    log.info('Creating contact %s' % alias)
                    infos = {'mail': alias}
                    name, domain = alias.split('@')

                    if os.path.isfile('%s/%s.png' % (AVATAR_DIR, name)):
                        infos.update({'avatar': '%s.png' % name})
                    Contact.create(user, infos)
        res = agent.process(mailfrom, rcpts, msg.mail.as_string())
        log.info('Process result %r' % res)
Пример #2
0
def import_email(email, import_path, format, **kwargs):

    from caliopen.core.user import User
    from caliopen.core.contact import Contact, ContactLookup
    from caliopen.core.mail import MailMessage
    from caliopen.smtp.agent import DeliveryAgent

    AVATAR_DIR = '../../../caliopen.ng/src/assets/images/avatars'

    if format == 'maildir':
        emails = Maildir(import_path, factory=message_from_file)
        mode = 'maildir'
    else:
        if os.path.isdir(import_path):
            mode = 'mbox_directory'
            emails = {}
            files = [f for f in listdir(import_path) if
                     os.path.isfile(os.path.join(import_path, f))]
            for f in files:
                with open('%s/%s' % (import_path, f)) as fh:
                    emails[f] = message_from_file(fh)
        else:
            mode = 'mbox'
            emails = mbox(import_path)

    print email
    user = User.get(email)

    agent = DeliveryAgent()
    mailfrom = ''
    rcpts = [email]

    log.info("Processing mode %s" % mode)
    msgs = []
    for key, mail in emails.iteritems():
        # Create contact for user
        log.info('Processing mail %s' % key)
        msgs.append(MailMessage(mail))

    msgs = sorted(msgs, key=lambda msg: msg.date)

    for msg in msgs:
        for type, addresses in msg.recipients.iteritems():
            if not addresses:
                continue
            for alias, _address in addresses:
                lookup = ContactLookup.get(user, alias)
                if not lookup:
                    log.info('Creating contact %s' % alias)
                    infos = {'mail': alias}
                    name, domain = alias.split('@')

                    if os.path.isfile('%s/%s.png' % (AVATAR_DIR, name)):
                        infos.update({'avatar': '%s.png' % name})
                    Contact.create(user, infos)
        res = agent.process(mailfrom, rcpts, msg.mail.as_string())
        log.info('Process result %r' % res)
Пример #3
0
def create_user(**kwargs):

    from caliopen.core.user import User
    email = kwargs['email']
    password = kwargs['password']
    first_name = kwargs.get('first_name')
    last_name = kwargs.get('last_name')
    user = User.create(user_id=email, password=password,
                       first_name=first_name, last_name=last_name)
    user.save()
    log.info('User %s created' % user.user_id)
Пример #4
0
def _validate_authentication_credentials(params):
    """
    Check the user credentials and returns the associated user object.

    @param dict params  the sanitized parameters for authentication
    @return user
    """
    try:
        user = User.authenticate(params['username'], params['password'])
        user = ReturnUser.build(user).serialize()

    except ValidationError, e:
        raise ValidationError(e.messages)
Пример #5
0
def validate_signup_params(username, email, password, password_repeat, given_name='', family_name=''):
    """Retrieve matching username and password.
    If no match is found, then return errors.

    @param string username
    @param string password
    @return Authentication
    """
    creation = namedtuple('Creation', ['success', 'user', 'errors'])
    errors = {
            'globals': [],
            'given_name': [],
            'family_name': [],
            'username': [],
            'password': [],
            'password_repeat': []
        }

    # validate user input
    has_error = False;
    if not password == password_repeat:
        errors['password_repeat'].append(messages['password_missmatch'])
        has_error = True

    # if an error occurred, then return the failed attempt
    if has_error:
        return creation(success=False, errors=errors, user=None)

    # create the user from parameters
    try:
        param = NewUser()
        param.name = email
        param.password = password
        contact = NewContact()
        contact.given_name = given_name
        contact.family_name = family_name
        param.contact = contact
        user = User.create(param)
        user.save()
        user = user.to_api()
        return  creation(success=True, user=user, errors=None)

    except (KeyError, Exception), exc:
        # prepare errors
        errors['globals'].append(str(exc))
        return creation(success=False, errors=errors, user=None)