<s> from __future__ import unicode_literals <EOL> import frappe <EOL> from frappe import msgprint , _ <EOL> import json <EOL> import csv , cStringIO <EOL> from frappe . utils import encode , cstr , cint , flt , comma_or <EOL> def read_csv_content_from_uploaded_file ( ignore_encoding = False ) : <EOL> if getattr ( frappe , "<STR_LIT>" , None ) : <EOL> with open ( frappe . uploaded_file , "<STR_LIT:r>" ) as upfile : <EOL> fcontent = upfile . read ( ) <EOL> else : <EOL> from frappe . utils . file_manager import get_uploaded_content <EOL> fname , fcontent = get_uploaded_content ( ) <EOL> return read_csv_content ( fcontent , ignore_encoding ) <EOL> def read_csv_content_from_attached_file ( doc ) : <EOL> fileid = frappe . db . get_value ( "<STR_LIT>" , { "<STR_LIT>" : doc . doctype , <EOL> "<STR_LIT>" : doc . name } , "<STR_LIT:name>" ) <EOL> if not fileid : <EOL> msgprint ( _ ( "<STR_LIT>" ) ) <EOL> raise Exception <EOL> try : <EOL> from frappe . utils . file_manager import get_file <EOL> fname , fcontent = get_file ( fileid ) <EOL> return read_csv_content ( fcontent , frappe . form_dict . get ( '<STR_LIT>' ) ) <EOL> except Exception : <EOL> frappe . msgprint ( _ ( "<STR_LIT>" ) ) <EOL> raise Exception <EOL> def read_csv_content ( fcontent , ignore_encoding = False ) : <EOL> rows = [ ] <EOL> if not isinstance ( fcontent , unicode ) : <EOL> decoded = False <EOL> for encoding in [ "<STR_LIT:utf-8>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> try : <EOL> fcontent = unicode ( fcontent , encoding ) <EOL> decoded = True <EOL> break <EOL> except UnicodeDecodeError : <EOL> continue <EOL> if not decoded : <EOL> frappe . msgprint ( _ ( "<STR_LIT>" ) , <EOL> raise_exception = True ) <EOL> fcontent = fcontent . encode ( "<STR_LIT:utf-8>" ) . splitlines ( True ) <EOL> try : <EOL> rows = [ ] <EOL> for row in csv . reader ( fcontent ) : <EOL> r = [ ] <EOL> for val in row : <EOL> val = unicode ( val , "<STR_LIT:utf-8>" ) . strip ( ) <EOL> if val == "<STR_LIT>" : <EOL> r . append ( None ) <EOL> else : <EOL> r . append ( val ) <EOL> rows . append ( r ) <EOL> return rows <EOL> except Exception : <EOL> frappe . msgprint ( _ ( "<STR_LIT>" ) ) <EOL> raise <EOL> @ frappe . whitelist ( ) <EOL> def send_csv_to_client ( args ) : <EOL> if isinstance ( args , basestring ) : <EOL> args = json . loads ( args ) <EOL> args = frappe . _dict ( args ) <EOL> frappe . response [ "<STR_LIT:result>" ] = cstr ( to_csv ( args . data ) ) <EOL> frappe . response [ "<STR_LIT>" ] = args . filename <EOL> frappe . response [ "<STR_LIT:type>" ] = "<STR_LIT>" <EOL> def to_csv ( data ) : <EOL> writer = UnicodeWriter ( ) <EOL> for row in data : <EOL> writer . writerow ( row ) <EOL> return writer . getvalue ( ) <EOL> class UnicodeWriter : <EOL> def __init__ ( self , encoding = "<STR_LIT:utf-8>" ) : <EOL> self . encoding = encoding <EOL> self . queue = cStringIO . StringIO ( ) <EOL> self . writer = csv . writer ( self . queue , quoting = csv . QUOTE_NONNUMERIC ) <EOL> def writerow ( self , row ) : <EOL> row = encode ( row , self . encoding ) <EOL> self . writer . writerow ( row ) <EOL> def getvalue ( self ) : <EOL> return self . queue . getvalue ( ) <EOL> def check_record ( d ) : <EOL> """<STR_LIT>""" <EOL> from frappe . utils . dateutils import parse_date <EOL> doc = frappe . get_doc ( d ) <EOL> for key in d : <EOL> docfield = doc . meta . get_field ( key ) <EOL> val = d [ key ] <EOL> if docfield : <EOL> if docfield . reqd and ( val == '<STR_LIT>' or val == None ) : <EOL> frappe . msgprint ( _ ( "<STR_LIT>" ) . format ( docfield . label ) , raise_exception = <NUM_LIT:1> ) <EOL> if docfield . fieldtype == '<STR_LIT>' and val and docfield . options : <EOL> if val not in docfield . options . split ( '<STR_LIT:\n>' ) : <EOL> frappe . throw ( _ ( "<STR_LIT>" ) . format ( _ ( docfield . label ) , comma_or ( docfield . options . split ( "<STR_LIT:\n>" ) ) ) ) <EOL> if val and docfield . fieldtype == '<STR_LIT>' : <EOL> d [ key ] = parse_date ( val ) <EOL> elif val and docfield . fieldtype in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> d [ key ] = cint ( val ) <EOL> elif val and docfield . fieldtype in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> d [ key ] = flt ( val ) <EOL> def import_doc ( d , doctype , overwrite , row_idx , submit = False , ignore_links = False ) : <EOL> """<STR_LIT>""" <EOL> if d . get ( "<STR_LIT:name>" ) and frappe . db . exists ( doctype , d [ '<STR_LIT:name>' ] ) : <EOL> if overwrite : <EOL> doc = frappe . get_doc ( doctype , d [ '<STR_LIT:name>' ] ) <EOL> doc . flags . ignore_links = ignore_links <EOL> doc . update ( d ) <EOL> if d . get ( "<STR_LIT>" ) == <NUM_LIT:1> : <EOL> doc . update_after_submit ( ) <EOL> else : <EOL> doc . save ( ) <EOL> return '<STR_LIT>' % ( row_idx + <NUM_LIT:1> , getlink ( doctype , d [ '<STR_LIT:name>' ] ) ) <EOL> else : <EOL> return '<STR_LIT>' % ( row_idx + <NUM_LIT:1> , <EOL> getlink ( doctype , d [ '<STR_LIT:name>' ] ) ) <EOL> else : <EOL> doc = frappe . get_doc ( d ) <EOL> doc . flags . ignore_links = ignore_links <EOL> doc . insert ( ) <EOL> if submit : <EOL> doc . submit ( ) <EOL> return '<STR_LIT>' % ( row_idx + <NUM_LIT:1> , getlink ( doctype , <EOL> doc . get ( '<STR_LIT:name>' ) ) ) <EOL> def getlink ( doctype , name ) : <EOL> return '<STR_LIT>' % locals ( ) </s>
