Beispiel #1
0
 def get(self):
     contact_id = self.request.matchdict.get('contact_id')
     try:
         contact = CoreContact.get(self.user, contact_id)
     except NotFound:
         raise ResourceNotFound('No such contact')
     return {'contacts': ReturnContact.build(contact).serialize()}
Beispiel #2
0
 def get(self):
     contact_id = self.request.matchdict.get('contact_id')
     try:
         contact = CoreContact.get(self.user, contact_id)
     except NotFound:
         raise ResourceNotFound('No such contact')
     return {'contacts': ReturnContact.build(contact).serialize()}
Beispiel #3
0
 def collection_get(self):
     results = CoreContact.find_index(self.user, None,
                                      limit=self.get_limit(),
                                      offset=self.get_offset())
     data = [ReturnIndexShortContact.build(x).serialize()
             for x in results['data']]
     return {'contacts': data, 'total': results['total']}
Beispiel #4
0
 def _create(self, contact_id, params, add_func, return_obj):
     """Create sub object from param using add_func."""
     contact = CoreContact.get(self.user, contact_id)
     created = getattr(contact, add_func)(params)
     log.debug('Created object {} for contact {}'.
               format(created.address_id, contact.contact_id))
     return return_obj.build(created).serialize()
Beispiel #5
0
 def collection_get(self):
     results = CoreContact.find_index(self.user,
                                      None,
                                      limit=self.get_limit(),
                                      offset=self.get_offset())
     data = [
         ReturnIndexShortContact.build(x).serialize()
         for x in results['data']
     ]
     return {'contacts': data, 'total': results['total']}
Beispiel #6
0
 def collection_post(self):
     """Create a new contact from json post data structure."""
     data = self.request.json
     contact_param = NewContact(data)
     try:
         contact_param.validate()
     except Exception as exc:
         raise ValidationError(exc)
     contact = CoreContact.create(self.user, contact_param)
     out_contact = ReturnContact.build(contact).serialize()
     return Response(status=201, body={'contacts': out_contact})
Beispiel #7
0
 def collection_post(self):
     """Create a new contact from json post data structure."""
     data = self.request.json
     contact_param = NewContact(data)
     try:
         contact_param.validate()
     except Exception as exc:
         raise ValidationError(exc)
     contact = CoreContact.create(self.user, contact_param)
     out_contact = ReturnContact.build(contact).serialize()
     return Response(status=201, body={'contacts': out_contact})
Beispiel #8
0
 def expand_contacts(self, user, contacts):
     results = []
     for contact in contacts:
         results.append(Contact.get(user, contact['contact_id']))
     return results
Beispiel #9
0
 def __init__(self, request):
     self.request = request
     self.user = request.authenticated_userid
     contact_id = self.request.matchdict.get('contact_id')
     self.contact = CoreContact.get(self.user, contact_id)
Beispiel #10
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)
Beispiel #11
0
 def __init__(self, request):
     self.request = request
     self.user = request.authenticated_userid
     contact_id = self.request.matchdict.get('contact_id')
     self.contact = CoreContact.get(self.user, contact_id)
Beispiel #12
0
 def expand_contacts(self, user, contacts):
     results = []
     for contact in contacts:
         results.append(Contact.get(user, contact['contact_id']))
     return results
Beispiel #13
0
 def _delete(self, relation_id, delete_func):
     """Delete sub object relation_id using delete_fund."""
     contact_id = self.request.validated['contact_id']
     contact = CoreContact.get(self.user, contact_id)
     return getattr(contact, delete_func)(relation_id)