Преглед изворни кода

Make events plugin work with i18n_subsites

Previously the events plugin and i18n_subsites could not be used together. Two issues had to be fixed:
1. Due to rewriting the output-path to a not already existing folder of i18n_subsites the events plugin failed with an IOError during writing the .ics file. Now we check the existance of the path before writing.

2. For a multi-language site we do also want to have multi-language calendars and not one calendar with every language in it (and thus, multiple events at the same time with differently named events although they are the same). To fix this I added some checks and when i18n_subsite plugin usage is detected we filter out duplicates of the events and create the calendar file on a per language base and not for the overall folder.
Thomas Stieglmaier пре 8 година
родитељ
комит
666c1c0f45
1 измењених фајлова са 20 додато и 1 уклоњено
  1. 20 1
      events/events.py

+ 20 - 1
events/events.py

@@ -118,8 +118,24 @@ def generate_ical_file(generator):
     ical.add('prodid', '-//My calendar product//mxm.dk//')
     ical.add('version', '2.0')
 
-    for e in events:
+    multiLanguageSupportNecessary = "i18n_subsites" in generator.settings["PLUGINS"]
+    if multiLanguageSupportNecessary:
+        currentLang = os.path.basename(os.path.normpath(generator.settings['OUTPUT_PATH']))
+        sortedUniqueEvents = sorted(events)
+        last = sortedUniqueEvents[-1]
+        for i in range(len(sortedUniqueEvents)-2, -1,-1):
+            if last == sortedUniqueEvents[i]:
+                del sortedUniqueEvents[i]
+            else:
+                last = sortedUniqueEvents[i]
+    else:
+        sortedUniqueEvents = events
+
+    for e in sortedUniqueEvents:
         dtstart, dtend, metadata = e
+        if multiLanguageSupportNecessary and currentLang != metadata['lang']:
+            log.debug("%s is not equal to %s" %(currentLang, metadata['lang']))
+            continue
 
         ie = icalendar.Event(
             summary=metadata['summary'],
@@ -134,6 +150,9 @@ def generate_ical_file(generator):
 
         ical.add_component(ie)
 
+    if not os.path.exists(generator.settings['OUTPUT_PATH']):
+        os.makedirs(generator.settings['OUTPUT_PATH'])
+
     with open(ics_fname, 'wb') as f:
         f.write(ical.to_ical())