Exemplo n.º 1
0
    def __call__(self, action, portlethash):
        """Main method that accepts action to perform.

        Args:
          @action - one of allowed actions, like emails, create, send, etc...
          @portlethash - portlet hash so we get portlet assignement data.

        This method accepts only post requests.
        It returns json.
        It authenticates before any further actions.
        """
        # check if method is POST
        request = self.request
        if request.method != 'POST':
            return self._error(_(u"Request method is not allowed."))

        # check if action is valid
        if action not in ('emails', 'email'):
            return self._error(_(u"Requested action is not allowed."))

        mails = []
        if action == 'emails':
            mails = self.get_emails(
                request.get('folder') or None,
                int(request.get('offset') or '0'),
                int(request.get('limit') or '100'),
                request.get('recip') or '1',
                request.get('sortBy') or 'dateDesc'
            )

        elif action == 'email':
            mails = self.get_email(request.get('eid'))

        return simplejson.dumps(mails)
Exemplo n.º 2
0
    def check_credentials(self):
        "Verifies the current user zimbra credetials. error='' if Ok."
        username, password = self._get_credentials()

        lost_keys = []
        if not username:
            lost_keys.append(_(u'zimbra_username'))
        if not password:
            lost_keys.append(_(u'zimbra_password'))

        error = ''
        if lost_keys:
            error = _(u"Your zimbra account isn't configured. You need to set "
                      u"the following fields: ")
            error = error + ', '.join(lost_keys)
            return error

        authenticated = self.authenticate()
        if not authenticated:
            error = _(u"There was a network error or the"
                      u" credentials for your zimbra account are incorrect.")
            return error

        return error
Exemplo n.º 3
0
    def getPageInfo(self):
        "Gets zimbra calendar html or an error message"
        if not self.base_rel_url:
            return _('Missing Calendar URL'), ''

        error = self.check_credentials()
        if error:
            return error, ''

        zimbraUtil = getUtility(IZimbraUtil)
        username, password = zimbraUtil._get_credentials()
        response = requests.get(self.base_rel_url,
                                auth=HTTPBasicAuth(username, password))

        if response.status_code != 200:
            return 'Network error %s' % response.status_code, ''

        html = self._fix_calendar_html(response.text)
        return None, html
Exemplo n.º 4
0
    def get_email(self, eid):
        """Returns conversation emails by given id.

        It also marks conversation as read.
        """
        if not eid:
            return {'error': _(u"Conversation id is not valid.")}

        conversation = self.client.get_conversation(eid)
        thread = []
        for message in conversation.messages:
            from_ = _name(message, EmailAddress.FROM)

            if len(from_):
                from_ = 'from: ' + from_[0]
            else:
                from_ = ''

            to = u', '.join(_mail(message, EmailAddress.TO))

            soup = BeautifulSoup(findMsgBody(message.raw))
            [elem.decompose() for elem in soup.findAll(['script', 'style'])]

            for tag in soup():
                for attribute in ['style']:
                    del tag[attribute]
                if tag.name in ['html', 'head', 'body']:
                    tag.append(' ')
                    tag.replaceWithChildren()

            thread.append({
                'from': '<div class="item-from">' + from_ + '</div>',
                'to': to,
                'body': ''.join(unicode(soup)),
                'id': message.original_id,
                'date': message.date,
            })

        return {'conversation': ''.join([t['from'] + '<div class="item-thread">' + t['body'] + '</div>'
            for t in thread])}
Exemplo n.º 5
0
 def validateBothDates(data):
     if not data.startDate and not data.endDate:
         return
     if not data.startDate or not data.endDate:
         raise BothDatesError(_(u"You must set both start and end "
                                u"date or none."))