# -*- coding: UTF-8 -*-
import xlrd
import datetime
import uuid
import os


def process_cell(table,i,j):
  _temp=table.cell(i,j)
  if _temp.ctype==1:
    return _temp.value
  if _temp.ctype==2:
    return str(int(_temp.value))
  #raise "Error xls!"

def processPWXLS(FILE):
  BASE_DIR = os.path.dirname(os.path.dirname(__file__))
  xlsname = os.path.join(BASE_DIR, 'uploads', '%s.xls' % str(uuid.uuid4()))
  destination = open(xlsname,'wb')
  for chunk in FILE.chunks(): 
      destination.write(chunk)
  destination.close()
  data = xlrd.open_workbook(xlsname)
  table = data.sheet_by_index(0)
  head = table.row_values(0)
  assert len(head)==6
  assert head[0].strip()==u'作品ID'
  assert head[1].strip()==u'参赛者用户名'
  assert head[2].strip()==u'作品标题'
  assert head[3].strip()==u'作品描述'
  assert head[4].strip()==u'文件名'
  assert head[5].strip()==u'文件扩展名'



  rets=[]
  for i in range(table.nrows-1):
    col = table.row_values(i+1)
    pwid = process_cell(table,i+1,0)
    user = process_cell(table,i+1,1)
    title = process_cell(table,i+1,2)
    desc = process_cell(table,i+1,3)
    filename = process_cell(table,i+1,4)
    fileext = process_cell(table,i+1,5)
    rets.append((pwid,user,title,desc,filename,fileext))
  return rets

