Пример #1
0
def create_user(**kwargs):

    from caliopen.base.user.core import User
    from caliopen.base.user.parameters import NewUser
    from caliopen.base.user.parameters import NewContact
    param = NewUser()
    param.name = kwargs['email']
    param.password = kwargs['password']
    contact = NewContact()
    contact.given_name = kwargs.get('given_name')
    contact.family_name = kwargs.get('family_name')
    param.contact = contact
    user = User.create(param)
    user.save()
    log.info('User %s (%s) created' % (user.user_id, user.user_name))
Пример #2
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)
Пример #3
0
def import_email(email, import_path, format, **kwargs):

    from caliopen.base.user.core import User
    from caliopen.base.user.core import Contact, ContactLookup
    from caliopen.base.message.format.mail import MailMessage
    from caliopen.base.user.parameters import NewContact, NewEmail
    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)

    user = User.by_name(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:
                try:
                    ContactLookup.get(user, alias)
                except NotFound:
                    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 = NewContact()
                    contact.family_name = name
                    email = NewEmail()
                    email.address = alias
                    Contact.create(user, contact, emails=[email])
        res = agent.process(mailfrom, rcpts, msg.mail.as_string())
        log.info("Process result %r" % res)