1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
|
from datetime import date
from datetime import datetime
from datetime import timedelta
from os import linesep
from django.test import TestCase
from django.test.client import RequestFactory
from dateutil import tz
import icalendar
from django_ical import utils
from django_ical.feedgenerator import ICal20Feed
from django_ical.views import ICalFeed
class TestICalFeed(ICalFeed):
feed_type = ICal20Feed
title = "Test Feed"
description = "Test ICal Feed"
items = []
class TestItemsFeed(ICalFeed):
feed_type = ICal20Feed
title = "Test Feed"
description = "Test ICal Feed"
def items(self):
return [
{
"component_type": "event",
"title": "Title1",
"description": "Description1",
"link": "/event/1",
"start": datetime(2012, 5, 1, 18, 0),
"end": datetime(2012, 5, 1, 20, 0),
"recurrences": {
"rrules": [
utils.build_rrule(freq="DAILY", byhour=10),
utils.build_rrule(freq="MONTHLY", bymonthday=4),
],
"xrules": [
utils.build_rrule(freq="MONTHLY", bymonthday=-4),
utils.build_rrule(freq="MONTHLY", byday="+3TU"),
],
"rdates": [date(1999, 9, 2), date(1998, 1, 1)],
"xdates": [date(1999, 8, 1), date(1998, 2, 1)],
},
"geolocation": (37.386013, -122.082932),
"organizer": "john.doe@example.com",
"participants": [
{
"email": "joe.unresponsive@example.com",
"cn": "Joe Unresponsive",
"partstat": "NEEDS-ACTION",
},
{
"email": "jane.attender@example.com",
"cn": "Jane Attender",
"partstat": "ACCEPTED",
},
{
"email": "dan.decliner@example.com",
"cn": "Dan Decliner",
"partstat": "DECLINED",
},
{
"email": "mary.maybe@example.com",
"cn": "Mary Maybe",
"partstat": "TENTATIVE",
},
],
"modified": datetime(2012, 5, 2, 10, 0),
},
{
"component_type": "event",
"title": "Title2",
"description": "Description2",
"link": "/event/2",
"start": datetime(2012, 5, 6, 18, 0),
"end": datetime(2012, 5, 6, 20, 0),
"recurrences": {
"rrules": [
utils.build_rrule(
freq="WEEKLY", byday=["MO", "TU", "WE", "TH", "FR"]
)
],
"xrules": [utils.build_rrule(freq="MONTHLY", byday="-3TU")],
"rdates": [date(1997, 9, 2)],
"xdates": [date(1997, 8, 1)],
},
"geolocation": (37.386013, -122.082932),
"modified": datetime(2012, 5, 7, 10, 0),
"organizer": {
"cn": "John Doe",
"email": "john.doe@example.com",
"role": "CHAIR",
},
"categories": ['Cat1', 'Cat2'],
"alarms": [
{
"trigger": timedelta(minutes=-30),
"action": "DISPLAY",
"description": "Alarm2a",
},
{
"trigger": timedelta(days=-1),
"action": "DISPLAY",
"description": "Alarm2b",
},
],
},
{
"component_type": "todo",
"title": "Submit Revised Internet-Draft",
"description": "an important test",
"link": "/event/3",
"start": datetime(2007, 5, 14, 0),
"due": datetime(2007, 5, 16, 0),
"completed": datetime(2007, 3, 20),
"priority": 1,
"status": "NEEDS-ACTION",
"organizer": {
"cn": "Bossy Martin",
"email": "bossy.martin@example.com",
"role": "CHAIR"
},
"modified": datetime(2012, 5, 2, 10, 0),
"geolocation": (37.386013, 2.238985),
"categories": ['CLEANING'],
"percent_complete": 89,
},
]
def item_component_type(self, obj):
return obj.get("component_type", None)
def item_title(self, obj):
return obj["title"]
def item_description(self, obj):
return obj["description"]
def item_start_datetime(self, obj):
return obj["start"]
def item_end_datetime(self, obj):
return obj.get("end", None)
def item_due_datetime(self, obj):
return obj.get("due", None)
def item_rrule(self, obj):
return obj.get("recurrences", {}).get("rrules", None)
def item_exrule(self, obj):
return obj.get("recurrences", {}).get("xrules", None)
def item_rdate(self, obj):
return obj.get("recurrences", {}).get("rdates", None)
def item_exdate(self, obj):
return obj.get("recurrences", {}).get("xdates", None)
def item_link(self, obj):
return obj["link"]
def item_geolocation(self, obj):
return obj.get("geolocation", None)
def item_updateddate(self, obj):
return obj.get("modified", None)
def item_pubdate(self, obj):
return obj.get("modified", None)
def item_completed(self, obj):
return obj.get("completed", None)
def item_percent_complete(self, obj):
return obj.get("percent_complete", None)
def item_priority(self, obj):
return obj.get("priority", None)
def item_due(self, obj):
return obj.get("due", None)
def item_categories(self, obj):
return obj.get("categories") or []
def item_organizer(self, obj):
organizer_dic = obj.get("organizer", None)
if organizer_dic:
if isinstance(organizer_dic, dict):
organizer = icalendar.vCalAddress("MAILTO:%s" % organizer_dic["email"])
for key, val in organizer_dic.items():
if key != "email":
organizer.params[key] = icalendar.vText(val)
else:
organizer = icalendar.vCalAddress("MAILTO:%s" % organizer_dic)
return organizer
def item_attendee(self, obj):
"""All calendars support ATTENDEE attribute, however, at this time, Apple calendar (desktop & iOS) and Outlook
display event attendees, while Google does not. For SUBSCRIBED calendars it seems that it is not possible to
use the default method to respond. As an alternative, you may review adding custom links to your description
or setting up something like CalDav with authentication, which can enable the ability for attendees to respond
via the default icalendar protocol."""
participants = obj.get("participants", None)
if participants:
attendee_list = list()
default_attendee_params = {
"cutype": icalendar.vText("INDIVIDUAL"),
"role": icalendar.vText("REQ-PARTICIPANT"),
"rsvp": icalendar.vText(
"TRUE"
), # Does not seem to work for subscribed calendars.
}
for participant in participants:
attendee = icalendar.vCalAddress("MAILTO:%s" % participant.pop("email"))
participant_dic = default_attendee_params.copy()
participant_dic.update(participant)
for key, val in participant_dic.items():
attendee.params[key] = icalendar.vText(val)
attendee_list.append(attendee)
return attendee_list
def item_valarm(self, obj):
alarms = obj.get("alarms", None)
if alarms:
alarm_list = list()
for alarm in alarms:
valarm = icalendar.Alarm()
for key, value in alarm.items():
valarm.add(key, value)
alarm_list.append(valarm)
return alarm_list
class TestFilenameFeed(ICalFeed):
feed_type = ICal20Feed
title = "Test Filename Feed"
description = "Test ICal Feed"
def get_object(self, request):
return {"id": 123}
def items(self, obj):
return [obj]
def file_name(self, obj):
return "%s.ics" % obj["id"]
def item_link(self, item):
return "" # Required by the syndication framework
class ICal20FeedTest(TestCase):
def test_basic(self):
request = RequestFactory().get("/test/ical")
view = TestICalFeed()
response = view(request)
calendar = icalendar.Calendar.from_ical(response.content)
self.assertEqual(calendar["X-WR-CALNAME"], "Test Feed")
self.assertEqual(calendar["X-WR-CALDESC"], "Test ICal Feed")
def test_items(self):
request = RequestFactory().get("/test/ical")
view = TestItemsFeed()
response = view(request)
calendar = icalendar.Calendar.from_ical(response.content)
self.assertEqual(len(calendar.subcomponents), 3)
self.assertEqual(calendar.subcomponents[0]["SUMMARY"], "Title1")
self.assertEqual(calendar.subcomponents[0]["DESCRIPTION"], "Description1")
self.assertTrue(calendar.subcomponents[0]["URL"].endswith("/event/1"))
self.assertEqual(
calendar.subcomponents[0]["DTSTART"].to_ical(), b"20120501T180000"
)
self.assertEqual(
calendar.subcomponents[0]["DTEND"].to_ical(), b"20120501T200000"
)
self.assertEqual(
calendar.subcomponents[0]["GEO"].to_ical(), "37.386013;-122.082932"
)
self.assertEqual(
calendar.subcomponents[0]["LAST-MODIFIED"].to_ical(), b"20120502T100000Z"
)
self.assertEqual(
calendar.subcomponents[0]["ORGANIZER"].to_ical(),
b"MAILTO:john.doe@example.com",
)
self.assertEqual(
calendar.subcomponents[0]["ATTENDEE"][0].to_ical(),
b"MAILTO:joe.unresponsive@example.com",
)
self.assertEqual(
calendar.subcomponents[0]["ATTENDEE"][0].params.to_ical(),
b'CN="Joe Unresponsive";CUTYPE=INDIVIDUAL;PARTSTAT=NEEDS-ACTION;ROLE=REQ-PARTICIPANT;'
b"RSVP=TRUE",
)
self.assertEqual(
calendar.subcomponents[0]["ATTENDEE"][1].to_ical(),
b"MAILTO:jane.attender@example.com",
)
self.assertEqual(
calendar.subcomponents[0]["ATTENDEE"][1].params.to_ical(),
b'CN="Jane Attender";CUTYPE=INDIVIDUAL;PARTSTAT=ACCEPTED;ROLE=REQ-PARTICIPANT;RSVP=TRUE',
)
self.assertEqual(
calendar.subcomponents[0]["ATTENDEE"][2].to_ical(),
b"MAILTO:dan.decliner@example.com",
)
self.assertEqual(
calendar.subcomponents[0]["ATTENDEE"][2].params.to_ical(),
b'CN="Dan Decliner";CUTYPE=INDIVIDUAL;PARTSTAT=DECLINED;ROLE=REQ-PARTICIPANT;RSVP=TRUE',
)
self.assertEqual(
calendar.subcomponents[0]["ATTENDEE"][3].to_ical(),
b"MAILTO:mary.maybe@example.com",
)
self.assertEqual(
calendar.subcomponents[0]["ATTENDEE"][3].params.to_ical(),
b'CN="Mary Maybe";CUTYPE=INDIVIDUAL;PARTSTAT=TENTATIVE;ROLE=REQ-PARTICIPANT;RSVP=TRUE',
)
self.assertEqual(
calendar.subcomponents[0]["RRULE"][0].to_ical(), b"FREQ=DAILY;BYHOUR=10"
)
self.assertEqual(
calendar.subcomponents[0]["RRULE"][1].to_ical(),
b"FREQ=MONTHLY;BYMONTHDAY=4",
)
self.assertEqual(
calendar.subcomponents[0]["EXRULE"][0].to_ical(),
b"FREQ=MONTHLY;BYMONTHDAY=-4",
)
self.assertEqual(
calendar.subcomponents[0]["EXRULE"][1].to_ical(), b"FREQ=MONTHLY;BYDAY=+3TU"
)
self.assertEqual(
calendar.subcomponents[0]["RDATE"].to_ical(), b"19990902,19980101"
)
self.assertEqual(
calendar.subcomponents[0]["EXDATE"].to_ical(), b"19990801,19980201"
)
self.assertEqual(calendar.subcomponents[1]["SUMMARY"], "Title2")
self.assertEqual(calendar.subcomponents[1]["DESCRIPTION"], "Description2")
self.assertTrue(calendar.subcomponents[1]["URL"].endswith("/event/2"))
self.assertEqual(
calendar.subcomponents[1]["DTSTART"].to_ical(), b"20120506T180000"
)
self.assertEqual(
calendar.subcomponents[1]["DTEND"].to_ical(), b"20120506T200000"
)
self.assertEqual(
calendar.subcomponents[1]["RRULE"].to_ical(),
b"FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR",
)
self.assertEqual(
calendar.subcomponents[1]["EXRULE"].to_ical(), b"FREQ=MONTHLY;BYDAY=-3TU"
)
self.assertEqual(calendar.subcomponents[1]["RDATE"].to_ical(), b"19970902")
self.assertEqual(calendar.subcomponents[1]["EXDATE"].to_ical(), b"19970801")
self.assertEqual(
calendar.subcomponents[1]["GEO"].to_ical(), "37.386013;-122.082932"
)
self.assertEqual(
calendar.subcomponents[1]["LAST-MODIFIED"].to_ical(), b"20120507T100000Z"
)
self.assertEqual(
calendar.subcomponents[1]["ORGANIZER"].to_ical(),
b"MAILTO:john.doe@example.com",
)
self.assertEqual(
calendar.subcomponents[1]["CATEGORIES"].to_ical(), b"Cat1,Cat2"
)
self.assertIn(
b"BEGIN:VALARM\r\nACTION:DISPLAY\r\nDESCRIPTION:Alarm2a\r\nTRIGGER:-PT30M\r\nEND:VALARM\r\n",
[comp.to_ical() for comp in calendar.subcomponents[1].subcomponents],
)
self.assertIn(
b"BEGIN:VALARM\r\nACTION:DISPLAY\r\nDESCRIPTION:Alarm2b\r\nTRIGGER:-P1D\r\nEND:VALARM\r\n",
[comp.to_ical() for comp in calendar.subcomponents[1].subcomponents],
)
self.assertEqual(calendar.subcomponents[2]["SUMMARY"], "Submit Revised Internet-Draft")
self.assertTrue(calendar.subcomponents[2]["URL"].endswith("/event/3"))
self.assertEqual(
calendar.subcomponents[2]["DTSTART"].to_ical(), b"20070514T000000"
)
self.assertEqual(
calendar.subcomponents[2]["DUE"].to_ical(), b"20070516T000000"
)
self.assertEqual(
calendar.subcomponents[2]["GEO"].to_ical(), "37.386013;2.238985"
)
self.assertEqual(
calendar.subcomponents[2]["LAST-MODIFIED"].to_ical(), b"20120502T100000Z"
)
self.assertEqual(
calendar.subcomponents[2]["ORGANIZER"].to_ical(),
b"MAILTO:bossy.martin@example.com",
)
self.assertEqual(
calendar.subcomponents[2]["PRIORITY"].to_ical(), b"1"
)
self.assertEqual(
calendar.subcomponents[2]["CATEGORIES"].to_ical(), b"CLEANING"
)
self.assertEqual(
calendar.subcomponents[2]["PERCENT-COMPLETE"].to_ical(), b"89"
)
def test_wr_timezone(self):
"""
Test for the x-wr-timezone property.
"""
class TestTimezoneFeed(TestICalFeed):
timezone = "Asia/Tokyo"
request = RequestFactory().get("/test/ical")
view = TestTimezoneFeed()
response = view(request)
calendar = icalendar.Calendar.from_ical(response.content)
self.assertEqual(calendar["X-WR-TIMEZONE"], "Asia/Tokyo")
def test_timezone(self):
tokyo = tz.gettz("Asia/Tokyo") # or JST or Japan Standard Time
us_eastern = tz.gettz("US/Eastern") # or EDT or Eastern (Daylight) Time
class TestTimezoneFeed(TestItemsFeed):
def items(self):
return [
{
"title": "Title1",
"description": "Description1",
"link": "/event/1",
"start": datetime(2012, 5, 1, 18, 00, tzinfo=tokyo),
"end": datetime(2012, 5, 1, 20, 00, tzinfo=tokyo),
"recurrences": {
"rrules": [],
"xrules": [],
"rdates": [],
"xdates": [],
},
},
{
"title": "Title2",
"description": "Description2",
"link": "/event/2",
"start": datetime(2012, 5, 6, 18, 00, tzinfo=us_eastern),
"end": datetime(2012, 5, 6, 20, 00, tzinfo=us_eastern),
"recurrences": {
"rrules": [],
"xrules": [],
"rdates": [],
"xdates": [],
},
},
]
request = RequestFactory().get("/test/ical")
view = TestTimezoneFeed()
response = view(request)
calendar = icalendar.Calendar.from_ical(response.content)
self.assertEqual(len(calendar.subcomponents), 2)
self.assertEqual(
calendar.subcomponents[0]["DTSTART"].to_ical(), b"20120501T180000"
)
self.assertEqual(calendar.subcomponents[0]["DTSTART"].params["TZID"], "JST")
self.assertEqual(
calendar.subcomponents[0]["DTEND"].to_ical(), b"20120501T200000"
)
self.assertEqual(calendar.subcomponents[0]["DTEND"].params["TZID"], "JST")
self.assertEqual(
calendar.subcomponents[1]["DTSTART"].to_ical(), b"20120506T180000"
)
self.assertEqual(calendar.subcomponents[1]["DTSTART"].params["TZID"], "EDT")
self.assertEqual(
calendar.subcomponents[1]["DTEND"].to_ical(), b"20120506T200000"
)
self.assertEqual(calendar.subcomponents[1]["DTEND"].params["TZID"], "EDT")
def test_file_name(self):
request = RequestFactory().get("/test/ical")
view = TestFilenameFeed()
response = view(request)
self.assertIn("Content-Disposition", response)
self.assertEqual(
response["content-disposition"], 'attachment; filename="123.ics"'
)
def test_file_type(self):
request = RequestFactory().get("/test/ical")
view = TestFilenameFeed()
response = view(request)
self.assertIn("Content-Type", response)
self.assertEqual(response["content-type"], "text/calendar; charset=utf-8")
def test_file_header(self):
request = RequestFactory().get("/test/ical")
view = TestFilenameFeed()
response = view(request)
header = b"BEGIN:VCALENDAR\r\nVERSION:2.0"
self.assertTrue(response.content.startswith(header))
|