Пример #1
0
 def test_super_methods(self):
     tz = EWSTimeZone('Europe/Copenhagen')
     self.assertIsInstance(EWSDateTime.now(), EWSDateTime)
     self.assertIsInstance(EWSDateTime.now(tz=tz), EWSDateTime)
     self.assertIsInstance(EWSDateTime.utcnow(), EWSDateTime)
     self.assertIsInstance(EWSDateTime.fromtimestamp(123456789),
                           EWSDateTime)
     self.assertIsInstance(EWSDateTime.fromtimestamp(123456789, tz=tz),
                           EWSDateTime)
     self.assertIsInstance(EWSDateTime.utcfromtimestamp(123456789),
                           EWSDateTime)
    def create_meeting(self, username, start_time, end_time, subject, body, required_attendees, optional_attendees):
        """Create a meeting object"""
        account = self.connect_to_account(username, impersonation=(username != self.email))

        if required_attendees:
            required_attendees = [ra.strip() for ra in required_attendees.split(',')]
        if optional_attendees:
            optional_attendees = [oa.strip() for oa in optional_attendees.split(',')]

        tz = EWSTimeZone.timezone('Etc/GMT')

        meeting = CalendarItem(
            account=account,
            folder=account.calendar,
            start=EWSDateTime.fromtimestamp(start_time/1000, tz=tz),
            end=EWSDateTime.fromtimestamp(end_time/1000, tz=tz),
            subject=subject,
            body=body,
            required_attendees=required_attendees,
            optional_attendees=optional_attendees
        )
        return meeting
    def get_emails(self, username, folder_path=None, email_ids = None, sender=None, subject=None, body=None,
                   start_date=None, end_date=None, has_attachments=None, order_by_recency=None, num_emails=None,
                   search_subfolders=False):
        """Get queried emails"""

        # Default folder path if no folder path is specified
        folder_path = self.default_folder_path if folder_path is None else folder_path

        # Get list of all specified folders, preserving commas wrapped in quotes
        split_folder_paths = re.findall('(?:[^,"]|"(?:\\.|[^"])*")+', folder_path)
        folders = [self.go_to_folder(username, folder.strip()) for folder in split_folder_paths]

        # Subfolder query check
        if search_subfolders:
            subfolders = []
            # For each specified folder
            for folder in folders:
                if folder.child_folder_count > 0:
                    subfolders += list(folder.walk().get_folders())
            folders += subfolders

        folder_collection = FolderCollection(account=self.connect_to_account(username), folders=folders)
        filtered_emails = folder_collection.all()

        # filter by ids
        if email_ids:
            id_query = Q()
            for email_id in email_ids.split(','):
                id_query = id_query | Q(message_id=email_id.strip())
            filtered_emails = filtered_emails.filter(id_query)

        # filter by sender
        if sender:
            filtered_emails = filtered_emails.filter(sender=sender)

        if subject:
            filtered_emails = filtered_emails.filter(subject__contains=subject)

        if body:
            filtered_emails = filtered_emails.filter(body__contains=body)

        # filter by date
        if start_date:
            # get YYYY/MM/DD from epoch time in milliseconds
            tz = EWSTimeZone.timezone('Etc/GMT')
            start_date = EWSDateTime.fromtimestamp(start_date/1000, tz=tz)
            filtered_emails = filtered_emails.filter(datetime_received__gte=start_date)
        if end_date:
            # get YYYY/MM/DD from epoch time in milliseconds
            tz = EWSTimeZone.timezone('Etc/GMT')
            end_date = EWSDateTime.fromtimestamp(end_date/1000, tz=tz)
            filtered_emails = filtered_emails.filter(datetime_received__lte=end_date)

        # Check attachments
        if has_attachments is not None:
            filtered_emails = filtered_emails.filter(has_attachments=has_attachments)

        # Order by date
        if order_by_recency is not None:
            if order_by_recency:
                filtered_emails = filtered_emails.order_by('-datetime_received')
            else:
                filtered_emails = filtered_emails.order_by('datetime_received')

        # Only get num_emails
        if num_emails:
            filtered_emails = filtered_emails[:num_emails]

        return filtered_emails