def process_row(self, row_data, calculate_dict): u''' 特殊情况给开发人员提供的接口 row_data 这一行的数据 calculate_dict 文档附加的列,如部门编号, 员工表是没有部门编号的,部门编号是用来初始化员工字段DeptID的 ''' from mysite.personnel.models.model_issuecard import IssueCard, CARD_VALID from mysite.personnel.models import Employee key = u"%s" % _(u"人员编号") emp_pin = u"%s" % calculate_dict[key] emp_pk = calculate_dict["user_pk"] if 'postgresql_psycopg2' in connection.__module__: userid_key = '"' + "UserID_id" + '"' cardstatus_key = '"' + "cardstatus" + '"' else: userid_key = "UserID_id" cardstatus_key = "cardstatus" if emp_pk: row_data[userid_key] = u'%s' % emp_pk #初始化人员外键 else: obj_emp = Employee() obj_emp.PIN = emp_pin obj_emp.DeptID_id = 1 obj_emp.save() row_data[userid_key] = u'%s' % obj_emp.pk #初始化部门 row_data[cardstatus_key] = u'%s' % CARD_VALID return row_data
def action(self,upload_attlog): from django.db import connection import datetime from mysite.personnel.models.model_emp import format_pin, Employee cursor = connection.cursor() if self.request.FILES: f=self.request.FILES['upload_attlog'] f_format=str(f).split('.') format_list=['dat'] ret = [] try: format_list.index(str(f_format[1])) except: raise Exception (_(u"考勤文件格式无效!")) try: file_data = f.read() file_data = file_data.strip() log_list = file_data.split("\r\n") sql = [] MYSQL_INSERT = u'''INSERT INTO checkinout(userid,checktime,checktype,verifycode,workcode) VALUES %(batch_rows)s ''' SQLSERVER_INSERT = u'''INSERT INTO checkinout(userid,checktime,checktype,verifycode,workcode) VALUES('%(uid)s','%(time)s','%(type)s','%(vf)s','%(wc)s') ''' for row in log_list: try: elems = row.split("\t") if len(elems) != 6: ret.append(u"%(r)s:%(info)s"%{"r":row,"info":_(u"数据格式不正确")}) continue pin = elems[0].strip() #用户PIN号 ttime = elems[1].strip() #考勤时间 sdevice_id = elems[2].strip() #设备号 state = elems[3].strip() #考勤状态 verify = elems[4].strip() # 验证方式 workcode = elems[5].strip() # 工作代码 try: ttime = datetime.datetime.strptime(ttime,"%Y-%m-%d %H:%M:%S") except: ret.append(u"%(r)s:%(info)s"%{"r":row,"info":_(u"时间格式不正确")}) continue obj_emp = None try: obj_emp = Employee.objects.get(PIN = format_pin(pin)) except: #创建用户 obj_emp = Employee() obj_emp.PIN = pin obj_emp.DeptID_id = 1 obj_emp.save() obj_emp.attarea = [1,] if db_select == IS_SQLSERVER_DB: sql.append( SQLSERVER_INSERT%{ "uid":obj_emp.pk, "time":ttime, "type":normal_state(state), "vf":normal_verify(verify), "wc":workcode } ) elif db_select == IS_MYSQL_DB: sql.append( u'''('%(uid)s','%(time)s','%(type)s','%(vf)s','%(wc)s')'''%{ "uid":obj_emp.pk, "time":ttime, "type":normal_state(state), "vf":normal_verify(verify), "wc":workcode } ) if len(sql)>200: if db_select == IS_SQLSERVER_DB: cursor.execute(";".join(sql)) elif db_select == IS_MYSQL_DB: cursor.execute( MYSQL_INSERT%({"batch_rows":",".join(sql)}) ) sql = [] except IntegrityError: if sql: for elem in sql: try: if db_select == IS_SQLSERVER_DB: cursor.execute(elem) elif db_select == IS_MYSQL_DB: cursor.execute( MYSQL_INSERT%({"batch_rows":elem}) ) except: pass sql = [] except Exception,e: ret.append(u"%s"%e) if sql: if db_select == IS_SQLSERVER_DB: cursor.execute(";".join(sql)) elif db_select == IS_MYSQL_DB: cursor.execute( MYSQL_INSERT%({"batch_rows":",".join(sql)}) ) except IntegrityError: if sql: for elem in sql: try: if db_select == IS_SQLSERVER_DB: cursor.execute(elem) elif db_select == IS_MYSQL_DB: cursor.execute( MYSQL_INSERT%({"batch_rows":elem}) ) except: pass