Bladeren bron

fix: Handle duration fieldtype for Data Import

version-14
Rucha Mahabal 4 jaren geleden
bovenliggende
commit
c0b4532ea5
4 gewijzigde bestanden met toevoegingen van 44 en 3 verwijderingen
  1. +5
    -0
      frappe/core/doctype/data_import/exporter.py
  2. +3
    -1
      frappe/core/doctype/data_import/importer.py
  3. +3
    -2
      frappe/core/doctype/data_import_legacy/importer.py
  4. +33
    -0
      frappe/utils/data.py

+ 5
- 0
frappe/core/doctype/data_import/exporter.py Bestand weergeven

@@ -8,6 +8,7 @@ from frappe.model import (
no_value_fields,
table_fields as table_fieldtypes,
)
from frappe.utils import flt, format_duration
from frappe.utils.csvutils import build_csv_response
from frappe.utils.xlsxutils import build_xlsx_response

@@ -148,6 +149,10 @@ class Exporter:
continue
row[i] = doc.get(df.fieldname, "")

if df.fieldtype == "Duration":
value = flt(doc.get(df.fieldname, 0))
row[i] = format_duration(value, df.hide_days)

return rows

def get_data_as_docs(self):


+ 3
- 1
frappe/core/doctype/data_import/importer.py Bestand weergeven

@@ -9,7 +9,7 @@ import timeit
import json
from datetime import datetime, date
from frappe import _
from frappe.utils import cint, flt, update_progress_bar, cstr
from frappe.utils import cint, flt, update_progress_bar, cstr, duration_to_seconds
from frappe.utils.csvutils import read_csv_content, get_csv_content_from_google_sheets
from frappe.utils.xlsxutils import (
read_xlsx_file_from_attached_file,
@@ -692,6 +692,8 @@ class Row:
value = flt(value)
elif df.fieldtype in ["Date", "Datetime"]:
value = self.get_date(value, col)
elif df.fieldtype == "Duration":
value = duration_to_seconds(value)

return value



+ 3
- 2
frappe/core/doctype/data_import_legacy/importer.py Bestand weergeven

@@ -15,7 +15,7 @@ from frappe import _
from frappe.utils.csvutils import getlink
from frappe.utils.dateutils import parse_date

from frappe.utils import cint, cstr, flt, getdate, get_datetime, get_url, get_absolute_url
from frappe.utils import cint, cstr, flt, getdate, get_datetime, get_url, get_absolute_url, duration_to_seconds
from six import string_types


@@ -164,7 +164,8 @@ def upload(rows = None, submit_after_import=None, ignore_encoding_errors=False,
d[fieldname] = get_datetime(_date + " " + _time)
else:
d[fieldname] = None

elif df.fieldtype == "Duration":
d[fieldname] = duration_to_seconds(cstr(d[fieldname]))
elif fieldtype in ("Image", "Attach Image", "Attach"):
# added file to attachments list
attachments.append(d[fieldname])


+ 33
- 0
frappe/utils/data.py Bestand weergeven

@@ -346,6 +346,11 @@ def format_datetime(datetime_string, format_string=None):
return formatted_datetime

def format_duration(seconds, hide_days=False):
"""Converts the given duration value in float(seconds) to duration format

example: converts 12885 to '3h 34m 45s' where 12885 = seconds in float
"""

total_duration = {
'days': math.floor(seconds / (3600 * 24)),
'hours': math.floor(seconds % (3600 * 24) / 3600),
@@ -373,6 +378,34 @@ def format_duration(seconds, hide_days=False):

return duration

def duration_to_seconds(duration):
"""Converts the given duration formatted value to duration value in seconds

example: converts '3h 34m 45s' to 12885 (value in seconds)
"""
value = 0
if 'd' in duration:
val = duration.split('d')
days = val[0]
value += cint(days) * 24 * 60 * 60
duration = val[1]
if 'h' in duration:
val = duration.split('h')
hours = val[0]
value += cint(hours) * 60 * 60
duration = val[1]
if 'm' in duration:
val = duration.split('m')
mins = val[0]
value += cint(mins) * 60
duration = val[1]
if 's' in duration:
val = duration.split('s')
secs = val[0]
value += cint(secs)

return value

def get_weekdays():
return ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']



Laden…
Annuleren
Opslaan