Exemplo n.º 1
0
    def exportCalendar(self, REQUEST=None):
        """ Export the contents of this Calendar as an iCalendar file """
        if not self.has_ical_support():
            return ''

        ct = getToolByName(self, 'portal_calendar')
        eventprovider = IEventProvider(self.context)
        variables = self.context.REQUEST.form
        events = [x.context.getObject() for x in eventprovider.getEvents(**variables)]
        self.request.RESPONSE.setHeader(
            'Content-Type', 'text/calendar;charset=utf-8')
        return ct.exportCalendar(events=events, REQUEST=REQUEST)
 def afterSetUp(self):
     self.eventSetUp()
     self.loginAsPortalOwner()
     try:
         # Plone < 4.2
         self.folder.invokeFactory("Topic", id="calendar-topic")
         self.topic = self.folder["calendar-topic"]
         criteria = self.topic.addCriterion("portal_type", "ATPortalTypeCriterion")
         criteria.value = (u"Event",)
     except ValueError:
         # Plone >= 4.2
         self.folder.invokeFactory("Collection", id="calendar-topic")
         self.topic = self.folder["calendar-topic"]
         query = [{"i": "Type", "o": "plone.app.querystring.operation.string.is", "v": "Event"}]
         self.topic.setQuery(query)
     self.provider = IEventProvider(self.topic)
class TopicEventProviderTest(ATEventProviderTest):
    def afterSetUp(self):
        self.eventSetUp()
        self.loginAsPortalOwner()
        try:
            # Plone < 4.2
            self.folder.invokeFactory("Topic", id="calendar-topic")
            self.topic = self.folder["calendar-topic"]
            criteria = self.topic.addCriterion("portal_type", "ATPortalTypeCriterion")
            criteria.value = (u"Event",)
        except ValueError:
            # Plone >= 4.2
            self.folder.invokeFactory("Collection", id="calendar-topic")
            self.topic = self.folder["calendar-topic"]
            query = [{"i": "Type", "o": "plone.app.querystring.operation.string.is", "v": "Event"}]
            self.topic.setQuery(query)
        self.provider = IEventProvider(self.topic)

    def test_createlink(self):
        cal = self.folder["calendar-topic"]
        creator = IWebEventCreator(cal)
        self.failIf(creator.canCreate())

    def test_gather_events(self):
        # Make sure that restrictions made by the context topic and
        # our own query don't clash, see
        # http://plone4artists.org/products/plone4artistscalendar/issues/35

        # First off, we're going to set a date restriction so that
        # only events in the future are shown:
        try:
            date_crit = self.topic.addCriterion("start", "ATFriendlyDateCriteria")
            date_crit.setValue(0)
            date_crit.setDateRange("+")
            date_crit.setOperation("more")

            # Adding a criteria other than time
            subject_crit = self.topic.addCriterion("Subject", "ATListCriterion")
            subject_crit.setValue(["foo", "bar"])
        except AttributeError:
            # Plone 4.2 or later
            q = self.topic.getField("query").getRaw(self.topic)
            q.append({"i": "start", "o": "plone.app.querystring.operation.date.afterToday", "v": None})
            # Adding a criteria other than time
            q.append({"i": "Subject", "o": "plone.app.querystring.operation.selection.is", "v": ["foo", "bar"]})
            self.topic.setQuery(q)

        calls = []

        class my_catalog:
            def __call__(self, **kwargs):
                calls.append(kwargs)
                return LazyCat([])

            searchResults = __call__

            def indexes(self):
                return ["Subject", "start", "end", "Type"]

        self.folder.portal_catalog = my_catalog()

        # Now let's make sure that a call to the catalog is done with
        # the correct set of arguments, and that the criteria defined
        # in the topic didn't interfere:
        start = datetime.now()
        stop = start + timedelta(seconds=3600)
        self.provider.getEvents(start=start, stop=stop)

        # XXX I don't like this test, it has too much internal knowledge.
        # Better to add more functional tests.
        self.assertEqual(len(calls), 2)
        # test to make sure that the recurrence search still has the extra
        # parameters in it
        self.failIf("Subject" not in calls[1].keys())

        # We don't mind the timezone at this point:
        self.assertEqual(calls[0]["end"]["query"].strftime("%Y%m%d %H:%M"), start.strftime("%Y%m%d %H:%M"))
        self.assertEqual(calls[0]["start"]["query"][1].strftime("%Y%m%d %H:%M"), stop.strftime("%Y%m%d %H:%M"))