Merge pull request #7136 from chdecultot/calendar_corrections
fix: Google calendar connexion + data migration tool
This commit is contained in:
commit
d676783ce5
6 changed files with 150 additions and 57 deletions
|
|
@ -7,6 +7,7 @@ from googleapiclient.errors import HttpError
|
|||
import time
|
||||
from datetime import datetime
|
||||
from frappe.utils import add_days, add_years
|
||||
from frappe.desk.doctype.event.event import has_permission
|
||||
|
||||
class CalendarConnector(BaseConnection):
|
||||
def __init__(self, connector):
|
||||
|
|
@ -64,24 +65,21 @@ class CalendarConnector(BaseConnection):
|
|||
|
||||
def insert(self, doctype, doc):
|
||||
if doctype == 'Events':
|
||||
from frappe.desk.doctype.event.event import has_permission
|
||||
d = frappe.get_doc("Event", doc["name"])
|
||||
if has_permission(d, self.account.name):
|
||||
if doc["start_datetime"] >= datetime.now():
|
||||
try:
|
||||
doctype = "Event"
|
||||
e = self.insert_events(doctype, doc)
|
||||
return e
|
||||
except Exception:
|
||||
frappe.log_error(frappe.get_traceback(), "GCalendar Synchronization Error")
|
||||
try:
|
||||
doctype = "Event"
|
||||
e = self.insert_events(doctype, doc)
|
||||
return e
|
||||
except Exception:
|
||||
frappe.log_error(frappe.get_traceback(), "GCalendar Synchronization Error")
|
||||
|
||||
|
||||
def update(self, doctype, doc, migration_id):
|
||||
if doctype == 'Events':
|
||||
from frappe.desk.doctype.event.event import has_permission
|
||||
d = frappe.get_doc("Event", doc["name"])
|
||||
if has_permission(d, self.account.name):
|
||||
if doc["start_datetime"] >= datetime.now() and migration_id is not None:
|
||||
if migration_id is not None:
|
||||
try:
|
||||
doctype = "Event"
|
||||
return self.update_events(doctype, doc, migration_id)
|
||||
|
|
@ -217,23 +215,23 @@ class CalendarConnector(BaseConnection):
|
|||
|
||||
day = []
|
||||
if e.repeat_on == "Every Day":
|
||||
if e.monday is not None:
|
||||
if e.monday == 1:
|
||||
day.append("MO")
|
||||
if e.tuesday is not None:
|
||||
if e.tuesday == 1:
|
||||
day.append("TU")
|
||||
if e.wednesday is not None:
|
||||
if e.wednesday == 1:
|
||||
day.append("WE")
|
||||
if e.thursday is not None:
|
||||
if e.thursday == 1:
|
||||
day.append("TH")
|
||||
if e.friday is not None:
|
||||
if e.friday == 1:
|
||||
day.append("FR")
|
||||
if e.saturday is not None:
|
||||
if e.saturday == 1:
|
||||
day.append("SA")
|
||||
if e.sunday is not None:
|
||||
if e.sunday == 1:
|
||||
day.append("SU")
|
||||
|
||||
day = "BYDAY=" + ",".join(str(d) for d in day)
|
||||
frequency = "FREQ=DAILY"
|
||||
frequency = "FREQ=WEEKLY"
|
||||
|
||||
elif e.repeat_on == "Every Week":
|
||||
frequency = "FREQ=WEEKLY"
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ from __future__ import unicode_literals
|
|||
import frappe, json, math
|
||||
from frappe.model.document import Document
|
||||
from frappe import _
|
||||
from frappe.utils import get_source_value
|
||||
from frappe.utils import get_source_value, cstr
|
||||
|
||||
class DataMigrationRun(Document):
|
||||
def run(self):
|
||||
|
|
@ -213,19 +213,19 @@ class DataMigrationRun(Document):
|
|||
def get_deleted_local_data(self):
|
||||
'''Fetch local deleted data using `frappe.get_all`. Used during Push'''
|
||||
mapping = self.get_mapping(self.current_mapping)
|
||||
or_filters = self.get_or_filters(mapping)
|
||||
filters = dict(
|
||||
deleted_doctype=mapping.local_doctype
|
||||
)
|
||||
filters = self.get_last_modified_condition()
|
||||
filters.update({
|
||||
"deleted_doctype": mapping.local_doctype
|
||||
})
|
||||
|
||||
data = frappe.get_all('Deleted Document', fields=['data'],
|
||||
filters=filters, or_filters=or_filters)
|
||||
data = frappe.get_all('Deleted Document', fields=['name', 'data'],
|
||||
filters=filters)
|
||||
|
||||
_data = []
|
||||
for d in data:
|
||||
doc = json.loads(d.data)
|
||||
if doc.get(mapping.migration_id_field):
|
||||
doc['_deleted_document_name'] = d.name
|
||||
doc['_deleted_document_name'] = d["name"]
|
||||
_data.append(doc)
|
||||
|
||||
return _data
|
||||
|
|
@ -306,8 +306,8 @@ class DataMigrationRun(Document):
|
|||
self.update_log('push_insert', 1)
|
||||
# post process after insert
|
||||
self.post_process_doc(local_doc=d, remote_doc=response_doc)
|
||||
except Exception:
|
||||
self.update_log('push_failed', d.name)
|
||||
except Exception as e:
|
||||
self.update_log('push_failed', {d.name: cstr(e)})
|
||||
|
||||
# update page_start
|
||||
self.db_set('current_mapping_start',
|
||||
|
|
@ -338,8 +338,8 @@ class DataMigrationRun(Document):
|
|||
self.update_log('push_update', 1)
|
||||
# post process after update
|
||||
self.post_process_doc(local_doc=d, remote_doc=response_doc)
|
||||
except Exception:
|
||||
self.update_log('push_failed', d.name)
|
||||
except Exception as e:
|
||||
self.update_log('push_failed', {d.name: cstr(e)})
|
||||
|
||||
# update page_start
|
||||
self.db_set('current_mapping_start',
|
||||
|
|
@ -370,8 +370,8 @@ class DataMigrationRun(Document):
|
|||
self.update_log('push_delete', 1)
|
||||
# post process only when action is success
|
||||
self.post_process_doc(local_doc=d, remote_doc=response_doc)
|
||||
except Exception:
|
||||
self.update_log('push_failed', d.name)
|
||||
except Exception as e:
|
||||
self.update_log('push_failed', {d.name: cstr(e)})
|
||||
|
||||
# update page_start
|
||||
self.db_set('current_mapping_start',
|
||||
|
|
@ -414,7 +414,7 @@ class DataMigrationRun(Document):
|
|||
self.post_process_doc(remote_doc=d, local_doc=local_doc)
|
||||
except Exception:
|
||||
# failed, append to log
|
||||
self.update_log('pull_failed', migration_id_value)
|
||||
self.update_log('pull_failed', {migration_id_value: cstr(e)})
|
||||
|
||||
if len(data) < mapping.page_length:
|
||||
# last page, done with pull
|
||||
|
|
|
|||
|
|
@ -33,6 +33,51 @@
|
|||
"local_fieldname": "repeat_this_event",
|
||||
"remote_fieldname": "repeat_this_event"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "repeat_on",
|
||||
"remote_fieldname": "repeat_on"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "repeat_till",
|
||||
"remote_fieldname": "repeat_till"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "monday",
|
||||
"remote_fieldname": "monday"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "tuesday",
|
||||
"remote_fieldname": "tuesday"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "wednesday",
|
||||
"remote_fieldname": "wednesday"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "thursday",
|
||||
"remote_fieldname": "thursday"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "friday",
|
||||
"remote_fieldname": "friday"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "saturday",
|
||||
"remote_fieldname": "saturday"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "sunday",
|
||||
"remote_fieldname": "sunday"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "name",
|
||||
|
|
@ -45,8 +90,8 @@
|
|||
"mapping_name": "Event to GCalendar",
|
||||
"mapping_type": "Push",
|
||||
"migration_id_field": "gcalendar_sync_id",
|
||||
"modified": "2018-05-18 14:38:43.658069",
|
||||
"modified_by": "chdecultot@dokos.io",
|
||||
"modified": "2019-03-26 10:16:45.400150",
|
||||
"modified_by": "Administrator",
|
||||
"name": "Event to GCalendar",
|
||||
"owner": "Administrator",
|
||||
"page_length": 10,
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
import frappe
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
from dateutil.parser import parse
|
||||
from pytz import timezone
|
||||
from frappe.utils import add_days
|
||||
|
|
@ -30,7 +30,7 @@ def pre_process(events):
|
|||
event_tz = events["start"]["timeZone"]
|
||||
else:
|
||||
event_tz = events["calendar_tz"]
|
||||
start_dt= timezone(event_tz).localize(start_dt)
|
||||
start_dt = timezone(event_tz).localize(start_dt)
|
||||
|
||||
if end_dt.tzinfo is None or end_dt.tzinfo.utcoffset(end_dt) is None:
|
||||
if "timeZone" in events["end"]:
|
||||
|
|
@ -79,32 +79,37 @@ def get_recurrence_event_fields_value(recur_rule, starts_on):
|
|||
else: repeat_on = "Every Year"
|
||||
elif "UNTIL" in _str:
|
||||
# get repeat till
|
||||
date = datetime.strptime(_str.split("=")[1], "%Y%m%dT%H%M%SZ")
|
||||
date = parse(_str.split("=")[1])
|
||||
repeat_till = get_repeat_till_date(date)
|
||||
elif "COUNT" in _str:
|
||||
# get repeat till
|
||||
date = datetime.strptime(starts_on, "%Y-%m-%d %H:%M:%S")
|
||||
date = parse(starts_on)
|
||||
repeat_till = get_repeat_till_date(date, count=_str.split("=")[1], repeat_on=repeat_on)
|
||||
elif "BYDAY" in _str:
|
||||
days = _str.split("=")[1]
|
||||
if repeat_on == "DAILY":
|
||||
repeat_days.update({
|
||||
"sunday": 1 if "SU" in days else 0,
|
||||
"monday": 1 if "MO" in days else 0,
|
||||
"tuesday": 1 if "TU" in days else 0,
|
||||
"wednesday": 1 if "WD" in days else 0,
|
||||
"thursday": 1 if "TU" in days else 0,
|
||||
"friday": 1 if "TU" in days else 0,
|
||||
"saturday": 1 if "TU" in days else 0,
|
||||
})
|
||||
repeat_days.update({
|
||||
"sunday": 1 if "SU" in days else 0,
|
||||
"monday": 1 if "MO" in days else 0,
|
||||
"tuesday": 1 if "TU" in days else 0,
|
||||
"wednesday": 1 if "WD" in days else 0,
|
||||
"thursday": 1 if "TH" in days else 0,
|
||||
"friday": 1 if "FR" in days else 0,
|
||||
"saturday": 1 if "SA" in days else 0,
|
||||
})
|
||||
repeat_on = "Every Day"
|
||||
|
||||
return {
|
||||
recurrence = {
|
||||
"repeat_on": repeat_on,
|
||||
"repeat_till": repeat_till,
|
||||
"repeat_this_event": 1,
|
||||
"repeat_days": repeat_days
|
||||
"repeat_this_event": 1
|
||||
}
|
||||
|
||||
if repeat_days:
|
||||
recurrence.update(repeat_days)
|
||||
|
||||
return recurrence
|
||||
|
||||
|
||||
def get_repeat_till_date(date, count=None, repeat_on=None):
|
||||
if count:
|
||||
if repeat_on == "Every Day":
|
||||
|
|
|
|||
|
|
@ -33,6 +33,51 @@
|
|||
"local_fieldname": "repeat_this_event",
|
||||
"remote_fieldname": "repeat_this_event"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "repeat_on",
|
||||
"remote_fieldname": "repeat_on"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "repeat_till",
|
||||
"remote_fieldname": "repeat_till"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "monday",
|
||||
"remote_fieldname": "monday"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "tuesday",
|
||||
"remote_fieldname": "tuesday"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "wednesday",
|
||||
"remote_fieldname": "wednesday"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "thursday",
|
||||
"remote_fieldname": "thursday"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "friday",
|
||||
"remote_fieldname": "friday"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "saturday",
|
||||
"remote_fieldname": "saturday"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "sunday",
|
||||
"remote_fieldname": "sunday"
|
||||
},
|
||||
{
|
||||
"is_child_table": 0,
|
||||
"local_fieldname": "gcalendar_sync_id",
|
||||
|
|
@ -50,8 +95,8 @@
|
|||
"mapping_name": "GCalendar to Event",
|
||||
"mapping_type": "Pull",
|
||||
"migration_id_field": "gcalendar_sync_id",
|
||||
"modified": "2018-05-18 14:38:43.694867",
|
||||
"modified_by": "chdecultot@dokos.io",
|
||||
"modified": "2019-03-26 10:16:45.426138",
|
||||
"modified_by": "Administrator",
|
||||
"name": "GCalendar to Event",
|
||||
"owner": "Administrator",
|
||||
"page_length": 250,
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
"creation": "2018-03-23 19:10:23.715161",
|
||||
"docstatus": 0,
|
||||
"doctype": "Data Migration Plan",
|
||||
"idx": 4,
|
||||
"idx": 22,
|
||||
"mappings": [
|
||||
{
|
||||
"enabled": 1,
|
||||
|
|
@ -13,8 +13,8 @@
|
|||
"mapping": "GCalendar to Event"
|
||||
}
|
||||
],
|
||||
"modified": "2018-05-18 14:38:43.559026",
|
||||
"modified_by": "chdecultot@dokos.io",
|
||||
"modified": "2019-03-26 10:16:45.369037",
|
||||
"modified_by": "Administrator",
|
||||
"module": "Integrations",
|
||||
"name": "GCalendar Sync",
|
||||
"owner": "Administrator",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue